fix: router and net

This commit is contained in:
Guillaume ARM 2022-07-18 01:52:18 +02:00
parent 93771b8d8a
commit e94616313e
9 changed files with 65 additions and 26 deletions

0
.cuberc Normal file
View File

1
.routerstartup Normal file
View File

@ -0,0 +1 @@
router --silent

View File

@ -17,6 +17,7 @@ local function isPacketOk(packet)
return false; return false;
end end
if not packet.routerId or not packet.sourceId then if not packet.routerId or not packet.sourceId then
return false; return false;
end end
@ -69,7 +70,7 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
local sourceLabel = os.getComputerLabel(); local sourceLabel = os.getComputerLabel();
local routerId = nil; local routerId = nil;
if _G.isRouterEnabled == true then if _G.isRouterEnabled then
routerId = sourceId routerId = sourceId
end end
@ -87,7 +88,7 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
return nil; return nil;
end end
if packet.destId == sourceLabel then if packet.destId == nil or packet.destId == sourceLabel then
os.queueEvent('modem_message', peripheral.getName(modem), channel, channel, packet, 0); os.queueEvent('modem_message', peripheral.getName(modem), channel, channel, packet, 0);
end end

View File

@ -147,6 +147,17 @@ local COMMANDS = {
return; return;
end end
if tonumber(machineId) then
local ok, result, packet = net.sendRequest(CUBE_CHANNEL, 'reboot', true, tonumber(machineId))
if not ok then
error(result)
end
print('reboot machine \'' .. tostring(packet.sourceId) .. '\'.');
return
end
local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'reboot', true, machineId); local ok, results, packets = net.sendMultipleRequests(CUBE_CHANNEL, 'reboot', true, machineId);
if not ok then if not ok then

View File

@ -1,4 +1,4 @@
local _VERSION = '1.2.0'; local _VERSION = '1.3.1';
local firstArg = ...; local firstArg = ...;
@ -7,28 +7,30 @@ if firstArg == '-version' or firstArg == '--version' then
return; return;
end end
if firstArg then local verbose = true
error('Invalid router argument.'); local printVerbose = print
return
if firstArg == '-silent' or firstArg == '--silent' then
verbose = false
printVerbose = function() end
end end
local ROUTER_CHANNEL = 10; local ROUTER_CHANNEL = 10;
local VERBOSE = true;
local modem = peripheral.find("modem") or error("modem not found"); local modem = peripheral.find("modem") or error("modem not found");
modem.open(ROUTER_CHANNEL); modem.open(ROUTER_CHANNEL);
print('started router on port ' .. tostring(ROUTER_CHANNEL) .. '...') printVerbose('started router on port ' .. tostring(ROUTER_CHANNEL) .. '...')
local routerId = os.getComputerID(); local routerId = os.getComputerID();
_G.isRouterEnabled = true; _G.isRouterEnabled = true;
while true do while true do
local channel, replyChannel, payload; local channel, replyChannel, payload, distance;
repeat repeat
_, _, channel, replyChannel, payload = os.pullEvent("modem_message"); _, _, channel, replyChannel, payload, distance = os.pullEvent("modem_message");
local channelOk = channel == ROUTER_CHANNEL; local channelOk = channel == ROUTER_CHANNEL;
local payloadOk = type(payload) == 'table' and not payload.routerId; local payloadOk = type(payload) == 'table' and not payload.routerId;
@ -38,17 +40,21 @@ while true do
if payload and not payload.routerId then if payload and not payload.routerId then
payload.routerId = routerId; payload.routerId = routerId;
if payload.destId == nil or payload.destId == os.getComputerID() or payload.destId == os.getComputerLabel() then
os.queueEvent('modem_message', peripheral.getName(modem), replyChannel, replyChannel, payload, distance);
end
if payload.destId ~= os.getComputerID() then
modem.transmit(replyChannel, replyChannel, payload); modem.transmit(replyChannel, replyChannel, payload);
end end
end
if VERBOSE then
if payload.destId then if payload.destId then
print("Routed message from " .. tostring(payload.sourceId) printVerbose("Routed message from " .. tostring(payload.sourceId)
.. " to " .. tostring(payload.destId) .. " to " .. tostring(payload.destId)
.. " using channel " .. tostring(replyChannel)); .. " using channel " .. tostring(replyChannel));
else else
print("Broadcasted message from " .. tostring(payload.sourceId) printVerbose("Broadcasted message from " .. tostring(payload.sourceId)
.. " using channel " .. tostring(replyChannel)); .. " using channel " .. tostring(replyChannel));
end end
end
end end

View File

@ -4,8 +4,8 @@ local net = require('/apis/net')();
local CUBE_CHANNEL = 64; local CUBE_CHANNEL = 64;
local function trim(str) local function trim(s)
return str:gsub("%s+", ""); return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end end
local function readFile(path) local function readFile(path)

View File

@ -1,7 +1,7 @@
local _VERSION = '1.0.0'; local _VERSION = '1.0.0';
local function trim(str) local function trim(s)
return str:gsub("%s+", ""); return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end end
local function readFile(path) local function readFile(path)
@ -21,7 +21,7 @@ local startupCommand = trim(readFile('.cubestartup') or "");
if startupCommand ~= "" then if startupCommand ~= "" then
print('cube-startup v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...') print('cube-startup v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...');
shell.run(startupCommand); shell.run(startupCommand);
else else
print('cube-startup v' .. _VERSION .. ' no startup command detected.') print('cube-startup v' .. _VERSION .. ' no startup command detected.')

View File

@ -22,6 +22,7 @@ local net = createNet(nil, modem);
net.listenRequest(PING_CHANNEL, 'ping', function(message, reply) net.listenRequest(PING_CHANNEL, 'ping', function(message, reply)
if message == 'ping' then if message == 'ping' then
-- print('=======> ping received !');
reply('pong'); reply('pong');
end end
end) end)

View File

@ -1,4 +1,4 @@
local _VERSION = '1.1.0' local _VERSION = '1.1.1'
local SERVERS = { local SERVERS = {
"servers/ping-server", "servers/ping-server",
@ -6,6 +6,25 @@ local SERVERS = {
"servers/cube-startup.lua", "servers/cube-startup.lua",
}; };
if periphemu then
-- attach modem
periphemu.create('top', 'modem');
if os.getComputerID() == 0 then
-- attach computers
os.sleep(0.1)
periphemu.create(1, 'computer');
os.sleep(0.1)
periphemu.create(2, 'computer');
-- attach router
os.sleep(0.1)
periphemu.create(10, 'computer');
end
end
local function shellFn() local function shellFn()
os.sleep(0.1); os.sleep(0.1);
shell.run("shell"); shell.run("shell");