Added some endpoints
This commit is contained in:
parent
dfd8665920
commit
58c65c3037
|
@ -4,12 +4,12 @@ graph of the entire network, and handles requests to find paths to route
|
||||||
traffic through.
|
traffic through.
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
local RECEIVE_CHANNEL = 45453
|
local RECEIVE_CHANNEL = 45452
|
||||||
|
|
||||||
-- ONLY FOR DEBUGGING
|
-- ONLY FOR DEBUGGING
|
||||||
-- inspect = require("inspect")
|
-- inspect = require("inspect")
|
||||||
local modem = peripheral.wrap("top") or error("Missing top modem")
|
-- local modem = peripheral.wrap("top") or error("Missing top modem")
|
||||||
modem.open(RECEIVE_CHANNEL)
|
-- modem.open(RECEIVE_CHANNEL)
|
||||||
|
|
||||||
local function generateStandardNode(id, edgeIds)
|
local function generateStandardNode(id, edgeIds)
|
||||||
local node = {id = id, connections = {}, type = "JUNCTION"}
|
local node = {id = id, connections = {}, type = "JUNCTION"}
|
||||||
|
@ -229,6 +229,25 @@ local function findPath(graph, startNode, endNode)
|
||||||
return nil
|
return nil
|
||||||
end
|
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)
|
local function handleRouteRequest(graph, replyChannel, msg)
|
||||||
if not msg.startNode or not msg.endNode then
|
if not msg.startNode or not msg.endNode then
|
||||||
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Invalid request"})
|
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})
|
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = true, route = path})
|
||||||
end
|
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)
|
local function handleRequests(graph)
|
||||||
while true do
|
while true do
|
||||||
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
|
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 channel == RECEIVE_CHANNEL and msg and msg.command and type(msg.command) == "string" then
|
||||||
if msg.command == "ROUTE" then
|
if msg.command == "ROUTE" then
|
||||||
handleRouteRequest(graph, replyChannel, msg)
|
handleRouteRequest(graph, replyChannel, msg)
|
||||||
|
elseif msg.command == "GET_ROUTES" then
|
||||||
|
handleGetRoutesRequest(graph, replyChannel, msg)
|
||||||
else
|
else
|
||||||
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Invalid command"})
|
modem.transmit(replyChannel, RECEIVE_CHANNEL, {success = false, error = "Invalid command"})
|
||||||
end
|
end
|
||||||
|
@ -261,12 +296,13 @@ local function handleRequests(graph)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
handleRequests(loadGraph())
|
-- handleRequests(loadGraph())
|
||||||
|
|
||||||
-- local graph = loadGraph()
|
-- local graph = loadGraph()
|
||||||
-- print("GRAPH:")
|
-- print("GRAPH:")
|
||||||
-- print(inspect(graph))
|
-- print(inspect(graph))
|
||||||
-- local startNode = findNodeById(graph, "station-handievale")
|
-- local startNode = findNodeById(graph, "station-handievale")
|
||||||
|
-- print(inspect(getReachableStations(graph, startNode)))
|
||||||
-- local endNode = findNodeById(graph, "station-foundry")
|
-- local endNode = findNodeById(graph, "station-foundry")
|
||||||
-- print("\n\nPATH:")
|
-- print("\n\nPATH:")
|
||||||
-- local path = findPath(graph, startNode, endNode)
|
-- local path = findPath(graph, startNode, endNode)
|
||||||
|
|
16
router.lua
16
router.lua
|
@ -5,7 +5,7 @@ It also serves as the GUI that users of the system interact with.
|
||||||
]]--
|
]]--
|
||||||
local SWITCH_CHANNEL = 45450
|
local SWITCH_CHANNEL = 45450
|
||||||
local STATION_BROADCAST_CHANNEL = 45451
|
local STATION_BROADCAST_CHANNEL = 45451
|
||||||
local STATION_REQUEST_CHANNEL = 45452
|
local SERVER_CHANNEL = 45452
|
||||||
local MY_CHANNEL = 45460
|
local MY_CHANNEL = 45460
|
||||||
|
|
||||||
local g = require("simple-graphics")
|
local g = require("simple-graphics")
|
||||||
|
@ -35,7 +35,7 @@ end
|
||||||
local function waitForStation(stationName)
|
local function waitForStation(stationName)
|
||||||
while true do
|
while true do
|
||||||
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
|
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
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -44,7 +44,7 @@ end
|
||||||
local function listenForAnyStation()
|
local function listenForAnyStation()
|
||||||
while true do
|
while true do
|
||||||
local event, side, channel, replyChannel, msg, dist = os.pullEvent("modem_message")
|
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)
|
os.queueEvent("rail_station_nearby", msg, dist)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -55,8 +55,8 @@ local function waitForNoStation(targetName)
|
||||||
while os.epoch() - lastPing < 5000 do
|
while os.epoch() - lastPing < 5000 do
|
||||||
parallel.waitForAny(
|
parallel.waitForAny(
|
||||||
function ()
|
function ()
|
||||||
local event, name, dist = os.pullEvent("rail_station_nearby")
|
local event, data, dist = os.pullEvent("rail_station_nearby")
|
||||||
if not targetName or targetName == name then
|
if not targetName or targetName == data.name then
|
||||||
stationPresent = true
|
stationPresent = true
|
||||||
lastPing = os.epoch()
|
lastPing = os.epoch()
|
||||||
end
|
end
|
||||||
|
@ -95,11 +95,11 @@ local function handleNearbyStation()
|
||||||
g.drawText(term, 1, 3, "see available routes.", colors.gray, colors.white)
|
g.drawText(term, 1, 3, "see available routes.", colors.gray, colors.white)
|
||||||
os.sleep(1)
|
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.clear(term, colors.white)
|
||||||
g.drawXLine(term, 1, W, 1, colors.lightBlue)
|
g.drawXLine(term, 1, W, 1, colors.lightBlue)
|
||||||
g.drawText(term, 1, 1, "Found a station!", colors.black, 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)
|
g.drawText(term, 1, 5, "Fetching routes...", colors.gray, colors.white)
|
||||||
os.sleep(1)
|
os.sleep(1)
|
||||||
|
|
||||||
|
@ -149,7 +149,7 @@ local function handleNearbyStation()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
function () waitForNoStation(name) end
|
function () waitForNoStation(stationData.name) end
|
||||||
)
|
)
|
||||||
-- Quit our main loop if the user has chosen a route.
|
-- Quit our main loop if the user has chosen a route.
|
||||||
if routeChosen then return end
|
if routeChosen then return end
|
||||||
|
|
33
station.lua
33
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 modem = peripheral.wrap("top") or error("Missing top modem")
|
||||||
local BROADCAST_CHANNEL = 45451
|
local BROADCAST_CHANNEL = 45451
|
||||||
local RECEIVE_CHANNEL = 45452
|
|
||||||
|
|
||||||
modem.open(RECEIVE_CHANNEL)
|
|
||||||
|
|
||||||
local function readConfig()
|
local function readConfig()
|
||||||
local f = io.open("station_config.tbl", "r")
|
local f = io.open("station_config.tbl", "r")
|
||||||
|
@ -24,41 +21,17 @@ local function readConfig()
|
||||||
return cfg
|
return cfg
|
||||||
end
|
end
|
||||||
|
|
||||||
local function broadcastName(config)
|
local function broadcast(config)
|
||||||
while true do
|
while true do
|
||||||
modem.transmit(BROADCAST_CHANNEL, BROADCAST_CHANNEL, config.name)
|
modem.transmit(BROADCAST_CHANNEL, BROADCAST_CHANNEL, config)
|
||||||
os.sleep(1)
|
os.sleep(1)
|
||||||
end
|
end
|
||||||
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()
|
local config = readConfig()
|
||||||
term.clear()
|
term.clear()
|
||||||
term.setCursorPos(1, 1)
|
term.setCursorPos(1, 1)
|
||||||
print("Running station transponder for \""..config.name.."\".")
|
print("Running station transponder for \""..config.name.."\".")
|
||||||
print(" Display Name: "..config.displayName)
|
print(" Display Name: "..config.displayName)
|
||||||
print(" Range: "..config.range.." blocks")
|
print(" Range: "..config.range.." blocks")
|
||||||
print(" Routes:")
|
broadcast(config)
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
Loading…
Reference in New Issue