From 7ad3a153c64ec1878c6714c57272965df2b1d09d Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Wed, 19 Aug 2020 12:43:28 +0300 Subject: [PATCH 1/6] allow loopless iteration --- mqtt/client.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mqtt/client.lua b/mqtt/client.lua index 22d4959..bd41edb 100644 --- a/mqtt/client.lua +++ b/mqtt/client.lua @@ -908,7 +908,12 @@ function client_mt:_ioloop_iteration() if conn then -- network connection opened -- perform packet receiving using ioloop receive function - local ok, err = self:_io_iteration(ioloop_recv) + local ok, err + if loop then + ok, err = self:_io_iteration(ioloop_recv) + else + ok, err = self:_sync_iteration() + end if ok then -- send PINGREQ if keep_alive interval is reached From b67757f51189cfc409b5be3dedc1cbd9f6af6c4e Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Wed, 19 Aug 2020 12:46:22 +0300 Subject: [PATCH 2/6] add copas connector --- mqtt/luasocket-copas.lua | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 mqtt/luasocket-copas.lua diff --git a/mqtt/luasocket-copas.lua b/mqtt/luasocket-copas.lua new file mode 100644 index 0000000..cc743a0 --- /dev/null +++ b/mqtt/luasocket-copas.lua @@ -0,0 +1,47 @@ +-- DOC: http://w3.impa.br/~diego/software/luasocket/tcp.html + +-- module table +local luasocket = {} + +local socket = require("socket") +local copas = require("copas") + +-- Open network connection to .host and .port in conn table +-- Store opened socket to conn table +-- Returns true on success, or false and error text on failure +function luasocket.connect(conn) + local sock, err = socket.connect(conn.host, conn.port) + if not sock then + return false, "socket.connect failed: "..err + end + conn.sock = sock + return true +end + +-- Shutdown network connection +function luasocket.shutdown(conn) + conn.sock:shutdown() +end + +-- Send data to network connection +function luasocket.send(conn, data, i, j) + local ok, err = copas.send(conn.sock, data, i, j) + return ok, err +end + +-- Receive given amount of data from network connection +function luasocket.receive(conn, size) + local ok, err = copas.receive(conn.sock, size) + return ok, err +end + +-- Set connection's socket to non-blocking mode and set a timeout for it +function luasocket.settimeout(conn, timeout) + conn.timeout = timeout + conn.sock:settimeout(0) +end + +-- export module table +return luasocket + +-- vim: ts=4 sts=4 sw=4 noet ft=lua From 2df3ab3b1dc16effe8bd5acd4d77ac5228c9711d Mon Sep 17 00:00:00 2001 From: Vladimir Dronnikov Date: Wed, 19 Aug 2020 14:06:28 +0300 Subject: [PATCH 3/6] fix for loopless iteration --- mqtt/client.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mqtt/client.lua b/mqtt/client.lua index bd41edb..8c94b7e 100644 --- a/mqtt/client.lua +++ b/mqtt/client.lua @@ -938,7 +938,7 @@ function client_mt:_ioloop_iteration() self.reconnect_timer_start = nil self:start_connecting() else - loop:can_sleep() + if loop then loop:can_sleep() end end else self.reconnect_timer_start = os_time() @@ -946,7 +946,7 @@ function client_mt:_ioloop_iteration() end else -- finish working with client - loop:remove(self) + if loop then loop:remove(self) end end end end From ba5f8d3e9fde3f4566ecdb8858f6346982bfbf8b Mon Sep 17 00:00:00 2001 From: Alexander Kiranov Date: Sun, 13 Sep 2020 14:26:52 +0300 Subject: [PATCH 4/6] codestyle fix --- mqtt/client.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mqtt/client.lua b/mqtt/client.lua index 8c94b7e..d3ccf4e 100644 --- a/mqtt/client.lua +++ b/mqtt/client.lua @@ -938,7 +938,9 @@ function client_mt:_ioloop_iteration() self.reconnect_timer_start = nil self:start_connecting() else - if loop then loop:can_sleep() end + if loop then + loop:can_sleep() + end end else self.reconnect_timer_start = os_time() @@ -946,7 +948,9 @@ function client_mt:_ioloop_iteration() end else -- finish working with client - if loop then loop:remove(self) end + if loop then + loop:remove(self) + end end end end From b4eb97cb68a6411a84bbc6667e3823b16aaafabd Mon Sep 17 00:00:00 2001 From: Alexander Kiranov Date: Sun, 13 Sep 2020 15:43:09 +0300 Subject: [PATCH 5/6] pull #26: improved copas support + added test --- mqtt/luasocket-copas.lua | 17 ++++---- tests/run-for-all-lua-versions.sh | 1 + tests/spec/mqtt-client.lua | 65 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/mqtt/luasocket-copas.lua b/mqtt/luasocket-copas.lua index cc743a0..069229e 100644 --- a/mqtt/luasocket-copas.lua +++ b/mqtt/luasocket-copas.lua @@ -1,7 +1,8 @@ --- DOC: http://w3.impa.br/~diego/software/luasocket/tcp.html +-- DOC: https://keplerproject.github.io/copas/ +-- NOTE: you will need to install copas like this: luarocks install copas -- module table -local luasocket = {} +local connector = {} local socket = require("socket") local copas = require("copas") @@ -9,7 +10,7 @@ local copas = require("copas") -- Open network connection to .host and .port in conn table -- Store opened socket to conn table -- Returns true on success, or false and error text on failure -function luasocket.connect(conn) +function connector.connect(conn) local sock, err = socket.connect(conn.host, conn.port) if not sock then return false, "socket.connect failed: "..err @@ -19,29 +20,29 @@ function luasocket.connect(conn) end -- Shutdown network connection -function luasocket.shutdown(conn) +function connector.shutdown(conn) conn.sock:shutdown() end -- Send data to network connection -function luasocket.send(conn, data, i, j) +function connector.send(conn, data, i, j) local ok, err = copas.send(conn.sock, data, i, j) return ok, err end -- Receive given amount of data from network connection -function luasocket.receive(conn, size) +function connector.receive(conn, size) local ok, err = copas.receive(conn.sock, size) return ok, err end -- Set connection's socket to non-blocking mode and set a timeout for it -function luasocket.settimeout(conn, timeout) +function connector.settimeout(conn, timeout) conn.timeout = timeout conn.sock:settimeout(0) end -- export module table -return luasocket +return connector -- vim: ts=4 sts=4 sw=4 noet ft=lua diff --git a/tests/run-for-all-lua-versions.sh b/tests/run-for-all-lua-versions.sh index f397b6f..1f3b169 100755 --- a/tests/run-for-all-lua-versions.sh +++ b/tests/run-for-all-lua-versions.sh @@ -26,6 +26,7 @@ for ver in -l5.1 -l5.2 -l5.3 -j2.0 -j2.1; do luarocks install luabitop > /dev/null 2>&1 fi luarocks install busted > /dev/null 2>&1 + luarocks install copas > /dev/null 2>&1 if [ -d /usr/lib/x86_64-linux-gnu ]; then # debian-based OS [ -f /etc/lsb-release ] && . /etc/lsb-release diff --git a/tests/spec/mqtt-client.lua b/tests/spec/mqtt-client.lua index 21786a8..c26844e 100644 --- a/tests/spec/mqtt-client.lua +++ b/tests/spec/mqtt-client.lua @@ -504,5 +504,70 @@ describe("no_local flag for subscription: ", function() end) end) +describe("copas connector", function() + local mqtt = require("mqtt") + local copas = require("copas") + local prefix = "luamqtt/" .. tostring(math.floor(math.random()*1e13)) + + it("test", function() + -- NOTE: more about flespi tokens: + -- https://flespi.com/kb/tokens-access-keys-to-flespi-platform + local flespi_token = "stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9" + + local client = mqtt.client{ + uri = "mqtt.flespi.io", + clean = true, + username = flespi_token, + version = mqtt.v50, + + connector = require("mqtt.luasocket-copas"), + } + + client:on{ + connect = function() + -- print("client connected") + assert(client:subscribe{topic=prefix.."/copas", qos=1, callback=function() + -- print("subscribed") + copas.sleep(2) + assert(client:publish{ + topic = prefix.."/copas", + payload = "copas test", + qos = 1, + }) + end}) + end, + + message = function(msg) + assert(client:acknowledge(msg)) + -- print("received", msg) + if msg.topic == prefix.."/copas" and msg.payload == "copas test" then + -- print("disconnect") + assert(client:disconnect()) + end + end + } + + local ticks = 0 + local mqtt_finished = false + + copas.addthread(function() + mqtt.run_sync(client) + mqtt_finished = true + assert.is_true(ticks > 1, "expecting mqtt client run takes at least one tick") + end) + + copas.addthread(function() + for _ = 1, 3 do + copas.sleep(1) + ticks = ticks + 1 + end + end) + + copas.loop() + + assert.is_true(mqtt_finished, "expecting mqtt client to finish its work") + assert.is_true(ticks == 3, "expecting 3 ticks") + end) +end) -- vim: ts=4 sts=4 sw=4 noet ft=lua From a826ef1eace1e41603899811afa6617f1c7cd44a Mon Sep 17 00:00:00 2001 From: Alexander Kiranov Date: Sun, 13 Sep 2020 15:55:30 +0300 Subject: [PATCH 6/6] pull #27: version bump --- luamqtt-3.3.2-1.rockspec => luamqtt-3.4.1-1.rockspec | 4 ++-- mqtt/init.lua | 2 +- openwrt/Makefile | 2 +- openwrt/make-package-without-openwrt-sources.sh | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) rename luamqtt-3.3.2-1.rockspec => luamqtt-3.4.1-1.rockspec (96%) diff --git a/luamqtt-3.3.2-1.rockspec b/luamqtt-3.4.1-1.rockspec similarity index 96% rename from luamqtt-3.3.2-1.rockspec rename to luamqtt-3.4.1-1.rockspec index e27cb9f..ee64207 100644 --- a/luamqtt-3.3.2-1.rockspec +++ b/luamqtt-3.4.1-1.rockspec @@ -1,8 +1,8 @@ package = "luamqtt" -version = "3.3.2-1" +version = "3.4.1-1" source = { url = "git://github.com/xHasKx/luamqtt", - tag = "v3.3.2", + tag = "v3.4.1", } description = { summary = "luamqtt - Pure-lua MQTT v3.1.1 and v5.0 client", diff --git a/mqtt/init.lua b/mqtt/init.lua index 6a01adb..5156f41 100644 --- a/mqtt/init.lua +++ b/mqtt/init.lua @@ -24,7 +24,7 @@ local mqtt = { v50 = 5, -- supported protocol version, MQTT v5.0 -- mqtt library version - _VERSION = "3.3.2", + _VERSION = "3.4.1", } -- load required stuff diff --git a/openwrt/Makefile b/openwrt/Makefile index 82b6e6c..751bb86 100644 --- a/openwrt/Makefile +++ b/openwrt/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luamqtt -PKG_VERSION:=3.3.2 +PKG_VERSION:=3.4.1 PKG_RELEASE:=1 PKG_MAINTAINER:=Alexander Kiranov diff --git a/openwrt/make-package-without-openwrt-sources.sh b/openwrt/make-package-without-openwrt-sources.sh index c9332b0..6679087 100755 --- a/openwrt/make-package-without-openwrt-sources.sh +++ b/openwrt/make-package-without-openwrt-sources.sh @@ -50,7 +50,7 @@ rm -rf $PKG_ROOT/usr # prepare control.tar.gz cat << EOF > $PKG_ROOT/control Package: luamqtt -Version: 3.3.2-1 +Version: 3.4.1-1 Depends: libc, lua, luasocket, luabitop, luasec Source: https://github.com/xHasKx/luamqtt SourceName: luamqtt