Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local network = {}
- local modem = require("component").modem
- local event = require("event")
- ip = nil -- Here he has his own IP
- function network.send(dip, port, message)
- modem.open(port)
- return modem.send(network.lookup(dip),port,ip,message)
- end
- function network.receive(port, timeout)
- modem.open(port)
- while (true) do
- local _,_,from,fport,_,fip,data = event.pull(timeout, "modem_message")
- if(fport==port) then
- local rip = network.lookup(fip)
- if (rip==from) then
- return rip,data
- end
- end
- end
- end
- function network.lookup(lip)
- -- print("lip: "..lip)
- modem.open(1)
- modem.broadcast(1, tostring("WHO IS " .. lip))
- while true do
- local _,_,from,port,_,data = event.pull("modem_message")
- --print("type: " ..type(data))
- if type(data)=="string" then
- --print(data)
- local x = string.find(data," ")
- local str1,str2 = string.sub(data,1,x-1),string.sub(data,x+1,#data)
- if string.sub(str2,1,2) == "IS" then
- local x = string.find(str2, " ")
- local str3,str4 = string.sub(str2,1,x-1),string.sub(str2,x+1,#str2)
- modem.close(1)
- return(str4)
- else
- print("Corrupted data")
- end
- end
- end
- modem.close(1)
- end
- return network
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement