fix: implement net, ping and cube

This commit is contained in:
Guillaume ARM 2022-07-17 23:30:12 +02:00
parent 653a22ce4d
commit 93771b8d8a
8 changed files with 73 additions and 96 deletions

View File

@ -1,4 +1,4 @@
-- eventloop 2.0.0 local _VERSION = '2.0.0'
-- Basic event loop library for computer craft -- Basic event loop library for computer craft
-- --

View File

@ -1,4 +1,4 @@
-- Network API v2.0.0 local _VERSION = '2.1.1';
local createEventLoop = require('/apis/eventloop'); local createEventLoop = require('/apis/eventloop');
@ -21,9 +21,9 @@ local function isPacketOk(packet)
return false; return false;
end end
if packet.sourceId == os.getComputerID() then -- if packet.sourceId == os.getComputerID() then
return false; -- return false;
end -- end
if packet.destId == nil then if packet.destId == nil then
return true; return true;
@ -40,32 +40,6 @@ local function isPacketOk(packet)
return false; return false;
end end
-- Une simple fonction pour chercher une valeur dans une table
local function find(predicate, values)
for k, v in ipairs(values) do
if predicate(v, k) then
return v;
end
end
return nil;
end
-- Fonction utilitaire pour pouvoir pull plusieurs events (modem_message et timer par exemple)
local function pullMultipleEvents(...)
local eventNames = table.pack(...);
while true do
local payload = table.pack(os.pullEvent());
local eventName = payload[1]
-- TODO index events
if find(function(e) return e == eventName end, eventNames) then
return table.unpack(payload);
end
end
end
-- -- Example: implementation simple de ping -- -- Example: implementation simple de ping
-- --
-- --
@ -91,14 +65,36 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
-- net.send function -- net.send function
local function sendRaw(channel, message, destId) local function sendRaw(channel, message, destId)
local sourceId = os.getComputerID()
local sourceLabel = os.getComputerLabel();
local routerId = nil;
if _G.isRouterEnabled == true then
routerId = sourceId
end
local packet = { local packet = {
sourceId = os.getComputerID(), sourceId = sourceId,
sourceLabel = os.getComputerLabel(), sourceLabel = sourceLabel,
routerId = nil, routerId = routerId,
destId = destId, destId = tonumber(destId) or destId,
message = message message = message
} }
if packet.destId ~= nil and packet.destId == sourceId then
packet.routerId = packet.sourceId;
os.queueEvent('modem_message', peripheral.getName(modem), channel, channel, packet, 0);
return nil;
end
if packet.destId == sourceLabel then
os.queueEvent('modem_message', peripheral.getName(modem), channel, channel, packet, 0);
end
if packet.routerId then
return modem.transmit(channel, channel, packet);
end
return modem.transmit(routingChannel, channel, packet); return modem.transmit(routingChannel, channel, packet);
end end
@ -155,8 +151,11 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
privateNet.stop(); privateNet.stop();
end, timeoutInSec); end, timeoutInSec);
privateNet.onStart(function()
privateNet.send(channel, eventType, payload, destId); privateNet.send(channel, eventType, payload, destId);
privateNet.start(); end)
privateNet.startLoop();
return ok, result, packetResult; return ok, result, packetResult;
end end
@ -182,8 +181,11 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
privateNet.stop(); privateNet.stop();
end, timeoutInSec); end, timeoutInSec);
privateNet.onStart(function()
privateNet.send(channel, eventType, payload, destId); privateNet.send(channel, eventType, payload, destId);
privateNet.start(); end)
privateNet.startLoop();
return ok, results, packetResults; return ok, results, packetResults;
end end
@ -248,6 +250,8 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
startLoop = start, startLoop = start,
stop = stop, stop = stop,
stopLoop = stop, stopLoop = stop,
onStart = el.onStart,
onStop = el.onStop,
} }
end end

View File

@ -1,4 +1,7 @@
local _VERSION = '1.0.0'; local _VERSION = '1.0.0';
local CUBE_CHANNEL = 64;
local net = require('/apis/net')();
local cubeCommand, firstArg, secondArg = ...; local cubeCommand, firstArg, secondArg = ...;
@ -144,7 +147,17 @@ local COMMANDS = {
return; return;
end end
print('TODO: reboot machine \'' .. tostring(machineId) .. '\'.'); local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'reboot', true, machineId);
if not ok then
error(results);
end
for k in ipairs(results) do
local packet = packets[k];
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'.');
end
end, end,
deploy = function() deploy = function()
if not isConfigFileExists() then if not isConfigFileExists() then

View File

@ -1,3 +1,5 @@
local _VERSION = '1.0.0'
local LIST_FILES = { local LIST_FILES = {
'startup/servers.lua', 'startup/servers.lua',
'servers/ping-server.lua', 'servers/ping-server.lua',

View File

@ -1,4 +1,4 @@
local _VERSION = '2.0.1'; local _VERSION = '2.0.2';
local firstArg = ...; local firstArg = ...;
if firstArg == '-version' or firstArg == '--version' then if firstArg == '-version' or firstArg == '--version' then
@ -17,15 +17,6 @@ local targetComputerId = tonumber(args[1]) or args[1];
local sourceId = os.getComputerID() local sourceId = os.getComputerID()
local sourceLabel = os.getComputerLabel(); local sourceLabel = os.getComputerLabel();
-- gérer les pongs locales
if targetComputerId == nil or targetComputerId == sourceId or targetComputerId == sourceLabel then
print("=> local pong from " .. tostring(sourceId)
.. (sourceLabel and " (label=" .. tostring(sourceLabel) .. ")" or ""));
end
if targetComputerId == sourceId then
return;
end
-- envoyer un message sur le canal 9 à la machine cible -- envoyer un message sur le canal 9 à la machine cible

View File

@ -22,66 +22,30 @@ print('started router on port ' .. tostring(ROUTER_CHANNEL) .. '...')
local routerId = os.getComputerID(); local routerId = os.getComputerID();
local function isPingForServer(payload) _G.isRouterEnabled = true;
if not payload.message then
return false;
end
if payload.message.type ~= 'ping' then
return false;
end
if payload.destId == routerId or payload.destId == os.getComputerLabel() then
return true;
end
if payload.destId == nil then
return true;
end
return false;
end
while true do while true do
local channel, replyChannel, payload, pingForServer; local channel, replyChannel, payload;
repeat repeat
_, _, channel, replyChannel, payload = os.pullEvent("modem_message"); _, _, channel, replyChannel, payload = os.pullEvent("modem_message");
pingForServer = isPingForServer(payload)
local channelOk = channel == ROUTER_CHANNEL; local channelOk = channel == ROUTER_CHANNEL;
local payloadOk = type(payload) == 'table' and not payload.routerId or pingForServer; local payloadOk = type(payload) == 'table' and not payload.routerId;
local loopFinished = channelOk and payloadOk; local loopFinished = channelOk and payloadOk;
until loopFinished until loopFinished
if payload and not payload.routerId then if payload and not payload.routerId then
if pingForServer then
local responseRouterPayload = {
sourceId = os.getComputerID(),
sourceLabel = os.getComputerLabel(),
routerId = routerId,
destId = payload.sourceId,
message = { type = "ping_response", payload = "pong" }
}
modem.transmit(replyChannel, replyChannel, responseRouterPayload)
end
if not pingForServer or payload.destId == nil then
payload.routerId = routerId; payload.routerId = routerId;
modem.transmit(replyChannel, replyChannel, payload); modem.transmit(replyChannel, replyChannel, payload);
end end
end
if VERBOSE then if VERBOSE then
if payload.destId then if payload.destId then
if pingForServer then
print('ping for server received!');
else
print("Routed message from " .. tostring(payload.sourceId) print("Routed message from " .. tostring(payload.sourceId)
.. " to " .. tostring(payload.destId) .. " to " .. tostring(payload.destId)
.. " using channel " .. tostring(replyChannel)); .. " using channel " .. tostring(replyChannel));
end
else else
print("Broadcasted message from " .. tostring(payload.sourceId) print("Broadcasted message from " .. tostring(payload.sourceId)
.. " using channel " .. tostring(replyChannel)); .. " using channel " .. tostring(replyChannel));

View File

@ -48,7 +48,10 @@ end)
-- reboot event -- reboot event
net.listenRequest(CUBE_CHANNEL, "reboot", function(_, reply) net.listenRequest(CUBE_CHANNEL, "reboot", function(_, reply)
reply(true); reply(true);
net.events.setTimeout(function()
os.reboot(); os.reboot();
end, 0.1);
end) end)
-- set-startup event -- set-startup event

View File

@ -1,4 +1,4 @@
-- Server Launcher v1.0.0 local _VERSION = '1.1.0'
local SERVERS = { local SERVERS = {
"servers/ping-server", "servers/ping-server",