annotate mod_pubsub_mqtt/mod_pubsub_mqtt.lua @ 4726:611bc06c4160

mod_rest: Add mapping for XEP-0215: External Service Discovery Enough to query and retrieve services.
author Kim Alvefur <zash@zash.se>
date Tue, 26 Oct 2021 17:52:15 +0200
parents 7dbde05b48a9
children 6ba2188e2686
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 module:set_global();
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 mqtt = module:require "mqtt";
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local st = require "util.stanza";
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 local pubsub_services = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local pubsub_subscribers = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local packet_handlers = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function handle_packet(session, packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 module:log("warn", "MQTT packet received! Length: %d", packet.length);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 for k,v in pairs(packet) do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 module:log("debug", "MQTT %s: %s", tostring(k), tostring(v));
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local handler = packet_handlers[packet.type];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if not handler 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("warn", "Unhandled command: %s", tostring(packet.type));
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 return;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 handler(session, packet);
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
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 function packet_handlers.connect(session, packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 session.conn:write(mqtt.serialize_packet{
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 type = "connack";
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 data = string.char(0x00, 0x00);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 });
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 function packet_handlers.disconnect(session, packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 session.conn:close();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function packet_handlers.publish(session, packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 module:log("warn", "PUBLISH to %s", packet.topic);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 local host, node = packet.topic:match("^([^/]+)/(.+)$");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local pubsub = pubsub_services[host];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if not pubsub then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 module:log("warn", "Unable to locate host/node: %s", packet.topic);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 local id = "mqtt";
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local ok, err = pubsub:publish(node, true, id,
1243
c2bf6b2102aa mod_pubsub_mqtt: Change namespace of 'data' element to make it generic
Matthew Wild <mwild1@gmail.com>
parents: 1240
diff changeset
44 st.stanza("data", { xmlns = "https://prosody.im/protocol/data" })
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 :text(packet.data)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 );
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if not ok then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 module:log("warn", "Error publishing MQTT data: %s", tostring(err));
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 function packet_handlers.subscribe(session, packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 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
54 module:log("warn", "SUBSCRIBE to %s", topic);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local host, node = topic:match("^([^/]+)/(.+)$");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local pubsub = pubsub_subscribers[host];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if not pubsub 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", "Unable to locate host/node: %s", topic);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 local node_subs = pubsub[node];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 if not node_subs then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 node_subs = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 pubsub[node] = node_subs;
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 session.subscriptions[topic] = true;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 node_subs[session] = true;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
1343
7dbde05b48a9 all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 1309
diff changeset
69
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 function packet_handlers.pingreq(session, packet)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 session.conn:write(mqtt.serialize_packet{type = "pingresp"});
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local sessions = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local mqtt_listener = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 function mqtt_listener.onconnect(conn)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 sessions[conn] = {
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 conn = conn;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 stream = mqtt.new_stream();
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 subscriptions = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 };
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
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 function mqtt_listener.onincoming(conn, data)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local session = sessions[conn];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if session then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 local packets = session.stream:feed(data);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 for i = 1, #packets do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 handle_packet(session, packets[i]);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 function mqtt_listener.ondisconnect(conn)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 local session = sessions[conn];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 for topic in pairs(session.subscriptions) do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 local host, node = topic:match("^([^/]+)/(.+)$");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 local subs = pubsub_subscribers[host];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 if subs then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 local node_subs = subs[node];
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 if node_subs then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 node_subs[session] = nil;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 sessions[conn] = nil;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 module:log("debug", "MQTT client disconnected");
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
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 module:provides("net", {
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 default_port = 1883;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 listener = mqtt_listener;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 });
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 local function tostring_content(item)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return tostring(item[1]);
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 data_translators = setmetatable({
1246
e76f7b6be20e mod_pubsub_mqtt: Fix namespace of translator too
Matthew Wild <mwild1@gmail.com>
parents: 1243
diff changeset
124 ["data https://prosody.im/protocol/data"] = tostring_content;
1240
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 ["json urn:xmpp:json:0"] = tostring_content;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 }, {
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 __index = function () return tostring; end;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 });
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 function module.add_host(module)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 local pubsub_module = hosts[module.host].modules.pubsub
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 if pubsub_module then
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 module:log("debug", "MQTT enabled for %s", module.host);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 module:depends("pubsub");
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 pubsub_services[module.host] = assert(pubsub_module.service);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 local subscribers = {};
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 pubsub_subscribers[module.host] = subscribers;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 local function handle_publish(event)
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 -- Build MQTT packet
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 local packet = mqtt.serialize_packet{
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 type = "publish";
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 id = "\000\000";
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 topic = module.host.."/"..event.node;
1309
d3579ea5ff33 mod_pubsub_mqtt: Avoid tracebacks about concatenating nil field
Vadim Misbakh-Soloviov <mva@mva.name>
parents: 1246
diff changeset
144 data = data_translators[tostring(event.item.name).." "..tostring(event.item.attr.xmlns)](event.item);
1240
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 -- Broadcast to subscribers
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 module:log("debug", "Broadcasting PUBLISH to subscribers of %s/%s", module.host, event.node);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 for session in pairs(subscribers[event.node] or {}) do
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 session.conn:write(packet);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 module:log("debug", "Sent to %s", tostring(session));
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 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 pubsub_services[module.host].events.add_handler("item-published", handle_publish);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 function module.unload()
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 module:log("debug", "MQTT disabled for %s", module.host);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 pubsub_module.service.remove_handler("item-published", handle_publish);
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 pubsub_services[module.host] = nil;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 pubsub_subscribers[module.host] = nil;
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
e0d97eb52ab8 mod_pubsub_mqtt: MQTT (a lightweight binary pubsub protocol) interface for mod_pubsub
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 end