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;
end
if not packet.routerId or not packet.sourceId then
return false;
end
@ -69,7 +70,7 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
local sourceLabel = os.getComputerLabel();
local routerId = nil;
if _G.isRouterEnabled == true then
if _G.isRouterEnabled then
routerId = sourceId
end
@ -87,7 +88,7 @@ local function createNetwork(el, modem, routingChannel, timeoutInSec)
return nil;
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);
end

View File

@ -147,6 +147,17 @@ local COMMANDS = {
return;
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);
if not ok then

View File

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

View File

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

View File

@ -1,7 +1,7 @@
local _VERSION = '1.0.0';
local function trim(str)
return str:gsub("%s+", "");
local function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
local function readFile(path)
@ -21,7 +21,7 @@ local startupCommand = trim(readFile('.cubestartup') or "");
if startupCommand ~= "" then
print('cube-startup v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...')
print('cube-startup v' .. _VERSION .. ': execute \'' .. startupCommand .. '\'...');
shell.run(startupCommand);
else
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)
if message == 'ping' then
-- print('=======> ping received !');
reply('pong');
end
end)

View File

@ -1,4 +1,4 @@
local _VERSION = '1.1.0'
local _VERSION = '1.1.1'
local SERVERS = {
"servers/ping-server",
@ -6,6 +6,25 @@ local SERVERS = {
"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()
os.sleep(0.1);
shell.run("shell");