Sources/libubox/examples/uloop 您所在的位置:网站首页 uloop_init Sources/libubox/examples/uloop

Sources/libubox/examples/uloop

2024-01-19 16:36| 来源: 网络整理| 查看: 265

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt

Git Wiki


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有