From 58c65c30371d6e3ec3e62ae3e900f11ba7b4d81f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 14 Sep 2023 11:09:26 -0400 Subject: [PATCH] Added some endpoints --- central_server.lua | 44 ++++++++++++++++++++++++++++++++++++++++---- router.lua | 16 ++++++++-------- station.lua | 33 +++------------------------------ 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/central_server.lua b/central_server.lua index baf0ffa..4966cb4 100644 --- a/central_server.lua +++ b/central_server.lua @@ -4,12 +4,12 @@ graph of the entire network, and handles requests to find paths to route traffic through. ]]-- -local RECEIVE_CHANNEL = 45453 +local RECEIVE_CHANNEL = 45452 -- ONLY FOR DEBUGGING -- inspect = require("inspect") -local modem = peripheral.wrap("top") or error("Missing top modem") -modem.open(RECEIVE_CHANNEL) +-- local modem = peripheral.wrap("top") or error("Missing top modem") +-- modem.open(RECEIVE_CHANNEL) local function generateStandardNode(id, edgeIds) local node = {id = id, connections = {}, type = "JUNCTION"} @@ -229,6 +229,25 @@ local function findPath(graph, startNode, endNode) return nil end +local function getReachableStations(graph, startNode) + local queue = findConnectedNodes(graph, startNode) + local stations = {} + local visitedNodeIds = {startNode.id} + while #queue > 0 do + local node = table.remove(queue, 1).node + if node.type == "STATION" and not tableContains(visitedNodeIds, node.id) then + table.insert(stations, node) + end + table.insert(visitedNodeIds, node.id) + for _, conn in pairs(findConnectedNodes(graph, node)) do + if not tableContains(visitedNodeIds, conn.node.id) then + table.insert(queue, conn) + end + end + end + return stations +end + local function handleRouteRequest(graph, replyChannel, msg) if not msg.startNode or not msg.endNode then modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Invalid request"}) @@ -248,12 +267,28 @@ local function handleRouteRequest(graph, replyChannel, msg) modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, route = path}) end +local function handleGetRoutesRequest(graph, replyChannel, msg) + if not msg.startNode then + modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Invalid request"}) + return + end + local startNode = findNodeById(graph, msg.startNode) + if not startNode then + modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Unknown node"}) + return + end + local stations = getReachableStations(graph, startNode) + modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, stations = stations}) +end + local function handleRequests(graph) while true do local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") if channel == RECEIVE_CHANNEL and msg and msg.command and type(msg.command) == "string" then if msg.command == "ROUTE" then handleRouteRequest(graph, replyChannel, msg) + elseif msg.command == "GET_ROUTES" then + handleGetRoutesRequest(graph, replyChannel, msg) else modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Invalid command"}) end @@ -261,12 +296,13 @@ local function handleRequests(graph) end end -handleRequests(loadGraph()) +-- handleRequests(loadGraph()) -- local graph = loadGraph() -- print("GRAPH:") -- print(inspect(graph)) -- local startNode = findNodeById(graph, "station-handievale") +-- print(inspect(getReachableStations(graph, startNode))) -- local endNode = findNodeById(graph, "station-foundry") -- print("\n\nPATH:") -- local path = findPath(graph, startNode, endNode) diff --git a/router.lua b/router.lua index 628c2d1..a777359 100644 --- a/router.lua +++ b/router.lua @@ -5,7 +5,7 @@ It also serves as the GUI that users of the system interact with. ]]-- local SWITCH_CHANNEL = 45450 local STATION_BROADCAST_CHANNEL = 45451 -local STATION_REQUEST_CHANNEL = 45452 +local SERVER_CHANNEL = 45452 local MY_CHANNEL = 45460 local g = require("simple-graphics") @@ -35,7 +35,7 @@ end local function waitForStation(stationName) while true do local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") - if channel == STATION_BROADCAST_CHANNEL and msg == stationName and dist <= 16 then + if channel == STATION_BROADCAST_CHANNEL and msg and msg.name == stationName and msg.range >= dist then return end end @@ -44,7 +44,7 @@ end local function listenForAnyStation() while true do local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") - if channel == STATION_BROADCAST_CHANNEL and type(msg) == "string" and dist <= 16 then + if channel == STATION_BROADCAST_CHANNEL and msg and msg.range >= dist then os.queueEvent("rail_station_nearby", msg, dist) end end @@ -55,8 +55,8 @@ local function waitForNoStation(targetName) while os.epoch() - lastPing < 5000 do parallel.waitForAny( function () - local event, name, dist = os.pullEvent("rail_station_nearby") - if not targetName or targetName == name then + local event, data, dist = os.pullEvent("rail_station_nearby") + if not targetName or targetName == data.name then stationPresent = true lastPing = os.epoch() end @@ -95,11 +95,11 @@ local function handleNearbyStation() g.drawText(term, 1, 3, "see available routes.", colors.gray, colors.white) os.sleep(1) - local event, name, dist = os.pullEvent("rail_station_nearby") + local event, stationData, dist = os.pullEvent("rail_station_nearby") g.clear(term, colors.white) g.drawXLine(term, 1, W, 1, colors.lightBlue) g.drawText(term, 1, 1, "Found a station!", colors.black, colors.lightBlue) - g.drawText(term, 1, 3, name, colors.blue, colors.white) + g.drawText(term, 1, 3, stationData.name, colors.blue, colors.white) g.drawText(term, 1, 5, "Fetching routes...", colors.gray, colors.white) os.sleep(1) @@ -149,7 +149,7 @@ local function handleNearbyStation() end end end, - function () waitForNoStation(name) end + function () waitForNoStation(stationData.name) end ) -- Quit our main loop if the user has chosen a route. if routeChosen then return end diff --git a/station.lua b/station.lua index 460a992..a0d19e5 100644 --- a/station.lua +++ b/station.lua @@ -12,9 +12,6 @@ You should add a "station_config.tbl" file containing: local modem = peripheral.wrap("top") or error("Missing top modem") local BROADCAST_CHANNEL = 45451 -local RECEIVE_CHANNEL = 45452 - -modem.open(RECEIVE_CHANNEL) local function readConfig() local f = io.open("station_config.tbl", "r") @@ -24,41 +21,17 @@ local function readConfig() return cfg end -local function broadcastName(config) +local function broadcast(config) while true do - modem.transmit(BROADCAST_CHANNEL, BROADCAST_CHANNEL, config.name) + modem.transmit(BROADCAST_CHANNEL, BROADCAST_CHANNEL, config) os.sleep(1) end end -local function handleRequests(config) - while true do - local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message") - if channel == RECEIVE_CHANNEL and dist <= config.range then - if msg == "GET_ROUTES" then - modem.transmit(replyChannel, RECEIVE_CHANNEL, config.routes) - print(textutils.formatTime(os.time()).." Sent routes to "..replyChannel) - end - end - end -end - local config = readConfig() term.clear() term.setCursorPos(1, 1) print("Running station transponder for \""..config.name.."\".") print(" Display Name: "..config.displayName) print(" Range: "..config.range.." blocks") -print(" Routes:") -for i, route in pairs(config.routes) do - local pathStr = "" - for j, segment in pairs(route.path) do - pathStr = pathStr .. segment - if j < #route.path then pathStr = pathStr .. "," end - end - print(" "..i..". "..route.name..": "..pathStr) -end -parallel.waitForAll( - function() broadcastName(config) end, - function() handleRequests(config) end -) +broadcast(config)