Last active
December 21, 2015 11:18
-
-
Save Tyderion/6297743 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modemside = "right" | |
master = 0 | |
function checkModem() | |
if (not rednet.isOpen(modemside)) then rednet.open(modemside) end | |
end | |
function detectMaster() | |
checkModem() | |
print("looking for master") | |
if (master > 0 ) then return end | |
rednet.broadcast("master") | |
while master == 0 do | |
print("Trying to reach master") | |
rednet.broadcast("master") | |
id, msg = rednet.receive(10) | |
if msg == "yes" then | |
master = id | |
sleep(1) | |
end | |
end | |
end | |
local lastID = 0 | |
detectMaster() | |
print("master is: " .. master) | |
clients = { | |
pending = {} | |
} | |
while true do | |
event, id, text = os.pullEvent() | |
if event == "rednet_message" then | |
if id == os.getComputerID() or string.match(text, "^master!$") or string.match(text, "^yes$") then | |
-- ignoreing | |
else | |
print(text) | |
if (string.match(text, "^master$")) then | |
print("got a client as relay") | |
rednet.send(id, "yes") | |
elseif (string.match(text, "^getRess:%a+")) then | |
print("sending " .. text .. " to master") | |
rednet.send(master, text) | |
elseif (string.match(text, "^getResourceList$")) then | |
print("sending " .. text .. " to master") | |
rednet.send(master, text) | |
elseif (string.match(text, "^update$")) then | |
shell.run("update") | |
else | |
print("sending " .. text .. " back to client:" .. lastID) | |
rednet.send(tonumber(lastID), text) | |
end | |
if not (id == master) then | |
lastID = id | |
print("lastID:" .. lastID) | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment