/
message.go
549 lines (468 loc) · 11.9 KB
/
message.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package core
// Message is a generic container for a WAMP message.
type message interface {
messageType() messageType
}
type messageType int
func (mt messageType) New() message {
switch mt {
case hELLO:
return new(hello)
case wELCOME:
return new(welcome)
case aBORT:
return new(abort)
case cHALLENGE:
return new(challenge)
case aUTHENTICATE:
return new(authenticate)
case gOODBYE:
return new(goodbye)
case hEARTBEAT:
return new(heartbeat)
case eRROR:
return new(errorMessage)
case pUBLISH:
return new(publish)
case pUBLISHED:
return new(published)
case sUBSCRIBE:
return new(subscribe)
case sUBSCRIBED:
return new(subscribed)
case uNSUBSCRIBE:
return new(unsubscribe)
case uNSUBSCRIBED:
return new(unsubscribed)
case eVENT:
return new(event)
case cALL:
return new(call)
case cANCEL:
return new(cancel)
case rESULT:
return new(result)
case rEGISTER:
return new(register)
case rEGISTERED:
return new(registered)
case uNREGISTER:
return new(unregister)
case uNREGISTERED:
return new(unregistered)
case iNVOCATION:
return new(invocation)
case iNTERRUPT:
return new(interrupt)
case yIELD:
return new(yield)
default:
return nil
}
}
func (mt messageType) String() string {
switch mt {
case hELLO:
return "HELLO"
case wELCOME:
return "WELCOME"
case aBORT:
return "ABORT"
case cHALLENGE:
return "CHALLENGE"
case aUTHENTICATE:
return "AUTHENTICATE"
case gOODBYE:
return "GOODBYE"
case hEARTBEAT:
return "HEARTBEAT"
case eRROR:
return "ERROR"
case pUBLISH:
return "PUBLISH"
case pUBLISHED:
return "PUBLISHED"
case sUBSCRIBE:
return "SUBSCRIBE"
case sUBSCRIBED:
return "SUBSCRIBED"
case uNSUBSCRIBE:
return "UNSUBSCRIBE"
case uNSUBSCRIBED:
return "UNSUBSCRIBED"
case eVENT:
return "EVENT"
case cALL:
return "CALL"
case cANCEL:
return "CANCEL"
case rESULT:
return "RESULT"
case rEGISTER:
return "REGISTER"
case rEGISTERED:
return "REGISTERED"
case uNREGISTER:
return "UNREGISTER"
case uNREGISTERED:
return "UNREGISTERED"
case iNVOCATION:
return "INVOCATION"
case iNTERRUPT:
return "INTERRUPT"
case yIELD:
return "YIELD"
default:
panic("Invalid message type")
}
}
const (
hELLO messageType = 1
wELCOME messageType = 2
aBORT messageType = 3
cHALLENGE messageType = 4
aUTHENTICATE messageType = 5
gOODBYE messageType = 6
hEARTBEAT messageType = 7
eRROR messageType = 8
pUBLISH messageType = 16 // Tx Rx
pUBLISHED messageType = 17 // Rx Tx
sUBSCRIBE messageType = 32 // Rx Tx
sUBSCRIBED messageType = 33 // Tx Rx
uNSUBSCRIBE messageType = 34 // Rx Tx
uNSUBSCRIBED messageType = 35 // Tx Rx
eVENT messageType = 36 // Tx Rx
cALL messageType = 48 // Tx Rx
cANCEL messageType = 49 // Tx Rx
rESULT messageType = 50 // Rx Tx
rEGISTER messageType = 64 // Rx Tx
rEGISTERED messageType = 65 // Tx Rx
uNREGISTER messageType = 66 // Rx Tx
uNREGISTERED messageType = 67 // Tx Rx
iNVOCATION messageType = 68 // Tx Rx
iNTERRUPT messageType = 69 // Tx Rx
yIELD messageType = 70 // Rx Tx
)
// const messages map[int]string = map[int]string{
// hELLO: 1,
// wELCOME: 2,
// aBORT: 3,
// cHALLENGE: 4,
// aUTHENTICATE: 5,
// gOODBYE: 6,
// hEARTBEAT: 7,
// eRROR: 8,
// pUBLISH: 16,
// pUBLISHED: 17,
// sUBSCRIBE: 32,
// sUBSCRIBED: 33,
// uNSUBSCRIBE: 34,
// uNSUBSCRIBED: 35,
// eVENT: 36,
// cALL: 48,
// cANCEL: 49,
// rESULT: 50,
// rEGISTER: 64,
// rEGISTERED: 65,
// uNREGISTER: 66,
// uNREGISTERED: 67,
// iNVOCATION: 68,
// iNTERRUPT: 69,
// yIELD: 70,
// }
// [hELLO, Realm|uri, Details|dict]
type hello struct {
Realm string
Details map[string]interface{}
}
func (msg *hello) messageType() messageType {
return hELLO
}
// [wELCOME, Session|id, Details|dict]
type welcome struct {
Id uint64
Details map[string]interface{}
}
func (msg *welcome) messageType() messageType {
return wELCOME
}
// [aBORT, Details|dict, Reason|uri]
type abort struct {
Details map[string]interface{}
Reason string
}
func (msg *abort) messageType() messageType {
return aBORT
}
// [cHALLENGE, AuthMethod|string, Extra|dict]
type challenge struct {
AuthMethod string
Extra map[string]interface{}
}
func (msg *challenge) messageType() messageType {
return cHALLENGE
}
// [aUTHENTICATE, Signature|string, Extra|dict]
type authenticate struct {
Signature string
Extra map[string]interface{}
}
func (msg *authenticate) messageType() messageType {
return aUTHENTICATE
}
// [gOODBYE, Details|dict, Reason|uri]
type goodbye struct {
Details map[string]interface{}
Reason string
}
func (msg *goodbye) messageType() messageType {
return gOODBYE
}
// [hEARTBEAT, IncomingSeq|integer, OutgoingSeq|integer
// [hEARTBEAT, IncomingSeq|integer, OutgoingSeq|integer, Discard|string]
type heartbeat struct {
IncomingSeq uint64
OutgoingSeq uint64
Discard string
}
func (msg *heartbeat) messageType() messageType {
return hEARTBEAT
}
// [eRROR, REQUEST.Type|int, REQUEST.Request|id, Details|dict, Error|uri]
// [eRROR, REQUEST.Type|int, REQUEST.Request|id, Details|dict, Error|uri, Arguments|list]
// [eRROR, REQUEST.Type|int, REQUEST.Request|id, Details|dict, Error|uri, Arguments|list, ArgumentsKw|dict]
type errorMessage struct {
Type messageType
Request uint64
Details map[string]interface{}
Error string
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *errorMessage) messageType() messageType {
return eRROR
}
// [pUBLISH, Request|id, Options|dict, name|uri]
// [pUBLISH, Request|id, Options|dict, name|uri, Arguments|list]
// [pUBLISH, Request|id, Options|dict, name|uri, Arguments|list, ArgumentsKw|dict]
type publish struct {
Request uint64
Options map[string]interface{}
Name string
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *publish) messageType() messageType {
return pUBLISH
}
// [pUBLISHED, pUBLISH.Request|id, Publication|id]
type published struct {
Request uint64
Publication uint64
}
func (msg *published) messageType() messageType {
return pUBLISHED
}
// [sUBSCRIBE, Request|id, Options|dict, name|uri]
type subscribe struct {
Request uint64
Options map[string]interface{}
Name string
}
func (msg *subscribe) messageType() messageType {
return sUBSCRIBE
}
// [sUBSCRIBED, sUBSCRIBE.Request|id, Subscription|id]
type subscribed struct {
Request uint64
Subscription uint64
}
func (msg *subscribed) messageType() messageType {
return sUBSCRIBED
}
// [uNSUBSCRIBE, Request|id, sUBSCRIBED.Subscription|id]
type unsubscribe struct {
Request uint64
Subscription uint64
}
func (msg *unsubscribe) messageType() messageType {
return uNSUBSCRIBE
}
// [uNSUBSCRIBED, uNSUBSCRIBE.Request|id]
type unsubscribed struct {
Request uint64
}
func (msg *unsubscribed) messageType() messageType {
return uNSUBSCRIBED
}
// [eVENT, sUBSCRIBED.Subscription|id, pUBLISHED.Publication|id, Details|dict]
// [eVENT, sUBSCRIBED.Subscription|id, pUBLISHED.Publication|id, Details|dict, pUBLISH.Arguments|list]
// [eVENT, sUBSCRIBED.Subscription|id, pUBLISHED.Publication|id, Details|dict, pUBLISH.Arguments|list,
// pUBLISH.ArgumentsKw|dict]
type event struct {
Subscription uint64
Publication uint64
Details map[string]interface{}
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *event) messageType() messageType {
return eVENT
}
// CallResult represents the result of a cALL.
type callResult struct {
Args []interface{}
Kwargs map[string]interface{}
Err string
}
// [cALL, Request|id, Options|dict, name|uri]
// [cALL, Request|id, Options|dict, name|uri, Arguments|list]
// [cALL, Request|id, Options|dict, name|uri, Arguments|list, ArgumentsKw|dict]
type call struct {
Request uint64
Options map[string]interface{}
Name string
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *call) messageType() messageType {
return cALL
}
// [rESULT, cALL.Request|id, Details|dict]
// [rESULT, cALL.Request|id, Details|dict, yIELD.Arguments|list]
// [rESULT, cALL.Request|id, Details|dict, yIELD.Arguments|list, yIELD.ArgumentsKw|dict]
type result struct {
Request uint64
Details map[string]interface{}
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *result) messageType() messageType {
return rESULT
}
// [rEGISTER, Request|id, Options|dict, name|uri]
type register struct {
Request uint64
Options map[string]interface{}
Name string
}
func (msg *register) messageType() messageType {
return rEGISTER
}
// [rEGISTERED, rEGISTER.Request|id, Registration|id]
type registered struct {
Request uint64
Registration uint64
}
func (msg *registered) messageType() messageType {
return rEGISTERED
}
// [uNREGISTER, Request|id, rEGISTERED.Registration|id]
type unregister struct {
Request uint64
Registration uint64
}
func (msg *unregister) messageType() messageType {
return uNREGISTER
}
// [uNREGISTERED, uNREGISTER.Request|id]
type unregistered struct {
Request uint64
}
func (msg *unregistered) messageType() messageType {
return uNREGISTERED
}
// [iNVOCATION, Request|id, rEGISTERED.Registration|id, Details|dict]
// [iNVOCATION, Request|id, rEGISTERED.Registration|id, Details|dict, cALL.Arguments|list]
// [iNVOCATION, Request|id, rEGISTERED.Registration|id, Details|dict, cALL.Arguments|list, cALL.ArgumentsKw|dict]
type invocation struct {
Request uint64
Registration uint64
Details map[string]interface{}
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *invocation) messageType() messageType {
return iNVOCATION
}
// [yIELD, iNVOCATION.Request|id, Options|dict]
// [yIELD, iNVOCATION.Request|id, Options|dict, Arguments|list]
// [yIELD, iNVOCATION.Request|id, Options|dict, Arguments|list, ArgumentsKw|dict]
type yield struct {
Request uint64
Options map[string]interface{}
Arguments []interface{} `wamp:"omitempty"`
ArgumentsKw map[string]interface{} `wamp:"omitempty"`
}
func (msg *yield) messageType() messageType {
return yIELD
}
// [cANCEL, cALL.Request|id, Options|dict]
type cancel struct {
Request uint64
Options map[string]interface{}
}
func (msg *cancel) messageType() messageType {
return cANCEL
}
// [iNTERRUPT, iNVOCATION.Request|id, Options|dict]
type interrupt struct {
Request uint64
Options map[string]interface{}
}
func (msg *interrupt) messageType() messageType {
return iNTERRUPT
}
type NoDestinationError string
func (e NoDestinationError) Error() string {
return "cannot determine destination from: " + string(e)
}
// Given a message, return the intended endpoint
func destination(m *message) (string, error) {
msg := *m
switch msg := msg.(type) {
case *publish:
return msg.Name, nil
case *subscribe:
return msg.Name, nil
// Dealer messages
case *register:
return msg.Name, nil
case *call:
return msg.Name, nil
default:
return "", NoDestinationError(msg.messageType())
}
}
// Given a message, return the request uint64
func requestID(m message) (uint64, bool) {
switch msg := (m).(type) {
case *registered:
return msg.Request, true
case *subscribed:
return msg.Request, true
case *unsubscribed:
return msg.Request, true
case *unregistered:
return msg.Request, true
case *unsubscribe:
return msg.Request, true
case *unregister:
return msg.Request, true
case *result:
return msg.Request, true
case *errorMessage:
return msg.Request, true
case *publish:
return msg.Request, true
case *subscribe:
return msg.Request, true
case *register:
return msg.Request, true
case *call:
return msg.Request, true
default:
Warn("Cant get requestID for %s: %v", msg.messageType(), m)
return 0, false
}
}