If you *need* to see the code, it's here. It is pretty big though, just FYI.
Spoiler
--[[Variables]]--
version = "v0.1"
subRouters = {}
subComputers = {}
master = "nil";
connected = false;
headerOnline = "Router "..version.." | This router is connected to #";
headerOffline = "Router "..version.." | This router is currently LAN only.";
header = headerOffline;
--[[Functions]]--
function isTable(var)
if var[1] ~= nil then
return true;
else
return false;
end
end
function tryToConnect()
if connected then
message = "ping";
else
message = "CONNENT";
end
timeoutID = os.startTimer(.5);
sendMessage(channels.connectionChannel, channels.connectionChannel, nil, message)
local event, p1, p2, p3, p4, p5 = os.pullEvent();
if event == "modem_message" then
modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5;
if senderChannel == channels.connectionChannel then
success, sender, to, messageString = deobf(message);
if success then
if connected then
if messageString == "pong" then
return true, sender;
end
else
if messageString == "ACK" then
sendMessage(channels.connectionChannel, channels.connectionChannel, nil, "RCONNECTED "..computerID);
return true, sender;
end
end
end
end
elseif event == "timer" and p1 == timeoutID then
return false, "nil";
end
end
function searchForComputer(computerID)
for i = 1, #subComputers do
if subComputers[i] == computerID then
return 1
end
end
for i = 1, #subRouters do
sendMessage(channels.computerSearchChannel, channels.computerSearchChannel, nil, computerID);
end
end
function drawScreen()
term.setCursorPos(1,1);
term.clearLine();
write(header);
for i = 2, h do
term.setCursorPos(1,i);
if i == 2 or i == h then
for j = 1, w do
if j == 1 or j == w then
write("+");
else
write("-");
end
end
else
write("|");
term.setCursorPos(w,i);
write("|");
end
end
term.setCursorPos(2,3);
end
function sendMessage(channel, replyChannel, to, message)
message = {computerID, to, message};
modem.transmit(channel, replyChannel, textutils.serialize(message));
end
function deobf(message)
term.scroll(1);
term.setCursorPos(2,h-1);
term.clearLine()
sucess, desearMessage = pcall(textutils.unserialize, message);
if not pcall(isTable, desearMessage) then
write("The message isn't a table");
drawScreen()
return false;
else
drawScreen()
return true, desearMessage[1], desearMessage[2], desearMessage[3];
end
end
function log(message)
term.scroll(1);
term.setCursorPos(2,h-1);
term.clearLine()
write("Forwarded message from computer #"..desearMessage[1].." to #"..master);
drawScreen()
end
--[[Main Code]]--
connected, master = tryToConnect();
while true do
if connected then
header = headerOnline..master;
else
header = headerOffline;
end
timerID = os.startTimer(5);
drawScreen();
local event, p1, p2, p3, p4, p5 = os.pullEvent();
if event == "modem_message" then
modemSide, senderChannel, replyChannel, message, senderDistance = p1, p2, p3, p4, p5;
success, sender, to, messageString = deobf(message);
if success then
if senderChannel == channels.connectionChannel then
request = messageString;
if request == "CONNENT" then
sendMessage(channels.connectionChannel, channels.connectionChannel, sender, "ACK");
elseif string.sub(request, 1, 10) == "CCONNECTID" and to == computerID then
table.insert(subComputers, #subComputers + 1, tonum(string.sub(request, 12)));
elseif string.sub(request, 1, 10) == "RCONNECTID" and to == computerID then
table.insert(subRouters, #subRouters + 1, tonum(string.sub(request, 12)));
elseif request == "ping" then
sendMessage(channels.connectionChannel, channels.connectionChannel, sender, "pong");
end
else
connected, master = tryToConnect();
if senderChannel == channels.computerSearchChannel then
computerToFind = messageString;
searchForComputer(computerToFind);
else
--Search for the computer ID under this router, if not found, forward the message to the next router up.
log(message);
end
end
end
elseif event == "timer" and p1 == timerID then
print("TimerID = "..timerID);
print("p1 = "..p1);
sleep(1);
term.clear();
connected, master = tryToConnect();
end
end