-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprotocol.go
84 lines (71 loc) · 2.22 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package protocol
/*
The `NewProtocol` method is used to define the protocol and to register
the handlers that will be called if a certain type of message is received.
The handlers will be treated according to their signature.
The protocol-file defines the actions that the protocol needs to do in each
step. The root-node will call the `Start`-method of the protocol. Each
node will only use the `Handle`-methods, and not call `Start` again.
*/
import (
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
)
func init() {
network.RegisterMessage(Announce{})
network.RegisterMessage(Reply{})
_, err := onet.GlobalProtocolRegister(Name, NewProtocol)
if err != nil {
panic(err)
}
}
// TemplateProtocol holds the state of a given protocol.
//
// For this example, it defines a channel that will receive the number
// of children. Only the root-node will write to the channel.
type TemplateProtocol struct {
*onet.TreeNodeInstance
announceChan chan announceWrapper
repliesChan chan []replyWrapper
ChildCount chan int
}
// Check that *TemplateProtocol implements onet.ProtocolInstance
var _ onet.ProtocolInstance = (*TemplateProtocol)(nil)
// NewProtocol initialises the structure for use in one round
func NewProtocol(n *onet.TreeNodeInstance) (onet.ProtocolInstance, error) {
t := &TemplateProtocol{
TreeNodeInstance: n,
ChildCount: make(chan int),
}
if err := n.RegisterChannels(&t.announceChan, &t.repliesChan); err != nil {
return nil, err
}
return t, nil
}
// Start sends the Announce-message to all children
func (p *TemplateProtocol) Start() error {
log.Lvl3(p.ServerIdentity(), "Starting TemplateProtocol")
return p.SendTo(p.TreeNode(), &Announce{"cothority rulez!"})
}
// Dispatch implements the main logic of the protocol. The function is only
// called once. The protocol is considered finished when Dispatch returns and
// Done is called.
func (p *TemplateProtocol) Dispatch() error {
defer p.Done()
ann := <-p.announceChan
if p.IsLeaf() {
return p.SendToParent(&Reply{1})
}
p.SendToChildren(&ann.Announce)
replies := <-p.repliesChan
n := 1
for _, c := range replies {
n += c.ChildrenCount
}
if !p.IsRoot() {
return p.SendToParent(&Reply{n})
}
p.ChildCount <- n
return nil
}