fix: router and net
This commit is contained in:
parent
93771b8d8a
commit
e94616313e
1
.routerstartup
Normal file
1
.routerstartup
Normal file
@ -0,0 +1 @@
|
|||||||
|
router --silent
|
||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
11
cube.lua
11
cube.lua
@ -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
|
||||||
|
|||||||
42
router.lua
42
router.lua
@ -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;
|
||||||
modem.transmit(replyChannel, replyChannel, payload);
|
|
||||||
end
|
|
||||||
|
|
||||||
if VERBOSE then
|
if payload.destId == nil or payload.destId == os.getComputerID() or payload.destId == os.getComputerLabel() then
|
||||||
if payload.destId then
|
os.queueEvent('modem_message', peripheral.getName(modem), replyChannel, replyChannel, payload, distance);
|
||||||
print("Routed message from " .. tostring(payload.sourceId)
|
end
|
||||||
.. " to " .. tostring(payload.destId)
|
if payload.destId ~= os.getComputerID() then
|
||||||
.. " using channel " .. tostring(replyChannel));
|
modem.transmit(replyChannel, replyChannel, payload);
|
||||||
else
|
|
||||||
print("Broadcasted message from " .. tostring(payload.sourceId)
|
|
||||||
.. " using channel " .. tostring(replyChannel));
|
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -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.')
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -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");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user