-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstomp.go
469 lines (384 loc) · 9.55 KB
/
stomp.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
// Copyright 2019-2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package stompserver
import (
"fmt"
"log"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/go-stomp/stomp/v3"
"github.com/go-stomp/stomp/v3/frame"
"github.com/google/uuid"
)
type subscription struct {
id string
destination string
}
type StompConn interface {
// Return unique connection Id string
GetId() string
SendFrameToSubscription(f *frame.Frame, sub *subscription)
Close()
}
const (
maxHeartBeatDuration = time.Duration(999999999) * time.Millisecond
)
const (
connecting int32 = iota
connected
closed
)
type stompConn struct {
rawConnection RawConnection
state int32
version stomp.Version
inFrames chan *frame.Frame
outFrames chan *frame.Frame
readTimeoutMs int64
writeTimeout time.Duration
id string
events chan *ConnEvent
config StompConfig
subscriptions map[string]*subscription
currentMessageId uint64
closeOnce sync.Once
}
func NewStompConn(rawConnection RawConnection, config StompConfig, events chan *ConnEvent) StompConn {
conn := &stompConn{
rawConnection: rawConnection,
state: connecting,
inFrames: make(chan *frame.Frame, 32),
outFrames: make(chan *frame.Frame, 32),
config: config,
id: uuid.New().String(),
events: events,
subscriptions: make(map[string]*subscription),
}
go conn.run()
go conn.readInFrames()
return conn
}
func (conn *stompConn) SendFrameToSubscription(f *frame.Frame, sub *subscription) {
f.Header.Add(frame.Subscription, sub.id)
conn.outFrames <- f
}
func (conn *stompConn) Close() {
conn.closeOnce.Do(func() {
atomic.StoreInt32(&conn.state, closed)
conn.rawConnection.Close()
conn.events <- &ConnEvent{
ConnId: conn.GetId(),
eventType: ConnectionClosed,
conn: conn,
}
})
}
func (conn *stompConn) GetId() string {
return conn.id
}
func (conn *stompConn) run() {
defer conn.Close()
var timerChannel <-chan time.Time
var timer *time.Timer
for {
if atomic.LoadInt32(&conn.state) == closed {
return
}
if timer == nil && conn.writeTimeout > 0 {
timer = time.NewTimer(conn.writeTimeout)
timerChannel = timer.C
}
select {
case f, ok := <-conn.outFrames:
if !ok {
// close connection
return
}
// reset heart-beat timer
if timer != nil {
timer.Stop()
timer = nil
}
conn.populateMessageIdHeader(f)
// write the frame to the client
err := conn.rawConnection.WriteFrame(f)
if err != nil || f.Command == frame.ERROR {
return
}
case f, ok := <-conn.inFrames:
if !ok {
return
}
if err := conn.handleIncomingFrame(f); err != nil {
conn.sendError(err)
return
}
case <-timerChannel:
// write a heart-beat
err := conn.rawConnection.WriteFrame(nil)
if err != nil {
return
}
if timer != nil {
timer.Stop()
timer = nil
}
}
}
}
func (conn *stompConn) handleIncomingFrame(f *frame.Frame) error {
switch f.Command {
case frame.CONNECT, frame.STOMP:
return conn.handleConnect(f)
case frame.DISCONNECT:
return conn.handleDisconnect(f)
case frame.SEND:
return conn.handleSend(f)
case frame.SUBSCRIBE:
return conn.handleSubscribe(f)
case frame.UNSUBSCRIBE:
return conn.handleUnsubscribe(f)
}
return unsupportedStompCommandError
}
// Returns true if the frame contains ANY of the specified
// headers
func containsHeader(f *frame.Frame, headers ...string) bool {
for _, h := range headers {
if _, ok := f.Header.Contains(h); ok {
return true
}
}
return false
}
func (conn *stompConn) handleConnect(f *frame.Frame) error {
if atomic.LoadInt32(&conn.state) == connected {
return unexpectedStompCommandError
}
if containsHeader(f, frame.Receipt) {
return invalidHeaderError
}
var err error
conn.version, err = determineVersion(f)
if err != nil {
log.Println("cannot determine version")
return err
}
if conn.version == stomp.V10 {
return unsupportedStompVersionError
}
cxDuration, cyDuration, err := getHeartBeat(f)
if err != nil {
log.Println("invalid heart-beat")
return err
}
min := time.Duration(conn.config.HeartBeat()) * time.Millisecond
if min > maxHeartBeatDuration {
min = maxHeartBeatDuration
}
// apply a minimum heartbeat
if cxDuration > 0 {
if min == 0 || cxDuration < min {
cxDuration = min
}
}
if cyDuration > 0 {
if min == 0 || cyDuration < min {
cyDuration = min
}
}
conn.writeTimeout = cyDuration
cx, cy := int64(cxDuration/time.Millisecond), int64(cyDuration/time.Millisecond)
atomic.StoreInt64(&conn.readTimeoutMs, cx)
response := frame.New(frame.CONNECTED,
frame.Version, string(conn.version),
frame.Server, "stompServer/0.0.1",
frame.HeartBeat, fmt.Sprintf("%d,%d", cy, cx))
err = conn.rawConnection.WriteFrame(response)
if err != nil {
return err
}
atomic.StoreInt32(&conn.state, connected)
conn.events <- &ConnEvent{
ConnId: conn.GetId(),
eventType: ConnectionEstablished,
conn: conn,
}
return nil
}
func (conn *stompConn) handleDisconnect(f *frame.Frame) error {
if atomic.LoadInt32(&conn.state) == connecting {
return notConnectedStompError
}
conn.sendReceiptResponse(f)
conn.Close()
return nil
}
func (conn *stompConn) handleSubscribe(f *frame.Frame) error {
switch atomic.LoadInt32(&conn.state) {
case connecting:
return notConnectedStompError
case closed:
return nil
}
subId, ok := f.Header.Contains(frame.Id)
if !ok {
return invalidSubscriptionError
}
dest, ok := f.Header.Contains(frame.Destination)
if !ok {
return invalidFrameError
}
if _, exists := conn.subscriptions[subId]; exists {
// subscription already exists
return nil
}
conn.subscriptions[subId] = &subscription{
id: subId,
destination: dest,
}
conn.events <- &ConnEvent{
ConnId: conn.GetId(),
eventType: SubscribeToTopic,
destination: dest,
conn: conn,
sub: conn.subscriptions[subId],
frame: f,
}
return nil
}
func (conn *stompConn) handleUnsubscribe(f *frame.Frame) error {
switch atomic.LoadInt32(&conn.state) {
case connecting:
return notConnectedStompError
case closed:
return nil
}
id, ok := f.Header.Contains(frame.Id)
if !ok {
return invalidSubscriptionError
}
conn.sendReceiptResponse(f)
sub, ok := conn.subscriptions[id]
if !ok {
// subscription already removed
return nil
}
// remove the subscription
delete(conn.subscriptions, id)
conn.events <- &ConnEvent{
ConnId: conn.GetId(),
eventType: UnsubscribeFromTopic,
conn: conn,
sub: sub,
destination: sub.destination,
}
return nil
}
func (conn *stompConn) handleSend(f *frame.Frame) error {
switch atomic.LoadInt32(&conn.state) {
case connecting:
return notConnectedStompError
case closed:
return nil
}
// TODO: Remove if we start supporting transactions
if containsHeader(f, frame.Transaction) {
return unsupportedStompCommandError
}
// no destination triggers an error
dest, ok := f.Header.Contains(frame.Destination)
if !ok {
return invalidFrameError
}
// reject SENDing directly to non-request channels by clients
if !conn.config.IsAppRequestDestination(f.Header.Get(frame.Destination)) {
return invalidSendDestinationError
}
err := conn.sendReceiptResponse(f)
if err != nil {
return err
}
f.Command = frame.MESSAGE
conn.events <- &ConnEvent{
ConnId: conn.GetId(),
eventType: IncomingMessage,
destination: dest,
frame: f,
conn: conn,
}
return nil
}
func (conn *stompConn) sendReceiptResponse(f *frame.Frame) error {
if receipt, ok := f.Header.Contains(frame.Receipt); ok {
f.Header.Del(frame.Receipt)
return conn.rawConnection.WriteFrame(frame.New(frame.RECEIPT, frame.ReceiptId, receipt))
}
return nil
}
func (conn *stompConn) readInFrames() {
defer func() {
close(conn.inFrames)
}()
infiniteTimeout := time.Time{}
var readTimeoutMs int64 = 0
for {
readTimeoutMs = atomic.LoadInt64(&conn.readTimeoutMs)
if readTimeoutMs > 0 {
conn.rawConnection.SetReadDeadline(time.Now().Add(
time.Duration(readTimeoutMs) * time.Millisecond))
} else {
conn.rawConnection.SetReadDeadline(infiniteTimeout)
}
f, err := conn.rawConnection.ReadFrame()
if err != nil {
return
}
if f == nil {
// heartbeat frame
continue
}
conn.inFrames <- f
}
}
func determineVersion(f *frame.Frame) (stomp.Version, error) {
if acceptVersion, ok := f.Header.Contains(frame.AcceptVersion); ok {
versions := strings.Split(acceptVersion, ",")
for _, supportedVersion := range []stomp.Version{stomp.V12, stomp.V11, stomp.V10} {
for _, v := range versions {
if v == supportedVersion.String() {
// return the highest supported version
return supportedVersion, nil
}
}
}
} else {
return stomp.V10, nil
}
var emptyVersion stomp.Version
return emptyVersion, unsupportedStompVersionError
}
func getHeartBeat(f *frame.Frame) (cx, cy time.Duration, err error) {
if heartBeat, ok := f.Header.Contains(frame.HeartBeat); ok {
return frame.ParseHeartBeat(heartBeat)
}
return 0, 0, nil
}
func (conn *stompConn) sendError(err error) {
errorFrame := frame.New(frame.ERROR,
frame.Message, err.Error())
conn.rawConnection.WriteFrame(errorFrame)
}
func (conn *stompConn) populateMessageIdHeader(f *frame.Frame) {
if f.Command == frame.MESSAGE {
// allocate the value of message-id for this frame
conn.currentMessageId++
messageId := strconv.FormatUint(conn.currentMessageId, 10)
f.Header.Set(frame.MessageId, messageId)
// remove the Ack header (if any) as we don't support those
f.Header.Del(frame.Ack)
}
}