-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
62 lines (50 loc) · 1.22 KB
/
pool.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
package main
import (
"bufio"
"os"
"strings"
)
// Client state enum (starts at 1)
const (
// All clients playing
Playing = iota + 1
// All clients paused
Paused
)
// Pool is a collection of all connected clients.
type Pool struct {
Clients []*Client
// Notification channel shared by all clients, used to notify
// other clients of incoming notifications such as one client being paused.
Notification chan string
// Used to keep track of the global state of the clients
State int
// Channel to inform clients of pool State
StateInformer chan int
}
// NewPoolFromFile creates a Pool from client
// identifiers written in a text file located at path.
// Format: hostname:port,username,password
func NewPoolFromFile(path string) *Pool {
file, err := os.Open(path)
if err != nil {
LogFatal(err)
}
pool := &Pool{
Clients: []*Client{},
Notification: make(chan string),
State: Playing,
StateInformer: make(chan int),
}
// Read line by line
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Skip if hashtag
if scanner.Text()[0] == byte('#') {
continue
}
identifier := strings.Split(scanner.Text(), ",")
pool.NewClient(identifier[0], identifier[1], identifier[2])
}
return pool
}