diff mbox

[LEDE-DEV] Lua examples using ubus publish and subscribe.

Message ID 1463655861-14776-1-git-send-email-iainf@netduma.com
State Deferred
Headers show

Commit Message

Iain Fraser May 19, 2016, 11:04 a.m. UTC
---
 lua/publisher.lua  |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 lua/subscriber.lua |   25 ++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 lua/publisher.lua
 create mode 100644 lua/subscriber.lua
diff mbox

Patch

diff --git a/lua/publisher.lua b/lua/publisher.lua
new file mode 100644
index 0000000..ba5f6a4
--- /dev/null
+++ b/lua/publisher.lua
@@ -0,0 +1,60 @@ 
+#!/usr/bin/env lua
+
+require "ubus"
+require "uloop"
+
+--[[
+  A demo of ubus publisher binding. Should be run before subscriber.lua
+--]]
+
+
+uloop.init()
+
+local conn = ubus.connect()
+if not conn then
+	error("Failed to connect to ubus")
+end
+
+local ubus_objects = {
+	test = {
+		hello = {
+			function(req, msg)
+				conn:reply(req, {message="foo"});
+				print("Call to function 'hello'")
+				for k, v in pairs(msg) do
+					print("key=" .. k .. " value=" .. tostring(v))
+				end
+			end, {id = ubus.INT32, msg = ubus.STRING }
+		},
+		hello1 = {
+			function(req)
+				conn:reply(req, {message="foo1"});
+				conn:reply(req, {message="foo2"});
+				print("Call to function 'hello1'")
+			end, {id = ubus.INT32, msg = ubus.STRING }
+		},
+		__subscriber_cb = function( subs )
+			print("total subs: ", subs )
+		end
+	}
+}
+
+conn:add( ubus_objects )
+print("Objects added, starting loop")
+
+-- start time
+local timer
+local counter = 0
+function t()
+	counter = counter + 1
+	local params = {
+		count = counter 
+	}
+	conn:notify( ubus_objects.test.__ubusobj, "test.alarm", params )
+	timer:set(10000)
+end
+timer = uloop.timer(t)
+timer:set(1000)
+
+
+uloop.run()
diff --git a/lua/subscriber.lua b/lua/subscriber.lua
new file mode 100644
index 0000000..b0abf96
--- /dev/null
+++ b/lua/subscriber.lua
@@ -0,0 +1,25 @@ 
+#!/usr/bin/env lua
+
+--[[
+  A demo of ubus subscriber binding. Should be run after publisher.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,
+}
+
+conn:subscribe( "test", sub )
+
+uloop.run()