-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
46 lines (37 loc) · 1.08 KB
/
Copy pathnode.go
File metadata and controls
46 lines (37 loc) · 1.08 KB
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
package mycelium
import "fmt"
type Node struct {
Address string
Port uint16
NodeState
}
func (n Node) Name() string {
return fmt.Sprintf("%s:%d", n.Address, n.Port)
}
// NodeState represents the state and the counter (an amount of times this state was confirmed)
type NodeState int16
const (
UnknownNodeState NodeState = 0
AliveNodeState NodeState = 1
SuspectedNodeState NodeState = 2
DeadNodeState NodeState = 3
)
// Is returns true if NodeStates are the same (ignoring the count).
// Equal is not working, since NodeState also contains 8 bit conuter part, that doesn't affect the state.
func (ns NodeState) Is(other NodeState) bool {
nsLast := (int16(ns) << 8) >> 8
otherLast := (int16(other) << 8) >> 8
return nsLast == otherLast
}
// Count returns the count part from the NodeState.
func (ns NodeState) Count() int {
return int(ns) >> 8
}
// Inc increments the NodeState count.
func (ns NodeState) Inc() NodeState {
return ns + (1 << 8)
}
// Flush makes NodeState's count equal to 0.
func (ns NodeState) Flush() NodeState {
return NodeState((int16(ns) << 8) >> 8)
}