Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ldb2")
- local db = "rnc2"
- local modems = {peripheral.find("modem")}
- local modem = nil
- i = 1
- repeat
- modem = modems[i]
- if modem == nil then
- error("Wireless modem not found", 0)
- end
- i = i + 1
- until modem.isWireless()
- print("API:RNC2 - Inited modem")
- local function rmvdot(str)
- return string.gsub(str,"%.", "")
- end
- local portWasOpen = false
- function setPort(prog, _nPort)
- if portWasOpen then modem.close(_nPort) end portWasOpen = true
- ldb2.set(db .. "/" .. rmvdot(prog), "port", _nPort)
- modem.open(_nPort)
- end
- function getPort(prog)
- return ldb2.get(db .. "/" .. rmvdot(prog), "port")
- end
- function setIdenty(prog, _nIdenty)
- ldb2.set(db .. "/" .. rmvdot(prog), "identy", _nIdenty)
- end
- function getIdenty(prog)
- return ldb2.get(db .. "/" .. rmvdot(prog), "identy")
- end
- function setTimeOut(prog, _nTimeout)
- ldb2.set(db .. "/" .. rmvdot(prog), "timeout", _nTimeout)
- end
- function getTimeOut(prog)
- return ldb2.get(db .. "/" .. rmvdot(prog), "timeout")
- end
- function setTunnel(prog, _nTunnel)
- ldb2.set(db .. "/" .. rmvdot(prog), "tunnel", _nTunnel)
- end
- function getTunnel(prog)
- return ldb2.get(db .. "/" .. rmvdot(prog), "tunnel")
- end
- function resive(prog, identy, timeout)
- identy = identy or getIdenty(prog)
- timeout = timeout or getTimeOut(prog)
- timer = os.startTimer( timeout )
- while true do
- local event, side, fec, refec, message, distance = os.pullEvent()
- if event == "modem_message" then
- -- print("p:" .. port)
- -- print("i:" .. identy)
- -- print("fi:" .. refec)
- if refec == identy then
- if(message == "ping") then
- sleep(0.01)
- modem.transmit(getPort(prog), identy, "pong")
- else
- return textutils.unserialize(message)
- end
- end
- elseif event == "timer" then
- if side == timer then
- break
- end
- end
- end
- return nil
- end
- function ping(prog, identy)
- identy = identy or getIdenty(prog)
- modem.transmit(getPort(prog), identy, "ping")
- end
- function getEmptyTunnel(prog)
- tunnel = 0
- repeat
- tunnel = math.random(101,1000)
- until checkTunnel(prog, tunnel) == 0
- setTunnel(prog, tunnel)
- return tunnel
- end
- function checkTunnel(prog, identy)
- identy = identy or getIdenty(prog)
- local pingC = 0
- timer = os.startTimer(1)
- modem.transmit(getPort(prog), identy, "ping")
- while true do
- local event, side, fec, refec, message, distance = os.pullEvent()
- if event == "modem_message" then
- if refec == identy then
- if(message == "pong") then
- pingC = pingC + 1
- end
- end
- elseif event == "timer" then
- if side == timer then
- break
- end
- end
- end
- return pingC
- end
- function send(prog, message, identy)
- identy = identy or getIdenty(prog)
- modem.transmit(getPort(prog), identy, textutils.serialize(message))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement