-
Notifications
You must be signed in to change notification settings - Fork 29
/
overlay.go
903 lines (801 loc) · 26.3 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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
package onet
import (
"fmt"
"sync"
"time"
"github.com/google/uuid"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
"golang.org/x/xerrors"
)
// timeout used to clean up protocol state so that children can keep
// asking for resources for this range of time (i.e. tree)
const globalProtocolTimeout = 10 * time.Minute
// 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
treeStorage *treeStorage
// 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,
treeStorage: newTreeStorage(globalProtocolTimeout),
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
ResponseTreeMsgID, // send a tree back to a request
RequestRosterMsgID,
SendRosterMsgID,
SendTreeMsgID,
ConfigMsgID) // fetch config information
return o
}
// 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 {
case info.RequestTree != nil:
o.handleRequestTree(env.ServerIdentity, info.RequestTree, io)
case info.ResponseTree != nil:
o.handleSendTree(env.ServerIdentity, info.ResponseTree, io)
case info.TreeMarshal != nil:
o.handleSendTreeMarshal(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,
Size: env.Size,
}
err = o.TransmitMsg(protoMsg, io)
if err != nil {
log.Errorf("Msg %s from %s produced error: %+v", protoMsg.MsgType,
protoMsg.ServerIdentity, err)
}
}
}
// 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 falls back to the default wire protocol.
func (o *Overlay) TransmitMsg(onetMsg *ProtocolMsg, io MessageProxy) error {
if onetMsg != nil && onetMsg.From != nil {
log.TraceID(onetMsg.From.RoundID[:])
}
log.Lvl3("got new message of type:", onetMsg.MsgType)
// Get the tree if it exists and prevent any pending deletion
// if required. The tree will be clean when this instance is
// over (or the last instance using the tree).
tree := o.treeStorage.getAndRefresh(onetMsg.To.TreeID)
if tree == nil {
// request anyway because we need to store the pending message
// the following routine will take care of requesting once
err := o.requestTree(onetMsg.ServerIdentity, onetMsg, io)
if err != nil {
return xerrors.Errorf("requesting tree: %v", err)
}
return nil
}
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.TreeNodeFromTree(tree, onetMsg.To.TreeNodeID)
if err != nil {
return xerrors.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())
if config == nil {
config = onetMsg.Config
}
tni.config = config
// request the PI from the Service and binds the two
pi, err = o.server.serviceManager.newProtocol(tni, config)
if err != nil {
o.instancesLock.Lock()
o.nodeDelete(onetMsg.To)
o.instancesLock.Unlock()
return xerrors.Errorf("creating protocol: %v", err)
}
if pi == nil {
return nil
}
go func() {
if onetMsg != nil && onetMsg.From != nil {
log.TraceID(onetMsg.From.RoundID[:])
}
defer func() {
if r := recover(); r != nil {
svc := ServiceFactory.Name(tni.Token().ServiceID)
log.Errorf("Panic in call to protocol <%s>.Dispatch("+
") from service <%s> at address %s: %v",
tni.ProtocolName(), svc, o.server.ServerIdentity, r)
log.Error(log.Stack())
}
}()
err := pi.Dispatch()
if err != nil {
svc := ServiceFactory.Name(tni.Token().ServiceID)
log.Errorf("%v %s.Dispatch() returned error %+v",
o.server.ServerIdentity, svc, err)
}
}()
if err := o.RegisterProtocolInstance(pi); err != nil {
return xerrors.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
log.Lvl4("starting procprotomsg")
pi.ProcessProtocolMsg(onetMsg)
log.Lvl4("done with procprotomsg")
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) {
// This goroutine has no recover because the underlying code should never panic
// and TransmitMsg does its own recovering
go func() {
o.pendingMsgLock.Lock()
var newPending []pendingMsg
var remaining []pendingMsg
// Keep msg not related to that tree in the pending list
for _, msg := range o.pendingMsg {
if t.ID.Equal(msg.To.TreeID) {
remaining = append(remaining, msg)
} else {
newPending = append(newPending, msg)
}
}
o.pendingMsg = newPending
o.pendingMsgLock.Unlock()
for _, msg := range remaining {
err := o.TransmitMsg(msg.ProtocolMsg, msg.MessageProxy)
if err != nil {
log.Error("TransmitMsg failed:", err)
continue
}
}
}()
}
// 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 roster
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)
// try to prepare the message before locking the storage
msg, err := io.Wrap(nil, &OverlayMsg{
RequestTree: &RequestTree{TreeID: onetMsg.To.TreeID, Version: 1},
})
if err != nil {
return xerrors.Errorf("wrapping message: %v", err)
}
if o.treeStorage.IsRegistered(onetMsg.To.TreeID) {
// request already sent
return nil
}
// register the tree as known (can be stored)
o.treeStorage.Register(onetMsg.To.TreeID)
// no need to record sentLen because Overlay uses Server's CounterIO
_, err = o.server.Send(si, msg)
if err != nil {
o.treeStorage.Unregister(onetMsg.To.TreeID)
return xerrors.Errorf("sending tree request: %v", err)
}
return nil
}
// RegisterTree takes a tree and puts it in the map
func (o *Overlay) RegisterTree(t *Tree) {
o.treeStorage.Set(t)
o.checkPendingMessages(t)
}
// TreeNodeFromTree returns the treeNode corresponding to the id
func (o *Overlay) TreeNodeFromTree(tree *Tree, id TreeNodeID) (*TreeNode, error) {
tn := tree.Search(id)
if tn == nil {
return nil, xerrors.New("didn't find treenode")
}
return tn, nil
}
// TreeNodeFromToken returns the treeNode corresponding to a token
// Deprecated: only the overlay should create treeNodes but use
// TreeNodeFromTree if you need to create one treeNode
func (o *Overlay) TreeNodeFromToken(t *Token) (*TreeNode, error) {
if t == nil {
return nil, xerrors.New("didn't find tree-node: No token given")
}
tree := o.treeStorage.Get(t.TreeID)
if tree == nil {
return nil, xerrors.New("didn't find tree")
}
tn := tree.Search(t.TreeNodeID)
if tn == nil {
return nil, xerrors.New("didn't find treenode")
}
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()
}
// Send the tree or do nothing when it is not known
func (o *Overlay) handleRequestTree(si *network.ServerIdentity, req *RequestTree, io MessageProxy) {
tree := o.treeStorage.Get(req.TreeID)
if tree == nil {
// XXX Should we think of a way of sending back an "error" ?
log.Error("couldn't find the tree")
return
}
treeM := tree.MakeTreeMarshal()
if req.Version == 0 {
log.Warnf("[DEPRECATION] got an old version of the RequestTree from %s", si)
o.handleRequestTreeDeprecated(si, treeM, io)
return
}
msg, err := io.Wrap(nil, &OverlayMsg{
ResponseTree: &ResponseTree{
TreeMarshal: treeM,
Roster: tree.Roster,
},
})
if err != nil {
log.Error("couldn't wrap ResponseTree:", err)
return
}
_, err = o.server.Send(si, msg)
if err != nil {
log.Error("Couldn't send tree:", err)
}
}
// Deprecated: a new handler has been written to handle RequestTree messages
func (o *Overlay) handleRequestTreeDeprecated(si *network.ServerIdentity, tm *TreeMarshal, io MessageProxy) {
msg, err := io.Wrap(nil, &OverlayMsg{TreeMarshal: tm})
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)
}
}
// Deprecated: recent versions use the message ResponseTree
func (o *Overlay) handleSendTreeMarshal(si *network.ServerIdentity, tm *TreeMarshal, io MessageProxy) {
if tm.TreeID.IsNil() {
log.Error("received en empty tree")
return
}
if !o.treeStorage.IsRegistered(tm.TreeID) {
// we only accept known trees to prevent a denial of service
// by filling up the storage
log.Error("ignoring unknown tree")
return
}
var ro *Roster
for _, inst := range o.instances {
if inst.Roster().ID.Equal(tm.RosterID) {
ro = inst.Roster()
}
}
if ro == nil {
log.Lvl1("unknown roster")
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)
}
o.addPendingTreeMarshal(tm)
return
}
rt := &ResponseTree{TreeMarshal: tm, Roster: ro}
o.handleSendTree(si, rt, io)
}
// Receive a tree from a peer
func (o *Overlay) handleSendTree(si *network.ServerIdentity, rt *ResponseTree, io MessageProxy) {
if rt.TreeMarshal == nil || rt.TreeMarshal.TreeID.IsNil() {
log.Error("received an empty tree")
return
}
if rt.Roster == nil {
log.Error("received an empty roster")
return
}
if !o.treeStorage.IsRegistered(rt.TreeMarshal.TreeID) {
// we only accept known trees to prevent a denial of service
// by filling up the storage
log.Error("ignoring unknown tree")
return
}
tree, err := rt.TreeMarshal.MakeTree(rt.Roster)
if err != nil {
log.Error("Couldn't create tree:", err)
return
}
log.Lvl4("Received new tree")
o.RegisterTree(tree)
}
// Deprecated: roster is not sent anymore, only the tree
func (o *Overlay) handleRequestRoster(si *network.ServerIdentity, req *RequestRoster, io MessageProxy) {
ro := o.treeStorage.GetRoster(req.RosterID)
if ro == nil {
// XXX Bad reaction to request...
log.Lvl2("Requested roster that we don't have")
ro = &Roster{}
}
msg, err := io.Wrap(nil, &OverlayMsg{Roster: ro})
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 roster from host:", o.server.ServerIdentity.String(), err)
return
}
}
// Deprecated: roster is not sent anymore, only the tree
func (o *Overlay) handleSendRoster(si *network.ServerIdentity, roster *Roster) {
if roster.ID.IsNil() {
log.Error("received an empty roster")
return
}
o.checkPendingTreeMarshal(roster)
}
// 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 happen only if a bad packet gets through
log.Error(o.server.Address(), "Wrong config type, most likely invalid packet got through.")
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)
// first send the config if present
var confMsg *ConfigMsg
if c != nil {
confMsg = &ConfigMsg{*c, tokenTo.ID()}
}
// then send the message
var final interface{}
info := &OverlayMsg{
// For backwards compatibility reason, we keep sending the config message
// but we also send it with the actual message to be sure it will be handled.
Config: c,
TreeNodeInfo: &TreeNodeInfo{
From: from,
To: tokenTo,
},
}
final, err := io.Wrap(msg, info)
if err != nil {
return 0, xerrors.Errorf("wrapping message: %v", err)
}
var sentLen uint64
if confMsg != nil {
sentLen, err = o.server.Send(to.ServerIdentity, confMsg, final)
} else {
sentLen, err = o.server.Send(to.ServerIdentity, final)
}
if err != nil {
err = xerrors.Errorf("sending: %v", err)
}
return sentLen, 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)
o.cleanTreeStorage(token)
// mark it done !
o.instancesInfo[tok] = true
}
// checks if another instance is using the same tree and clean it
// only if not. Note that this function assumes that o.instances
// is locked (e.g. Overlay.nodeDone)
func (o *Overlay) cleanTreeStorage(token *Token) {
notUsed := true
for _, inst := range o.instances {
if inst.token.TreeID.Equal(token.TreeID) {
// another instance is using the same tree
notUsed = false
}
}
if notUsed {
o.treeStorage.Remove(token.TreeID)
}
}
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())
}
// force cleaning routines to shutdown
o.treeStorage.Close()
}
// 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, xerrors.Errorf("instantiating protocol: %v", err)
}
if err = o.RegisterProtocolInstance(pi); err != nil {
return nil, xerrors.Errorf("registering protocol instance: %v", err)
}
go func() {
defer func() {
if r := recover(); r != nil {
log.Errorf("Panic in %s.Dispatch(): %v", name, r)
log.Error(log.Stack())
}
}()
err := pi.Dispatch()
if err != nil {
log.Errorf("%s.Dispatch() created in service %s returned error %s",
name, ServiceFactory.Name(sid), 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, xerrors.Errorf("creating protocol: %v", err)
}
go func() {
defer func() {
if r := recover(); r != nil {
log.Errorf("Panic in %s.Start(): %v", name, r)
log.Error(log.Stack())
}
}()
err := pi.Start()
if err != nil {
log.Error("Error while starting:", err)
}
}()
return pi, nil
}
// 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.Must(uuid.NewRandom())),
}
tni := o.newTreeNodeInstanceFromToken(tn, tok, io)
o.RegisterTree(t)
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.Must(uuid.NewRandom())),
}
tni := o.newTreeNodeInstanceFromToken(tn, tok, io)
o.RegisterTree(t)
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 = xerrors.New("This TreeNodeInstance doesn't exist")
// ErrProtocolRegistered is when the protocolinstance is already registered to
// the overlay
var ErrProtocolRegistered = xerrors.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
}
// 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, xerrors.Errorf("marshaling: %v", err)
}
typ := network.MessageType(msg)
protoMsg := &ProtocolMsg{
From: info.TreeNodeInfo.From,
To: info.TreeNodeInfo.To,
Config: info.Config,
MsgSlice: buff,
MsgType: typ,
}
return protoMsg, nil
}
var returnMsg interface{}
switch {
case info.RequestTree != nil:
returnMsg = info.RequestTree
case info.ResponseTree != nil:
returnMsg = info.ResponseTree
case info.TreeMarshal != nil:
returnMsg = info.TreeMarshal
case info.RequestRoster != nil:
returnMsg = info.RequestRoster
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, xerrors.Errorf("unmarshaling: %v", err)
}
// Put the msg into ProtocolMsg
returnOverlay.TreeNodeInfo = &TreeNodeInfo{
To: onetMsg.To,
From: onetMsg.From,
}
returnMsg = protoMsg
case *RequestTree:
returnOverlay.RequestTree = inner
case *ResponseTree:
returnOverlay.ResponseTree = inner
case *TreeMarshal:
returnOverlay.TreeMarshal = inner
case *RequestRoster:
returnOverlay.RequestRoster = inner
case *Roster:
returnOverlay.Roster = inner
default:
err = xerrors.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"
}