test.lua 1.3 KB

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