summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-11-18 00:48:13 +0800
committerruki <[email protected]>2022-11-18 00:48:13 +0800
commit6136908a543470d0f87d49de1ec50a4245188f6b (patch)
tree0c6f1ce129d0f46edd7197a81afab05180036cd8
parentea21365296e8b6724dea12bc6416e432bf6e4aa0 (diff)
improve to ping host
-rw-r--r--xmake/modules/net/ping.lua55
1 files changed, 23 insertions, 32 deletions
diff --git a/xmake/modules/net/ping.lua b/xmake/modules/net/ping.lua
index 93e9b4bec..98766cece 100644
--- a/xmake/modules/net/ping.lua
+++ b/xmake/modules/net/ping.lua
@@ -20,9 +20,29 @@
-- imports
import("core.cache.detectcache")
-import("detect.tools.find_ping")
+import("lib.detect.find_tool")
import("private.async.runjobs")
+-- ping host
+function _ping(ping, host)
+ local data = nil
+ if is_host("windows") then
+ data = try { function () return os.iorun("%s -n 1 -w 1000 %s", ping.program, host) end }
+ elseif is_host("macosx") then
+ data = try { function () return os.iorun("%s -c 1 -t 1 %s", ping.program, host) end }
+ else
+ data = try { function () return os.iorun("%s -c 1 -W 1 %s", ping.program, host) end }
+ end
+ local timeval = "65535"
+ if data then
+ timeval = data:match("time=([%d%s%.]-)ms", 1, true) or data:match("=([%d%s%.]-)ms TTL", 1, true) or "65535"
+ end
+ if timeval then
+ timeval = tonumber(timeval:trim())
+ end
+ return timeval
+end
+
-- send ping to hosts
--
-- @param hosts the hosts
@@ -32,29 +52,22 @@ import("private.async.runjobs")
--
function main(hosts, opt)
- -- init options
opt = opt or {}
-
- -- find ping
- local ping = find_ping(opt)
+ local ping = find_tool("ping", opt)
if not ping then
return {}
end
- -- do not force ping? enable cache
local cacheinfo = nil
if not opt.force then
cacheinfo = detectcache:get("net.ping")
end
- -- run tasks
local results = {}
hosts = table.wrap(hosts)
runjobs("ping", function (index)
local host = hosts[index]
if host then
-
- -- get time value from cache first
local timeval = nil
if cacheinfo then
timeval = cacheinfo[host]
@@ -62,38 +75,16 @@ function main(hosts, opt)
if timeval then
results[host] = timeval
else
- -- ping it, timeout: 1s
- local data = nil
- if is_host("windows") then
- data = try { function () return os.iorun("%s -n 1 -w 1000 %s", ping, host) end }
- elseif is_host("macosx") then
- data = try { function () return os.iorun("%s -c 1 -t 1 %s", ping, host) end }
- else
- data = try { function () return os.iorun("%s -c 1 -W 1 %s", ping, host) end }
- end
-
- -- find time
- local timeval = "65535"
- if data then
- timeval = data:match("time=([%d%s%.]-)ms", 1, true) or data:match("=([%d%s%.]-)ms TTL", 1, true) or "65535"
- end
-
- -- save results
- if timeval then
- timeval = tonumber(timeval:trim())
- end
+ timeval = _ping(ping, host)
results[host] = timeval
if cacheinfo then
cacheinfo[host] = timeval
end
-
- -- trace
vprint("pinging for the host(%s) ... %d ms", host, math.floor(timeval))
end
end
end, {total = #hosts})
- -- save cache
if cacheinfo then
detectcache:set("net.ping", cacheinfo)
detectcache:save()