/
coordinator.go
221 lines (182 loc) · 5.57 KB
/
coordinator.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
package node
import (
"context"
"fmt"
"github.com/ChristianMct/helium/coordinator"
"github.com/ChristianMct/helium/services/compute"
"github.com/ChristianMct/helium/services/setup"
)
type Event struct {
SetupEvent *setup.Event
ComputeEvent *compute.Event
}
func (ev Event) IsCompute() bool {
return ev.ComputeEvent != nil
}
func (ev Event) IsSetup() bool {
return ev.SetupEvent != nil
}
func (ev Event) String() string {
switch {
case ev.SetupEvent != nil:
return fmt.Sprintf("SETUP PROTOCOL %s", ev.SetupEvent.String())
case ev.ComputeEvent != nil:
switch {
case ev.ComputeEvent.CircuitEvent != nil:
return fmt.Sprintf("COMPUTE CIRCUIT %s", ev.ComputeEvent.CircuitEvent.String())
case ev.ComputeEvent.ProtocolEvent != nil:
return fmt.Sprintf("COMPUTE PROTOCOL %s", ev.ComputeEvent.ProtocolEvent.String())
}
}
return "INVALID EVENT"
}
type Coordinator coordinator.Coordinator[Event]
type setupCoordinator struct {
incoming, outgoing chan setup.Event
}
type computeCoordinator struct {
incoming, outgoing chan compute.Event
}
type ServicesCoordinator struct {
setupCoordinator
computeCoordinator
setupCoordDone, computeCoordDone chan struct{}
}
func newServicesCoordinator(ctx context.Context, upstream Coordinator) (*ServicesCoordinator, error) {
sc := &ServicesCoordinator{
setupCoordinator: setupCoordinator{outgoing: make(chan setup.Event)},
computeCoordinator: computeCoordinator{outgoing: make(chan compute.Event)},
}
upstreamChan, present, err := upstream.Register(ctx)
if err != nil {
return nil, err
}
setupEvs := make([]setup.Event, 0, present)
computeEvs := make([]compute.Event, 0, present)
for i := 0; i < present; i++ {
ev := <-upstreamChan.Incoming
switch {
case ev.SetupEvent != nil:
setupEvs = append(setupEvs, *ev.SetupEvent)
case ev.ComputeEvent != nil:
computeEvs = append(computeEvs, *ev.ComputeEvent)
default:
return nil, fmt.Errorf("invalid node event: not a setup nor a compute event")
}
}
sc.setupCoordinator.incoming = make(chan setup.Event, len(setupEvs))
for _, ev := range setupEvs {
sc.setupCoordinator.incoming <- ev
}
sc.computeCoordinator.incoming = make(chan compute.Event, len(computeEvs))
for _, ev := range computeEvs {
sc.computeCoordinator.incoming <- ev
}
go func() {
for ev := range upstreamChan.Incoming {
switch {
case ev.SetupEvent != nil:
sc.setupCoordinator.incoming <- *ev.SetupEvent
case ev.ComputeEvent != nil:
sc.computeCoordinator.incoming <- *ev.ComputeEvent
default:
panic(fmt.Errorf("invalid node event: not a setup nor a compute event")) // TODO
}
}
close(sc.setupCoordinator.incoming)
close(sc.computeCoordinator.incoming)
}()
sc.setupCoordDone = make(chan struct{})
sc.computeCoordDone = make(chan struct{})
go func() {
for ev := range sc.setupCoordinator.outgoing {
ev := ev
upstreamChan.Outgoing <- Event{SetupEvent: &ev}
}
close(sc.setupCoordDone)
}()
go func() {
for ev := range sc.computeCoordinator.outgoing {
ev := ev
upstreamChan.Outgoing <- Event{ComputeEvent: &ev}
}
close(sc.computeCoordDone)
}()
go func() {
<-sc.setupCoordDone
<-sc.computeCoordDone
if upstreamChan.Outgoing != nil {
close(upstreamChan.Outgoing)
}
}()
return sc, nil
}
func (sc setupCoordinator) Register(ctx context.Context) (evChan *coordinator.Channel[setup.Event], present int, err error) {
evChan = &coordinator.Channel[setup.Event]{
Incoming: sc.incoming,
Outgoing: sc.outgoing,
}
return evChan, len(sc.incoming), nil
}
func (sc computeCoordinator) Register(ctx context.Context) (evChan *coordinator.Channel[compute.Event], present int, err error) {
evChan = &coordinator.Channel[compute.Event]{
Incoming: sc.incoming,
Outgoing: sc.outgoing,
}
return evChan, len(sc.incoming), nil
}
// func NewCentralizedCoordinator(srv *centralized.HeliumServer) *CentralizedCoordinator {
// cc := &CentralizedCoordinator{
// srv: srv,
// setupCoordinator: setupCoordinator{outgoing: make(chan protocol.Event)},
// computeCoordinator: computeCoordinator{outgoing: make(chan circuit.Event)},
// done: make(chan struct{}),
// }
// go func() {
// for ev := range cc.setupCoordinator.outgoing {
// pev := ev
// cc.srv.AppendEventToLog(coordinator.Event{ProtocolEvent: &pev})
// }
// close(cc.done)
// }()
// go func() {
// for ev := range cc.computeCoordinator.outgoing {
// pev := ev
// cc.srv.AppendEventToLog(coordinator.Event{ProtocolEvent: &pev})
// }
// close(cc.done)
// }()
// return cc
// }
// func (cc *CentralizedCoordinator) Register(ctx context.Context) (evChan *protocol.EventChannel, present int, err error) {
// evChan = &protocol.EventChannel{
// Incoming: cc.incoming,
// Outgoing: cc.outgoing,
// }
// return evChan, 0, nil
// }
// type CentralizedCoordinatorClient struct {
// cli *centralized.HeliumClient
// incoming, outgoing chan protocol.Event
// done chan struct{}
// }
// func NewCentralizedCoordinatorClient(cli *centralized.HeliumClient) *CentralizedCoordinatorClient {
// cc := &CentralizedCoordinatorClient{
// cli: cli,
// incoming: make(chan protocol.Event),
// outgoing: make(chan protocol.Event),
// done: make(chan struct{}),
// }
// return cc
// }
// func (cc *CentralizedCoordinatorClient) Register(ctx context.Context) (evChan *protocol.EventChannel, present int, err error) {
// // events, present, err := cc.cli.Register(ctx)
// // if err != nil {
// // return nil, nil, err
// // }
// evChan = &protocol.EventChannel{
// Incoming: cc.incoming,
// Outgoing: cc.outgoing,
// }
// return evChan, 0, nil
// }