Improve client connection and packet forwarding logic
Enhanced packet handling and client management in mesh router.
This commit is contained in:
@@ -20,6 +20,7 @@ local function entrypoint_recieving(channel, msg)
|
|||||||
if msg.protocol == "entrypoint_connect" then
|
if msg.protocol == "entrypoint_connect" then
|
||||||
if not clients[msg.sender] and msg.target == os.getComputerID() then
|
if not clients[msg.sender] and msg.target == os.getComputerID() then
|
||||||
clients[msg.sender] = true
|
clients[msg.sender] = true
|
||||||
|
distancemap[msg.sender] = {dist = 1, sender = msg.sender}
|
||||||
print("New client connected:",msg.sender)
|
print("New client connected:",msg.sender)
|
||||||
queue_message({protocol="route",destination=msg.sender,distance=1})
|
queue_message({protocol="route",destination=msg.sender,distance=1})
|
||||||
modem.transmit(15125,15125,{protocol="entrypoint_acknowledge",sender=os.getComputerID()})
|
modem.transmit(15125,15125,{protocol="entrypoint_acknowledge",sender=os.getComputerID()})
|
||||||
@@ -35,9 +36,12 @@ local function entrypoint_recieving(channel, msg)
|
|||||||
heartbeat_entrypoint = true
|
heartbeat_entrypoint = true
|
||||||
end
|
end
|
||||||
elseif msg.protocol == "packet" then
|
elseif msg.protocol == "packet" then
|
||||||
if clients[msg.sender] then
|
if clients[msg.sender] and (msg.hops or 0 ) == 0 then
|
||||||
print("Packet received from client",tostring(msg.sender)..":",msg.content,"to",msg.destination)
|
print("Packet received from client",tostring(msg.sender),"to",msg.destination)
|
||||||
if distancemap[msg.destination] ~= nil then
|
if clients[msg.destination] then
|
||||||
|
msg.hops = (msg.hops or 0) + 1
|
||||||
|
modem.transmit(15125,15125,msg)
|
||||||
|
elseif distancemap[msg.destination] ~= nil then
|
||||||
msg.hops = (msg.hops or 0) + 1
|
msg.hops = (msg.hops or 0) + 1
|
||||||
queue_message(msg, distancemap[msg.destination].sender)
|
queue_message(msg, distancemap[msg.destination].sender)
|
||||||
else
|
else
|
||||||
@@ -135,24 +139,32 @@ while true do
|
|||||||
end
|
end
|
||||||
elseif msg.protocol == "route_erase" then
|
elseif msg.protocol == "route_erase" then
|
||||||
if distancemap[msg.destination] ~= nil and msg.destination ~= os.getComputerID() and not is_in_table(msg.visited,os.getComputerID()) then
|
if distancemap[msg.destination] ~= nil and msg.destination ~= os.getComputerID() and not is_in_table(msg.visited,os.getComputerID()) then
|
||||||
distancemap[msg.destination] = nil
|
if clients[msg.destination] then
|
||||||
if (not msg.visited) then msg.visited = {} end
|
queue_message({protocol="route",destination=msg.destination,distance=1})
|
||||||
msg.visited[#msg.visited+1] = os.getComputerID()
|
else
|
||||||
print("route to",msg.destination,"erased!")
|
distancemap[msg.destination] = nil
|
||||||
queue_message(msg)
|
if (not msg.visited) then msg.visited = {} end
|
||||||
|
msg.visited[#msg.visited+1] = os.getComputerID()
|
||||||
|
print("route to",msg.destination,"erased!")
|
||||||
|
queue_message(msg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
elseif msg.protocol == "packet" then
|
elseif msg.protocol == "packet" then
|
||||||
if msg.destination == os.getComputerID() then
|
if msg.destination == os.getComputerID() then
|
||||||
print("Packet received from",tostring(msg.sender)..":",msg.content)
|
print("Packet received from",tostring(msg.sender)..":",msg.content)
|
||||||
else
|
else
|
||||||
if distancemap[msg.destination] ~= nil then
|
if msg._target then
|
||||||
msg.hops = msg.hops + 1
|
if clients[msg.destination] then
|
||||||
queue_message(msg, distancemap[msg.destination].sender)
|
print("Forwarding packet to client",msg.destination)
|
||||||
elseif clients[msg.destination] then
|
msg.hops = msg.hops + 1
|
||||||
msg.hops = msg.hops + 1
|
modem.transmit(15125,15125,msg)
|
||||||
modem.transmit(15125,15125,msg)
|
elseif distancemap[msg.destination] ~= nil then
|
||||||
else
|
print("Forwarding packet to node",msg.destination)
|
||||||
print("No route to",msg.destination)
|
msg.hops = msg.hops + 1
|
||||||
|
queue_message(msg, distancemap[msg.destination].sender)
|
||||||
|
else
|
||||||
|
print("No route to",msg.destination)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif msg.protocol == "heartbeat" then
|
elseif msg.protocol == "heartbeat" then
|
||||||
@@ -164,7 +176,7 @@ while true do
|
|||||||
queue_message(response, msg._sender)
|
queue_message(response, msg._sender)
|
||||||
elseif msg.protocol == "getroutes_response" then
|
elseif msg.protocol == "getroutes_response" then
|
||||||
for k,v in pairs(msg.routes) do
|
for k,v in pairs(msg.routes) do
|
||||||
if v.dist+1 < (distancemap[k] or {dist = math.huge}).dist and k ~= os.getComputerID() then
|
if v.dist+1 < (distancemap[k] or {dist = math.huge}).dist and k ~= os.getComputerID() and v.sender ~= os.getComputerID() then
|
||||||
v.sender = msg._sender
|
v.sender = msg._sender
|
||||||
v.dist = v.dist + 1
|
v.dist = v.dist + 1
|
||||||
distancemap[k] = v
|
distancemap[k] = v
|
||||||
@@ -196,7 +208,7 @@ end
|
|||||||
local function heartbeat_f()
|
local function heartbeat_f()
|
||||||
while true do
|
while true do
|
||||||
for k,v in pairs(distancemap) do
|
for k,v in pairs(distancemap) do
|
||||||
if k ~= os.getComputerID() then
|
if k ~= os.getComputerID() and not clients[v.sender] then
|
||||||
heartbeat = false
|
heartbeat = false
|
||||||
interactions.send({protocol="heartbeat"}, v.sender)
|
interactions.send({protocol="heartbeat"}, v.sender)
|
||||||
parallel.waitForAny(function()
|
parallel.waitForAny(function()
|
||||||
@@ -230,7 +242,7 @@ local function healthcheck()
|
|||||||
sleep(0.1)
|
sleep(0.1)
|
||||||
end, function()
|
end, function()
|
||||||
while not heartbeat_entrypoint do
|
while not heartbeat_entrypoint do
|
||||||
modem.transmit(15125,15125,{protocol="heartbeat"})
|
modem.transmit(15125,15125,{protocol="heartbeat",sender=os.getComputerID(),target=client})
|
||||||
sleep()
|
sleep()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user