forked from nytlabs/streamtools
/
blockmanager.go
407 lines (336 loc) · 9 KB
/
blockmanager.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package server
import (
"errors"
"fmt"
"net/url"
"strconv"
"github.com/nytlabs/streamtools/st/library"
"github.com/nytlabs/streamtools/st/blocks"
"sync"
)
type BlockInfo struct {
Id string
Type string
Rule interface{}
Position *Coords
chans blocks.BlockChans
}
type ConnectionInfo struct {
Id string
FromId string
ToId string
ToRoute string
chans blocks.BlockChans
}
type Coords struct {
X float64
Y float64
}
type BlockManager struct {
blockMap map[string]*BlockInfo
connMap map[string]*ConnectionInfo
genId chan string
Mu *sync.Mutex
}
func IDService(idChan chan string) {
i := 1
for {
id := strconv.Itoa(i)
idChan <- id
i += 1
}
}
func NewBlockManager() *BlockManager {
idChan := make(chan string)
go IDService(idChan)
return &BlockManager{
blockMap: make(map[string]*BlockInfo),
connMap: make(map[string]*ConnectionInfo),
genId: idChan,
Mu: &sync.Mutex{},
}
}
func (b *BlockManager) GetId() string {
id := <-b.genId
ok := b.IdExists(id)
for ok {
id = <-b.genId
ok = b.IdExists(id)
}
return id
}
func (b *BlockManager) IdExists(id string) bool {
_, okB := b.blockMap[id]
_, okC := b.connMap[id]
return okB || okC
}
func (b *BlockManager) IdSafe(id string) bool {
return url.QueryEscape(id) == id && id != "DAEMON"
}
func (b *BlockManager) Create(blockInfo *BlockInfo) (*BlockInfo, error) {
if blockInfo == nil {
return nil, errors.New(fmt.Sprintf("Cannot create block %s: no block data.", blockInfo.Id))
}
// check to see if the ID is OK
if !b.IdSafe(blockInfo.Id) {
return nil, errors.New(fmt.Sprintf("Cannot create block %s: invalid id", blockInfo.Id))
}
// create ID if there is none
if blockInfo.Id == "" {
blockInfo.Id = b.GetId()
}
// make sure ID doesn't already exist
if b.IdExists(blockInfo.Id) {
return nil, errors.New(fmt.Sprintf("Cannot create block %s: id already exists", blockInfo.Id))
}
// give the block a position if it doesn't have one.
if blockInfo.Position == nil {
blockInfo.Position = &Coords{
X: 0,
Y: 0,
}
}
_, ok := library.Blocks[blockInfo.Type]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot create block %s: invalid block type %s", blockInfo.Id, blockInfo.Type))
}
// create the block
newBlock := library.Blocks[blockInfo.Type]()
newBlockChans := blocks.BlockChans{
InChan: make(chan *blocks.Msg),
QueryChan: make(chan *blocks.QueryMsg),
AddChan: make(chan *blocks.AddChanMsg),
DelChan: make(chan *blocks.Msg),
ErrChan: make(chan error),
QuitChan: make(chan bool),
}
newBlock.SetId(blockInfo.Id)
newBlock.Build(newBlockChans)
go blocks.BlockRoutine(newBlock)
// save state
blockInfo.chans = newBlockChans
b.blockMap[blockInfo.Id] = blockInfo
if blockInfo.Rule != nil {
err := b.Send(blockInfo.Id, "rule", blockInfo.Rule)
if err != nil {
return nil, err
}
} else {
b.updateRule(blockInfo.Id)
}
return blockInfo, nil
}
func (b *BlockManager) UpdateBlock(id string, coord *Coords) (*BlockInfo, error) {
block, ok := b.blockMap[id]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot update block %s: does not exist", id))
}
block.Position = coord
return block, nil
}
func (b *BlockManager) Send(id string, route string, msg interface{}) error {
_, ok := b.blockMap[id]
if !ok {
return errors.New(fmt.Sprintf("Cannot send to block %s: does not exist", id))
}
// send message to block here
b.blockMap[id].chans.InChan <- &blocks.Msg{
Msg: msg,
Route: route,
}
return nil
}
func (b *BlockManager) QueryBlock(id string, route string) (interface{}, error) {
_, ok := b.blockMap[id]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot query block %s: does not exist", id))
}
returnToSender := make(chan interface{})
b.blockMap[id].chans.QueryChan <- &blocks.QueryMsg{
Route: route,
RespChan: returnToSender,
}
q := <- returnToSender
return q, nil
}
func (b *BlockManager) QueryConnection(id string, route string) (interface{}, error) {
_, ok := b.connMap[id]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot query block %s: does not exist", id))
}
returnToSender := make(chan interface{})
b.connMap[id].chans.QueryChan <- &blocks.QueryMsg{
Route: route,
RespChan: returnToSender,
}
q := <- returnToSender
return q, nil
}
func (b *BlockManager) Connect(connInfo *ConnectionInfo) (*ConnectionInfo, error) {
if connInfo == nil {
return nil, errors.New("Cannot create: no connection data.")
}
// check to see if the ID is OK
if !b.IdSafe(connInfo.Id) {
return nil, errors.New(fmt.Sprintf("Cannot create block %s: invalid id", connInfo.Id))
}
// create ID if there is none
if connInfo.Id == "" {
connInfo.Id = b.GetId()
}
// make sure ID doesn't already exist
if b.IdExists(connInfo.Id) {
return nil, errors.New(fmt.Sprintf("Cannot create connection %s: id already exists", connInfo.Id))
}
// check to see if the blocks that we are attaching to exist
fromExists := b.IdExists(connInfo.FromId)
if !fromExists {
return nil, errors.New(fmt.Sprintf("Cannot create connection %s: FromId block does not exist", connInfo.Id))
}
toExists := b.IdExists(connInfo.ToId)
if !toExists {
return nil, errors.New(fmt.Sprintf("Cannot create connection %s: ToId ID does not exist", connInfo.Id))
}
// create connection info for server
// and create connection routine
newConn := &blocks.Connection{
ToRoute: connInfo.ToRoute,
}
newConnChans := blocks.BlockChans{
InChan: make(chan *blocks.Msg),
QueryChan: make(chan *blocks.QueryMsg),
AddChan: make(chan *blocks.AddChanMsg),
DelChan: make(chan *blocks.Msg),
ErrChan: make(chan error),
QuitChan: make(chan bool),
}
newConn.SetId(connInfo.Id)
newConn.Build(newConnChans)
go blocks.ConnectionRoutine(newConn)
connInfo.chans = newConnChans
b.connMap[connInfo.Id] = connInfo
// ask to connect the blocks together
b.blockMap[connInfo.FromId].chans.AddChan <- &blocks.AddChanMsg{
Route: connInfo.Id,
Channel: connInfo.chans.InChan,
}
b.connMap[connInfo.Id].chans.AddChan <- &blocks.AddChanMsg{
Route: connInfo.ToId,
Channel: b.blockMap[connInfo.ToId].chans.InChan,
}
return connInfo, nil
}
func (b *BlockManager) GetSocket(fromId string) (chan *blocks.Msg, string) {
wsChan := make(chan *blocks.Msg)
id := b.GetId()
b.blockMap[fromId].chans.AddChan <- &blocks.AddChanMsg{
Route: id,
Channel: wsChan,
}
return wsChan, id
}
func (b *BlockManager) DeleteSocket(blockId string, connId string) error {
b.blockMap[blockId].chans.DelChan <- &blocks.Msg{
Route: connId,
}
return nil
}
func (b *BlockManager) updateRule(id string) {
rule := false
block := b.blockMap[id]
for _, b := range library.BlockDefs[block.Type].QueryRoutes {
rule = b == "rule"
if rule {
break
}
}
if rule {
q, err := b.QueryBlock(id, "rule")
if err != nil {
return
}
block.Rule = q
}
}
func (b *BlockManager) GetBlock(id string) (*BlockInfo, error) {
block, ok := b.blockMap[id]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot get block %s: does not exist", id))
}
b.updateRule(id)
return block, nil
}
func (b *BlockManager) GetConnection(id string) (*ConnectionInfo, error) {
_, ok := b.connMap[id]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot get connection %s: does not exist", id))
}
return b.connMap[id], nil
}
func (b *BlockManager) DeleteBlock(id string) ([]string, error) {
var delIds []string
_, ok := b.blockMap[id]
if !ok {
return nil, errors.New(fmt.Sprintf("Cannot delete block %s: does not exist", id))
}
// delete connections that reference this block
for _, c := range b.connMap {
if c.FromId == id {
delFromId, err := b.DeleteConnection(c.Id)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot delete block %s: FromId %s does not exist", id, c.FromId))
}
delIds = append(delIds, delFromId)
}
if c.ToId == id {
delToId, err := b.DeleteConnection(c.Id)
if err != nil {
return nil, errors.New(fmt.Sprintf("Cannot delete block %s: ToId %s does not exist", id, c.ToId))
}
delIds = append(delIds, delToId)
}
}
// turn off block here
// close channels, whatever.
b.blockMap[id].chans.QuitChan <- true
delete(b.blockMap, id)
delIds = append(delIds, id)
return delIds, nil
}
func (b *BlockManager) DeleteConnection(id string) (string, error) {
_, ok := b.connMap[id]
if !ok {
return "", errors.New(fmt.Sprintf("Cannot delete connection %s: does not exist", id))
}
b.blockMap[b.connMap[id].FromId].chans.DelChan <- &blocks.Msg{
Route: id,
}
b.connMap[id].chans.QuitChan <- true
// call disconnecting stuff here
// remove channel from FromBlock, etc
// turn off connection block
delete(b.connMap, id)
return id, nil
}
func (b *BlockManager) ListBlocks() []*BlockInfo {
i := 0
blocks := make([]*BlockInfo, len(b.blockMap), len(b.blockMap))
for k, _ := range b.blockMap {
v, err := b.GetBlock(k)
if err != nil {
continue
}
blocks[i] = v
i++
}
return blocks
}
func (b *BlockManager) ListConnections() []*ConnectionInfo {
i := 0
conns := make([]*ConnectionInfo, len(b.connMap), len(b.connMap))
for _, v := range b.connMap {
conns[i] = v
i++
}
return conns
}