-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
98 lines (77 loc) · 2.02 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package connection
import (
"encoding/gob"
"fmt"
"log"
"net"
"sync"
)
type Pool struct {
connections []net.Conn
mx sync.Mutex
}
func (p *Pool) Add(conn net.Conn) error {
p.mx.Lock()
defer p.mx.Unlock()
if len(p.connections) >= 4 {
return fmt.Errorf("Connection pool is full")
}
p.connections = append(p.connections, conn)
log.Println("Added new connection", conn.RemoteAddr())
return nil
}
func (p *Pool) Remove(conn net.Conn) {
p.mx.Lock()
defer p.mx.Unlock()
for i, c := range p.connections {
if c == conn {
p.connections = append(p.connections[:i], p.connections[i+1:]...)
log.Println("Removed connection", conn.RemoteAddr())
return
}
}
}
func (p *Pool) Broadcast(msg Message) {
p.mx.Lock()
defer p.mx.Unlock()
for _, conn := range p.connections {
encoder := gob.NewEncoder(conn)
if err := encoder.Encode(msg); err != nil {
log.Println(err)
continue
}
log.Println("Broadcasted message", msg.Kind, "to:", conn.RemoteAddr())
}
}
func (p *Pool) ListConnections() []net.Conn {
return p.connections
}
func NewPool(peers []string) *Pool {
pool := &Pool{
connections: make([]net.Conn, 0),
}
for _, peer := range peers {
conn, err := net.Dial("tcp", peer)
if err != nil {
log.Println(err)
} else {
pool.connections = append(pool.connections, conn)
log.Println("Connected to", peer)
}
}
return pool
}
func HandleConnection(pool *Pool, conn net.Conn, callback func(message string)) {
decoder := gob.NewDecoder(conn)
for {
var msg Message
if err := decoder.Decode(&msg); err != nil {
log.Println("Failed to decode message from", conn.RemoteAddr(), " | ", err)
pool.Remove(conn)
conn.Close()
return
}
callback(msg.Kind)
pool.Broadcast(msg)
}
}