Advertisement
EnterYourName

CCWDHCP

Jan 20th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. local allocchannel = 31255
  2. local modem = peripheral.find("modem")
  3. local lastip = {["0"]=100,["1"]=100,["2"]=100,["3"]=100 }
  4. local lastinc = 0
  5.  
  6. function deserialize(message)
  7.     return textutils.unserialize(message)
  8. end
  9.  
  10. function splitip(ip)
  11.  
  12.     if ip:match("%d+%.%d+%.%d+%.%d+") == nil then
  13.         return nil
  14.     end
  15.  
  16.     local t={} ; i=0
  17.     for str in string.gmatch(ip, "(%d+)") do
  18.         t[i] = tonumber(str)
  19.         i = i + 1
  20.     end
  21.     return t
  22.  
  23. end
  24.  
  25. local h = fs.open("lastip","r");
  26. if h ~= nil then
  27.     local str = h.readAll();
  28.     h.close();
  29.     if str ~= nil then
  30.  
  31.         local t = deserialize(str);
  32.         lastip = (t["ip"] or lastip)
  33.  
  34.     end
  35. end
  36.  
  37. modem.open(allocchannel)
  38.  
  39. function send(channel, table)
  40.     modem.transmit(channel,0,textutils.serialize(table))
  41. end
  42.  
  43. while true do
  44.  
  45.     local _, _, _, replyChannel, message, _ = os.pullEvent("modem_message")
  46.  
  47.     term.clear()
  48.     term.setCursorPos(1,1)
  49.     print("Received event")
  50.     local messageTable
  51.     if pcall(function() messageTable = deserialize(message) end) then
  52.         print("Message: "..(message or "nil"))
  53.     else
  54.         print("Message: nil")
  55.     end
  56.     print("SenderChannel: "..(allocchannel or "nil"))
  57.     print("Replychannel: "..(replyChannel or "nil"))
  58.  
  59.     if messageTable ~= nil then
  60.  
  61.         if messageTable["id"] ~= null then
  62.  
  63.             if messageTable["status"] == nil or messageTable["status"] ~= "reply" then
  64.  
  65.                 lastinc = (lastinc+1)%4
  66.                 print(lastip)
  67.                 for k,v in pairs(lastip) do
  68.                     print(k)
  69.                     print(v)
  70.                 end
  71.                 print(textutils.serialize(lastip))
  72.  
  73.                 lastip[""..lastinc] = lastip[""..lastinc] + 1;
  74.                 local h = fs.open("lastip","w")
  75.                 h.write(textutils.serialize({["ip"]=lastip}))
  76.                 h.close()
  77.  
  78.                 local table = {["status"]="reply",["id"]=messageTable["id"],["ip"]=lastip["0"].."."..lastip["1"].."."..lastip["2"].."."..lastip["3"]}
  79.                 send(replyChannel, table)
  80.  
  81.             end
  82.  
  83.         end
  84.  
  85.     end
  86.  
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement