Created
March 4, 2022 19:57
-
-
Save jow-/4aa520b4e495cc7729a82247224f9229 to your computer and use it in GitHub Desktop.
ucode ubus pub/sub
This file contains 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
#!ucode -RS | |
let ubus = require("ubus"); | |
let uloop = require("uloop"); | |
let conn = ubus.connect(); | |
print([ conn, conn.publish ], "\n"); | |
uloop.init(); | |
let rv = conn.publish("ucode", { | |
test: { | |
call: function(req) { | |
return { | |
got: { | |
args: req.args, | |
info: req.info | |
} | |
}; | |
}, | |
args: { | |
i1: 8, | |
i2: 16, | |
i3: 32, | |
i4: 64, | |
hello: "world", | |
foo: [ 1, 2, 3 ], | |
bar: { baz: true }, | |
d: 1.4 | |
} | |
} | |
}, function() { | |
printf("Subscription status change! Have subscribers? %s\n", | |
this.subscribed() ? "yes" : "no"); | |
if (this.subscribed()) { | |
this.notify("narf", { blurb: true }, | |
function(type, data) { printf("Got data callback: %s\n", [ type, data ]) }, | |
function(idx, ret) { printf("Got status callback: %s\n", [ idx, ret ]) }, | |
function(idx, ret) { printf("Got complete callback: %s\n", [ idx, ret ]) }, 100); | |
} | |
}); | |
if (!rv) | |
warn("Can't publish: " + ubus.error() + "\n"); | |
uloop.timer(1000, function() { | |
if (rv.subscribed()) | |
rv.notify("narf", { blurb: true }, | |
function(type, data) { printf("Got data callback: %s\n", [ type, data ]) }, | |
function(idx, ret) { printf("Got status callback: %s\n", [ idx, ret ]) }, | |
function(idx, ret) { printf("Got complete callback: %s\n", [ idx, ret ]) }, 100); | |
this.set(1000); | |
}); | |
uloop.run(); |
This file contains 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
$ sudo ./ubus.uc | |
[ "<ubus.connection 0x55f34c2b28e0>", "function publish(...) { [native code] }" ] | |
Subscription status change! Have subscribers? yes | |
Got data callback: [ 2, { "foo": true } ] | |
Got status callback: [ 1, 3 ] | |
Got complete callback: [ 0, 0 ] | |
Got data callback: [ 2, { "foo": true } ] | |
Got status callback: [ 1, 3 ] | |
Got complete callback: [ 0, 0 ] | |
Got data callback: [ 2, { "foo": true } ] | |
Got status callback: [ 1, 3 ] | |
Got complete callback: [ 0, 0 ] | |
Got data callback: [ 2, { "foo": true } ] | |
Got status callback: [ 1, 3 ] | |
Got complete callback: [ 0, 0 ] | |
Got data callback: [ 2, { "foo": true } ] | |
Got status callback: [ 1, 3 ] | |
Got complete callback: [ 0, 0 ] | |
^C |
This file contains 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
#!ucode -RS | |
let ubus = require("ubus"); | |
let uloop = require("uloop"); | |
let conn = ubus.connect(); | |
print([ conn, conn.subscriber ], "\n"); | |
uloop.init(); | |
let sub = conn.subscriber( | |
function(notify) { | |
printf("Got notify: %s\n", [ notify.info, notify.data ]); | |
notify.reply({ foo: true }, 3); | |
}, | |
function(id) { | |
printf("Object %x gone away\n", id); | |
} | |
); | |
if (!sub.subscribe("ucode")) | |
warn("Subscribe error: " + ubus.error()); | |
uloop.run(); |
This file contains 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
$ sudo ./sub.uc | |
[ "<ubus.connection 0x55b0a137c750>", "function subscriber(...) { [native code] }" ] | |
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ] | |
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ] | |
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ] | |
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ] | |
Got notify: [ { "acl": { "user": "root", "group": "root" }, "object": { "id": 2199602527 } }, { "blurb": true } ] | |
Object d949cdc7 gone away | |
^C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment