-
Notifications
You must be signed in to change notification settings - Fork 10
/
peer.go
160 lines (140 loc) · 4.33 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
package graph
import (
"context"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/controllerbus/config"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/aperturerobotics/controllerbus/controller/configset"
"github.com/aperturerobotics/controllerbus/controller/resolver/static"
"github.com/libp2p/go-libp2p/core/crypto"
)
// FactoryCtor is a constructor for a factory.
type FactoryCtor func(bus.Bus) controller.Factory
// FactoryAdder is a adder for a factory.
type FactoryAdder func(b bus.Bus, sr *static.Resolver)
// Peer is a participating peer in the network.
// This corresponds to a controller bus and an inproc transport.
type Peer struct {
Node
// peerPriv is the private key
peerPriv crypto.PrivKey
// peerID is the peer id
peerID peer.ID
// extraControllers contains extra controller configurations.
extraControllers configset.ConfigSet
// extraFactories contains extra factory constructors.
extraFactories []FactoryCtor
// extraFactoryAdders contains extra factory adders.
extraFactoryAdders []FactoryAdder
}
// GenerateAddPeer generates a peer private key and adds it.
func GenerateAddPeer(ctx context.Context, g *Graph) (*Peer, error) {
npeer, err := peer.NewPeer(nil)
if err != nil {
return nil, err
}
peerPriv, err := npeer.GetPrivKey(ctx)
if err != nil {
return nil, err
}
return AddPeer(g, peerPriv)
}
// AddPeer adds a peer to the network graph.
func AddPeer(g *Graph, peerPriv crypto.PrivKey) (*Peer, error) {
peerID, err := peer.IDFromPrivateKey(peerPriv)
if err != nil {
return nil, err
}
l := &Peer{
Node: g.BuildNode(),
peerID: peerID,
peerPriv: peerPriv,
extraControllers: configset.ConfigSet{},
}
g.AddNode(l)
return l, nil
}
// AddFactory adds a factory constructor to the set.
func (p *Peer) AddFactory(ct FactoryCtor) {
p.extraFactories = append(p.extraFactories, ct)
}
// AddFactoryAdder adds a factory adder to the set.
func (p *Peer) AddFactoryAdder(ct FactoryAdder) {
p.extraFactoryAdders = append(p.extraFactoryAdders, ct)
}
// GetExtraFactories returns the slice of extra factories.
func (p *Peer) GetExtraFactories() []FactoryCtor {
return p.extraFactories
}
// GetExtraFactoryAdders returns the slice of extra factory adders.
func (p *Peer) GetExtraFactoryAdders() []FactoryAdder {
return p.extraFactoryAdders
}
// MergeConfigSet merges in a configset to the extra controllers set.
func (p *Peer) MergeConfigSet(other configset.ConfigSet) {
for k, v := range other {
p.extraControllers[k] = v
}
}
// AddConfig adds a controller configuration to the peer.
func (p *Peer) AddConfig(id string, conf config.Config) {
p.AddControllerConfig(id, configset.NewControllerConfig(1, conf))
}
// GetConfigSet returns the extra controllers config set.
func (p *Peer) GetConfigSet() configset.ConfigSet {
return p.extraControllers
}
// DeleteConfig removes a configuration from the configset with id.
func (p *Peer) DeleteConfig(id string) {
delete(p.extraControllers, id)
}
// AddControllerConfig adds a controller configuration to the peer.
func (p *Peer) AddControllerConfig(id string, conf configset.ControllerConfig) {
p.extraControllers[id] = conf
}
// GetPeerID returns the peer ID.
func (p *Peer) GetPeerID() peer.ID {
return p.peerID
}
// GetPeerPriv returns the peer private key.
func (p *Peer) GetPeerPriv() crypto.PrivKey {
return p.peerPriv
}
// GetLinkedPeers returns all peers that should have a link with the peer.
// This includes other peers directly linked as well as those linked by a lan or multiple lans.
func (p *Peer) GetLinkedPeers(g *Graph) []*Peer {
var stack []Node = g.FromNodes(p)
var peers []*Peer
seenNodes := map[Node]struct{}{p: {}}
for len(stack) != 0 {
nn := stack[len(stack)-1]
stack[len(stack)-1] = nil
stack = stack[:len(stack)-1]
if _, ok := seenNodes[nn]; ok {
continue
}
seenNodes[nn] = struct{}{}
switch nnp := nn.(type) {
case *LAN:
// if it's a lan, continue traversal
stack = append(stack, g.FromNodes(nn)...)
case *Peer:
peers = append(peers, nnp)
}
}
return peers
}
// AllPeers returns the set of all peers in the graph.
func (g *Graph) AllPeers() []*Peer {
it := g.graph.Nodes()
var nodes []*Peer
for it.Next() {
nod := it.Node()
p, pOk := nod.(*Peer)
if pOk {
nodes = append(nodes, p)
}
}
return nodes
}