Implemented publish/subscribe lua bindings to libubus-lua with example lua files.
[project/ubus.git] / lua / publisher.lua
1 #!/usr/bin/env lua
2
3 require "ubus"
4 require "uloop"
5
6 --[[
7 A demo of ubus publisher binding. Should be run before subscriber.lua
8 --]]
9
10
11 uloop.init()
12
13 local conn = ubus.connect()
14 if not conn then
15 error("Failed to connect to ubus")
16 end
17
18 local ubus_objects = {
19 test = {
20 hello = {
21 function(req, msg)
22 conn:reply(req, {message="foo"});
23 print("Call to function 'hello'")
24 for k, v in pairs(msg) do
25 print("key=" .. k .. " value=" .. tostring(v))
26 end
27 end, {id = ubus.INT32, msg = ubus.STRING }
28 },
29 hello1 = {
30 function(req)
31 conn:reply(req, {message="foo1"});
32 conn:reply(req, {message="foo2"});
33 print("Call to function 'hello1'")
34 end, {id = ubus.INT32, msg = ubus.STRING }
35 },
36 __subscriber_cb = function( subs )
37 print("total subs: ", subs )
38 end
39 }
40 }
41
42 conn:add( ubus_objects )
43 print("Objects added, starting loop")
44
45 -- start time
46 local timer
47 local counter = 0
48 function t()
49 counter = counter + 1
50 local params = {
51 count = counter
52 }
53 conn:notify( ubus_objects.test.__ubusobj, "test.alarm", params )
54 timer:set(10000)
55 end
56 timer = uloop.timer(t)
57 timer:set(1000)
58
59
60 uloop.run()