cc-libs/programs/ping.lua

36 lines
971 B
Lua

local firstArg = ...;
if firstArg == '-version' or firstArg == '--version' then
print('v' .. require('/apis/libversion')().forSelf());
return;
end
if firstArg == '-help' or firstArg == '--help' then
print('Usage: ping [<id|label>]');
return;
end
local createNet = require('/apis/net');
local net = createNet();
local args = table.pack(...);
local targetComputerId = tonumber(args[1]) or args[1];
local sourceId = os.getComputerID();
local sourceLabel = os.getComputerLabel();
local ok, results, packets = net.callMultiple('ping', 'ping', { destId = targetComputerId });
if not ok and targetComputerId ~= sourceId and targetComputerId ~= sourceLabel then
error(results);
end
if not ok then return end
for k, message in ipairs(results) do
if message == 'pong' then
local packet = packets[k];
print("=> pong from " .. tostring(packet.sourceId)
.. (packet.sourceLabel and " (label=" .. tostring(packet.sourceLabel) .. ")" or ""));
end
end