Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local allocchannel = 31255
- local modem = peripheral.find("modem")
- local lastip = {["0"]=100,["1"]=100,["2"]=100,["3"]=100 }
- local lastinc = 0
- function deserialize(message)
- return textutils.unserialize(message)
- end
- function splitip(ip)
- if ip:match("%d+%.%d+%.%d+%.%d+") == nil then
- return nil
- end
- local t={} ; i=0
- for str in string.gmatch(ip, "(%d+)") do
- t[i] = tonumber(str)
- i = i + 1
- end
- return t
- end
- local h = fs.open("lastip","r");
- if h ~= nil then
- local str = h.readAll();
- h.close();
- if str ~= nil then
- local t = deserialize(str);
- lastip = (t["ip"] or lastip)
- end
- end
- modem.open(allocchannel)
- function send(channel, table)
- modem.transmit(channel,0,textutils.serialize(table))
- end
- while true do
- local _, _, _, replyChannel, message, _ = os.pullEvent("modem_message")
- term.clear()
- term.setCursorPos(1,1)
- print("Received event")
- local messageTable
- if pcall(function() messageTable = deserialize(message) end) then
- print("Message: "..(message or "nil"))
- else
- print("Message: nil")
- end
- print("SenderChannel: "..(allocchannel or "nil"))
- print("Replychannel: "..(replyChannel or "nil"))
- if messageTable ~= nil then
- if messageTable["id"] ~= null then
- if messageTable["status"] == nil or messageTable["status"] ~= "reply" then
- lastinc = (lastinc+1)%4
- print(lastip)
- for k,v in pairs(lastip) do
- print(k)
- print(v)
- end
- print(textutils.serialize(lastip))
- lastip[""..lastinc] = lastip[""..lastinc] + 1;
- local h = fs.open("lastip","w")
- h.write(textutils.serialize({["ip"]=lastip}))
- h.close()
- local table = {["status"]="reply",["id"]=messageTable["id"],["ip"]=lastip["0"].."."..lastip["1"].."."..lastip["2"].."."..lastip["3"]}
- send(replyChannel, table)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement