Refactor message queue and packet handling
Refactored message queue functions and improved packet forwarding logic.
This commit is contained in:
@@ -3,6 +3,15 @@ local radio = peripheral.find("radio_tower")
|
|||||||
local modem = peripheral.find("modem")
|
local modem = peripheral.find("modem")
|
||||||
local distancemap = {}
|
local distancemap = {}
|
||||||
local message_queue = {}
|
local message_queue = {}
|
||||||
|
|
||||||
|
local function queue_message(msg, target)
|
||||||
|
table.insert(message_queue,#message_queue+1, {msg,target})
|
||||||
|
end
|
||||||
|
local function dequeue_message()
|
||||||
|
return table.unpack(table.remove(message_queue,1) or {})
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local function isInDMap(id)
|
local function isInDMap(id)
|
||||||
for _,v in pairs(distancemap) do
|
for _,v in pairs(distancemap) do
|
||||||
if v.sender == id then
|
if v.sender == id then
|
||||||
@@ -22,13 +31,6 @@ local function is_in_table(table,value)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local function queue_message(msg, target)
|
|
||||||
table.insert(message_queue,#message_queue+1, {msg,target})
|
|
||||||
end
|
|
||||||
local function dequeue_message()
|
|
||||||
return table.unpack(table.remove(message_queue,1) or {})
|
|
||||||
end
|
|
||||||
|
|
||||||
if radio and modem then
|
if radio and modem then
|
||||||
print("in router and bridge mode")
|
print("in router and bridge mode")
|
||||||
radio.setFrequency(15124)
|
radio.setFrequency(15124)
|
||||||
@@ -113,11 +115,14 @@ while true do
|
|||||||
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 distancemap[msg.destination] ~= nil then
|
||||||
queue_message(msg, distancemap[msg.destination].sender)
|
print("Forwarding packet to node",msg.destination)
|
||||||
else
|
msg.hops = msg.hops + 1
|
||||||
print("No route to",msg.destination)
|
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
|
||||||
@@ -129,7 +134,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
|
||||||
@@ -165,7 +170,7 @@ local function heartbeat_f()
|
|||||||
heartbeat = false
|
heartbeat = false
|
||||||
interactions.send({protocol="heartbeat"}, v.sender)
|
interactions.send({protocol="heartbeat"}, v.sender)
|
||||||
parallel.waitForAny(function()
|
parallel.waitForAny(function()
|
||||||
sleep(0.5)
|
sleep(5)
|
||||||
end, function()
|
end, function()
|
||||||
while not heartbeat do
|
while not heartbeat do
|
||||||
sleep()
|
sleep()
|
||||||
|
|||||||
Reference in New Issue
Block a user