uloop-example.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env lua
  2. local socket = require "socket"
  3. local uloop = require("uloop")
  4. uloop.init()
  5. local udp = socket.udp()
  6. udp:settimeout(0)
  7. udp:setsockname('*', 8080)
  8. -- timer example 1 (will run repeatedly)
  9. local timer
  10. function t()
  11. print("1000 ms timer run");
  12. timer:set(1000)
  13. end
  14. timer = uloop.timer(t)
  15. timer:set(1000)
  16. -- timer example 2 (will run once)
  17. uloop.timer(function() print("2000 ms timer run"); end, 2000)
  18. -- timer example 3 (will never run)
  19. uloop.timer(function() print("3000 ms timer run"); end, 3000):cancel()
  20. -- periodic interval timer
  21. local intv
  22. intv = uloop.interval(function()
  23. print(string.format("Interval expiration #%d - %dms until next expiration",
  24. intv:expirations(), intv:remaining()))
  25. -- after 5 expirations, lower interval to 500ms
  26. if intv:expirations() >= 5 then
  27. intv:set(500)
  28. end
  29. -- cancel after 10 expirations
  30. if intv:expirations() >= 10 then
  31. intv:cancel()
  32. end
  33. end, 1000)
  34. -- process
  35. function p1(r)
  36. print("Process 1 completed")
  37. print(r)
  38. end
  39. function p2(r)
  40. print("Process 2 completed")
  41. print(r)
  42. end
  43. uloop.timer(
  44. function()
  45. uloop.process("uloop_pid_test.sh", {"foo", "bar"}, {"PROCESS=1"}, p1)
  46. end, 1000
  47. )
  48. uloop.timer(
  49. function()
  50. uloop.process("uloop_pid_test.sh", {"foo", "bar"}, {"PROCESS=2"}, p2)
  51. end, 2000
  52. )
  53. -- SIGINT handler
  54. uloop.signal(function(signo)
  55. print(string.format("Terminating on SIGINT (#%d)!", signo))
  56. -- end uloop to terminate program
  57. uloop.cancel()
  58. end, uloop.SIGINT)
  59. local sig
  60. sig = uloop.signal(function(signo)
  61. print(string.format("Got SIGUSR2 (#%d)!", signo))
  62. -- remove signal handler, next SIGUSR2 will terminate program
  63. sig:delete()
  64. end, uloop.SIGUSR2)
  65. -- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced
  66. -- .delete will manually untrack.
  67. udp_ev = uloop.fd_add(udp, function(ufd, events)
  68. local words, msg_or_ip, port_or_nil = ufd:receivefrom()
  69. print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
  70. if words == "Stop!" then
  71. udp_ev:delete()
  72. end
  73. end, uloop.ULOOP_READ)
  74. udp_count = 0
  75. udp_send_timer = uloop.timer(
  76. function()
  77. local s = socket.udp()
  78. local words
  79. if udp_count > 3 then
  80. words = "Stop!"
  81. udp_send_timer:cancel()
  82. else
  83. words = 'Hello!'
  84. udp_send_timer:set(1000)
  85. end
  86. print('Send UDP packet to 127.0.0.1:8080 :'..words)
  87. s:sendto(words, '127.0.0.1', 8080)
  88. s:close()
  89. udp_count = udp_count + 1
  90. end, 3000
  91. )
  92. uloop.run()