Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local expect = dofile("rom/modules/main/cc/expect.lua")
- local TP = {}
- function TP:init(sProtocol, bEndpoint)
- expect(1, sProtocol, "string")
- expect(2, bEndpoint, "boolean", "nil")
- self.destinations = {}
- self.forced = {}
- self.protocol = sProtocol
- self.endpoint = bEndpoint or false
- end
- function TP:register(sName)
- expect(1, sName, "string")
- self.name = sName
- rednet.host(self.protocol, self.name)
- rednet.broadcast({
- sType = "lookup response",
- sProtocol = self.protocol,
- sHostname = self.name,
- }, "dns")
- end
- function TP:unregister()
- rednet.unhost(self.protocol)
- end
- function TP:force(nId, sName)
- expect(1, nId, "number")
- expect(2, sName, "string")
- self.forced[nId] = sName
- end
- function TP:unforce(nId)
- expect(1, nId, "number")
- self.forced[nId] = nil
- end
- function TP:teleport(nId)
- expect(1, nId, "number")
- if self.forced[nId] or self.destinations[nId] and rednet.lookup(self.protocol, self.destinations[nId]) then
- os.queueEvent("tp_trigger")
- rednet.send(nId, nil, self.protocol)
- end
- end
- function TP:refreshList()
- self.destinations = {}
- for k, v in pairs(self.forced) do
- self.destinations[k] = v
- end
- rednet.broadcast({
- sType = "lookup",
- sProtocol = self.protocol,
- }, "dns")
- end
- function TP:getList()
- return self.destinations
- end
- function TP.daemon()
- while true do
- local event, id, msg, prot = os.pullEventRaw("rednet_message")
- if prot == TP.protocol and TP.endpoint then
- sleep(2)
- os.queueEvent("tp_trigger")
- elseif prot == "dns" and type(msg) == "table" and msg.sType == "lookup response" and msg.sProtocol == TP.protocol and type(msg.sHostname) == "string" then
- TP.destinations[id] = msg.sHostname
- os.queueEvent("tp_dns")
- end
- end
- end
- return TP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement