annotate mod_pubsub_mqtt/mqtt.lib.lua @ 2423:1b6027ef5191

mod_pubsub_feeds: Load config option once
author Kim Alvefur <zash@zash.se>
date Mon, 19 Dec 2016 07:49:40 +0100
parents 7dbde05b48a9
children d2a84e6aed2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local bit = require "bit";
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
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 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
33 "connect", "connack",
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 "publish", "puback", "pubrec", "pubrel", "pubcomp",
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 "subscribe", "subak", "unsubscribe", "unsuback",
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 "pingreq", "pingresp",
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 "disconnect"
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 };
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 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
41 local packet = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 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
43 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
44 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
45 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
46 packet.retain = bit.band(header, 0x01) == 0x01;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
47
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 -- Get length
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 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
50 repeat
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 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
52 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
53 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
54 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
55 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
56 if packet.type == "connect" then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if self:read_string() ~= "MQIsdp" then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 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
59 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
60 else
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 packet.version = 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
62 packet.connect_flags = 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
63 packet.keepalive_timer = 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
64 length = length - 11;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 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
67 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
68 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
69 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
70 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
71 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
72 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 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
74 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
75 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
76 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
77 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local topics = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 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
80 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
81 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
82 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
83 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
84 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 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
86 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 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
88 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
89 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return packet;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 end);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 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
105 module:log("debug", "Feeding %d bytes", #data);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 local packets = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 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
108 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
109 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
110 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
111 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
112 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 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
114 return packets;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 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
118 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
119 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
120 return stream;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 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
124 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
125 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
126 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
127 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
128 break;
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 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 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
132
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 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
134 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
135 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
136 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
137 local t = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 for _, topic in ipairs(packet.topics) do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 table.insert(t, string.char(bit.band(#topic, 0xff00), bit.band(#topic, 0x00ff))..topic.."\000");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 packet.data = table.concat(t);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
143
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 -- Get length
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 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
146 repeat
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 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
148 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
149 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
150 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
151 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 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
153 until length <= 0;
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1240
diff changeset
154
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 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
156 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 return {
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 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
160 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
161 };