forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.go
875 lines (793 loc) · 24.6 KB
/
overlay.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
package onet
import (
"errors"
"fmt"
"sync"
"time"
"github.com/dedis/onet/log"
"github.com/dedis/onet/network"
"gopkg.in/satori/go.uuid.v1"
)
// Overlay keeps all trees and entity-lists for a given Server. It creates
// Nodes and ProtocolInstances upon request and dispatches the messages.
type Overlay struct {
server *Server
// mapping from Tree.Id to Tree
trees map[TreeID]*Tree
treesMut sync.Mutex
// mapping from Roster.id to Roster
entityLists map[RosterID]*Roster
entityListLock sync.Mutex
// cache for relating token(~Node) to TreeNode
cache *treeNodeCache
// TreeNodeInstance part
instances map[TokenID]*TreeNodeInstance
instancesInfo map[TokenID]bool
instancesLock sync.Mutex
protocolInstances map[TokenID]ProtocolInstance
// treeMarshal that needs to be converted to Tree but host does not have the
// entityList associated yet.
// map from Roster.ID => trees that use this entity list
pendingTreeMarshal map[RosterID][]*TreeMarshal
// lock associated with pending TreeMarshal
pendingTreeLock sync.Mutex
// pendingMsg is a list of message we received that does not correspond
// to any local Tree or/and Roster. We first request theses so we can
// instantiate properly protocolInstance that will use these ProtocolMsg msg.
pendingMsg []pendingMsg
// lock associated with pending ProtocolMsg
pendingMsgLock sync.Mutex
transmitMux sync.Mutex
protoIO *messageProxyStore
pendingConfigs map[TokenID]*GenericConfig
pendingConfigsMut sync.Mutex
}
// NewOverlay creates a new overlay-structure
func NewOverlay(c *Server) *Overlay {
o := &Overlay{
server: c,
trees: make(map[TreeID]*Tree),
entityLists: make(map[RosterID]*Roster),
cache: newTreeNodeCache(),
instances: make(map[TokenID]*TreeNodeInstance),
instancesInfo: make(map[TokenID]bool),
protocolInstances: make(map[TokenID]ProtocolInstance),
pendingTreeMarshal: make(map[RosterID][]*TreeMarshal),
pendingConfigs: make(map[TokenID]*GenericConfig),
}
o.protoIO = newMessageProxyStore(c.suite, c, o)
// messages going to protocol instances
c.RegisterProcessor(o,
ProtocolMsgID, // protocol instance's messages
RequestTreeMsgID, // request a tree
SendTreeMsgID, // send a tree back to a request
RequestRosterMsgID, // request a roster
SendRosterMsgID, // send a roster back to request
ConfigMsgID) // fetch config information
return o
}
// stop stops goroutines associated with this overlay.
func (o *Overlay) stop() {
o.cache.stop()
}
// Process implements the Processor interface so it process the messages that it
// wants.
func (o *Overlay) Process(env *network.Envelope) {
// Messages handled by the overlay directly without any messageProxyIO
if env.MsgType.Equal(ConfigMsgID) {
o.handleConfigMessage(env)
return
}
// get messageProxy or default one
io := o.protoIO.getByPacketType(env.MsgType)
inner, info, err := io.Unwrap(env.Msg)
if err != nil {
log.Error("unwrapping: ", err)
return
}
switch true {
case info.RequestTree != nil:
o.handleRequestTree(env.ServerIdentity, info.RequestTree, io)
case info.TreeMarshal != nil:
o.handleSendTree(env.ServerIdentity, info.TreeMarshal, io)
case info.RequestRoster != nil:
o.handleRequestRoster(env.ServerIdentity, info.RequestRoster, io)
case info.Roster != nil:
o.handleSendRoster(env.ServerIdentity, info.Roster)
default:
typ := network.MessageType(inner)
protoMsg := &ProtocolMsg{
From: info.TreeNodeInfo.From,
To: info.TreeNodeInfo.To,
ServerIdentity: env.ServerIdentity,
Msg: inner,
MsgType: typ,
}
o.TransmitMsg(protoMsg, io)
}
}
// TransmitMsg takes a message received from the host and treats it. It might
// - ask for the identityList
// - ask for the Tree
// - create a new protocolInstance
// - pass it to a given protocolInstance
// io is the messageProxy to use if a specific wireformat protocol is used.
// It can be nil: in that case it fall backs to default wire protocol.
func (o *Overlay) TransmitMsg(onetMsg *ProtocolMsg, io MessageProxy) error {
tree := o.Tree(onetMsg.To.TreeID)
if tree == nil {
return o.requestTree(onetMsg.ServerIdentity, onetMsg, io)
}
o.transmitMux.Lock()
defer o.transmitMux.Unlock()
// TreeNodeInstance
var pi ProtocolInstance
o.instancesLock.Lock()
pi, ok := o.protocolInstances[onetMsg.To.ID()]
done := o.instancesInfo[onetMsg.To.ID()]
o.instancesLock.Unlock()
if done {
log.Lvl5("Message for TreeNodeInstance that is already finished")
return nil
}
// if the TreeNodeInstance is not there, creates it
if !ok {
log.Lvlf4("Creating TreeNodeInstance at %s %x", o.server.ServerIdentity, onetMsg.To.ID())
tn, err := o.TreeNodeFromToken(onetMsg.To)
if err != nil {
return errors.New("No TreeNode defined in this tree here")
}
tni := o.newTreeNodeInstanceFromToken(tn, onetMsg.To, io)
// retrieve the possible generic config for this message
config := o.getConfig(onetMsg.To.ID())
// request the PI from the Service and binds the two
pi, err = o.server.serviceManager.newProtocol(tni, config)
if err != nil {
return err
}
if pi == nil {
return nil
}
go pi.Dispatch()
if err := o.RegisterProtocolInstance(pi); err != nil {
return errors.New("Error Binding TreeNodeInstance and ProtocolInstance:" +
err.Error())
}
log.Lvl4(o.server.Address(), "Overlay created new ProtocolInstace msg => ",
fmt.Sprintf("%+v", onetMsg.To))
}
// TODO Check if TreeNodeInstance is already Done
pi.ProcessProtocolMsg(onetMsg)
return nil
}
// addPendingTreeMarshal adds a treeMarshal to the list.
// This list is checked each time we receive a new Roster
// so trees using this Roster can be constructed.
func (o *Overlay) addPendingTreeMarshal(tm *TreeMarshal) {
o.pendingTreeLock.Lock()
var sl []*TreeMarshal
var ok bool
// initiate the slice before adding
if sl, ok = o.pendingTreeMarshal[tm.RosterID]; !ok {
sl = make([]*TreeMarshal, 0)
}
sl = append(sl, tm)
o.pendingTreeMarshal[tm.RosterID] = sl
o.pendingTreeLock.Unlock()
}
// checkPendingMessages is called each time we receive a new tree if there are
// some pending ProtocolMessage messages using this tree. If there are, we can
// make an instance of a protocolinstance and give it the message.
func (o *Overlay) checkPendingMessages(t *Tree) {
go func() {
o.pendingMsgLock.Lock()
var newPending []pendingMsg
for _, pending := range o.pendingMsg {
if t.ID.Equal(pending.ProtocolMsg.To.TreeID) {
// if this message references t, instantiate it and go
err := o.TransmitMsg(pending.ProtocolMsg, pending.MessageProxy)
if err != nil {
log.Error("TransmitMsg failed:", err)
continue
}
} else {
newPending = append(newPending, pending)
}
}
o.pendingMsg = newPending
o.pendingMsgLock.Unlock()
}()
}
// checkPendingTreeMarshal is called each time we add a new Roster to the
// system. It checks if some treeMarshal use this entityList so they can be
// converted to Tree.
func (o *Overlay) checkPendingTreeMarshal(el *Roster) {
o.pendingTreeLock.Lock()
sl, ok := o.pendingTreeMarshal[el.ID]
if !ok {
// no tree for this entitty list
return
}
for _, tm := range sl {
tree, err := tm.MakeTree(el)
if err != nil {
log.Error("Tree from Roster failed")
continue
}
// add the tree into our "database"
o.RegisterTree(tree)
}
o.pendingTreeLock.Unlock()
}
func (o *Overlay) savePendingMsg(onetMsg *ProtocolMsg, io MessageProxy) {
o.pendingMsgLock.Lock()
o.pendingMsg = append(o.pendingMsg, pendingMsg{
ProtocolMsg: onetMsg,
MessageProxy: io,
})
o.pendingMsgLock.Unlock()
}
// requestTree will ask for the tree the ProtocolMessage is related to.
// it will put the message inside the pending list of ProtocolMessage waiting to
// have their trees.
// io is the wrapper to use to send the message, it can be nil.
func (o *Overlay) requestTree(si *network.ServerIdentity, onetMsg *ProtocolMsg, io MessageProxy) error {
o.savePendingMsg(onetMsg, io)
var msg interface{}
om := &OverlayMsg{
RequestTree: &RequestTree{onetMsg.To.TreeID},
}
msg, err := io.Wrap(nil, om)
if err != nil {
return err
}
// no need to record sentLen because Overlay uses Server's CounterIO
_, err = o.server.Send(si, msg)
return err
}
// RegisterTree takes a tree and puts it in the map
func (o *Overlay) RegisterTree(t *Tree) {
o.treesMut.Lock()
o.trees[t.ID] = t
o.treesMut.Unlock()
o.checkPendingMessages(t)
}
// TreeFromToken searches for the tree corresponding to a token.
func (o *Overlay) TreeFromToken(tok *Token) *Tree {
o.treesMut.Lock()
defer o.treesMut.Unlock()
return o.trees[tok.TreeID]
}
// Tree returns the tree given by treeId or nil if not found
func (o *Overlay) Tree(tid TreeID) *Tree {
o.treesMut.Lock()
defer o.treesMut.Unlock()
return o.trees[tid]
}
// RegisterRoster puts an entityList in the map
func (o *Overlay) RegisterRoster(el *Roster) {
o.entityListLock.Lock()
defer o.entityListLock.Unlock()
o.entityLists[el.ID] = el
}
// RosterFromToken returns the entitylist corresponding to a token
func (o *Overlay) RosterFromToken(tok *Token) *Roster {
return o.entityLists[tok.RosterID]
}
// Roster returns the entityList given by RosterID
func (o *Overlay) Roster(elid RosterID) *Roster {
o.entityListLock.Lock()
defer o.entityListLock.Unlock()
return o.entityLists[elid]
}
// TreeNodeFromToken returns the treeNode corresponding to a token
func (o *Overlay) TreeNodeFromToken(t *Token) (*TreeNode, error) {
if t == nil {
return nil, errors.New("didn't find tree-node: No token given")
}
// First, check the cache
if tn := o.cache.GetFromToken(t); tn != nil {
return tn, nil
}
// If cache has not, then search the tree
tree := o.Tree(t.TreeID)
if tree == nil {
return nil, errors.New("didn't find tree")
}
tn := tree.Search(t.TreeNodeID)
if tn == nil {
return nil, errors.New("didn't find treenode")
}
// Since we found treeNode, cache it.
o.cache.Set(tree, tn)
return tn, nil
}
// Rx implements the CounterIO interface, should be the same as the server
func (o *Overlay) Rx() uint64 {
return o.server.Rx()
}
// Tx implements the CounterIO interface, should be the same as the server
func (o *Overlay) Tx() uint64 {
return o.server.Tx()
}
func (o *Overlay) handleRequestTree(si *network.ServerIdentity, req *RequestTree, io MessageProxy) {
tid := req.TreeID
tree := o.Tree(tid)
var err error
var treeM *TreeMarshal
var msg interface{}
if tree != nil {
treeM = tree.MakeTreeMarshal()
} else {
// XXX Take care here for we must verify at the other side that
// the tree is Nil. Should we think of a way of sending back an
// "error" ?
treeM = (&Tree{}).MakeTreeMarshal()
}
msg, err = io.Wrap(nil, &OverlayMsg{
TreeMarshal: treeM,
})
if err != nil {
log.Error("couldn't wrap TreeMarshal:", err)
return
}
_, err = o.server.Send(si, msg)
if err != nil {
log.Error("Couldn't send tree:", err)
}
}
func (o *Overlay) handleSendTree(si *network.ServerIdentity, tm *TreeMarshal, io MessageProxy) {
if tm.TreeID.IsNil() {
log.Error("Received an empty Tree")
return
}
roster := o.Roster(tm.RosterID)
// The roster does not exists, we should request that, too
if roster == nil {
msg, err := io.Wrap(nil, &OverlayMsg{
RequestRoster: &RequestRoster{tm.RosterID},
})
if err != nil {
log.Error("could not wrap RequestRoster:", err)
}
if _, err := o.server.Send(si, msg); err != nil {
log.Error("Requesting Roster in SendTree failed", err)
}
// put the tree marshal into pending queue so when we receive the
// entitylist we can create the real Tree.
o.addPendingTreeMarshal(tm)
return
}
tree, err := tm.MakeTree(roster)
if err != nil {
log.Error("Couldn't create tree:", err)
return
}
log.Lvl4("Received new tree")
o.RegisterTree(tree)
}
func (o *Overlay) handleRequestRoster(si *network.ServerIdentity, req *RequestRoster, io MessageProxy) {
id := req.RosterID
roster := o.Roster(id)
var err error
var msg interface{}
if roster == nil {
// XXX Bad reaction to request...
log.Lvl2("Requested entityList that we don't have")
roster = &Roster{}
}
msg, err = io.Wrap(nil, &OverlayMsg{
Roster: roster,
})
if err != nil {
log.Error("error wraping up roster:", err)
return
}
_, err = o.server.Send(si, msg)
if err != nil {
log.Error("Couldn't send empty entity list from host:",
o.server.ServerIdentity.String(),
err)
return
}
}
func (o *Overlay) handleSendRoster(si *network.ServerIdentity, roster *Roster) {
if roster.ID.IsNil() {
log.Lvl2("Received an empty Roster")
} else {
o.RegisterRoster(roster)
// Check if some trees can be constructed from this entitylist
o.checkPendingTreeMarshal(roster)
}
log.Lvl4("Received new entityList")
}
// handleConfigMessage stores the config message so it can be dispatched
// alongside with the protocol message later to the service.
func (o *Overlay) handleConfigMessage(env *network.Envelope) {
config, ok := env.Msg.(*ConfigMsg)
if !ok {
// This should never happen <=> assert
log.Panic(o.server.Address(), "wrong config type")
return
}
o.pendingConfigsMut.Lock()
defer o.pendingConfigsMut.Unlock()
o.pendingConfigs[config.Dest] = &config.Config
}
// getConfig returns the generic config corresponding to this node if present,
// and removes it from the list of pending configs.
func (o *Overlay) getConfig(id TokenID) *GenericConfig {
o.pendingConfigsMut.Lock()
defer o.pendingConfigsMut.Unlock()
c := o.pendingConfigs[id]
delete(o.pendingConfigs, id)
return c
}
// SendToTreeNode sends a message to a treeNode
// from is the sender token
// to is the treenode of the destination
// msg is the message to send
// io is the messageproxy used to correctly create the wire format
// c is the generic config that should be sent beforehand in order to get passed
// in the `NewProtocol` method if a Service has created the protocol and set the
// config with `SetConfig`. It can be nil.
func (o *Overlay) SendToTreeNode(from *Token, to *TreeNode, msg network.Message, io MessageProxy, c *GenericConfig) (uint64, error) {
tokenTo := from.ChangeTreeNodeID(to.ID)
var totSentLen uint64
// first send the config if present
if c != nil {
sentLen, err := o.server.Send(to.ServerIdentity, &ConfigMsg{*c, tokenTo.ID()})
totSentLen += sentLen
if err != nil {
log.Error("sending config failed:", err)
return totSentLen, err
}
}
// then send the message
var final interface{}
info := &OverlayMsg{
TreeNodeInfo: &TreeNodeInfo{
From: from,
To: tokenTo,
},
}
final, err := io.Wrap(msg, info)
if err != nil {
return totSentLen, err
}
sentLen, err := o.server.Send(to.ServerIdentity, final)
totSentLen += sentLen
return totSentLen, err
}
// nodeDone is called by node to signify that its work is finished and its
// ressources can be released
func (o *Overlay) nodeDone(tok *Token) {
o.instancesLock.Lock()
o.nodeDelete(tok)
o.instancesLock.Unlock()
}
// nodeDelete needs to be separated from nodeDone, as it is also called from
// Close, but due to locking-issues here we don't lock.
func (o *Overlay) nodeDelete(token *Token) {
tok := token.ID()
tni, ok := o.instances[tok]
if !ok {
log.Lvlf2("Node %s already gone", tok)
return
}
log.Lvl4("Closing node", tok)
err := tni.closeDispatch()
if err != nil {
log.Error("Error while closing node:", err)
}
delete(o.protocolInstances, tok)
delete(o.instances, tok)
// mark it done !
o.instancesInfo[tok] = true
}
func (o *Overlay) suite() network.Suite {
return o.server.Suite()
}
// Close calls all nodes, deletes them from the list and closes them
func (o *Overlay) Close() {
o.instancesLock.Lock()
defer o.instancesLock.Unlock()
for _, tni := range o.instances {
log.Lvl4(o.server.Address(), "Closing TNI", tni.TokenID())
o.nodeDelete(tni.Token())
}
}
// CreateProtocol creates a ProtocolInstance, registers it to the Overlay.
// Additionally, if sid is different than NilServiceID, sid is added to the token
// so the protocol will be picked up by the correct service and handled by its
// NewProtocol method. If the sid is NilServiceID, then the protocol is handled by onet alone.
func (o *Overlay) CreateProtocol(name string, t *Tree, sid ServiceID) (ProtocolInstance, error) {
io := o.protoIO.getByName(name)
tni := o.NewTreeNodeInstanceFromService(t, t.Root, ProtocolNameToID(name), sid, io)
pi, err := o.server.protocolInstantiate(tni.token.ProtoID, tni)
if err != nil {
return nil, err
}
if err = o.RegisterProtocolInstance(pi); err != nil {
return nil, err
}
go func() {
err := pi.Dispatch()
if err != nil {
log.Error(err)
}
}()
return pi, err
}
// StartProtocol will create and start a ProtocolInstance.
func (o *Overlay) StartProtocol(name string, t *Tree, sid ServiceID) (ProtocolInstance, error) {
pi, err := o.CreateProtocol(name, t, sid)
if err != nil {
return nil, err
}
go func() {
err := pi.Start()
if err != nil {
log.Error("Error while starting:", err)
}
}()
return pi, err
}
// NewTreeNodeInstanceFromProtoName takes a protocol name and a tree and
// instantiate a TreeNodeInstance for this protocol.
func (o *Overlay) NewTreeNodeInstanceFromProtoName(t *Tree, name string) *TreeNodeInstance {
io := o.protoIO.getByName(name)
return o.NewTreeNodeInstanceFromProtocol(t, t.Root, ProtocolNameToID(name), io)
}
// NewTreeNodeInstanceFromProtocol takes a tree and a treenode (normally the
// root) and and protocolID and returns a fresh TreeNodeInstance.
func (o *Overlay) NewTreeNodeInstanceFromProtocol(t *Tree, tn *TreeNode, protoID ProtocolID, io MessageProxy) *TreeNodeInstance {
tok := &Token{
TreeNodeID: tn.ID,
TreeID: t.ID,
RosterID: t.Roster.ID,
ProtoID: protoID,
RoundID: RoundID(uuid.NewV4()),
}
tni := o.newTreeNodeInstanceFromToken(tn, tok, io)
o.RegisterTree(t)
o.RegisterRoster(t.Roster)
return tni
}
// NewTreeNodeInstanceFromService takes a tree, a TreeNode and a service ID and
// returns a TNI.
func (o *Overlay) NewTreeNodeInstanceFromService(t *Tree, tn *TreeNode, protoID ProtocolID, servID ServiceID, io MessageProxy) *TreeNodeInstance {
tok := &Token{
TreeNodeID: tn.ID,
TreeID: t.ID,
RosterID: t.Roster.ID,
ProtoID: protoID,
ServiceID: servID,
RoundID: RoundID(uuid.NewV4()),
}
tni := o.newTreeNodeInstanceFromToken(tn, tok, io)
o.RegisterTree(t)
o.RegisterRoster(t.Roster)
return tni
}
// ServerIdentity Returns the entity of the Host
func (o *Overlay) ServerIdentity() *network.ServerIdentity {
return o.server.ServerIdentity
}
// newTreeNodeInstanceFromToken is to be called by the Overlay when it receives
// a message it does not have a treenodeinstance registered yet. The protocol is
// already running so we should *not* generate a new RoundID.
func (o *Overlay) newTreeNodeInstanceFromToken(tn *TreeNode, tok *Token, io MessageProxy) *TreeNodeInstance {
tni := newTreeNodeInstance(o, tok, tn, io)
o.instancesLock.Lock()
defer o.instancesLock.Unlock()
o.instances[tok.ID()] = tni
return tni
}
// ErrWrongTreeNodeInstance is returned when you already binded a TNI with a PI.
var ErrWrongTreeNodeInstance = errors.New("This TreeNodeInstance doesn't exist")
// ErrProtocolRegistered is when the protocolinstance is already registered to
// the overlay
var ErrProtocolRegistered = errors.New("a ProtocolInstance already has been registered using this TreeNodeInstance")
// RegisterProtocolInstance takes a PI and stores it for dispatching the message
// to it.
func (o *Overlay) RegisterProtocolInstance(pi ProtocolInstance) error {
o.instancesLock.Lock()
defer o.instancesLock.Unlock()
var tni *TreeNodeInstance
var tok = pi.Token()
var ok bool
// if the TreeNodeInstance doesn't exist
if tni, ok = o.instances[tok.ID()]; !ok {
return ErrWrongTreeNodeInstance
}
if tni.isBound() {
return ErrProtocolRegistered
}
tni.bind(pi)
o.protocolInstances[tok.ID()] = pi
log.Lvlf4("%s registered ProtocolInstance %x", o.server.Address(), tok.ID())
return nil
}
// RegisterMessageProxy registers a message proxy only for this overlay
func (o *Overlay) RegisterMessageProxy(m MessageProxy) {
o.protoIO.RegisterMessageProxy(m)
}
// pendingMsg is used to store messages destined for ProtocolInstances but when
// the tree designated is not known to the Overlay. When the tree is sent to the
// overlay, then the pendingMsg that are relying on this tree will get
// processed.
type pendingMsg struct {
*ProtocolMsg
MessageProxy
}
// treeNodeCache is a cache that maps from token to treeNode. Since
// the mapping is not 1-1 (many Tokens can point to one TreeNode, but
// one token leads to one TreeNode), we have to do certain lookup, but
// that's better than searching the tree each time.
type treeNodeCache struct {
Entries map[TreeID]*cacheEntry
stopCh chan (struct{})
stopOnce sync.Once
sync.Mutex
}
type cacheEntry struct {
treeNodeMap map[TreeNodeID]*TreeNode
expiration time.Time
}
var cacheTime = 5 * time.Minute
var cleanEvery = 1 * time.Minute
func newTreeNodeCache() *treeNodeCache {
tnc := &treeNodeCache{
Entries: make(map[TreeID]*cacheEntry),
stopCh: make(chan struct{}),
}
go tnc.cleaner()
return tnc
}
func (tnc *treeNodeCache) stop() {
tnc.stopOnce.Do(func() { close(tnc.stopCh) })
}
func (tnc *treeNodeCache) cleaner() {
for {
select {
case <-time.After(cleanEvery):
tnc.clean()
case <-tnc.stopCh:
return
}
}
}
func (tnc *treeNodeCache) clean() {
tnc.Lock()
now := time.Now()
for k := range tnc.Entries {
if now.After(tnc.Entries[k].expiration) {
delete(tnc.Entries, k)
}
}
tnc.Unlock()
}
// Set sets an entry in the cache. It will also cache the parent and
// children of the treenode since that's most likely what we are going
// to query.
func (tnc *treeNodeCache) Set(tree *Tree, treeNode *TreeNode) {
tnc.Lock()
ce, ok := tnc.Entries[tree.ID]
if !ok {
ce = &cacheEntry{
treeNodeMap: make(map[TreeNodeID]*TreeNode),
expiration: time.Now().Add(cacheTime),
}
}
// add treenode
ce.treeNodeMap[treeNode.ID] = treeNode
// add parent if not root
if treeNode.Parent != nil {
ce.treeNodeMap[treeNode.Parent.ID] = treeNode.Parent
}
// add children
for _, c := range treeNode.Children {
ce.treeNodeMap[c.ID] = c
}
// add cache
tnc.Entries[tree.ID] = ce
tnc.Unlock()
}
// GetFromToken returns the TreeNode that the token is pointing at, or
// nil if there is none for this token.
func (tnc *treeNodeCache) GetFromToken(tok *Token) *TreeNode {
tnc.Lock()
defer tnc.Unlock()
if tok == nil {
return nil
}
ce, ok := tnc.Entries[tok.TreeID]
if !ok || time.Now().After(ce.expiration) {
// no tree cached for this token
return nil
}
ce.expiration = time.Now().Add(cacheTime)
tn, ok := ce.treeNodeMap[tok.TreeNodeID]
if !ok {
// no treeNode cached for this token
return nil
}
return tn
}
// defaultProtoIO implements the ProtocoIO interface but using the "regular/old"
// wire format protocol,i.e. it wraps a message into a ProtocolMessage
type defaultProtoIO struct {
suite network.Suite
}
// Wrap implements the MessageProxy interface for the Overlay.
func (d *defaultProtoIO) Wrap(msg interface{}, info *OverlayMsg) (interface{}, error) {
if msg != nil {
buff, err := network.Marshal(msg)
if err != nil {
return nil, err
}
typ := network.MessageType(msg)
protoMsg := &ProtocolMsg{
From: info.TreeNodeInfo.From,
To: info.TreeNodeInfo.To,
MsgSlice: buff,
MsgType: typ,
}
return protoMsg, nil
}
var returnMsg interface{}
switch true {
case info.RequestTree != nil:
returnMsg = info.RequestTree
case info.RequestRoster != nil:
returnMsg = info.RequestRoster
case info.TreeMarshal != nil:
returnMsg = info.TreeMarshal
case info.Roster != nil:
returnMsg = info.Roster
default:
panic("overlay: default wrapper has nothing to wrap")
}
return returnMsg, nil
}
// Unwrap implements the MessageProxy interface for the Overlay.
func (d *defaultProtoIO) Unwrap(msg interface{}) (interface{}, *OverlayMsg, error) {
var returnMsg interface{}
var returnOverlay = new(OverlayMsg)
var err error
switch inner := msg.(type) {
case *ProtocolMsg:
onetMsg := inner
var err error
_, protoMsg, err := network.Unmarshal(onetMsg.MsgSlice, d.suite)
if err != nil {
return nil, nil, err
}
// Put the msg into ProtocolMsg
returnOverlay.TreeNodeInfo = &TreeNodeInfo{
To: onetMsg.To,
From: onetMsg.From,
}
returnMsg = protoMsg
case *RequestTree:
returnOverlay.RequestTree = inner
case *RequestRoster:
returnOverlay.RequestRoster = inner
case *TreeMarshal:
returnOverlay.TreeMarshal = inner
case *Roster:
returnOverlay.Roster = inner
default:
err = errors.New("default protoIO: unwraping an unknown message type")
}
return returnMsg, returnOverlay, err
}
// Unwrap implements the MessageProxy interface for the Overlay.
func (d *defaultProtoIO) PacketType() network.MessageTypeID {
return network.MessageTypeID([16]byte{})
}
// Name implements the MessageProxy interface. It returns the value "default".
func (d *defaultProtoIO) Name() string {
return "default"
}