-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
545 lines (509 loc) · 14 KB
/
node.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// Copyright 2020 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zyre
import (
"bytes"
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"strconv"
"sync"
"time"
zmq "github.com/go-zeromq/zmq4"
)
type node struct {
ctx context.Context // socket context
requests chan *Cmd // requests from API
replies chan *Cmd // replies to API
events chan *Event // events to API
msgs chan *ZreMsg // msgs from the inbox socket
terminated chan interface{} // API shut us down
wg sync.WaitGroup // used to wait for all goroutines
verbose bool // Log all traffic
beaconPort int // Beacon UDP port number
interval time.Duration // Beacon interval
beacon *Beacon // Beacon object
uuid []byte // Our UUID
inbox zmq.Socket // Our inbox socket (ROUTER)
name string // Our public name
endpoint string // Our public endpoint
port uint16 // Our inbox port, if any
status byte // Our own change counter
peers map[string]*Peer // Hash of known peers, fast lookup
peerGroups map[string]*Group // Groups that our peers are in
ownGroups map[string]*Group // Groups that we are in
headers map[string]string // Our header values
}
// BeaconMsg frame has this format:
//
// Z R E 3 bytes
// version 1 byte 0x01 | 0x03
// UUID 16 bytes
// port 2 bytes in network order
type BeaconMsg struct {
Protocol [3]byte
Version byte
UUID []byte
Port uint16
}
// pack converts Beacon to an array of bytes
func (b *BeaconMsg) pack() []byte {
result := new(bytes.Buffer)
binary.Write(result, binary.BigEndian, b.Protocol)
binary.Write(result, binary.BigEndian, b.Version)
binary.Write(result, binary.BigEndian, b.UUID)
binary.Write(result, binary.BigEndian, b.Port)
return result.Bytes()
}
// unpack converts Beacon to an array of bytes
func (b *BeaconMsg) unpack(msg []byte) {
buffer := bytes.NewBuffer(msg)
binary.Read(buffer, binary.BigEndian, &b.Protocol)
binary.Read(buffer, binary.BigEndian, &b.Version)
uuid := make([]byte, 16)
binary.Read(buffer, binary.BigEndian, uuid)
b.UUID = append(b.UUID, uuid...)
binary.Read(buffer, binary.BigEndian, &b.Port)
}
const (
beaconVersion = 0x1
zreDiscoveryPort = 5670
reapInterval = 1 * time.Second
)
// newNode creates a new node.
func newNode(ctx context.Context, requests chan *Cmd, replies chan *Cmd,
events chan *Event) *node {
n := &node{
ctx: ctx,
requests: requests,
replies: replies,
events: events,
msgs: make(chan *ZreMsg),
terminated: make(chan interface{}),
beaconPort: zreDiscoveryPort,
beacon: NewBeacon(),
inbox: zmq.NewRouter(ctx),
peers: make(map[string]*Peer),
peerGroups: make(map[string]*Group),
ownGroups: make(map[string]*Group),
headers: make(map[string]string),
}
n.uuid = make([]byte, 16)
io.ReadFull(rand.Reader, n.uuid)
// Default name for node is first 6 characters of UUID:
// the shorter string is more readable in logs
n.name = fmt.Sprintf("%.3X", n.uuid)
n.wg.Add(1)
go n.actor()
return n
}
func getEndpoint(addr string, port uint16) string {
ip := net.ParseIP(addr)
ipAddr := ip.String()
if ip.To4() == nil {
ipAddr = "[" + ipAddr + "]"
}
endpoint := fmt.Sprintf("tcp://%s:%d", ipAddr, port)
return endpoint
}
func getHexUpper(data []byte) string {
return fmt.Sprintf("%X", data)
}
// start node, return 0 if OK, 1 if not possible
func (n *node) start() error {
// If application didn't bind explicitly, we grab an ephemeral port
// on all available network interfaces. This is orthogonal to
// beaconing, since we can connect to other peers and they will
// gossip our endpoint to others.
if n.beaconPort > 0 {
// Start UDP beaconing
addr := n.beacon.SetBeaconCmd(&Cmd{ID: "CONFIGURE", Payload: n.beaconPort})
n.beacon.SetBeaconCmd(&Cmd{ID: "SUBSCRIBE", Payload: []byte("ZRE")})
if n.interval > 0 {
n.beacon.SetBeaconCmd(&Cmd{ID: "INTERVAL", Payload: n.interval})
}
err := n.inbox.Listen("tcp://*:0")
if err != nil {
return err
}
_, pstring, _ := net.SplitHostPort(n.inbox.Addr().String())
port, _ := strconv.ParseUint(pstring, 0, 16)
n.port = uint16(port)
b := &BeaconMsg{
Protocol: [...]byte{'Z', 'R', 'E'},
Version: beaconVersion,
UUID: n.uuid,
Port: n.port}
n.beacon.SetBeaconCmd(&Cmd{ID: "PUBLISH", Payload: b.pack()})
n.endpoint = getEndpoint(addr.Payload.(string), n.port)
n.wg.Add(1)
go func() {
defer n.wg.Done()
for {
select {
case <-n.terminated:
close(n.msgs)
return
default:
m, err := Recv(n.inbox)
if err == nil {
n.msgs <- m
}
}
}
}()
}
return nil
}
// stop node discovery and interconnection
func (n *node) stop() {
if n.beacon != nil {
b := &BeaconMsg{
Protocol: [...]byte{'Z', 'R', 'E'},
Version: beaconVersion,
UUID: n.uuid,
Port: 0}
n.beacon.SetBeaconCmd(&Cmd{ID: "PUBLISH", Payload: b.pack()})
time.Sleep(n.interval + 1*time.Millisecond) // FIXME
n.beacon.Close()
}
n.beaconPort = 0
n.inbox.Close()
}
// recvAPI handle the different control messages from the front-end
func (n *node) recvAPI(c *Cmd) {
switch c.ID {
case "UUID":
n.replies <- &Cmd{Payload: n.uuid}
case "NAME":
n.replies <- &Cmd{Payload: n.name}
case "SET NAME":
n.name = c.Payload.(string)
case "SET HEADER":
keyPayload := c.Payload.([]string)
n.headers[keyPayload[0]] = keyPayload[1]
case "SET VERBOSE":
n.verbose = c.Payload.(bool)
case "SET PORT":
n.beaconPort = c.Payload.(int)
case "SET INTERVAL":
n.interval = c.Payload.(time.Duration)
case "SET INTERFACE":
n.beacon.SetBeaconCmd(&Cmd{ID: "INTERFACE", Payload: c.Payload.(string)})
case "START":
err := n.start()
n.replies <- &Cmd{Err: err}
case "STOP":
n.stop()
close(n.terminated)
go func() {
n.wg.Wait()
n.replies <- &Cmd{}
}()
case "WHISPER":
peerPayload := c.Payload.([]string)
// Get peer to send message to
peer, ok := n.peers[peerPayload[0]]
// Send frame on out to peer's mailbox, drop message
// if peer doesn't exist (may have been destroyed)
if ok {
m := NewZreMsg(WhisperID)
m.Content = []byte(peerPayload[1])
peer.Send(m)
}
case "SHOUT":
groupPayload := c.Payload.([]string)
// Get group to send message to
group := groupPayload[0]
m := NewZreMsg(ShoutID)
m.Group = group
m.Content = []byte(groupPayload[1])
if g, ok := n.peerGroups[group]; ok {
g.Send(m)
}
case "JOIN":
group := c.Payload.(string)
if _, ok := n.ownGroups[group]; !ok {
// Only send if we're not already in group
n.ownGroups[group] = NewGroup(group)
m := NewZreMsg(JoinID)
m.Group = group
n.status++
m.Status = n.status
for _, peer := range n.peers {
peer.Send(m)
}
}
case "LEAVE":
group := c.Payload.(string)
if _, ok := n.ownGroups[group]; ok {
// Only send if we're actually in group
// FIMXE function for JOIN and LEAVE
m := NewZreMsg(LeaveID)
m.Group = group
n.status++
m.Status = n.status
for _, peer := range n.peers {
peer.Send(m)
}
delete(n.ownGroups, group)
}
case "PEERS":
var peersIDs []string
for k := range n.peers {
peersIDs = append(peersIDs, k)
}
n.replies <- &Cmd{Payload: peersIDs}
default:
log.Printf("invalid command %q", c.ID)
}
}
// purgePeer deletes peer for a given endpoint
func purgePeer(peer *Peer, endpoint string) {
if peer.endpoint == endpoint {
peer.Disconnect()
}
}
// requirePeer find or create peer via its UUID
func (n *node) requirePeer(identity string, endpoint string) (*Peer, error) {
peer, ok := n.peers[identity]
if !ok {
// Purge any previous peer on same endpoint
for _, p := range n.peers {
purgePeer(p, endpoint)
}
peer = NewPeer(n.ctx, identity)
n.peers[identity] = peer
peer.origin = n.name
err := peer.Connect(n.uuid, endpoint)
if err != nil {
return nil, err
}
// Handshake discovery by sending HELLO as first message
m := NewZreMsg(HelloID)
m.Endpoint = n.endpoint
m.Status = n.status
m.Name = n.name
for key := range n.ownGroups {
m.Groups = append(m.Groups, key)
}
for key, header := range n.headers {
m.Headers[key] = header
}
peer.Send(m)
}
return peer, nil
}
// deletePeer removes peer from group, if it's a member
func deletePeer(peer *Peer, group *Group) {
group.Leave(peer)
}
// removePeer removes a peer from our data structures
func (n *node) removePeer(peer *Peer) {
if peer == nil {
return
}
// Tell the calling application the peer has gone
n.sendEvent(&Event{Type: "EXIT", PeerUUID: peer.Identity, PeerName: peer.Name})
// Remove peer from any groups we've got it in
for _, group := range n.peerGroups {
deletePeer(peer, group)
}
// To destroy peer, we remove from peers hash table
peer.Disconnect()
delete(n.peers, peer.Identity)
}
// requirePeerGroup finds or creates group via its name
func (n *node) requirePeerGroup(name string) *Group {
group, ok := n.peerGroups[name]
if !ok {
group = NewGroup(name)
n.peerGroups[name] = group
}
return group
}
// joinPeerGroup peer joins group
func (n *node) joinPeerGroup(peer *Peer, name string) *Group {
group := n.requirePeerGroup(name)
group.Join(peer)
// Now tell the caller about the peer joined group
n.sendEvent(&Event{Type: "JOIN", PeerUUID: peer.Identity,
PeerName: peer.Name, Group: name})
return group
}
// leavePeerGroup peer leaves group
func (n *node) leavePeerGroup(peer *Peer, name string) *Group {
group := n.requirePeerGroup(name)
group.Leave(peer)
// Now tell the caller about the peer left group
n.sendEvent(&Event{Type: "LEAVE", PeerUUID: peer.Identity,
PeerName: peer.Name, Group: name})
return group
}
// recvPeer handles messages coming from other peers
func (n *node) recvPeer(msg *ZreMsg) {
routingID := msg.routingID
if len(routingID) < 1 {
return
}
// Router socket tells us the identity of this peer
// Identity must be [1] followed by 16-byte UUID
identity := getHexUpper(routingID[1:])
peer := n.peers[identity]
// On HELLO we may create the peer if it's unknown
// On other commands the peer must already exist
if msg.id == HelloID {
if peer != nil {
// remove fake peers
if peer.ready {
n.removePeer(peer)
} else if peer.endpoint == n.endpoint {
// We ignore HELLO, if peer has same endpoint as current node
return
}
}
var err error
peer, err = n.requirePeer(identity, msg.Endpoint)
if err == nil {
peer.ready = true
}
}
// Ignore command if peer isn't ready
if peer == nil || !peer.ready {
if peer != nil {
n.removePeer(peer)
}
return
}
// FIXME review this
if !peer.MessagesLost(msg) {
log.Printf("%s messages lost from %s", n.name, identity)
return
}
// Now process each command
switch msg.id {
case HelloID:
// Store properties from HELLO command into peer
peer.Name = msg.Name
event := &Event{
Type: "ENTER",
PeerUUID: peer.Identity,
PeerName: peer.Name,
PeerAddr: peer.endpoint,
Headers: make(map[string]string),
}
for key, val := range msg.Headers {
peer.Headers[key] = val
event.Headers[key] = val
}
n.sendEvent(event)
// Join peer to listed groups
for _, group := range msg.Groups {
n.joinPeerGroup(peer, group)
}
// Now take peer's status from HELLO, after joining groups
peer.status = msg.Status
case WhisperID:
// Pass up to caller API as WHISPER event
n.sendEvent(&Event{Type: "WHISPER", PeerUUID: identity,
PeerName: peer.Name, Msg: msg.Content})
case ShoutID:
// Pass up to caller API as SHOUT event
n.sendEvent(&Event{Type: "SHOUT", PeerUUID: identity,
PeerName: peer.Name, Group: msg.Group, Msg: msg.Content})
case PingID:
ping := NewZreMsg(PingOkID)
peer.Send(ping)
case JoinID:
n.joinPeerGroup(peer, msg.Group)
if msg.Status != peer.status {
panic(fmt.Sprintf("[%X] msg.Status != peer.status (%d != %d)", n.uuid, msg.Status, peer.status))
}
case LeaveID:
n.leavePeerGroup(peer, msg.Group)
if msg.Status != peer.status {
panic(fmt.Sprintf("[%X] msg.Status != peer.status (%d != %d)", n.uuid, msg.Status, peer.status))
}
}
// Activity from peer resets peer timers
peer.Refresh()
}
func (n *node) recvBeacon(s *Signal) {
b := &BeaconMsg{}
b.unpack(s.Transmit)
// Ignore anything that isn't a valid beacon
if b.Version == beaconVersion {
identity := getHexUpper(b.UUID)
if b.Port != 0 {
// Check that the peer, identified by its UUID, exists
peer, err := n.requirePeer(identity, getEndpoint(s.Addr, b.Port))
if err == nil {
peer.Refresh()
} else if n.verbose {
log.Printf("%s %s", n.name, err)
}
} else {
// Zero port means peer is going away; remove it if
// we had any knowledge of it already
peer := n.peers[identity]
n.removePeer(peer)
}
} else if n.verbose {
log.Printf("invalid ZRE Beacon version %d", b.Version)
}
}
// We do this once a second:
// - if peer has gone quiet, send TCP ping and emit EVASIVE event
// - if peer has disappeared, expire it
func (n *node) pingPeer(peer *Peer) {
if time.Now().Unix() >= peer.expiredAt.Unix() {
n.removePeer(peer)
} else if time.Now().Unix() >= peer.evasiveAt.Unix() {
// If peer is being evasive, force a TCP ping.
// TODO: do this only once for a peer in this state;
// it would be nicer to use a proper state machine
// for peer management.
m := NewZreMsg(PingID)
peer.Send(m)
}
}
func (n *node) actor() {
defer n.wg.Done()
tick := time.Tick(reapInterval)
for {
select {
case r := <-n.requests:
n.recvAPI(r)
case m, ok := <-n.msgs:
if !ok {
// clear peer tables
for peerID, peer := range n.peers {
peer.Disconnect()
delete(n.peers, peerID)
}
return
}
n.recvPeer(m)
case s := <-n.beacon.Signals:
n.recvBeacon(s)
case <-tick:
for _, peer := range n.peers {
n.pingPeer(peer)
}
}
}
}
func (n *node) sendEvent(e *Event) {
select {
case n.events <- e:
default:
if n.verbose {
log.Printf("event channel is full, dropping: %s", e.Type)
}
}
}