Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function open()
- local bOpen, sFreeSide = false, nil
- for n,sSide in pairs(rs.getSides()) do
- if peripheral.getType( sSide ) == "modem" then
- sFreeSide = sSide
- if rednet.isOpen( sSide ) then
- bOpen = true
- break
- end
- end
- end
- if not bOpen then
- if sFreeSide then
- print( "No modem active. Opening "..sFreeSide.." modem" )
- rednet.open( sFreeSide )
- return true
- else
- print( "No modem attached" )
- return false
- end
- end
- return true
- end
- local localIP
- local MainGate
- function getIP(route)
- MainGate=nil
- localIP=nil
- route=route or '*'
- rednet.broadcast('T=1|D='..route..'|S=*|getip')
- local mess,sIP,dIP,gate
- repeat
- mess,sIP,dIP,gate=receive(2)
- -- print(mess,' ',sIP)
- if mess=='setip' then
- MainGate=gate
- localIP=dIP
- break
- end
- until not mess
- if localIP then print("IP is ",localIP)
- else print("Can't receive IP.") end
- return localIP
- end
- function send(dIP,mess,T)
- if MainGate then
- rednet.send(MainGate,'T='..(T or 99)..'|D='..dIP..'|S='..localIP..'|'..mess)
- end
- end
- function receive(timeout)
- local gate,mess
- local sIP,dIP
- while true do
- gate,mess=rednet.receive(timeout)
- -- print(gate,' ',mess)
- if not gate then return end --timeout
- if not MainGate or MainGate==gate then
- dIP,sIP,mess=mess:match('T=.-|D=(.-)|S=(.-)|(.*)')
- if mess=='ping' then
- send(sIP,'pong')
- else
- return mess,sIP,dIP,gate
- end
- end
- end
- end
- function sendrec(dIP,mess,timeout)
- send(dIP,mess)
- return receive(timeout or 3)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement