-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
194 lines (166 loc) · 4.91 KB
/
main.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
package main
import (
"bytes"
"context"
"os"
"testing"
"time"
"github.com/blang/semver"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
bcli "github.com/aperturerobotics/bifrost/cli"
nats "github.com/aperturerobotics/bifrost/pubsub/nats"
nats_controller "github.com/aperturerobotics/bifrost/pubsub/nats/controller"
"github.com/aperturerobotics/bifrost/sim/tests"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/bifrost/pubsub"
pubsub_relay "github.com/aperturerobotics/bifrost/pubsub/relay"
"github.com/aperturerobotics/bifrost/sim/graph"
"github.com/aperturerobotics/bifrost/sim/simulate"
"github.com/aperturerobotics/controllerbus/controller"
)
var logTrace = false
var (
addPeer = tests.AddPeer
initSimulator = tests.InitSimulator
)
var privKeyPath string
var bDaemonArgs bcli.DaemonArgs
// Version is the version of the controller implementation.
var Version = semver.MustParse("0.0.1")
func main() {
app := cli.NewApp()
app.Name = "nats-host"
app.Usage = "bifrost with embedded nats router example"
app.HideVersion = true
app.Action = runNatsExample
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "priv-key",
Usage: "path to private key to use",
Value: "priv-key.pem",
Destination: &privKeyPath,
},
}
app.Flags = append(
app.Flags,
(&bDaemonArgs).BuildFlags()...,
)
if err := app.Run(os.Args); err != nil {
logrus.Fatal(err.Error())
}
}
func runNatsExample(c *cli.Context) error {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
g := graph.NewGraph()
p0 := addPeer(nil, g)
p1 := addPeer(nil, g)
p2 := addPeer(nil, g)
lan1 := graph.AddLAN(g)
lan1.AddPeer(g, p0)
lan1.AddPeer(g, p1) // XXX
// lan2 := graph.AddLAN(g)
// lan2.AddPeer(g, p1)
// lan2.AddPeer(g, p2)
lan1.AddPeer(g, p2)
// replicate p2 -> [lan2] -> p1 -> [lan1] -> p0
// Add pubsub configurations.
topics := []string{"test-topic-1"}
for _, peer := range g.AllPeers() {
peer.AddFactory(func(b bus.Bus) controller.Factory { return nats_controller.NewFactory(b) })
peer.AddConfig("pubsub", &nats_controller.Config{
PeerId: peer.GetPeerID().String(),
NatsConfig: &nats.Config{
LogTrace: logTrace,
},
})
peer.AddFactory(func(b bus.Bus) controller.Factory { return pubsub_relay.NewFactory(b) })
peer.AddConfig("pubsub-relay", &pubsub_relay.Config{
TopicIds: topics,
})
}
sim := initSimulator(nil, ctx, le, g)
var t *testing.T
assertConnectivity := func(p0, p1 *graph.Peer) {
px0 := sim.GetPeerByID(p0.GetPeerID())
px1 := sim.GetPeerByID(p1.GetPeerID())
if err := simulate.TestConnectivity(ctx, px0, px1); err != nil {
le.WithError(err).Error("unsuccessful connectivity test from 0 to 1")
// t.Fatal(err.Error())
} else {
le.Infof(
"successful connectivity test between %s and %s",
p0.GetPeerID().String(),
p1.GetPeerID().String(),
)
}
}
assertConnectivity(p0, p1)
assertConnectivity(p1, p2)
// Attempt to open a channel and communicate.
testingData := []byte("hello world")
for _, channelID := range topics {
lp2 := sim.GetPeerByID(p2.GetPeerID())
lp2tb := lp2.GetTestbed()
tpv2, _, tpv2Ref, err := bus.ExecOneOff(
ctx,
lp2tb.Bus,
pubsub.NewBuildChannelSubscription(channelID, lp2tb.PrivKey),
bus.ReturnWhenIdle(),
nil,
)
if err != nil {
t.Fatal(err.Error())
}
s2 := tpv2.GetValue().(pubsub.BuildChannelSubscriptionValue)
le.Infof("built channel subscription for channel %s on peer p2", channelID)
lp0 := sim.GetPeerByID(p0.GetPeerID())
lp0tb := lp0.GetTestbed()
tpv0, _, tpv0Ref, err := bus.ExecOneOff(
ctx,
lp0tb.Bus,
pubsub.NewBuildChannelSubscription(channelID, lp0tb.PrivKey),
bus.ReturnWhenIdle(),
nil,
)
if err != nil {
t.Fatal(err.Error())
}
s0 := tpv0.GetValue().(pubsub.BuildChannelSubscriptionValue)
le.Infof("built channel subscription for channel %s on peer p0", channelID)
msgRx := make(chan pubsub.Message, 1)
s0.AddHandler(func(m pubsub.Message) {
select {
case msgRx <- m:
default:
}
})
// TODO: remove this delay... needs a little time to "settle"
<-time.After(time.Millisecond * 200)
testReplicate := func() {
le.Infof("publishing data on p2 with peer %s", p2.GetPeerID().String())
s2.Publish(testingData)
rmsg := <-msgRx
if bytes.Compare(rmsg.GetData(), testingData) != 0 {
t.Fatalf("pubsub data mismatch %v != expected %v", rmsg.GetData(), testingData)
}
le.Info("successful pubsub replication from p2 -> [lan2] -> p1 -> [lan1] -> p0 ")
}
testReplicate()
le.Info("interrupting connectivity between p2 and p1")
for _, l := range lp2.GetTransportController().GetPeerLinks(p1.GetPeerID()) {
le.Infof("closing link %v", l.GetUUID())
l.Close()
}
// re-connect
le.Info("expecting re-connect between peers")
assertConnectivity(p2, p1)
tpv0Ref.Release()
tpv2Ref.Release()
}
le.Info("tests successful")
return nil
}