-
Notifications
You must be signed in to change notification settings - Fork 0
/
broflake.go
187 lines (159 loc) · 4.92 KB
/
broflake.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
// broflake.go defines mid-layer abstractions for constructing and describing a Broflake instance
package clientcore
import (
"fmt"
"runtime"
"sync"
"time"
"github.com/getlantern/broflake/common"
netstatecl "github.com/getlantern/broflake/netstate/client"
)
type BroflakeEngine struct {
cTable *WorkerTable
pTable *WorkerTable
ui UI
wg *sync.WaitGroup
netstated string
tag string
netstateHeartbeat time.Duration
netstateStop chan struct{}
}
func NewBroflakeEngine(cTable, pTable *WorkerTable, ui UI, wg *sync.WaitGroup, netstated, tag string) *BroflakeEngine {
return &BroflakeEngine{
cTable,
pTable,
ui,
wg,
netstated,
tag,
1 * time.Minute,
make(chan struct{}, 0),
}
}
func (b *BroflakeEngine) start() {
b.cTable.Start()
b.pTable.Start()
common.Debug("▶ Broflake started!")
if b.netstated != "" {
go func() {
common.Debug("Netstate hearbeat ON")
for {
common.Debug("Netstate HEARTBEAT")
err := netstatecl.Exec(
b.netstated,
&netstatecl.Instruction{
Op: netstatecl.OpConsumerState,
Args: netstatecl.EncodeArgsOpConsumerState(connectedConsumers.slice()),
Tag: b.tag,
},
)
if err != nil {
common.Debugf("Netstate client Exec error: %v", err)
}
select {
case <-time.After(b.netstateHeartbeat):
// Do nothing, iterate the loop
case <-b.netstateStop:
defer common.Debug("Netstate heartbeat OFF")
return
}
}
}()
}
}
func (b *BroflakeEngine) stop() {
b.cTable.Stop()
b.pTable.Stop()
go func() {
b.wg.Wait()
if b.netstated != "" {
b.netstateStop <- struct{}{}
}
common.Debug("■ Broflake stopped.")
b.ui.OnReady()
}()
}
func (b *BroflakeEngine) debug() {
common.Debugf("NumGoroutine: %v", runtime.NumGoroutine())
}
func NewBroflake(bfOpt *BroflakeOptions, rtcOpt *WebRTCOptions, egOpt *EgressOptions) (bfconn *BroflakeConn, ui *UIImpl, err error) {
if bfOpt.ClientType != "desktop" && bfOpt.ClientType != "widget" {
err = fmt.Errorf("Invalid clientType '%v\n'", bfOpt.ClientType)
common.Debugf(err.Error())
return bfconn, ui, err
}
ui = &UIImpl{}
var cTable *WorkerTable
var cRouter TableRouter
var pTable *WorkerTable
var pRouter TableRouter
var wgReady sync.WaitGroup
if bfOpt == nil {
bfOpt = NewDefaultBroflakeOptions()
}
if rtcOpt == nil {
rtcOpt = NewDefaultWebRTCOptions()
}
if egOpt == nil {
egOpt = NewDefaultEgressOptions()
}
// The boot DAG:
// build cTable/pTable -> build the Broflake struct -> run ui.Init -> set up the bus and bind
// the upstream/downstream handlers -> build cRouter/pRouter -> start the bus, init the routers,
// call onStartup and onReady. This dependency graph currently requires us to implement two
// switches on clientType during the boot process, which can probably be improved upon.
// Step 1: Build consumer table and producer table
switch bfOpt.ClientType {
case "desktop":
// Desktop peers don't share connectivity for the MVP, so the consumer table only gets one
// workerFSM for the local user stream associated with their HTTP proxy
var producerUserStream *WorkerFSM
bfconn, producerUserStream = NewProducerUserStream(&wgReady)
cTable = NewWorkerTable([]WorkerFSM{*producerUserStream})
// Desktop peers consume connectivity over WebRTC
var pfsms []WorkerFSM
for i := 0; i < bfOpt.PTableSize; i++ {
pfsms = append(pfsms, *NewConsumerWebRTC(rtcOpt, &wgReady))
}
pTable = NewWorkerTable(pfsms)
case "widget":
// Widget peers share connectivity over WebRTC
var cfsms []WorkerFSM
for i := 0; i < bfOpt.CTableSize; i++ {
cfsms = append(cfsms, *NewProducerWebRTC(rtcOpt, &wgReady))
}
cTable = NewWorkerTable(cfsms)
// Widget peers consume connectivity from an egress server over WebSocket
var pfsms []WorkerFSM
for i := 0; i < bfOpt.PTableSize; i++ {
pfsms = append(pfsms, *NewEgressConsumerWebSocket(egOpt, &wgReady))
}
pTable = NewWorkerTable(pfsms)
}
// Step 2: Build Broflake
broflake := NewBroflakeEngine(cTable, pTable, ui, &wgReady, bfOpt.Netstated, rtcOpt.Tag)
// Step 3: Init the UI (this constructs and exposes the JavaScript API as required)
ui.Init(broflake)
// Step 4: Set up the bus, bind upstream and downstream UI handlers
var bus = NewIpcObserver(
bfOpt.BusBufferSz,
UpstreamUIHandler(*ui, bfOpt.Netstated, rtcOpt.Tag),
DownstreamUIHandler(*ui, bfOpt.Netstated, rtcOpt.Tag),
)
// Step 5: Build consumer router and producer router
switch bfOpt.ClientType {
case "desktop":
cRouter = NewConsumerRouter(bus.Downstream, cTable)
pRouter = NewProducerSerialRouter(bus.Upstream, pTable, cTable.Size())
case "widget":
cRouter = NewConsumerRouter(bus.Downstream, cTable)
pRouter = NewProducerPoolRouter(bus.Upstream, pTable)
}
// Step 6: Start the bus, init the routers, fire our UI events to announce that we're ready
bus.Start()
cRouter.Init()
pRouter.Init()
ui.OnReady()
ui.OnStartup()
return bfconn, ui, nil
}