-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyncServer.go
73 lines (60 loc) · 1.36 KB
/
syncServer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"net"
)
// SyncServer is a simple server that will tell all its clients to start playing their song
// at the same time
type SyncServer struct {
ln net.Listener
clients []net.Conn
stop chan byte
newClient chan byte
}
// NewSyncServer creates a new server listening on "bind"
func NewSyncServer(bind string) (*SyncServer, error) {
s := &SyncServer{}
ln, err := net.Listen("tcp", bind)
if err != nil {
return nil, err
}
s.ln = ln
s.stop = make(chan byte)
s.newClient = make(chan byte)
return s, nil
}
// WaitForClients waits for clients to connect to the server
func (s *SyncServer) WaitForClients() {
for {
go s.waitForClient()
select {
case <-s.newClient:
continue
case <-s.stop:
return
}
}
}
// StopWaiting stops waiting for new clients
func (s *SyncServer) StopWaiting() {
s.stop <- 1
}
// StartMusic sends the signal to start all the clients
func (s *SyncServer) StartMusic() {
for _, c := range s.clients {
go c.Write([]byte{1})
}
}
func (s *SyncServer) waitForClient() {
conn, err := s.ln.Accept()
if err != nil {
fmt.Println("Error while waiting for a client:")
panic(err)
}
s.newClient <- 1
go s.handleConnection(conn)
}
func (s *SyncServer) handleConnection(conn net.Conn) {
fmt.Printf("New connection from %s\n", conn.RemoteAddr().String())
s.clients = append(s.clients, conn)
}