Created
July 9, 2017 07:46
-
-
Save nicopace/6612d48880f07e61523c564364f5edab to your computer and use it in GitHub Desktop.
Pub/Sub messaging over ubus with Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
require "ubus" | |
require "uloop" | |
uloop.init() | |
local conn = ubus.connect() | |
if not conn then | |
error("Failed to connect to ubus") | |
end | |
-- This is used as namespace for the publishing of events | |
local ubus_objects = { | |
test = { | |
-- You get notified when someone subscribes to a channel | |
__subscriber_cb = function( subs ) | |
print("total subs: ", subs ) | |
end | |
} | |
} | |
conn:add( ubus_objects ) | |
-- We will send one message per second | |
local timer | |
local counter = 0 | |
function t() | |
counter = counter + 1 | |
-- this is the call that does the publishing | |
conn:notify(ubus_objects.test.__ubusobj, "test.alarm", {count = counter}) | |
timer:set(1000) | |
end | |
timer = uloop.timer(t) | |
timer:set(1000) | |
uloop.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env lua | |
require "ubus" | |
require "uloop" | |
uloop.init() | |
local conn = ubus.connect() | |
if not conn then | |
error("Failed to connect to ubus") | |
end | |
local sub = { | |
notify = function(msg) | |
print("Count: ", msg["count"]) | |
end, | |
} | |
-- this is the call that does the subscribing | |
conn:subscribe("test", sub) | |
uloop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love this ubus stuff. ;)
Currently, I try adding ubus bindings to mesh daemon babeld: openwrt/routing#630
uhostapd is just some code I wanted to upload somewhere, that access hostapd ubus bindings in lua.
I will make use of it for another project.