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")
- m = component.modem
- t = {}
- for address, componentType in component.list("tunnel") do
- t[tostring(address)] = component.proxy(address)
- end
- myAddress = nil
- for address, componentType in component.list("modem") do
- myAddress = address
- end
- rootPath = myAddress
- cellEndpoints = {}
- cellAdjacency = {}
- local function initRoot()
- print("Root ID: " .. myAddress)
- m.open(1)
- m.open(16)
- end
- local function messageData(msg)
- if msg == "join" then
- msgData = {["msgType"] = "joinReply", ["rootPath"] = myAddress}
- return serialization.serialize(msgData)
- -- elseif msg == "joinReply" then
- elseif msg == "data" then
- elseif msg == "keepalive" then
- else
- return
- end
- end
- function getTunnelAddress(nextHop)
- for hop, address in pairs(cellAdjacency) do
- if nextHop == hop then
- return address
- end
- end
- end
- function listen(msg, localAddress, remoteAddress, port, distance, data)
- print("Waiting for modem message...")
- print("Got a message!")
- local packet = serialization.unserialize(data)
- print("DEBUG: " .. packet.msgType)
- if packet.msgType == "join" then
- --code to check if this node has previously been added (is alreday in the table!)
- cellAdjacency[remoteAddress] = localAddress
- print("Sending reply to station: " .. remoteAddress)
- payload = serialization.serialize({["msgType"] = "joinReply", ["rootPath"] = getTunnelAddress(remoteAddress)})
- t[getTunnelAddress(remoteAddress)].send(payload)
- elseif packet.msgType == "announce" then
- print("Adding cell Endpoint to list: " .. packet.data.srcAddress)
- cellEndpoints[packet.data.srcAddress] = remoteAddress
- payload = {["dstAddress"] = packet.data.dstAddress, ["srcAddress"] = packet.data.srcAddress}
- end
- end
- local function startListener()
- event.listen("modem_message", listen)
- end
- event.ignore("modem_message", listen)
- initRoot()
- startListener()
- local function test()
- os.sleep(.01)
- return
- end
- while true do
- test()
- end
Add Comment
Please, Sign In to add comment