-
Notifications
You must be signed in to change notification settings - Fork 211
/
protocol.go
67 lines (54 loc) · 1.98 KB
/
protocol.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
package discovery
import (
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/node"
"github.com/spacemeshos/go-spacemesh/p2p/p2pcrypto"
"github.com/spacemeshos/go-spacemesh/p2p/server"
"github.com/spacemeshos/go-spacemesh/p2p/service"
"net"
"time"
)
type protocolRoutingTable interface {
GetAddress() *KnownAddress
AddAddresses(n []*node.Info, src *node.Info)
AddAddress(n *node.Info, src *node.Info)
AddressCache() []*node.Info
}
type protocol struct {
local *node.Info
table protocolRoutingTable
logger log.Log
msgServer *server.MessageServer
}
func (p *protocol) SetLocalAddresses(tcp, udp int) {
p.local.ProtocolPort = uint16(tcp)
p.local.DiscoveryPort = uint16(udp)
}
// Name is the name if the protocol.
const Name = "/udp/v2/discovery"
// MessageBufSize is the buf size we give to the messages channel
const MessageBufSize = 1000
// MessageTimeout is the timeout we tolerate when waiting for a message reply
const MessageTimeout = time.Second * 5 // TODO: Parametrize
// PingPong is the ping protocol ID
const PingPong = 0
// GetAddresses is the findnode protocol ID
const GetAddresses = 1
// newProtocol is a constructor for a protocol protocol provider.
func newProtocol(local p2pcrypto.PublicKey, rt protocolRoutingTable, svc server.Service, log log.Log) *protocol {
s := server.NewMsgServer(svc, Name, MessageTimeout, make(chan service.DirectMessage, MessageBufSize), log)
d := &protocol{
local: &node.Info{ID: local.Array(), IP: net.IPv4zero, ProtocolPort: 7513, DiscoveryPort: 7513},
table: rt,
msgServer: s,
logger: log,
}
// XXX Reminder: for discovery protocol to work you must call SetLocalAddresses with updated ports from the socket.
d.msgServer.RegisterMsgHandler(PingPong, d.newPingRequestHandler())
d.msgServer.RegisterMsgHandler(GetAddresses, d.newGetAddressesRequestHandler())
return d
}
// Close stops the message server protocol from serving requests.
func (p *protocol) Close() {
p.msgServer.Close()
}