Skip to content

Commit

Permalink
chore(examples) fix copas examples, remove sync example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Nov 5, 2021
1 parent 3f4433a commit 0938bc8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 98 deletions.
71 changes: 46 additions & 25 deletions examples/copas-example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

local mqtt = require("mqtt")
local copas = require("copas")
local mqtt_ioloop = require("mqtt.ioloop")

local num_pings = 10 -- total number of ping-pongs
local timeout = 1 -- timeout between ping-pongs
local suffix = tostring(math.random(1000000)) -- mqtt topic suffix to distinct simultaneous rinning of this script
local delay = 1 -- delay between ping-pongs
local suffix = tostring(math.random(1000000)) -- mqtt topic suffix to distinct simultaneous running of this script

-- NOTE: more about flespi tokens: https://flespi.com/kb/tokens-access-keys-to-flespi-platform
local token = "stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9"
Expand All @@ -16,31 +15,39 @@ local ping = mqtt.client{
username = token,
clean = true,
version = mqtt.v50,
-- NOTE: copas connector
connector = require("mqtt.luasocket-copas"),
}

local pong = mqtt.client{
uri = "mqtt.flespi.io",
username = token,
clean = true,
version = mqtt.v50,
-- NOTE: copas connector
connector = require("mqtt.luasocket-copas"),
}

ping:on{
connect = function(connack)
assert(connack.rc == 0)
print("ping connected")

for i = 1, num_pings do
copas.sleep(timeout)
print("ping", i)
assert(ping:publish{ topic = "luamqtt/copas-ping/"..suffix, payload = "ping"..i, qos = 1 })
end

copas.sleep(timeout)

print("ping done")
assert(ping:publish{ topic = "luamqtt/copas-ping/"..suffix, payload = "done", qos = 1 })
ping:disconnect()
-- adding another thread; copas handlers should return quickly, anything
-- that can wait should be off-loaded from the handler to a thread.
-- Especially anything that yields; socket reads/writes and sleeps, and the
-- code below does both, sleeping, and writing (implicit in 'publish')
copas.addthread(function()
for i = 1, num_pings do
copas.sleep(delay)
print("ping", i)
assert(ping:publish{ topic = "luamqtt/copas-ping/"..suffix, payload = "ping"..i, qos = 1 })
end

print("ping done")
assert(ping:publish{ topic = "luamqtt/copas-ping/"..suffix, payload = "done", qos = 1 })
ping:disconnect()
end)
end,
error = function(err)
print("ping MQTT client error:", err)
Expand Down Expand Up @@ -72,19 +79,33 @@ pong:on{
end,
}

local function add_client(cl)
-- add keep-alive timer
local timer = copas.addthread(function()
while cl do
copas.sleep(cl:check_keep_alive())
end
end)
-- add client to connect and listen
copas.addthread(function()
while cl do
local timeout = cl:step()
if not timeout then
cl = nil -- exiting, inform keep-alive timer
copas.wakeup(timer)
else
if timeout > 0 then
copas.sleep(timeout)
end
end
end
end)
end

print("running copas loop...")

copas.addthread(function()
local ioloop = mqtt_ioloop.create{ sleep = 0.01, sleep_function = copas.sleep }
ioloop:add(ping)
ioloop:run_until_clients()
end)

copas.addthread(function()
local ioloop = mqtt_ioloop.create{ sleep = 0.01, sleep_function = copas.sleep }
ioloop:add(pong)
ioloop:run_until_clients()
end)
add_client(ping)
add_client(pong)

copas.loop()

Expand Down
37 changes: 23 additions & 14 deletions examples/copas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,31 @@ client:on{
end
}

-- run io loop for client until connection close
copas.addthread(function()
print("running client in separated copas thread #1...")
mqtt.run_sync(client)

-- NOTE: in sync mode no automatic reconnect is working, but you may just wrap "mqtt.run_sync(client)" call in a loop like this:
-- while true do
-- mqtt.run_sync(client)
-- end
end)
local function add_client(cl)
-- add keep-alive timer
local timer = copas.addthread(function()
while cl do
copas.sleep(cl:check_keep_alive())
end
end)
-- add client to connect and listen
copas.addthread(function()
while cl do
local timeout = cl:step()
if not timeout then
cl = nil -- exiting
copas.wakeup(timer)
else
if timeout > 0 then
copas.sleep(timeout)
end
end
end
end)
end

copas.addthread(function()
print("execution of separated copas thread #2...")
copas.sleep(0.1)
print("thread #2 stopped")
end)

add_client(client)
copas.loop()
print("done, copas loop is stopped")
59 changes: 0 additions & 59 deletions examples/sync.lua

This file was deleted.

0 comments on commit 0938bc8

Please sign in to comment.