/
peer.go
244 lines (221 loc) · 5.26 KB
/
peer.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
package node
import (
"crypto/rsa"
"fmt"
xc "github.com/jddixon/xlCrypto_go"
xi "github.com/jddixon/xlNodeID_go"
xo "github.com/jddixon/xlOverlay_go"
xt "github.com/jddixon/xlTransport_go"
"strings"
"sync"
"time"
)
/**
* A Peer is another Node, a neighbor.
*/
type Peer struct {
connectors []xt.ConnectorI // to reach the peer
timeout int64 // ns from epoch
contacted int64 // last contact from this peer, ns from epoch
up bool // set to false if considered unreachable
mu sync.Mutex
BaseNode
}
func NewNewPeer(name string, id *xi.NodeID) (*Peer, error) {
return NewPeer(name, id, nil, nil, nil, nil)
}
func NewPeer(name string, id *xi.NodeID,
ck *rsa.PublicKey, sk *rsa.PublicKey,
o []xo.OverlayI, c []xt.ConnectorI) (p *Peer, err error) {
baseNode, err := NewBaseNode(name, id, ck, sk, o)
if err == nil {
var ctors []xt.ConnectorI // another empty slice
if c != nil {
count := len(c)
for i := 0; i < count; i++ {
ctors = append(ctors, c[i])
}
}
p = &Peer{
connectors: ctors,
BaseNode: *baseNode,
}
}
return
}
// Given a node, construct a Peer with the same properties.
func NewPeerFromNode(node *Node) (p *Peer, err error) {
if node == nil {
err = NilNode
} else {
id := node.GetNodeID().Value()
nodeID, err := xi.New(id)
if err == nil {
var o []xo.OverlayI
for i := 0; i < node.SizeOverlays(); i++ {
o = append(o, node.GetOverlay(i))
}
var ctors []xt.ConnectorI
for i := 0; i < node.SizeAcceptors(); i++ {
var ctor *xt.TcpConnector
ep := node.GetAcceptor(i).GetEndPoint()
ctor, err = xt.NewTcpConnector(ep)
if err != nil {
break
}
ctors = append(ctors, ctor)
}
if err == nil {
p, err = NewPeer(node.GetName(), nodeID,
node.GetCommsPublicKey(), node.GetSigPublicKey(),
o, ctors)
}
}
}
return
}
// CONNECTORS ///////////////////////////////////////////////////////
func (p *Peer) AddConnector(c xt.ConnectorI) error {
if c == nil {
return NilConnector
}
p.connectors = append(p.connectors, c)
return nil
}
/** @return a count of known Connectors for this Peer */
func (p *Peer) SizeConnectors() int {
return len(p.connectors)
}
/**
* Return a Connector, an Address-Protocol pair identifying
* an Acceptor for the Peer.
*
* XXX Could as easily return an EndPoint.
*
* @return the Nth Connector
*/
func (p *Peer) GetConnector(n int) xt.ConnectorI {
return p.connectors[n]
}
// EQUAL ////////////////////////////////////////////////////////////
func (p *Peer) Equal(any interface{}) bool {
if any == p {
return true
}
if any == nil {
return false
}
switch v := any.(type) {
case *Peer:
_ = v
default:
return false
}
other := any.(*Peer) // type assertion
// XXX THIS IS A VERY INCOMPLETE IMPLEMENTATION
return p.BaseNode.Equal(&other.BaseNode)
}
func (p *Peer) Strings() []string {
ss := []string{"peer {"}
bns := p.BaseNode.Strings()
for i := 0; i < len(bns); i++ {
ss = append(ss, " "+bns[i])
}
ss = append(ss, " connectors {")
for i := 0; i < len(p.connectors); i++ {
ss = append(ss, fmt.Sprintf(" %s", p.connectors[i].String()))
}
ss = append(ss, " }")
ss = append(ss, "}")
return ss
}
func (p *Peer) String() string {
return strings.Join(p.Strings(), "\n")
}
func CollectConnectors(peer *Peer, ss []string) (rest []string, err error) {
rest = ss
line, err := xc.NextNBLine(&rest)
if err == nil {
if line == "connectors {" {
for {
line, err = xc.NextNBLine(&rest)
if err == nil {
if line == "}" {
break
}
}
var ctor xt.ConnectorI
ctor, err = xt.ParseConnector(line)
if err != nil {
return
}
err = peer.AddConnector(ctor)
if err != nil {
return
}
}
}
}
// if there are no connectors, not a very useful peer
return
}
func ParsePeer(s string) (peer *Peer, rest []string, err error) {
ss := strings.Split(s, "\n")
return ParsePeerFromStrings(ss)
}
func ParsePeerFromStrings(ss []string) (peer *Peer, rest []string, err error) {
bn, rest, err := ParseBNFromStrings(ss, "peer")
if err == nil {
peer = &Peer{BaseNode: *bn}
rest, err = CollectConnectors(peer, rest)
if err == nil {
var line string
line, err = xc.NextNBLine(&rest)
if err == nil {
if line != "}" {
err = NotASerializedPeer
}
}
}
}
return
}
// LIVENESS /////////////////////////////////////////////////////////
// Return the time (ns from the Epoch) of the last communication with
// this peer.
func (p *Peer) LastContact() int64 {
p.mu.Lock()
defer p.mu.Unlock()
return p.contacted
}
// A communication with the peer has occurred: mark the time.
func (p *Peer) StillAlive() {
t := time.Now().UnixNano()
p.mu.Lock()
p.contacted = t
p.mu.Unlock()
}
// Return whether the peer is considered reachable.
func (p *Peer) IsUp() bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.up
}
// Clear the peer's up flag. It is no longer considered reachable.
// Return the flag's previous state.
func (p *Peer) MarkDown() (prevState bool) {
p.mu.Lock()
defer p.mu.Unlock()
prevState = p.up
p.up = false
return
}
// Set the peer's up flag. It is now considered reachable. Return
// the flag's previous state.
func (p *Peer) MarkUp() (prevState bool) {
p.mu.Lock()
defer p.mu.Unlock()
prevState = p.up
p.up = true
return
}