From 0f2dca23a76728278afaf32546f2e1c6f3e36302 Mon Sep 17 00:00:00 2001 From: CadenCoaster <114967401+cadenthecreator@users.noreply.github.com> Date: Sun, 19 Oct 2025 13:24:58 -0700 Subject: [PATCH] Refactor message queue and packet handling Refactored message queue functions and improved packet forwarding logic. --- meshrouter.lua | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/meshrouter.lua b/meshrouter.lua index 363f20a..db255e0 100644 --- a/meshrouter.lua +++ b/meshrouter.lua @@ -3,6 +3,15 @@ local radio = peripheral.find("radio_tower") local modem = peripheral.find("modem") local distancemap = {} 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) for _,v in pairs(distancemap) do if v.sender == id then @@ -22,13 +31,6 @@ local function is_in_table(table,value) return false 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 print("in router and bridge mode") radio.setFrequency(15124) @@ -113,11 +115,14 @@ while true do if msg.destination == os.getComputerID() then print("Packet received from",tostring(msg.sender)..":",msg.content) else - if distancemap[msg.destination] ~= nil then - msg.hops = msg.hops + 1 - queue_message(msg, distancemap[msg.destination].sender) - else - print("No route to",msg.destination) + if msg._target then + if distancemap[msg.destination] ~= nil then + print("Forwarding packet to node",msg.destination) + msg.hops = msg.hops + 1 + queue_message(msg, distancemap[msg.destination].sender) + else + print("No route to",msg.destination) + end end end elseif msg.protocol == "heartbeat" then @@ -129,7 +134,7 @@ while true do queue_message(response, msg._sender) elseif msg.protocol == "getroutes_response" then 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.dist = v.dist + 1 distancemap[k] = v @@ -165,7 +170,7 @@ local function heartbeat_f() heartbeat = false interactions.send({protocol="heartbeat"}, v.sender) parallel.waitForAny(function() - sleep(0.5) + sleep(5) end, function() while not heartbeat do sleep()