2012-09-27 13:47:46 +02:00
|
|
|
#!/usr/bin/env lua
|
|
|
|
|
2014-06-20 12:31:18 +02:00
|
|
|
local socket = require "socket"
|
|
|
|
|
2012-09-27 13:47:46 +02:00
|
|
|
local uloop = require("uloop")
|
|
|
|
uloop.init()
|
|
|
|
|
2014-06-20 12:31:18 +02:00
|
|
|
local udp = socket.udp()
|
|
|
|
udp:settimeout(0)
|
|
|
|
udp:setsockname('*', 8080)
|
|
|
|
|
2020-06-30 12:38:44 +02:00
|
|
|
-- timer example 1 (will run repeatedly)
|
2012-09-27 13:47:46 +02:00
|
|
|
local timer
|
|
|
|
function t()
|
|
|
|
print("1000 ms timer run");
|
|
|
|
timer:set(1000)
|
|
|
|
end
|
|
|
|
timer = uloop.timer(t)
|
|
|
|
timer:set(1000)
|
|
|
|
|
2020-06-30 12:38:44 +02:00
|
|
|
-- timer example 2 (will run once)
|
2012-09-27 13:47:46 +02:00
|
|
|
uloop.timer(function() print("2000 ms timer run"); end, 2000)
|
|
|
|
|
2020-06-30 12:38:44 +02:00
|
|
|
-- timer example 3 (will never run)
|
2012-09-27 13:47:46 +02:00
|
|
|
uloop.timer(function() print("3000 ms timer run"); end, 3000):cancel()
|
|
|
|
|
2023-10-15 00:17:36 +02:00
|
|
|
-- periodic interval timer
|
|
|
|
local intv
|
|
|
|
intv = uloop.interval(function()
|
|
|
|
print(string.format("Interval expiration #%d - %dms until next expiration",
|
|
|
|
intv:expirations(), intv:remaining()))
|
|
|
|
|
|
|
|
-- after 5 expirations, lower interval to 500ms
|
|
|
|
if intv:expirations() >= 5 then
|
|
|
|
intv:set(500)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- cancel after 10 expirations
|
|
|
|
if intv:expirations() >= 10 then
|
|
|
|
intv:cancel()
|
|
|
|
end
|
|
|
|
end, 1000)
|
|
|
|
|
2012-09-27 13:47:46 +02:00
|
|
|
-- process
|
|
|
|
function p1(r)
|
|
|
|
print("Process 1 completed")
|
|
|
|
print(r)
|
|
|
|
end
|
|
|
|
|
|
|
|
function p2(r)
|
|
|
|
print("Process 2 completed")
|
|
|
|
print(r)
|
|
|
|
end
|
|
|
|
|
|
|
|
uloop.timer(
|
|
|
|
function()
|
|
|
|
uloop.process("uloop_pid_test.sh", {"foo", "bar"}, {"PROCESS=1"}, p1)
|
|
|
|
end, 1000
|
|
|
|
)
|
|
|
|
uloop.timer(
|
|
|
|
function()
|
|
|
|
uloop.process("uloop_pid_test.sh", {"foo", "bar"}, {"PROCESS=2"}, p2)
|
|
|
|
end, 2000
|
|
|
|
)
|
|
|
|
|
2020-06-30 12:38:44 +02:00
|
|
|
-- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced
|
|
|
|
-- .delete will manually untrack.
|
2014-06-20 12:31:19 +02:00
|
|
|
udp_ev = uloop.fd_add(udp, function(ufd, events)
|
2014-06-20 12:31:18 +02:00
|
|
|
local words, msg_or_ip, port_or_nil = ufd:receivefrom()
|
|
|
|
print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
|
2014-06-20 12:31:19 +02:00
|
|
|
if words == "Stop!" then
|
|
|
|
udp_ev:delete()
|
|
|
|
end
|
2014-06-20 12:31:18 +02:00
|
|
|
end, uloop.ULOOP_READ)
|
|
|
|
|
2014-06-20 12:31:19 +02:00
|
|
|
udp_count = 0
|
2014-06-20 12:31:18 +02:00
|
|
|
udp_send_timer = uloop.timer(
|
|
|
|
function()
|
|
|
|
local s = socket.udp()
|
2014-06-20 12:31:19 +02:00
|
|
|
local words
|
|
|
|
if udp_count > 3 then
|
|
|
|
words = "Stop!"
|
|
|
|
udp_send_timer:cancel()
|
|
|
|
else
|
|
|
|
words = 'Hello!'
|
|
|
|
udp_send_timer:set(1000)
|
|
|
|
end
|
2014-06-20 12:31:18 +02:00
|
|
|
print('Send UDP packet to 127.0.0.1:8080 :'..words)
|
|
|
|
s:sendto(words, '127.0.0.1', 8080)
|
|
|
|
s:close()
|
|
|
|
|
2014-06-20 12:31:19 +02:00
|
|
|
udp_count = udp_count + 1
|
2014-06-20 12:31:18 +02:00
|
|
|
end, 3000
|
|
|
|
)
|
|
|
|
|
2012-09-27 13:47:46 +02:00
|
|
|
uloop.run()
|
|
|
|
|