-
Notifications
You must be signed in to change notification settings - Fork 1
/
mqtt.rkt
515 lines (452 loc) · 16.3 KB
/
mqtt.rkt
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
#lang racket
(require (rename-in bitsyntax
[bit-string-length length-in-bits])
racket/tcp
racket/port)
(provide mqtt-handlers
mqtt%
mqtt-debug)
(define DEBUG? false)
(define (mqtt-debug bool)
(set! DEBUG? bool))
(define-syntax-rule (debug fmt-str args ...)
(when DEBUG?
(printf fmt-str args ...)))
(define (packet-dbg bs)
(when (bit-string? bs)
(set! bs (bit-string-pack bs)))
(for ([b bs])
(debug "[0x~a]\t~a\t~a~n"
(hexpad (number->string b 16))
b
(number->string b 2)
)))
(define PROTOCOL-VERSION 'v3.1.1)
(define RESERVED-ZERO 0)
(define RESERVED-ONE 1)
(define packet-identifier (make-parameter 0))
(define (next-packet-identifier)
(define return (packet-identifier))
(packet-identifier (add1 (packet-identifier)))
return)
(define broker (make-parameter '("mq.thingmq.com" 1883)))
(broker '("broker.mqttdashboard.com" 1883))
(broker '("localhost" 9999))
(define (encode/utf-8 str)
(bit-string
[(bytes-length (string->bytes/utf-8 str)) :: bytes 2]
[(string->bytes/utf-8 str) :: binary]))
(define/contract (bit-string-length/bytes bs)
(-> bit-string? integer?)
(bytes-length (bit-string->bytes bs)))
(define (build-bit-string . bools)
(define bs 0)
(for ([flag (reverse (rest (reverse bools)))])
(define bit (if flag 1 0))
(set! bs (bitwise-ior bs bit))
(set! bs (arithmetic-shift bs 1)))
(set! bs (bitwise-ior (if (first (reverse bools)) 1 0) bs))
bs)
(define (build-version-string version)
(define version-string (make-parameter ""))
(case version
[(v3 version3 v3.1)
(version-string (bit-string
;; Tag Length
[#x06 :: bytes 2]
;; Protocol Tag
[#"MQlsdp" :: binary]
;; Protocol Level
[#x03 :: bytes 1]))]
[(v4 version4 v3.1.1)
(version-string (bit-string
;; Tag Length
[#x04 :: bytes 2]
;; Protocol Tag
[#"MQTT" :: binary]
;; Protocol Level
[#x04 :: bytes 1]))])
(version-string))
(define (variable-header version
#:username [username false]
#:password [password false]
#:will-retain [will-retain false]
#:will-qos [will-qos false]
#:will-flag [will-flag false]
#:clean-session [clean-session true]
#:keepalive [keepalive 60])
(bit-string
[(build-version-string version) :: binary]
;; Connect Flags - one byte
[(build-bit-string username
password
will-retain
will-qos
will-flag
clean-session
RESERVED-ZERO) :: bytes 1]
;; Keepalive - 60 second default, two bytes
[keepalive :: bytes 2]
))
(define (hexpad str)
(if (< (string-length str) 2)
(format "0~a" str)
str))
(struct connection (uri port in out))
(define (send-msg conn msg)
(define bls (bytes->list (bit-string->bytes msg)))
(packet-dbg bls)
(for ([b bls])
(write-byte b (connection-out conn)))
(flush-output (connection-out conn)))
(define (new-mqtt-connection uri port)
(define-values (inp outp)
(tcp-connect uri port))
(connection uri port inp outp))
(require racket/match)
(define (connect #:id [id "racket"]
#:version [version 'v3.1.1]
#:username [username false]
#:password [password false]
#:will-retain [will-retain false]
#:will-qos [will-qos false]
#:will-flag [will-flag false]
#:clean-session [clean-session true]
#:keepalive [keepalive 60])
(bit-string
;; 3.1 CONNECT – Client requests a connection to a Server
;; 3.1.1 Fixed header
#x10
;; 2.2.3 Remaining Length
[(+ (bit-string-length/bytes
(variable-header version))
(bit-string-length/bytes (encode/utf-8 id)))
:: bytes 1]
[(variable-header version
#:username username
#:password password
#:will-retain will-retain
#:will-qos will-qos
#:will-flag will-flag
#:keepalive keepalive
#:clean-session clean-session) :: binary]
[(encode/utf-8 id) :: binary]
))
(define (connack conn
#:session-present [sp false])
(define packet (read-bytes 4 (connection-in conn)))
(match (bytes->list packet)
[(list #x20 #x02 flags retcode)
(cond
[(or (and sp (= flags 1))
(and (not sp) (= flags 0)))
(case retcode
[(#x00) 'CONNACK:ACCEPTED]
[(#x01) 'CONNACK:REFUSED]
[(#x02) 'CONNACK:IDENTIFIER-REJECTED]
[(#x03) 'CONNACK:SERVER-UNAVAILABLE]
[(#x04) 'CONNACK:BAD-USERNAME-OR-PASSWORD]
[(#x05) 'CONNACK:NOT-AUTHORIZED]
[else 'CONNACK:RESERVED]
)]
[else 'CONNACK:BADFLAGS])]
[else 'CONNACK:BADPACKET]))
(define (pingreq)
(bit-string
#b11000000 #x00))
(define (pingresp conn)
(define packet (read-bytes 2 (connection-in conn)))
(match (bytes->list packet)
[(list #b11010000 #x00)
'PINGRESP:OK]
[else 'PINRESP:ERROR]))
(define (disconnect conn)
(define packet (bit-string #b11100000 #x00))
(send-msg conn packet)
(close-input-port (connection-in conn))
(close-output-port (connection-out conn)))
(define (connect/connack c #:id [id "racket"])
(send-msg c (connect #:id id
#:clean-session true))
(connack c #:session-present false))
(define (pingreq/resp c)
(send-msg c (pingreq))
(pingresp c))
(define/contract (subscription-payload channel)
(-> bytes? bit-string?)
(bit-string
[(bytes-length channel) :: bytes 2]
[channel :: binary]
;; FIXME: QoS
[#x00 :: bytes 1]))
(define/contract (subscribe channel)
(-> string? bit-string?)
(when (string? channel)
(set! channel (string->bytes/utf-8 channel)))
(define payload (subscription-payload channel))
(bit-string
;; Control packet type (8)
#b10000010
;; Remaining Length - ONE BYTE
;; Length of var header - 2 bytes
;; Length of payload: variable
[(+ (bit-string-length/bytes payload) 2) :: bytes 1]
;; Variable header
[(next-packet-identifier) :: bytes 2]
[payload :: binary]
))
(define/contract (publish-variable-header channel msg)
(-> bytes? bytes? bit-string?)
(bit-string
[(bytes-length channel) :: bytes 2]
[channel :: binary]
;; FIXME: Packet identifier should not be random.
[(random 1000) :: bytes 2]
;; The server knows how long the message is by subtracting the variable
;; header length from the total length.
[msg :: binary]))
(define-syntax-rule (->bytes! str)
(set! str (string->bytes/utf-8 str)))
(define/contract (publish channel msg)
(-> string? string? bit-string?)
(->bytes! channel)
(->bytes! msg)
(define payload (publish-variable-header channel msg))
(bit-string
;; FIXME: DUP and other flags here.
#b00110000
[(bit-string-length/bytes payload) :: bytes 1]
[payload :: binary]))
;; FIXME: It would be nice if we had the identifiers
;; that we were ACKing. This would allow for a check.
(define (suback conn)
(define packet (read-bytes 2 (connection-in conn)))
(match (bytes->list packet)
[(list #b10010000 remaining-length)
(define var-header
(read-bytes remaining-length
(connection-in conn)))
(match (bytes->list var-header)
[(list packet-id-msb packet-id-lsb qos-responses ...)
'SUBACK:SUCCESS]
[else
'SUBACK:BAD-VAR-HEADER])]
[else 'SUBACK:ERROR]))
(define (subscribe/ack c channel)
(send-msg c (subscribe channel))
(suback c))
;;;;;;;;;;;;;;;;;;;;;
(define mqtt%
(class object%
(init-field uri port
[id "racket"]
[keepalive 60]
[QoS 0]
)
;; STATEFUL ASPECTS OF CONNECTION
(define session-present false)
(define connected? false)
;; INTERNAL STATE
(define router-thread (make-parameter false))
(define keepalive-thread (make-parameter false))
(define <c> (new-mqtt-connection uri port))
(define handlers (make-hash))
(define (kill-all-threads)
(set! connected? false)
(for ([th (list (router-thread)
(keepalive-thread))])
(when th
(kill-thread th))))
(define/public (start)
(when (not connected?)
;; DO THE CONNECTION ON OBJECT CREATION
(send-msg <c> (connect #:id id
#:keepalive keepalive
#:clean-session true))
(set! connected? true)
(debug "START: hash-length: ~a~n" (hash-count handlers))
(for ([(ch fun) handlers])
(debug "START: Subscribing to ~a~n" ch)
(send-msg <c> (subscribe ch)))))
(define/public (stop)
(when connected?
(disconnect <c>)
(kill-all-threads)))
(define/public (ping)
(when connected?
(send-msg <c> (pingreq))))
(define/public (sub channel handler)
(hash-set! handlers channel handler)
(when connected?
(send-msg <c> (subscribe channel))))
(define/public (pub channel str)
(when connected?
(debug "PUB: ~a <- ~a~n" channel str)
(define packet (publish channel str))
(packet-dbg packet)
(send-msg <c> packet)))
(define (handle-server-msg conn header)
(when connected?
(cond
[(eof-object? header)
(kill-all-threads)
'MQTT:DISCONNECTED]
[else
(define bls (bytes->list header))
(match* ((first bls) (second bls))
;; PINGREQ
[(#b11010000 #x00)
'PINGRESP:OK]
;; SUBSCRIBE
[(#b10010000 remaining-length)
(define var-header
(read-bytes remaining-length
(connection-in conn)))
(match (bytes->list var-header)
[(list packet-id-msb packet-id-lsb qos-responses ...)
'SUBACK:SUCCESS]
[else
'SUBACK:BAD-VAR-HEADER])]
;; CONNACK
[(#x20 #x02)
(define-values (flags retcode)
(apply values (bytes->list
(read-bytes 2 (connection-in conn)))))
(cond
[(or (and session-present (= flags 1))
(and (not session-present) (= flags 0)))
(case retcode
[(#x00) 'CONNACK:ACCEPTED]
[(#x01) 'CONNACK:REFUSED]
[(#x02) 'CONNACK:IDENTIFIER-REJECTED]
[(#x03) 'CONNACK:SERVER-UNAVAILABLE]
[(#x04) 'CONNACK:BAD-USERNAME-OR-PASSWORD]
[(#x05) 'CONNACK:NOT-AUTHORIZED]
[else 'CONNACK:RESERVED]
)]
[else 'CONNACK:BADFLAGS])]
;; PUBLISH
[(flags remaining-length-b)
#:when (and (>= flags #b00110000)
(<= flags #b00111111))
;; FIXME: Flags are DUP, QoS, and RETAIN.
;; I should handle them.
(define identifier-length
(integer-bytes->integer (read-bytes 2 (connection-in conn))
false true))
(define remaining-length
(integer-bytes->integer (bytes 0 remaining-length-b)
false true))
(define content-length (- remaining-length identifier-length 2))
(debug "PUB: READING ~a ~a~n" identifier-length content-length)
(define incoming-channel-bytes
(read-bytes identifier-length (connection-in conn)))
;; Drop the length bytes.
(define pub-content
(list->bytes
(drop (bytes->list
(read-bytes content-length (connection-in conn)))
2)))
(debug "~a: ~a~n" incoming-channel-bytes pub-content)
;; Apply the handler to the published content
;; Handle wildcards
(define incoming-channel (bytes->string/utf-8 incoming-channel-bytes))
(for ([(chan fun) handlers])
(when (regexp-match "#" chan)
(define pattern (regexp-replace "\\$"
(regexp-replace* "#" chan "*")
"\\\\$"))
(debug "Checking '~a' with '~a'~n" incoming-channel pattern)
(when (regexp-match pattern incoming-channel)
(debug "MATCHED~n")
(cond
[(= (procedure-arity fun) 1)
(fun pub-content)]
[(= (procedure-arity fun) 2)
(fun incoming-channel pub-content)])
)))
;; Or, a straight lookup
(define fun (hash-ref handlers incoming-channel false))
(when fun
(cond
[(= (procedure-arity fun) 1)
(fun pub-content)]
[(= (procedure-arity fun) 2)
(fun incoming-channel pub-content)]))
'SERVER:HANDLER-INVOKED
]
[(else1 else2)
(debug "NO MATCH: ~a ~a~n" else1 else2)
'SERVER:NOMATCH]
)]
)))
(define (pinger)
(keepalive-thread
(thread (λ ()
(with-handlers ([exn:fail?
(λ (e)
(debug "P.ERR: ~a~n" e)
(printf "Shutting down pinger.~n")
(kill-all-threads))])
(let loop ()
(sleep (- keepalive (/ keepalive 10)))
(send this ping)
;; Lets keep the server collected
(collect-garbage)
(debug "MEM: ~a~n" (current-memory-use))
(loop)))))))
(define (router conn)
(router-thread
(thread (λ ()
(with-handlers ([exn:fail?
(λ (e)
(debug "R.ERROR: ~a~n" e)
(printf "Shutting down router.~n")
(kill-all-threads))])
(let loop ()
(sync (connection-in conn))
(define header (read-bytes 2 (connection-in conn)))
(define server-response (handle-server-msg conn header))
(debug "SR: ~a~n" server-response)
(unless (equal? server-response 'MQTT:DISCONNECTED)
(loop))
))))))
;; Launch under handlers that kill the threads.
(router <c>)
(pinger)
(super-new)))
#|
(define local (new mqtt%
[uri "localhost"]
[port 9999]
[id "alpha"]
[keepalive 5]))
(send local start)
(send local sub "$SYS/broker/bytes/received"
(λ (v) (printf "L: ~a~n" v)))
|#
#|
(define global (new mqtt%
[uri "test.mosquitto.org"]
[port 1883]
[id "racket"]
[keepalive 10]))
(for ([ch (list "$SYS/broker/bytes/#"
"$SYS/broker/clients/#"
"$SYS/broker/messages/sent"
)])
(send global sub ch (λ (chan v) (printf "~a: ~a~n" chan v))))
(send global start)
(sleep 20)
(send global stop)
|#
(define-syntax mqtt-handlers
(syntax-rules (config channels)
[(_ (confg configs ...) (channels (channel function) ...))
(let ([mqtt (new mqtt% configs ...)])
(for ([ch (list channel ...)]
[fun (list function ...)])
(send mqtt sub ch fun))
(send mqtt start)
mqtt)]
))