Suterusu

OC - Cell Tower B

Sep 17th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.88 KB | None | 0 0
  1.  
  2. local computer = require("computer")
  3. local component = require("component")
  4. local sides = require("sides")
  5. local event = require("event")
  6. local serialization = require("serialization")
  7. local term = require("term")
  8.  
  9. local m = component.modem
  10. local t = nil
  11. if component.isAvailable("tunnel") then
  12.     t = component.tunnel
  13. end
  14.  
  15. local signalMaxStrength = 400
  16. local signalStrength
  17. local nodeStatus = "disconnect"
  18. local rootPath = nil
  19. local cellEndpoints = {}
  20. local cellTower = {}
  21. local cellAdjacency = {}
  22. local myAddress = nil
  23. local rootPathList = {}
  24. for address, componentType in component.list("modem") do
  25.     myAddress = address
  26. end
  27.  
  28. local function initWireless()
  29.     m.open(1)
  30.     m.open(16)
  31.     m.setStrength(400)
  32. end
  33.  
  34. local function messageData(msg)
  35.     if msg == "discover" then
  36.         print("Replying back to node.")
  37.         return serialization.serialize({["msgType"] = "discoveryReply", ["status"] = nodeStatus, ["nodeCount"] = 0})
  38.     -- elseif msg == "discoverReply")
  39.         -- return serialization.serialize("discoverReply")
  40.     elseif msg == "join" then
  41.         msgData = {["msgType"] = "joinReply", ["rootPath"] = (myAddress ..  "." .. rootPath)}
  42.         return serialization.serialize(msgData)
  43.     -- elseif msg == "joinReply" then
  44.  
  45.     elseif msg == "data" then
  46.    
  47.     elseif msg == "keepalive" then
  48.     else
  49.         return
  50.     end
  51.  
  52. end
  53.  
  54. function listen(param, localAddress, remoteAddress, port, distance, data)
  55.     local packet = serialization.unserialize(data)
  56.     if packet.msgType == "discover" then
  57.         print("Got msg from node: " .. remoteAddress)
  58.         print(remoteAddress .. " 1 " .. messageData("discover"))
  59.         m.send(remoteAddress, 1, messageData("discover"))
  60.     -- elseif data.msgType == "discoveryReply" then
  61.         -- table.insert(cellAdjacency, remoteAddress)
  62.     elseif packet.msgType == "join" and packet.nodeType == "endPoint" then
  63.         --table.insert(cellEndpoints, {remoteAddress)
  64.         print("Sending joinReply to: " .. remoteAddress)
  65.         m.send(remoteAddress, 1, messageData("join"))
  66.     elseif packet.msgType == "join" and packet.nodeType == "tower" then
  67.         table.insert(cellAdjacency, remoteAddress)
  68.         print("Sending joinReply to: " .. remoteAddress)
  69.         m.send(remoteAddress, 1, messageData("join"))
  70.     elseif packet.msgType == "joinReply" then
  71.         rootPath = packet.rootPath
  72.         print("My root path is: " .. rootPath)
  73.     elseif packet.msgType == "announce" then
  74.         print("Got an announcement from: " .. packet.data.srcAddress)
  75.         cellEndpoints[packet.data.srcAddress] = remoteAddress
  76.         payload = {["dstAddress"] = packet.data.dstAddress, ["srcAddress"] = packet.data.srcAddress}
  77.         if t == nil then
  78.             m.send(rootPathList[1], 16, serialization.serialize({["msgType"] = "announce", ["data"] = payload}))
  79.         else
  80.             t.send(serialization.serialize({["msgType"] = "announce", ["data"] = payload}))
  81.         end
  82.     elseif packet.msgType == "data" then
  83.         if packet.payload.dstAddress == myAddress then
  84.             print("I got a packet for me!")
  85.             return
  86.         end
  87.         for address, nextHop in pairs(cellEndpoints) do
  88.             if address == packet.payload.dstAddress then
  89.                 print("Found a route entry!")
  90.                 local payload = {["dstAddress"] = packet.payload.dstAddress, ["srcAddress"] = packet.payload.srcAddress, ["data"] = packet.payload.data}
  91.                 m.send(nextHop, 16, serialization.serialize({["msgType"] = "data", ["payload"] = payload}))
  92.                 return
  93.             end
  94.         end
  95.         print("No route entry found, using default...")
  96.         local payload = {["dstAddress"] = packet.payload.dstAddress, ["srcAddress"] = packet.payload.srcAddress, ["data"] = packet.payload.data}
  97.         if t == nil then
  98.             m.send(rootPathList[1], 16, serialization.serialize({["msgType"] = "data", ["payload"] = payload}))
  99.         else
  100.             t.send(serialization.serialize({["msgType"] = "data", ["payload"] = payload}))
  101.         end
  102.         return
  103.     else
  104.     end
  105. end
  106.  
  107. local function discoverTower()
  108.     for i = 1, signalMaxStrength, 1 do
  109.         m.setStrength(i)
  110.         print("Sending discovery at signal strength " .. i )
  111.         m.broadcast(16, (serialization.serialize({["msgType"] = "discover"})))
  112.         local msg, localAddress, remoteAddress, port, distance, data = event.pull(1, "modem_message")
  113.         if msg == nil and i == 400 then
  114.             print("No tower in range!")
  115.             return
  116.         elseif msg == nil then
  117.  
  118.         else
  119.             signalStrength = i
  120.             m.setStrength(400)
  121.             print("Found a tower: " .. remoteAddress)
  122.             print("Distance of " .. distance .. " at signal strength of " .. signalStrength)
  123.             cellTower = {address = remoteAddress, distance = distance, signalStrength = signalStrength}
  124.             break
  125.         end
  126.     end
  127. end
  128.  
  129. local function procJoinReply()
  130.     --wait for joinReply after sending join
  131.     local msg, localAddress, remoteAddress, port, distance, data = event.pull(2, "modem_message")
  132.     if data == nil then
  133.         print("Join failed!")
  134.         return
  135.     end
  136.     --get data "payload" of the packet
  137.     local data = serialization.unserialize(data)
  138.     --parse out the rootPath string to each "hop" in a table
  139.     for node in string.gmatch(data.rootPath, "[^.]+") do
  140.         table.insert(rootPathList, node)
  141.     end
  142.     --set root path
  143.     rootPath = data.rootPath
  144.     term.write("My root path is: " .. rootPath, true)
  145. end
  146.  
  147. local function joinTower()
  148.     print("Sending join request to: " .. cellTower.address)
  149.     --send join message to tower, should hear back a joinReply if they accept
  150.     m.send(cellTower.address, 16, serialization.serialize({["msgType"] = "join", ["nodeType"] = "tower"}))
  151.     --process the joinReply message (or lack of)
  152.     procJoinReply()
  153. end
  154.  
  155. local function initTower()
  156.     if t == nil then
  157.         print("We are a relay tower!")
  158.         discoverTower()
  159.         joinTower()
  160.     else
  161.         --we are registering with a root
  162.         print("We are a base station!")
  163.         print("Sending join to root.")
  164.         t.send((serialization.serialize({["msgType"] = "join"})))
  165.         procJoinReply()
  166.     end
  167. end
  168.  
  169. local function startListener()
  170.     event.listen("modem_message", listen)
  171. end
  172.  
  173. event.ignore("modem_message", listen)
  174. initWireless()
  175. initTower()
  176. --discoverTower()
  177. startListener()
  178.  
  179. local function test()
  180.     os.sleep(.01)
  181.     return true
  182. end
  183.  
  184. while true do
  185.     test()
  186. end
Add Comment
Please, Sign In to add comment