small repo system progress
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
.settings
|
||||
@@ -20,7 +20,9 @@ local filesystem = {
|
||||
"startup/01_repo.lua",
|
||||
"libs/containers.lua",
|
||||
"libs/windows.lua",
|
||||
"apps/com.ruffles.launcher",
|
||||
"libs/deflate.lua",
|
||||
"libs/persistent-storage.lua",
|
||||
"apps/org.ruffles.launcher",
|
||||
},
|
||||
dirs = {
|
||||
"global-libraries",
|
||||
|
||||
6
scan.lua
6
scan.lua
@@ -1,7 +1,7 @@
|
||||
local exceptions = {
|
||||
["/scan.lua"] = true,
|
||||
["/install.lua"] = true,
|
||||
["scan.lua"] = true,
|
||||
["/install.lua"] = true,
|
||||
["install.lua"] = true,
|
||||
["/.settings"] = true,
|
||||
[".settings"] = true,
|
||||
@@ -10,7 +10,9 @@ local exceptions = {
|
||||
["/rom"] = true,
|
||||
["rom"] = true,
|
||||
["/scan.dat"] = true,
|
||||
["scan.dat"] = true
|
||||
["scan.dat"] = true,
|
||||
["/.gitignore"] = true,
|
||||
[".gitignore"] = true
|
||||
}
|
||||
local function scan(dir)
|
||||
local out = {}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
local max_distance = 120
|
||||
local expect = dofile("rom/modules/main/cc/expect.lua")
|
||||
local expect, field = expect.expect, expect.field
|
||||
local pullEvent = os.pullEventRaw
|
||||
local modem = peripheral.find("modem",function (s) return peripheral.wrap(s).isWireless() end)
|
||||
term.clear()
|
||||
@@ -12,26 +9,29 @@ if not modem then
|
||||
end
|
||||
local message_queue = {}
|
||||
modem.open(15125)
|
||||
local canidate = {id = -1, distance = max_distance}
|
||||
local canidate = {id = -1, distance = 764, max_distance=0}
|
||||
local last_heartbeat = os.epoch("utc")
|
||||
|
||||
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
|
||||
canidate.distance = distance
|
||||
last_heartbeat = os.epoch("utc")
|
||||
modem.transmit(15125,15125,{protocol="heartbeat_response",sender=os.getComputerID(),target=canidate.id})
|
||||
if distance > max_distance then
|
||||
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 = max_distance}
|
||||
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
|
||||
if distance < canidate.distance and distance < (msg.max_distance or 128) then
|
||||
canidate.max_distance = msg.max_distance
|
||||
canidate.id = msg.sender
|
||||
canidate.distance = distance
|
||||
end
|
||||
@@ -61,21 +61,20 @@ function _G.network.send(msg,destination)
|
||||
message_queue[#message_queue+1] = {protocol="packet",content=msg,destination=destination,sender=os.getComputerID(),hops=0}
|
||||
end
|
||||
|
||||
function _G.network.getCandidate()
|
||||
return canidate
|
||||
end
|
||||
|
||||
local function connect()
|
||||
while true do
|
||||
if os.epoch("utc") - last_heartbeat > 200 then
|
||||
canidate = {id = -1, distance = max_distance}
|
||||
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 < canidate.distance 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
|
||||
@@ -85,10 +84,14 @@ local function connect()
|
||||
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
|
||||
@@ -98,6 +101,9 @@ local function connect()
|
||||
sleep()
|
||||
end
|
||||
end
|
||||
function _G.network.getCandidate()
|
||||
return canidate
|
||||
end
|
||||
function _G.network.run()
|
||||
parallel.waitForAny(function () repeat sleep(0.1) until canidate.id ~= -1 end,
|
||||
function ()
|
||||
@@ -105,7 +111,9 @@ function _G.network.run()
|
||||
local _, _, channel, _, msg, distance = pullEvent("modem_message")
|
||||
if channel == 15125 then
|
||||
if msg.protocol == "entrypoint_advertise" then
|
||||
if distance < canidate.distance then
|
||||
if distance > msg.max_distance then
|
||||
modem.transmit(15125,15125,{protocol="out_of_range",sender=os.getComputerID(),target=msg.sender})
|
||||
elseif distance < canidate.distance then
|
||||
canidate.id = msg.sender
|
||||
canidate.distance = distance
|
||||
end
|
||||
|
||||
@@ -1,3 +1,54 @@
|
||||
local lib = {}
|
||||
local expect = require "cc.expect".expect
|
||||
local file = fs.open("/repos.dat","r")
|
||||
local repos = {}
|
||||
if file then
|
||||
repos = textutils.unserialise(file.readAll() or "") or repos
|
||||
end
|
||||
local function saveRepos()
|
||||
local file = fs.open("/repos.dat","w")
|
||||
file.write(textutils.serialise(repos))
|
||||
file.close()
|
||||
end
|
||||
function lib.addRepo(id)
|
||||
expect(1,id,"number")
|
||||
repos[id] = "computer"
|
||||
saveRepos()
|
||||
end
|
||||
function lib.getRepos()
|
||||
return repos
|
||||
end
|
||||
function lib.rmRepo(id)
|
||||
expect(1,id,"number")
|
||||
repos[id] = nil
|
||||
saveRepos()
|
||||
end
|
||||
function lib.listPackages(type)
|
||||
local queued_requests = {}
|
||||
local recorded_packages = {}
|
||||
for k,v in pairs(repos) do
|
||||
if v == "computer" then
|
||||
network.send({proto="query",package_type=type},k)
|
||||
queued_requests[k] = "computer"
|
||||
end
|
||||
end
|
||||
local next_timestamp = os.clock()+5
|
||||
repeat
|
||||
local event = {os.pullEvent()}
|
||||
if event[1] == "network_packet" then
|
||||
if queued_requests[event[3]] == "computer" and event[2].proto == "query_resp" and event[2].package_type == type then
|
||||
for name,data in pairs(event[2].data) do
|
||||
if recorded_packages[name] then
|
||||
recorded_packages[name].repos[event[3]] = event[4]
|
||||
else
|
||||
data.repos = {[event[3]] = event[4]}
|
||||
recorded_packages[name] = data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
until next(queued_requests) == nil or os.clock() >= next_timestamp
|
||||
return recorded_packages
|
||||
end
|
||||
|
||||
_G.repo = lib
|
||||
Reference in New Issue
Block a user