-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiator.go
307 lines (242 loc) · 4.95 KB
/
initiator.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package sputnik
import (
"sync"
"github.com/g41797/kissngoqueue"
)
type initiator struct {
sync.Mutex
sputnik Sputnik
actBlks activeBlocks
q *kissngoqueue.Queue[Msg]
runStarted bool
abortStarted bool
finishStarted bool
finishedBlks int
expectFinished int
done chan struct{}
connector *controller
}
// Factory of initiator:
func (inr *initiator) factory() *Block {
return NewBlock(
WithInit(inr.init),
WithRun(inr.run),
WithFinish(inr.finish),
WithOnMsg(inr.msgReceived),
)
}
func (inr *initiator) init(_ ConfFactory) error {
appBlks, err := inr.sputnik.createActiveBlocks()
if err != nil {
return err
}
ibs := make(activeBlocks, 0)
for _, abl := range appBlks {
err = abl.init(inr.sputnik.cnfFact)
if err != nil {
break
}
ibs = append(ibs, abl)
}
if err != nil {
for i := len(ibs) - 1; i >= 0; i-- {
ibs[i].finish()
}
return err
}
inr.actBlks = make(activeBlocks, 0)
inr.actBlks = append(inr.actBlks, inr.activeinitiator())
inr.actBlks = append(inr.actBlks, ibs...)
inr.addControllers()
inr.q = kissngoqueue.NewQueue[Msg]()
inr.setupConnector()
return nil
}
func (inr *initiator) setupConnector() {
connector := inr.sputnik.cnt
to := inr.sputnik.to
if connector == nil {
return
}
cbl, ok := inr.actBlks.getABl(DefaultConnectorResponsibility)
if !ok {
return
}
inr.connector = cbl.controller
setupMsg := make(Msg)
setupMsg["__connector"] = connector
setupMsg["__timeout"] = to
cbl.controller.Send(setupMsg)
return
}
func (inr *initiator) run(_ BlockCommunicator) {
inr.done = make(chan struct{})
defer close(inr.done)
if !inr.activate() {
return
}
// Main loop
for {
nm, ok := inr.q.Get()
if !ok { //all blocks finished
break
}
if nm == nil {
continue
}
inr.processMsg(nm)
}
return
}
func (inr *initiator) activate() bool {
inr.Lock()
defer inr.Unlock()
if inr.abortStarted {
return false
}
// Start active blacks on own goroutines
for _, abl := range inr.actBlks[1:] {
go func(fr Run, bc BlockCommunicator) {
fr(bc)
}(abl.block.run, abl.controller)
}
inr.runStarted = true
return true
}
func (inr *initiator) finish(init bool) {
if init {
return
}
inr.q.PutMT(FinishMsg())
return
}
func (inr *initiator) msgReceived(msg Msg) {
inr.q.PutMT(msg)
return
}
func (inr *initiator) onServerConnected(connection ServerConnection) {
for _, abl := range inr.actBlks[1:] {
abl.controller.ServerConnected(connection)
}
return
}
func (inr *initiator) onserverDisconnected() {
for _, abl := range inr.actBlks[1:] {
abl.controller.serverDisconnected()
}
return
}
func (inr *initiator) runInternal() (err error) {
inr.run(nil)
return nil
}
// TODO Add timeout for abort/ShootDown
func (inr *initiator) abort() {
if inr.finishBeforeLaunch() {
return
}
inr.finish(false)
select {
case <-inr.done:
return
}
}
func (inr *initiator) finishBeforeLaunch() bool {
inr.Lock()
defer inr.Unlock()
if inr.runStarted {
return false
}
for i := len(inr.actBlks) - 1; i > 0; i-- {
inr.actBlks[i].finish()
}
inr.abortStarted = true
return true
}
func (inr *initiator) activeinitiator() *activeBlock {
ibl := newActiveBlock(
BlockDescriptor{InitiatorResponsibility, InitiatorResponsibility}, inr.factory())
return &ibl
}
func (inr *initiator) addControllers() {
for _, abl := range inr.actBlks {
attachController(abl.descriptor.Responsibility, inr.actBlks)
}
return
}
func (inr *initiator) processMsg(m Msg) {
mn, exists := m["__name"]
if !exists {
return
}
name, ok := mn.(string)
if !ok {
return
}
switch name {
case finishMsg:
inr.processFinish()
case finishedMsg:
inr.processFinished()
case serverConnectedMsg:
inr.onServerConnected(m["__conn"])
case serverDisconnectedMsg:
inr.onserverDisconnected()
}
return
}
func (inr *initiator) processFinish() {
if inr.finishStarted {
return
}
inr.finishStarted = true
for i := len(inr.actBlks) - 1; i > 0; i-- {
contr := inr.actBlks[i].controller
if contr != inr.connector {
contr.Finish()
inr.expectFinished += 1
}
}
inr.finishedBlks = 0
}
func (inr *initiator) processFinished() {
inr.finishedBlks++
if inr.finishedBlks == inr.expectFinished {
if inr.connector == nil {
inr.q.CancelMT() // stop main loop
return
}
inr.finishedBlks = 0
inr.expectFinished = 1
inr.connector.Finish()
inr.connector = nil
}
return
}
const (
finishMsg = "finish"
finishedMsg = "finished"
serverConnectedMsg = "serverConnected"
serverDisconnectedMsg = "serverDisconnected"
)
func FinishMsg() Msg {
msg := make(Msg)
msg["__name"] = finishMsg
return msg
}
func FinishedMsg() Msg {
msg := make(Msg)
msg["__name"] = finishedMsg
return msg
}
func serverconnectedmsg(conn ServerConnection) Msg {
msg := make(Msg)
msg["__name"] = serverConnectedMsg
msg["__conn"] = conn
return msg
}
func serverdisconnectedmsg() Msg {
msg := make(Msg)
msg["__name"] = serverDisconnectedMsg
return msg
}