fix: implement net, ping and cube
This commit is contained in:
parent
653a22ce4d
commit
93771b8d8a
@ -1,4 +1,4 @@
|
||||
-- eventloop 2.0.0
|
||||
local _VERSION = '2.0.0'
|
||||
|
||||
-- Basic event loop library for computer craft
|
||||
--
|
||||
|
||||
80
apis/net.lua
80
apis/net.lua
@ -1,4 +1,4 @@
|
||||
-- Network API v2.0.0
|
||||
local _VERSION = '2.1.1';
|
||||
|
||||
local createEventLoop = require('/apis/eventloop');
|
||||
|
||||
@ -21,9 +21,9 @@ local function isPacketOk(packet)
|
||||
return false;
|
||||
end
|
||||
|
||||
if packet.sourceId == os.getComputerID() then
|
||||
return false;
|
||||
end
|
||||
-- if packet.sourceId == os.getComputerID() then
|
||||
-- return false;
|
||||
-- end
|
||||
|
||||
if packet.destId == nil then
|
||||
return true;
|
||||
@ -40,32 +40,6 @@ local function isPacketOk(packet)
|
||||
return false;
|
||||
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
|
||||
--
|
||||
--
|
||||
@ -91,14 +65,36 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
|
||||
|
||||
-- net.send function
|
||||
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 = {
|
||||
sourceId = os.getComputerID(),
|
||||
sourceLabel = os.getComputerLabel(),
|
||||
routerId = nil,
|
||||
destId = destId,
|
||||
sourceId = sourceId,
|
||||
sourceLabel = sourceLabel,
|
||||
routerId = routerId,
|
||||
destId = tonumber(destId) or destId,
|
||||
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);
|
||||
end
|
||||
|
||||
@ -155,8 +151,11 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
|
||||
privateNet.stop();
|
||||
end, timeoutInSec);
|
||||
|
||||
privateNet.send(channel, eventType, payload, destId);
|
||||
privateNet.start();
|
||||
privateNet.onStart(function()
|
||||
privateNet.send(channel, eventType, payload, destId);
|
||||
end)
|
||||
|
||||
privateNet.startLoop();
|
||||
|
||||
return ok, result, packetResult;
|
||||
end
|
||||
@ -182,8 +181,11 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
|
||||
privateNet.stop();
|
||||
end, timeoutInSec);
|
||||
|
||||
privateNet.send(channel, eventType, payload, destId);
|
||||
privateNet.start();
|
||||
privateNet.onStart(function()
|
||||
privateNet.send(channel, eventType, payload, destId);
|
||||
end)
|
||||
|
||||
privateNet.startLoop();
|
||||
|
||||
return ok, results, packetResults;
|
||||
end
|
||||
@ -248,6 +250,8 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
|
||||
startLoop = start,
|
||||
stop = stop,
|
||||
stopLoop = stop,
|
||||
onStart = el.onStart,
|
||||
onStop = el.onStop,
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
15
cube.lua
15
cube.lua
@ -1,4 +1,7 @@
|
||||
local _VERSION = '1.0.0';
|
||||
local CUBE_CHANNEL = 64;
|
||||
|
||||
local net = require('/apis/net')();
|
||||
|
||||
local cubeCommand, firstArg, secondArg = ...;
|
||||
|
||||
@ -144,7 +147,17 @@ local COMMANDS = {
|
||||
return;
|
||||
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,
|
||||
deploy = function()
|
||||
if not isConfigFileExists() then
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
local _VERSION = '1.0.0'
|
||||
|
||||
local LIST_FILES = {
|
||||
'startup/servers.lua',
|
||||
'servers/ping-server.lua',
|
||||
|
||||
11
ping.lua
11
ping.lua
@ -1,4 +1,4 @@
|
||||
local _VERSION = '2.0.1';
|
||||
local _VERSION = '2.0.2';
|
||||
|
||||
local firstArg = ...;
|
||||
if firstArg == '-version' or firstArg == '--version' then
|
||||
@ -17,15 +17,6 @@ local targetComputerId = tonumber(args[1]) or args[1];
|
||||
local sourceId = os.getComputerID()
|
||||
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
|
||||
|
||||
|
||||
52
router.lua
52
router.lua
@ -22,66 +22,30 @@ print('started router on port ' .. tostring(ROUTER_CHANNEL) .. '...')
|
||||
|
||||
local routerId = os.getComputerID();
|
||||
|
||||
local function isPingForServer(payload)
|
||||
|
||||
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
|
||||
_G.isRouterEnabled = true;
|
||||
|
||||
while true do
|
||||
local channel, replyChannel, payload, pingForServer;
|
||||
local channel, replyChannel, payload;
|
||||
|
||||
repeat
|
||||
_, _, channel, replyChannel, payload = os.pullEvent("modem_message");
|
||||
pingForServer = isPingForServer(payload)
|
||||
|
||||
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;
|
||||
until loopFinished
|
||||
|
||||
|
||||
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;
|
||||
modem.transmit(replyChannel, replyChannel, payload);
|
||||
end
|
||||
payload.routerId = routerId;
|
||||
modem.transmit(replyChannel, replyChannel, payload);
|
||||
end
|
||||
|
||||
if VERBOSE then
|
||||
if payload.destId then
|
||||
if pingForServer then
|
||||
print('ping for server received!');
|
||||
else
|
||||
print("Routed message from " .. tostring(payload.sourceId)
|
||||
.. " to " .. tostring(payload.destId)
|
||||
.. " using channel " .. tostring(replyChannel));
|
||||
end
|
||||
print("Routed message from " .. tostring(payload.sourceId)
|
||||
.. " to " .. tostring(payload.destId)
|
||||
.. " using channel " .. tostring(replyChannel));
|
||||
else
|
||||
print("Broadcasted message from " .. tostring(payload.sourceId)
|
||||
.. " using channel " .. tostring(replyChannel));
|
||||
|
||||
@ -48,7 +48,10 @@ end)
|
||||
-- reboot event
|
||||
net.listenRequest(CUBE_CHANNEL, "reboot", function(_, reply)
|
||||
reply(true);
|
||||
os.reboot();
|
||||
|
||||
net.events.setTimeout(function()
|
||||
os.reboot();
|
||||
end, 0.1);
|
||||
end)
|
||||
|
||||
-- set-startup event
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
-- Server Launcher v1.0.0
|
||||
local _VERSION = '1.1.0'
|
||||
|
||||
local SERVERS = {
|
||||
"servers/ping-server",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user