From patchwork Thu May 19 11:04:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Fraser X-Patchwork-Id: 623941 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2001:1868:205::9]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r9StD6K0Bz9sdn for ; Thu, 19 May 2016 21:06:12 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=netduma-com.20150623.gappssmtp.com header.i=@netduma-com.20150623.gappssmtp.com header.b=T83BgqOP; dkim-atps=neutral Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b3Ll5-0002gV-Ry; Thu, 19 May 2016 11:04:51 +0000 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b3Ll3-0002f6-Hv for lede-dev@lists.infradead.org; Thu, 19 May 2016 11:04:50 +0000 Received: by mail-wm0-x234.google.com with SMTP id n129so225019364wmn.1 for ; Thu, 19 May 2016 04:04:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netduma-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id; bh=z6nqJ2qofiktSt1ZwCAYRgTxXyknaWVnjBmLifS0nW0=; b=T83BgqOP1D3GZ39XRlgjVsmG48bo7fEKq9Mr8AavKCqpj9TBXXeBolUMUO98rbcjvW OjCsB9EWPAQ+a+9THY2b3m3BJvr+l1gMVD5y5gcOx49Onl6yntP6bJtPJDDzBxqyC/GX LZD7cHvh/0XAZ8zoZgy/FgzH5FDYYWALAh3b1liZmTBcSn/SOXczzrUPnIgSxh/ar0m2 Pe7pnt9wNNMg7nfBbjcdZVIOq7PemYVQ5ra+20Iutk3sjJS3yQ/r3zRnHmIOX0bXCS/q 8LgWKLeTjLsI8v/0yTh6QsQDei9EfAVX4kzsrA3wnMYUgrwXAmRh7FIVhvfxhWb7qN6C 3pGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=z6nqJ2qofiktSt1ZwCAYRgTxXyknaWVnjBmLifS0nW0=; b=GyIhtvTyQ7vq2eWsH5gYjPVcFzQIQOXbXkYcNd7DLqgS2xYZzgcUU0b24h41tzW3wM 5XHAO2Ii3ROTrHtEJjPoIN8tyd4V/peVTLuXfl196TXgskicCn0UZjc9nLk6VA5xeJSl TaMU/SHgjpW4zCJUD4VsJdLiWrzzzC8pERFMXPCQ+SIkQg7bgArd8G+v4YaWNQbI8mJW 2xJOLoSjvrA8ypZ12RCHS8oWdM783z/UUgSwdP2Q9F87ENxgdFSoj42MkWEJdYcoSmLE JKFGTlaN6FKxqa9IgXkymDCo7GHz6ffF3jJvMOYG7oB0sXLgnaJn3YZhRHXCG8XcLhoK ZaEQ== X-Gm-Message-State: AOPr4FU6hApXJMTnuQbzg0FrEUnCsRrp7lBIGmzBFa07X4t+nobGbnfTyFjqlbGcZNZBpA== X-Received: by 10.195.11.34 with SMTP id ef2mr12640259wjd.101.1463655867841; Thu, 19 May 2016 04:04:27 -0700 (PDT) Received: from iain-dev-laptop.lan ([31.24.219.131]) by smtp.gmail.com with ESMTPSA id lr9sm13743534wjb.39.2016.05.19.04.04.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 May 2016 04:04:27 -0700 (PDT) From: Iain Fraser To: lede-dev@lists.infradead.org Date: Thu, 19 May 2016 12:04:21 +0100 Message-Id: <1463655861-14776-1-git-send-email-iainf@netduma.com> X-Mailer: git-send-email 1.7.9.5 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160519_040449_751591_6A04ECD8 X-CRM114-Status: GOOD ( 11.34 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [2a00:1450:400c:c09:0:0:0:234 listed in] [list.dnswl.org] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid Subject: [LEDE-DEV] [PATCH] Lua examples using ubus publish and subscribe. X-BeenThere: lede-dev@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "Lede-dev" Errors-To: lede-dev-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org --- 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 --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()