Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local isMaster = true -- true, ha szerver, false, ha kliens
- local status = "offline"
- local protocol = "workshop"
- local timerID = 0
- if os.getComputerLabel() == nil then
- os.setComputerLabel("networking"..os.getComputerID())
- end
- function send(ID, msg)
- rednet.send(ID, msg, protocol)
- end
- function downloadStatus(ID)
- send(ID, "sendstatus")
- local sID, msg, dist, prot = rednet.receive(protocol)
- return msg
- end
- function getName(ID)
- send(ID, "sendname", protocol)
- local sID, msg, dist, prot = rednet.receive(protocol)
- return msg
- end
- function sendName(ID)
- send(ID, os.getComputerLabel())
- end
- function getStatus(ID)
- if ID == nil or ID == os.getComputerID() then
- return status
- end
- return downloadStatus(ID)
- end
- function setStatus(ID, stat)
- if ID == nil or ID == os.getComputerID() then
- status = stat
- end
- if isMaster then
- send(ID, stat)
- end
- end
- function sendStatus(ID)
- send(ID, status)
- end
- function turnOn(ID)
- setStatus(ID, "0")
- end
- function turnOff(ID)
- setStatus(ID, "offline")
- end
- function open(side)
- if not rednet.isOpen(side) then
- rednet.open(side)
- end
- end
- function close(side)
- if rednet.isOpen(side) then
- rednet.close(side)
- end
- end
- function getClients()
- return rednet.lookup(protocol)
- end
- function joinProtocol(prot)
- protocol = prot
- rednet.host(protocol, os.getComputerLabel())
- end
- function leaveProtocol()
- rednet.unhost(protocol, os.getComputerLabel())
- end
- function init(master, side, prot, stat)
- isMaster = master
- status = stat
- open(side)
- joinProtocol(prot)
- end
- function listen(timeout)
- if timeout ~= nil then
- timerID = os.startTimer(timeout)
- end
- local event, p2, p3, p4, p5 = os.pullEvent()
- if ((event == "timer") and (p2 == timerID)) then
- timerID = 0
- return nil
- end
- if p3 == "sendstatus" then
- sendStatus(ID)
- print("Status sent") -- debug
- end
- if p3 == "sendname" then
- sendName(ID)
- print("Name sent") -- debug
- return nil
- end
- return event, p2, p3, p4, p5
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement