Suterusu

OC - Cell Root

Sep 14th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. local computer = require("computer")
  2. local component = require("component")
  3. local sides = require("sides")
  4. local event = require("event")
  5. local serialization = require("serialization")
  6.  
  7. m = component.modem
  8. t = {}
  9. for address, componentType in component.list("tunnel") do
  10.     t[tostring(address)] = component.proxy(address)
  11. end
  12.  
  13. myAddress = nil
  14. for address, componentType in component.list("modem") do
  15.     myAddress = address
  16. end
  17. rootPath = myAddress
  18. cellEndpoints = {}
  19. cellAdjacency = {}
  20.  
  21. local function initRoot()
  22.     print("Root ID: " .. myAddress)
  23.     m.open(1)
  24.     m.open(16)
  25. end
  26.  
  27. local function messageData(msg)
  28.     if msg == "join" then
  29.         msgData = {["msgType"] = "joinReply", ["rootPath"] = myAddress}
  30.         return serialization.serialize(msgData)
  31.     -- elseif msg == "joinReply" then
  32.  
  33.     elseif msg == "data" then
  34.    
  35.     elseif msg == "keepalive" then
  36.     else
  37.         return
  38.     end
  39.  
  40. end
  41.  
  42. function getTunnelAddress(nextHop)
  43.     for hop, address in pairs(cellAdjacency) do
  44.         if nextHop == hop then
  45.             return address
  46.         end
  47.     end
  48. end
  49.  
  50. function listen(msg, localAddress, remoteAddress, port, distance, data)
  51.     print("Waiting for modem message...")
  52.     print("Got a message!")
  53.     local packet = serialization.unserialize(data)
  54.     print("DEBUG: " .. packet.msgType)
  55.     if packet.msgType == "join" then
  56.         --code to check if this node has previously been added (is alreday in the table!)
  57.         cellAdjacency[remoteAddress] = localAddress
  58.         print("Sending reply to station: " .. remoteAddress)
  59.         payload = serialization.serialize({["msgType"] = "joinReply", ["rootPath"] = getTunnelAddress(remoteAddress)})
  60.         t[getTunnelAddress(remoteAddress)].send(payload)
  61.     elseif packet.msgType == "announce" then
  62.         print("Adding cell Endpoint to list: " .. packet.data.srcAddress)
  63.         cellEndpoints[packet.data.srcAddress] = remoteAddress
  64.         payload = {["dstAddress"] = packet.data.dstAddress, ["srcAddress"] = packet.data.srcAddress}
  65.     end
  66. end
  67.  
  68.  
  69. local function startListener()
  70.     event.listen("modem_message", listen)
  71. end
  72.  
  73. event.ignore("modem_message", listen)
  74. initRoot()
  75. startListener()
  76.  
  77. local function test()
  78.     os.sleep(.01)
  79.     return
  80. end
  81.  
  82. while true do
  83.     test()
  84. end
Add Comment
Please, Sign In to add comment