Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computer = require("computer")
- local component = require("component")
- local sides = require("sides")
- local event = require("event")
- local serialization = require("serialization")
- local term = require("term")
- local m = component.modem
- local t = nil
- if component.isAvailable("tunnel") then
- t = component.tunnel
- end
- local signalMaxStrength = 400
- local signalStrength
- local nodeStatus = "disconnect"
- local rootPath = nil
- local cellEndpoints = {}
- local cellTower = {}
- local cellAdjacency = {}
- local myAddress = nil
- local rootPathList = {}
- for address, componentType in component.list("modem") do
- myAddress = address
- end
- local function initWireless()
- m.open(1)
- m.open(16)
- m.setStrength(400)
- end
- local function messageData(msg)
- if msg == "discover" then
- print("Replying back to node.")
- return serialization.serialize({["msgType"] = "discoveryReply", ["status"] = nodeStatus, ["nodeCount"] = 0})
- -- elseif msg == "discoverReply")
- -- return serialization.serialize("discoverReply")
- elseif msg == "join" then
- msgData = {["msgType"] = "joinReply", ["rootPath"] = (myAddress .. "." .. rootPath)}
- return serialization.serialize(msgData)
- -- elseif msg == "joinReply" then
- elseif msg == "data" then
- elseif msg == "keepalive" then
- else
- return
- end
- end
- function listen(param, localAddress, remoteAddress, port, distance, data)
- local packet = serialization.unserialize(data)
- if packet.msgType == "discover" then
- print("Got msg from node: " .. remoteAddress)
- print(remoteAddress .. " 1 " .. messageData("discover"))
- m.send(remoteAddress, 1, messageData("discover"))
- -- elseif data.msgType == "discoveryReply" then
- -- table.insert(cellAdjacency, remoteAddress)
- elseif packet.msgType == "join" and packet.nodeType == "endPoint" then
- --table.insert(cellEndpoints, {remoteAddress)
- print("Sending joinReply to: " .. remoteAddress)
- m.send(remoteAddress, 1, messageData("join"))
- elseif packet.msgType == "join" and packet.nodeType == "tower" then
- table.insert(cellAdjacency, remoteAddress)
- print("Sending joinReply to: " .. remoteAddress)
- m.send(remoteAddress, 1, messageData("join"))
- elseif packet.msgType == "joinReply" then
- rootPath = packet.rootPath
- print("My root path is: " .. rootPath)
- elseif packet.msgType == "announce" then
- print("Got an announcement from: " .. packet.data.srcAddress)
- cellEndpoints[packet.data.srcAddress] = remoteAddress
- payload = {["dstAddress"] = packet.data.dstAddress, ["srcAddress"] = packet.data.srcAddress}
- if t == nil then
- m.send(rootPathList[1], 16, serialization.serialize({["msgType"] = "announce", ["data"] = payload}))
- else
- t.send(serialization.serialize({["msgType"] = "announce", ["data"] = payload}))
- end
- elseif packet.msgType == "data" then
- if packet.payload.dstAddress == myAddress then
- print("I got a packet for me!")
- return
- end
- for address, nextHop in pairs(cellEndpoints) do
- if address == packet.payload.dstAddress then
- print("Found a route entry!")
- local payload = {["dstAddress"] = packet.payload.dstAddress, ["srcAddress"] = packet.payload.srcAddress, ["data"] = packet.payload.data}
- m.send(nextHop, 16, serialization.serialize({["msgType"] = "data", ["payload"] = payload}))
- return
- end
- end
- print("No route entry found, using default...")
- local payload = {["dstAddress"] = packet.payload.dstAddress, ["srcAddress"] = packet.payload.srcAddress, ["data"] = packet.payload.data}
- if t == nil then
- m.send(rootPathList[1], 16, serialization.serialize({["msgType"] = "data", ["payload"] = payload}))
- else
- t.send(serialization.serialize({["msgType"] = "data", ["payload"] = payload}))
- end
- return
- else
- end
- end
- local function discoverTower()
- for i = 1, signalMaxStrength, 1 do
- m.setStrength(i)
- print("Sending discovery at signal strength " .. i )
- m.broadcast(16, (serialization.serialize({["msgType"] = "discover"})))
- local msg, localAddress, remoteAddress, port, distance, data = event.pull(1, "modem_message")
- if msg == nil and i == 400 then
- print("No tower in range!")
- return
- elseif msg == nil then
- else
- signalStrength = i
- m.setStrength(400)
- print("Found a tower: " .. remoteAddress)
- print("Distance of " .. distance .. " at signal strength of " .. signalStrength)
- cellTower = {address = remoteAddress, distance = distance, signalStrength = signalStrength}
- break
- end
- end
- end
- local function procJoinReply()
- --wait for joinReply after sending join
- local msg, localAddress, remoteAddress, port, distance, data = event.pull(2, "modem_message")
- if data == nil then
- print("Join failed!")
- return
- end
- --get data "payload" of the packet
- local data = serialization.unserialize(data)
- --parse out the rootPath string to each "hop" in a table
- for node in string.gmatch(data.rootPath, "[^.]+") do
- table.insert(rootPathList, node)
- end
- --set root path
- rootPath = data.rootPath
- term.write("My root path is: " .. rootPath, true)
- end
- local function joinTower()
- print("Sending join request to: " .. cellTower.address)
- --send join message to tower, should hear back a joinReply if they accept
- m.send(cellTower.address, 16, serialization.serialize({["msgType"] = "join", ["nodeType"] = "tower"}))
- --process the joinReply message (or lack of)
- procJoinReply()
- end
- local function initTower()
- if t == nil then
- print("We are a relay tower!")
- discoverTower()
- joinTower()
- else
- --we are registering with a root
- print("We are a base station!")
- print("Sending join to root.")
- t.send((serialization.serialize({["msgType"] = "join"})))
- procJoinReply()
- end
- end
- local function startListener()
- event.listen("modem_message", listen)
- end
- event.ignore("modem_message", listen)
- initWireless()
- initTower()
- --discoverTower()
- startListener()
- local function test()
- os.sleep(.01)
- return true
- end
- while true do
- test()
- end
Add Comment
Please, Sign In to add comment