Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GLOBALS
- parent = -1
- myID = os.getComputerID()
- -- Functions & Stuff
- function processRequest(request, senderId)
- -- Standard responses for identifying and adding to networks.
- if request == "$HELLO" then
- print("Responding to network discovery.")
- rednet.send(senderId, "$HELLO")
- return
- elseif request == "$ADDME" then
- response = "$MASTER$ADDROUTER-"..senderId.."-"..myID
- print("Sending request to master, awaiting response.")
- rednet.send(parent, response)
- return
- end
- -- If we find either a server or master request, we'll just immediatly send it to the next router in the chain.
- if string.find(request, "$SERVER") or string.find(request, "$MASTER") then
- print("Forwarding message to parent.")
- rednet.send(parent, request)
- return
- end
- -- If we find that the request is targeted at an end-user, we'll find out which router to send it next to and send it.
- print("Finding - in "..request)
- local s, e, cap = request:find("(.-)".."-", 1)
- print("Found - at index "..s)
- if not cap or not tonumber(cap) then
- print("Invalid message.")
- return
- end
- print("Sending message to next router.")
- request = string.sub(request, e + 1)
- rednet.send(tonumber(cap), request)
- end
- -- Main Logic
- rednet.open("top")
- local id, message
- while true do
- print("Trying to connect...")
- rednet.broadcast("$HELLO")
- id, message = rednet.receive(5)
- if message then break end
- end
- print("Received message: "..message.." from "..id)
- rednet.send(id, "$ADDME")
- while true do
- id, message = rednet.receive(5)
- if not message then
- print("No network routers nearby, couldn't connect to network.")
- return
- end
- if message == "$ACK" then break end
- end
- print("Added to network, ID: "..myID)
- parent = id
- print("All messages are being sent to: "..parent)
- print("Check master server for confirmation.")
- while true do
- print("Listening for messages...")
- id, message = rednet.receive()
- print("Received message: "..message.." from "..id)
- processRequest(message, id)
- end
- rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement