summaryrefslogtreecommitdiff
path: root/xmake/modules/net/ping.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2017-09-04 16:46:30 +0800
committerruki <[email protected]>2017-09-04 16:46:30 +0800
commit4fead28a91b650fd7268e708afdf4dc97935d454 (patch)
treeacbc0c9f1e34689d8b3858f76acfd87861bf2499 /xmake/modules/net/ping.lua
parent08ec1c7dcab891641f6b2fb6a61c2c3b210f5f55 (diff)
add require to build and optmize ping
Diffstat (limited to 'xmake/modules/net/ping.lua')
-rw-r--r--xmake/modules/net/ping.lua58
1 files changed, 43 insertions, 15 deletions
diff --git a/xmake/modules/net/ping.lua b/xmake/modules/net/ping.lua
index 0026d6da5..48f97e394 100644
--- a/xmake/modules/net/ping.lua
+++ b/xmake/modules/net/ping.lua
@@ -23,24 +23,34 @@
--
-- imports
+import("lib.detect.cache")
import("detect.tools.find_ping")
-- send ping to hosts
--
--- @param ... the hosts
+-- @param hosts the hosts
+-- @param opt the options
--
--- @return the time or -1
+-- @return the time or -1
--
-function main(...)
+function main(hosts, opt)
+
+ -- init options
+ opt = opt or {}
-- find ping
- local ping = find_ping()
+ local ping = find_ping(opt)
if not ping then
return {}
end
+ -- do not force ping? enable cache
+ local cacheinfo = nil
+ if not opt.force then
+ cacheinfo = cache.load("net.ping")
+ end
+
-- run tasks
- local hosts = {...}
local results = {}
process.runjobs(function (index)
local host = hosts[index]
@@ -49,24 +59,42 @@ function main(...)
{
function ()
- -- ping it
- local data = nil
- if os.host() == "windows" then
- data = os.iorun("%s -n 1 %s", ping, host)
- else
- data = os.iorun("%s -c 1 %s", ping, host)
+ -- get time value from cache first
+ local timeval = nil
+ if cacheinfo then
+ timeval = cacheinfo[host]
end
+ if timeval then
+ results[host] = timeval
+ else
+ -- ping it
+ local data = nil
+ if os.host() == "windows" then
+ data = os.iorun("%s -n 1 %s", ping, host)
+ else
+ data = os.iorun("%s -c 1 %s", ping, host)
+ end
- -- find time
- local time = data:match("time=(.-)ms", 1, true)
- if time then
- results[host] = tonumber(time:trim())
+ -- find time
+ local time = data:match("time=(.-)ms", 1, true)
+ if time then
+ local timeval = tonumber(time:trim())
+ results[host] = timeval
+ if cacheinfo then
+ cacheinfo[host] = timeval
+ end
+ end
end
end
}
end
end, #hosts)
+ -- save cache
+ if cacheinfo then
+ cache.save("net.ping", cacheinfo)
+ end
+
-- ok?
return results
end