Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SIDES = {"left", "right", "top", "bottom", "front", "back"}
- local modem = nil
- function connect()
- if modem ~= nil and rednet.isOpen(modem) then
- return true
- end
- for _, side in ipairs(SIDES) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- if rednet.isOpen() then
- modem = side
- return true
- end
- end
- end
- error("could not establish connection")
- return false
- end
- function broadcast(msg, proto)
- connect()
- rednet.broadcast(msg, proto)
- end
- function getID(host, proto)
- connect()
- id = rednet.lookup(proto, host)
- if id == nil then
- error("no such host '" .. host .. "'")
- end
- return id
- end
- function sendToHost(msg, proto, host)
- connect()
- id = getID(host, proto)
- rednet.send(id, msg, proto)
- end
- function listen(proto, id)
- connect()
- while(true) do
- local senderID, msg = rednet.receive(proto)
- if id == nil or id == senderID then
- return senderID, msg
- end
- end
- end
Add Comment
Please, Sign In to add comment