-
Notifications
You must be signed in to change notification settings - Fork 106
/
host_msg.go
131 lines (110 loc) · 3.68 KB
/
host_msg.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
package sda
import (
"sync"
"github.com/satori/go.uuid"
"gopkg.in/dedis/cothority.v0/lib/network"
)
// SDAData is to be embedded in every message that is made for a
// ID of SDAData message as registered in network
var SDADataMessageID = network.RegisterMessageType(Data{})
// ID of RequestTree message as registered in network
var RequestTreeMessageID = network.RegisterMessageType(RequestTree{})
// ID of RequestEntityList message as registered in network
var RequestEntityListMessageID = network.RegisterMessageType(RequestEntityList{})
// ID of TreeMarshal message as registered in network
var SendTreeMessageID = TreeMarshalTypeID
// ID of EntityList message as registered in network
var SendEntityListMessageID = EntityListTypeID
// Data is to be embedded in every message that is made for a
// ProtocolInstance
type Data struct {
// Token uniquely identify the protocol instance this msg is made for
From *Token
// The TreeNodeId Where the message goes to
To *Token
// NOTE: this is taken from network.NetworkMessage
Entity *network.Entity
// MsgType of the underlying data
MsgType network.MessageTypeID
// The interface to the actual Data
Msg network.ProtocolMessage
// The actual data as binary blob
MsgSlice []byte
// Config the actual config
Config GenericConfig
}
// RoundID uniquely identifies a round of a protocol run
type RoundID uuid.UUID
// String returns the canonical representation of the rounds ID (wrapper around
// uuid.UUID.String())
func (rId RoundID) String() string {
return uuid.UUID(rId).String()
}
// TokenID uniquely identifies the start and end-point of a message by an ID
// (see Token struct)
type TokenID uuid.UUID
func (t *TokenID) String() string {
return uuid.UUID(*t).String()
}
// A Token contains all identifiers needed to uniquely identify one protocol
// instance. It gets passed when a new protocol instance is created and get used
// by every protocol instance when they want to send a message. That way, the
// host knows how to create the SDAData message around the protocol's message
// with the right fields set.
type Token struct {
EntityListID EntityListID
TreeID TreeID
// TO BE REMOVED
ProtoID ProtocolID
ServiceID ServiceID
RoundID RoundID
// TreeNodeID is defined by the
TreeNodeID TreeNodeID
cacheId TokenID
}
// Global mutex when we're working on Tokens. Needed because we
// copy Tokens in ChangeTreeNodeID.
var tokenMutex sync.Mutex
// Id returns the TokenID which can be used to identify by token in map
func (t *Token) Id() TokenID {
tokenMutex.Lock()
defer tokenMutex.Unlock()
if t.cacheId == TokenID(uuid.Nil) {
url := network.NamespaceURL + "token/" + t.EntityListID.String() +
t.RoundID.String() + t.ServiceID.String() + t.ProtoID.String() + t.TreeID.String() +
t.TreeNodeID.String()
t.cacheId = TokenID(uuid.NewV5(uuid.NamespaceURL, url))
}
return t.cacheId
}
// Clone returns a new token out of this one
func (t *Token) Clone() *Token {
t2 := *t
return &t2
}
// ChangeTreeNodeID return a new Token containing a reference to the given
// TreeNode
func (t *Token) ChangeTreeNodeID(newid TreeNodeID) *Token {
tokenMutex.Lock()
defer tokenMutex.Unlock()
t_other := *t
t_other.TreeNodeID = newid
t_other.cacheId = TokenID(uuid.Nil)
return &t_other
}
// RequestTree is used to ask the parent for a given Tree
type RequestTree struct {
// The treeID of the tree we want
TreeID TreeID
}
// RequestEntityList is used to ask the parent for a given EntityList
type RequestEntityList struct {
EntityListID EntityListID
}
// EntityListUnknown is used in case the entity list is unknown
type EntityListUnknown struct {
}
// SendEntity is the first message we send on creation of a link
type SendEntity struct {
Name string
}