From 65b64c4a92694d35d2688f762d14b8bd14109680 Mon Sep 17 00:00:00 2001 From: Rivulet Date: Fri, 2 Jan 2026 22:41:24 +0000 Subject: [PATCH] Add apprepo.lua --- apprepo.lua | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 apprepo.lua diff --git a/apprepo.lua b/apprepo.lua new file mode 100644 index 0000000..68db411 --- /dev/null +++ b/apprepo.lua @@ -0,0 +1,170 @@ +local pullEvent = os.pullEventRaw +local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end) +term.clear() +term.setCursorPos(1,1) +_G.network = {} +if not modem then + shell.run("shell") + os.shutdown() +end +local message_queue = {} +modem.open(15125) +local canidate = {id = -1, distance = 764, max_distance=0} +parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, +function () + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "entrypoint_advertise" then + if distance < canidate.distance and distance < (msg.max_distance or 128) then + canidate.max_distance = (msg.max_distance or 128) + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end +end) +local last_heartbeat = os.epoch("utc") +modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + +local function receive() + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "heartbeat" and msg.target == os.getComputerID() and msg.sender == canidate.id then + last_heartbeat = os.epoch("utc") + modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id}) + canidate.distance = distance + canidate.max_distance = msg.max_distance + if distance > (msg.max_distance or 764) then + modem.transmit(15125,15125,{protocol="entrypoint_disconnect",sender=os.getComputerID(),target=canidate.id}) + canidate = {id = -1, distance = 764, max_distance=0} + parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, + function () + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "entrypoint_advertise" then + if distance < canidate.distance then + canidate.max_distance = msg.max_distance + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end + end) + if canidate.id == -1 then + sleep(5) + else + modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + last_heartbeat = os.epoch("utc") + end + end + elseif msg.protocol == "packet" then + if msg.hops >= 1 and msg.destination == os.computerID() then + os.queueEvent("network_packet",msg.content,msg.sender,msg.hops) + end + end + end + end +end + +function _G.network.send(msg,destination) + if not msg then error("No message provided",2) end + if not destination then error("No destination provided",2) end + message_queue[#message_queue+1] = {protocol="packet",content=msg,destination=destination,sender=os.getComputerID(),hops=0} +end + +local function connect() + while true do + if os.epoch("utc") - last_heartbeat > math.max((300*canidate.distance)/100,300) then + canidate = {id = -1, distance = 764, max_distance=0} + parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end, + function () + while true do + local _, _, channel, _, msg, distance = pullEvent("modem_message") + if channel == 15125 then + if msg.protocol == "entrypoint_advertise" then + if distance > (msg.max_distance or 128) then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=msg.sender}) + elseif distance < canidate.distance then + canidate.max_distance = msg.max_distance + canidate.id = msg.sender + canidate.distance = distance + end + end + end + end + end) + if canidate.id == -1 then + sleep(5) + else + if canidate.distance > (canidate.max_distance or 128) then + modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=canidate.id}) + else + modem.transmit(15125,15125,{protocol="entrypoint_connect",sender=os.getComputerID(),target=canidate.id}) + last_heartbeat = os.epoch("utc") + end + end + else + local msg = table.remove(message_queue,1) + if msg then + modem.transmit(15125,15125,msg) + end + end + sleep() + end +end +function _G.network.getCandidate() + return canidate +end + +local function repo_server() +local appindex = {} +print("starting repo server") +local function index_apps(dir) + local index = {} + if not dir then dir = "/" end + for _,path in ipairs(fs.list(dir)) do + local path = fs.combine(dir,path) + if not fs.isReadOnly(path) then + if fs.isDir(path) then + local dir_index = index_apps(path) + for k,v in pairs(dir_index) do + index[k] = v + end + elseif not fs.isDir(path) then + local file = fs.open(path,"r") + if file then + local data = textutils.unserialise(file.readAll()) + if data and data.meta and data.package and data.meta.appid then + index[data.meta.appid] = data.meta + appindex[data.meta.appid] = data + end + end + end + end + end + return index +end +index_apps() +print(textutils.serialise(appindex)) +while true do + local _,msg,id,hops = os.pullEvent("network_packet") + print(textutils.serialise(msg),id) + if msg.proto == "query" then + if msg.package_type == "apps" then + network.send({proto="query_resp",package_type="apps",data=index_apps()},id) + end + elseif msg.proto == "get_data" then + if msg.package_type == "apps" then + network.send({proto="data_resp",package_type="apps",data=appindex[msg.id]},id) + end + end +end + +end + +parallel.waitForAny(receive, connect, repo_server) \ No newline at end of file