libubus: do not register/unregister with uloop during sync requests
[project/ubus.git] / lua / test.lua
1 #!/usr/bin/env lua
2
3 require "ubus"
4 require "uloop"
5
6 uloop.init()
7
8 local conn = ubus.connect()
9 if not conn then
10         error("Failed to connect to ubus")
11 end
12
13 local my_method = {
14         broken = {
15                 hello = 1,
16                 hello1 = {
17                         function(req)
18                         end, {id = "fail" }
19                 },
20         },
21         test = {
22                 hello = {
23                         function(req, msg)
24                                 conn:reply(req, {message="foo"});
25                                 print("Call to function 'hello'")
26                                 for k, v in pairs(msg) do
27                                         print("key=" .. k .. " value=" .. tostring(v))
28                                 end
29                         end, {id = ubus.INT32, msg = ubus.STRING }
30                 },
31                 hello1 = {
32                         function(req)
33                                 conn:reply(req, {message="foo1"});
34                                 conn:reply(req, {message="foo2"});
35                                 print("Call to function 'hello1'")
36                         end, {id = ubus.INT32, msg = ubus.STRING }
37                 },
38                 deferred = {
39                         function(req)
40                                 conn:reply(req, {message="wait for it"})
41                                 local def_req = conn:defer_request(req)
42                                 uloop.timer(function()
43                                                 conn:reply(def_req, {message="done"})
44                                                 conn:complete_deferred_request(def_req, 0)
45                                                 print("Deferred request complete")
46                                         end, 2000)
47                                 print("Call to function 'deferred'")
48                         end, {}
49                 }
50         }
51 }
52
53 conn:add(my_method)
54
55 local my_event = {
56         test = function(msg)
57                 print("Call to test event")
58                 for k, v in pairs(msg) do
59                         print("key=" .. k .. " value=" .. tostring(v))
60                 end
61         end,
62 }
63
64 conn:listen(my_event)
65
66 uloop.run()