Mercurial > prosody-modules
annotate mod_pubsub_mqtt/mqtt.lib.lua @ 6257:fa45ae704b79
mod_cloud_notify: Update Readme
diff --git a/mod_cloud_notify/README.md b/mod_cloud_notify/README.md
--- a/mod_cloud_notify/README.md
+++ b/mod_cloud_notify/README.md
@@ -1,109 +1,106 @@
----
-labels:
-- 'Stage-Beta'
-summary: 'XEP-0357: Cloud push notifications'
----
+# Introduction
-Introduction
-============
+This module enables support for sending "push notifications" to clients
+that need it, typically those running on certain mobile devices.
-This module enables support for sending "push notifications" to clients that
-need it, typically those running on certain mobile devices.
+As well as this module, your client must support push notifications (the
+apps that need it generally do, of course) and the app developer's push
+gateway must be reachable from your Prosody server (this happens over a
+normal XMPP server-to-server 's2s' connection).
-As well as this module, your client must support push notifications (the apps
-that need it generally do, of course) and the app developer's push gateway
-must be reachable from your Prosody server (this happens over a normal XMPP
-server-to-server 's2s' connection).
-
-Details
-=======
+# Details
Some platforms, notably Apple's iOS and many versions of Android, impose
-limits that prevent applications from running or accessing the network in the
-background. This makes it difficult or impossible for an XMPP application to
-remain reliably connected to a server to receive messages.
-
-In order for messaging and other apps to receive notifications, the OS vendors
-run proprietary servers that their OS maintains a permanent connection to in
-the background. Then they provide APIs to application developers that allow
-sending notifications to specific devices via those servers.
+limits that prevent applications from running or accessing the network
+in the background. This makes it difficult or impossible for an XMPP
+application to remain reliably connected to a server to receive
+messages.
-When you connect to your server with an app that requires push notifications,
-it will use this module to set up a "push registration". When you receive
-a message but your device is not connected to the server, this module will
-generate a notification and send it to the push gateway operated by your
-application's developers). Their gateway will then connect to your device's
-OS vendor and ask them to forward the notification to your device. When your
-device receives the notification, it will display it or wake up the app so it
-can connect to XMPP and receive any pending messages.
+In order for messaging and other apps to receive notifications, the OS
+vendors run proprietary servers that their OS maintains a permanent
+connection to in the background. Then they provide APIs to application
+developers that allow sending notifications to specific devices via
+those servers.
-This protocol is described for developers in [XEP-0357: Push Notifications].
+When you connect to your server with an app that requires push
+notifications, it will use this module to set up a "push registration".
+When you receive a message but your device is not connected to the
+server, this module will generate a notification and send it to the push
+gateway operated by your application's developers). Their gateway will
+then connect to your device's OS vendor and ask them to forward the
+notification to your device. When your device receives the notification,
+it will display it or wake up the app so it can connect to XMPP and
+receive any pending messages.
-For this module to work reliably, you must have [mod_smacks], [mod_mam] and
-[mod_carbons] also enabled on your server.
+This protocol is described for developers in \[XEP-0357: Push
+Notifications\].
+
+For this module to work reliably, you must have \[mod_smacks\],
+\[mod_mam\] and \[mod_carbons\] also enabled on your server.
-Some clients, notably Siskin and Snikket iOS need some additional extensions
-that are not currently defined in a standard XEP. To support these clients,
-see [mod_cloud_notify_extensions].
+Some clients, notably Siskin and Snikket iOS need some additional
+extensions that are not currently defined in a standard XEP. To support
+these clients, see \[mod_cloud_notify_extensions\].
-Configuration
-=============
+# Configuration
- Option Default Description
- ------------------------------------ ----------------- -------------------------------------------------------------------------------------------------------------------
- `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
- `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
- `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
- `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
- `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
- `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
+ Option Default Description
+ -------------------------------------- ---------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ `push_notification_important_body` `New Message!` The body text to use when the stanza is important (see above), no message body is sent if this is empty
+ `push_max_errors` `16` How much persistent push errors are tolerated before notifications for the identifier in question are disabled
+ `push_max_devices` `5` The number of allowed devices per user (the oldest devices are automatically removed if this threshold is reached)
+ `push_max_hibernation_timeout` `259200` (72h) Number of seconds to extend the smacks timeout if no push was triggered yet (default: 72 hours)
+ `push_notification_with_body` (\*) `false` Whether or not to send the real message body to remote pubsub node. Without end-to-end encryption, enabling this may expose your message contents to your client developers and OS vendor. Not recommended.
+ `push_notification_with_sender` (\*) `false` Whether or not to send the real message sender to remote pubsub node. Enabling this may expose your contacts to your client developers and OS vendor. Not recommended.
-(\*) There are privacy implications for enabling these options.
+(\*) There are privacy implications for enabling these options.[^1]
-Internal design notes
-=====================
+# Internal design notes
-App servers are notified about offline messages, messages stored by [mod_mam]
-or messages waiting in the smacks queue.
-The business rules outlined [here](//mail.jabber.org/pipermail/standards/2016-February/030925.html) are all honored[^2].
+App servers are notified about offline messages, messages stored by
+\[mod_mam\] or messages waiting in the smacks queue. The business rules
+outlined
+[here](//mail.jabber.org/pipermail/standards/2016-February/030925.html)
+are all honored[^2].
-To cooperate with [mod_smacks] this module consumes some events:
-`smacks-ack-delayed`, `smacks-hibernation-start` and `smacks-hibernation-end`.
-These events allow this module to send out notifications for messages received
-while the session is hibernated by [mod_smacks] or even when smacks
-acknowledgements for messages are delayed by a certain amount of seconds
-configurable with the [mod_smacks] setting `smacks_max_ack_delay`.
+To cooperate with \[mod_smacks\] this module consumes some events:
+`smacks-ack-delayed`, `smacks-hibernation-start` and
+`smacks-hibernation-end`. These events allow this module to send out
+notifications for messages received while the session is hibernated by
+\[mod_smacks\] or even when smacks acknowledgements for messages are
+delayed by a certain amount of seconds configurable with the
+\[mod_smacks\] setting `smacks_max_ack_delay`.
-The `smacks_max_ack_delay` setting allows to send out notifications to clients
-which aren't already in smacks hibernation state (because the read timeout or
-connection close didn't already happen) but also aren't responding to acknowledgement
-request in a timely manner. This setting thus allows conversations to be smoother
-under such circumstances.
+The `smacks_max_ack_delay` setting allows to send out notifications to
+clients which aren't already in smacks hibernation state (because the
+read timeout or connection close didn't already happen) but also aren't
+responding to acknowledgement request in a timely manner. This setting
+thus allows conversations to be smoother under such circumstances.
-The new event `cloud-notify-ping` can be used by any module to send out a cloud
-notification to either all registered endpoints for the given user or only the endpoints
-given in the event data.
+The new event `cloud-notify-ping` can be used by any module to send out
+a cloud notification to either all registered endpoints for the given
+user or only the endpoints given in the event data.
-The config setting `push_notification_important_body` can be used to specify an alternative
-body text to send to the remote pubsub node if the stanza is encrypted or has a body.
-This way the real contents of the message aren't revealed to the push appserver but it
-can still see that the push is important.
-This is used by Chatsecure on iOS to send out high priority pushes in those cases for example.
+The config setting `push_notification_important_body` can be used to
+specify an alternative body text to send to the remote pubsub node if
+the stanza is encrypted or has a body. This way the real contents of the
+message aren't revealed to the push appserver but it can still see that
+the push is important. This is used by Chatsecure on iOS to send out
+high priority pushes in those cases for example.
-Compatibility
-=============
-
-**Note:** This module should be used with Lua 5.2 and higher. Using it with
-Lua 5.1 may cause push notifications to not be sent to some clients.
+# Compatibility
------- -----------------------------------------------------------------------------
- trunk Works
- 0.12 Works
- 0.11 Works
- 0.10 Works
- 0.9 Support dropped, use last supported version [675726ab06d3](//hg.prosody.im/prosody-modules/raw-file/675726ab06d3/mod_cloud_notify/mod_cloud_notify.lua)
------- -----------------------------------------------------------------------------
+**Note:** This module should be used with Lua 5.2 and higher. Using it
+with Lua 5.1 may cause push notifications to not be sent to some
+clients.
+ ------- -----------------------------------------------------------------
+ trunk Works as of 25-06-13
+ 13 Works
+ 0.12 Works
+ ------- -----------------------------------------------------------------
-[^1]: The service which is expected to forward notifications to something like Google Cloud Messaging or Apple Notification Service
-[^2]: [business_rules.markdown](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.markdown)
+[^1]: The service which is expected to forward notifications to
+ something like Google Cloud Messaging or Apple Notification Service
+
+[^2]: [business_rules.md](//hg.prosody.im/prosody-modules/file/tip/mod_cloud_notify/business_rules.md)
| author | Menel <menel@snikket.de> |
|---|---|
| date | Fri, 13 Jun 2025 10:36:52 +0200 |
| parents | 58df53eefa28 |
| children | ef625180a869 |
| rev | line source |
|---|---|
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
1 local bit = require "util.bitcompat"; |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local stream_mt = {}; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 stream_mt.__index = stream_mt; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 function stream_mt:read_bytes(n_bytes) |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 module:log("debug", "Reading %d bytes... (buffer: %d)", n_bytes, #self.buffer); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local data = self.buffer; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 if not data then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 module:log("debug", "No data, pausing."); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 data = coroutine.yield(); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 module:log("debug", "Have %d bytes of data now (want %d)", #data, n_bytes); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if #data >= n_bytes then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 data, self.buffer = data:sub(1, n_bytes), data:sub(n_bytes+1); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 elseif #data < n_bytes then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 module:log("debug", "Not enough data (only %d bytes out of %d), pausing.", #data, n_bytes); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 self.buffer = data..coroutine.yield(); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 module:log("debug", "Now we have %d bytes, reading...", #data); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 return self:read_bytes(n_bytes); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 module:log("debug", "Returning %d bytes (buffer: %d)", #data, #self.buffer); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 return data; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 function stream_mt:read_string() |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local len1, len2 = self:read_bytes(2):byte(1,2); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 local len = bit.lshift(len1, 8) + len2; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 return self:read_bytes(len), len+2; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
32 function stream_mt:read_word() |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
33 local len1, len2 = self:read_bytes(2):byte(1,2); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
34 local result = bit.lshift(len1, 8) + len2; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
35 module:log("debug", "read_word(%02x, %02x) = %04x (%d)", len1, len2, result, result); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
36 return result; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
37 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
38 |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
39 local function hasbit(byte, n_bit) |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
40 return bit.band(byte, 2^n_bit) ~= 0; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
41 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
42 |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
43 local function encode_string(str) |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
44 return string.char(bit.band(#str, 0xff00), bit.band(#str, 0x00ff))..str; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
45 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
46 |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 local packet_type_codes = { |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 "connect", "connack", |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 "publish", "puback", "pubrec", "pubrel", "pubcomp", |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
50 "subscribe", "suback", "unsubscribe", "unsuback", |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 "pingreq", "pingresp", |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 "disconnect" |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 }; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 function stream_mt:read_packet() |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 local packet = {}; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 local header = self:read_bytes(1):byte(); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 packet.type = packet_type_codes[bit.rshift(bit.band(header, 0xf0), 4)]; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 packet.dup = bit.band(header, 0x08) == 0x08; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 packet.qos = bit.rshift(bit.band(header, 0x06), 1); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 packet.retain = bit.band(header, 0x01) == 0x01; |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1240
diff
changeset
|
62 |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 -- Get length |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 local length, multiplier = 0, 1; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 repeat |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 local digit = self:read_bytes(1):byte(); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 length = length + bit.band(digit, 0x7f)*multiplier; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 multiplier = multiplier*128; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 until bit.band(digit, 0x80) == 0; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 packet.length = length; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 if packet.type == "connect" then |
|
5114
d2a84e6aed2b
mod_pubsub_mqtt: Switch to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
1343
diff
changeset
|
72 if self:read_string() ~= "MQTT" then |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 module:log("warn", "Unexpected packet signature!"); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 packet.type = nil; -- Invalid packet |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 else |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 packet.version = self:read_bytes(1):byte(); |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
77 module:log("debug", "ver: %02x", packet.version); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
78 if packet.version ~= 0x04 then |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
79 module:log("warn", "MQTT version mismatch (got %02x, we support %02x", packet.version, 0x04); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
80 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
81 local flags = self:read_bytes(1):byte(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
82 module:log("debug", "flags: %02x", flags); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
83 packet.keepalive_timer = self:read_bytes(2):byte(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
84 module:log("debug", "keepalive: %d", packet.keepalive_timer); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
85 packet.connect_flags = {}; |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 length = length - 11; |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
87 packet.connect_flags = { |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
88 clean_session = hasbit(flags, 1); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
89 will = hasbit(flags, 2); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
90 will_qos = bit.band(bit.rshift(flags, 2), 0x02); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
91 will_retain = hasbit(flags, 5); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
92 user_name = hasbit(flags, 7); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
93 password = hasbit(flags, 6); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
94 }; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
95 module:log("debug", "%s", require "util.serialization".serialize(packet.connect_flags, "debug")); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
96 module:log("debug", "Reading client_id..."); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
97 packet.client_id = self:read_string(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
98 if packet.connect_flags.will then |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
99 module:log("debug", "Reading will..."); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
100 packet.will = { |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
101 topic = self:read_string(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
102 message = self:read_string(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
103 qos = packet.connect_flags.will_qos; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
104 retain = packet.connect_flags.will_retain; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
105 }; |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
106 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
107 if packet.connect_flags.user_name then |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
108 module:log("debug", "Reading username..."); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
109 packet.username = self:read_string(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
110 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
111 if packet.connect_flags.password then |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
112 module:log("debug", "Reading password..."); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
113 packet.password = self:read_string(); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
114 end |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
115 module:log("debug", "Done parsing connect!"); |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
116 length = 0; -- No payload left |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 elseif packet.type == "publish" then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 packet.topic = self:read_string(); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 length = length - (#packet.topic+2); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 if packet.qos == 1 or packet.qos == 2 then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 packet.id = self:read_bytes(2); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 length = length - 2; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 elseif packet.type == "subscribe" then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 if packet.qos == 1 or packet.qos == 2 then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 packet.id = self:read_bytes(2); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 length = length - 2; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 local topics = {}; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 while length > 0 do |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 local topic, len = self:read_string(); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 table.insert(topics, topic); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 self:read_bytes(1); -- QoS not used |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 length = length - (len+1); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 packet.topics = topics; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 if length > 0 then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 packet.data = self:read_bytes(length); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 end |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
142 module:log("debug", "MQTT packet complete!"); |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 return packet; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 local function new_parser(self) |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 return coroutine.wrap(function (data) |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 self.buffer = data; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 while true do |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 data = coroutine.yield(self:read_packet()); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 module:log("debug", "Parser: %d new bytes", #data); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 self.buffer = (self.buffer or "")..data; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 end); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 function stream_mt:feed(data) |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 local packets = {}; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 local packet = self.parser(data); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 while packet do |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 module:log("debug", "Received packet"); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 table.insert(packets, packet); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 packet = self.parser(""); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 module:log("debug", "Returning %d packets", #packets); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 return packets; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 local function new_stream() |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 local stream = setmetatable({}, stream_mt); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 stream.parser = new_parser(stream); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 return stream; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 local function serialize_packet(packet) |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 local type_num = 0; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 for i, v in ipairs(packet_type_codes) do -- FIXME: I'm so tired right now. |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 if v == packet.type then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 type_num = i; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 break; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 local header = string.char(bit.lshift(type_num, 4)); |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1240
diff
changeset
|
184 |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 if packet.type == "publish" then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 local topic = packet.topic or ""; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 packet.data = string.char(bit.band(#topic, 0xff00), bit.band(#topic, 0x00ff))..topic..packet.data; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 elseif packet.type == "suback" then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 local t = {}; |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
190 for i, result_code in ipairs(packet.results) do |
|
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
191 table.insert(t, string.char(result_code)); |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 end |
|
5833
58df53eefa28
mod_pubsub_mqtt: Update to MQTT 3.1.1
Matthew Wild <mwild1@gmail.com>
parents:
5114
diff
changeset
|
193 packet.data = packet.id..table.concat(t); |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 end |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1240
diff
changeset
|
195 |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 -- Get length |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 local length = #(packet.data or ""); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 repeat |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 local digit = length%128; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 length = math.floor(length/128); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 if length > 0 then |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 digit = bit.bor(digit, 0x80); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 header = header..string.char(digit); -- FIXME: ... |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 until length <= 0; |
|
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1240
diff
changeset
|
206 |
|
1240
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 return header..(packet.data or ""); |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 end |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 return { |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 new_stream = new_stream; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 serialize_packet = serialize_packet; |
|
e0d97eb52ab8
mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 }; |
