-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_pool.go
54 lines (48 loc) · 1.14 KB
/
connection_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
package main
import ()
type ConnectionPool struct {
Connections []*Connection
}
const (
MaxPeers = 10
)
func NewConnectionPool(peers []*Peer, metainfo *Metainfo) *ConnectionPool {
var connections []*Connection
for _, peer := range peers {
c := NewConnection(peer, metainfo)
connections = append(connections, c)
}
return &ConnectionPool{
Connections: connections,
}
}
func (cp *ConnectionPool) Start() {
for i, connection := range cp.Connections {
if i > MaxPeers {
break
}
connection.Open()
}
for i, connection := range cp.OpenConnections() {
if i > MaxPeers {
break
}
go connection.Listen()
connection.SendHandshakeMessage()
}
//for i, connection := range cp.OpenConnections() {
//if i > MaxPeers { break }
//interestedMessage := InterestedMessage()
//requestMessage := RequestMessage(0,0)
//connection.SendMessage(interestedMessage)
//connection.SendMessage(requestMessage)
//}
}
func (cp *ConnectionPool) OpenConnections() (connections []*Connection) {
for _, connection := range cp.Connections {
if connection.State == ConnectionStateConnected {
connections = append(connections, connection)
}
}
return
}