diff --git a/.cuberc b/.cuberc new file mode 100644 index 0000000..e69de29 diff --git a/.routerstartup b/.routerstartup new file mode 100644 index 0000000..f7fceb0 --- /dev/null +++ b/.routerstartup @@ -0,0 +1 @@ +router --silent \ No newline at end of file diff --git a/apis/net.lua b/apis/net.lua index 072bcc5..10b9985 100644 --- a/apis/net.lua +++ b/apis/net.lua @@ -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 diff --git a/cube.lua b/cube.lua index 7ed5717..1137720 100644 --- a/cube.lua +++ b/cube.lua @@ -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 diff --git a/router.lua b/router.lua index 2428136..c93b9db 100644 --- a/router.lua +++ b/router.lua @@ -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 diff --git a/servers/cube-server.lua b/servers/cube-server.lua index 196a346..90d6544 100644 --- a/servers/cube-server.lua +++ b/servers/cube-server.lua @@ -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) diff --git a/servers/cube-startup.lua b/servers/cube-startup.lua index e274ea9..bb27261 100644 --- a/servers/cube-startup.lua +++ b/servers/cube-startup.lua @@ -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.') diff --git a/servers/ping-server.lua b/servers/ping-server.lua index 3adb2bf..3cf2042 100644 --- a/servers/ping-server.lua +++ b/servers/ping-server.lua @@ -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) diff --git a/startup/servers.lua b/startup/servers.lua index 903a3ce..db9e7d9 100644 --- a/startup/servers.lua +++ b/startup/servers.lua @@ -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");