iteration

This commit is contained in:
Layla 2023-10-08 04:04:21 -04:00
parent c9e6e5d229
commit f0233a640d
13 changed files with 130 additions and 111 deletions

29
dig.lua
View File

@ -1,29 +0,0 @@
local args = { ... }
modem_location = "right"
lns_server = "default"
rednet.open(modem_location)
receiverID = rednet.lookup("lns", lns_server)
current_time = os.time()
data = {
["action"] = "lookup",
["hostname"] = args[1]
}
rednet.send(receiverID, data)
while true do
id, msg = rednet.receive()
if id == receiverID then
if msg == nil then
print("Hostname not found!")
break
else
io.write(msg .. "\n")
break
end
end
end

45
install.lua Normal file
View File

@ -0,0 +1,45 @@
local args = { ... }
local target_branch = "main"
local bin_path = "/bin/"
local lib_path = "/lib/"
local startup_path = "/startup/"
function download_lua(remote_path, local_path)
io.write("Downloading " .. remote_path .. " to " .. local_path .. "\n")
response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil)
if response.getResponseCode() == 200 then
file = fs.open(local_path, "w")
file.write(response.readAll())
file.close()
end
else
io.write("Failed to update " .. program_name .. ".lua\n")
io.write(response)
end
end
-- Startup
download_lua("utils/lsh.lua", startup_path .. "lsh.lua")
-- Lib
download_lua("lib/lns.lua", lib_path .. "lns.lua")
download_lua("lib/lum.lua", lib_path .. "lum.lua")
-- Bin
download_lua("lum.lua", bin_path .. "lum.lua")
-- Run lsh
ok = shell.run(startup_path .. "lsh.lua")
if not ok then
io.write("Failed to run lsh!\n")
return
end
-- Packages
lum.install("dig")
lum.install("auth")
io.write("Done!\n")

View File

@ -1,7 +1,4 @@
args = { ... } local cache = {}
cache = {}
settings.define("lns_client.allow_dynamic_lns_hosts", { settings.define("lns_client.allow_dynamic_lns_hosts", {
description = "Allow dynamic LNS hosts", description = "Allow dynamic LNS hosts",
@ -15,10 +12,8 @@ settings.define("lns_client.lns_server", {
default = 8 default = 8
}) })
allow_dynamic_lns_hosts = settings.get("lns_client.allow_dynamic_lns_hosts")
lns_server_id = settings.get("lns_client.lns_server")
function load_cache() function _load_cache()
if fs.exists("lns_cache.db") then if fs.exists("lns_cache.db") then
db = fs.open("lns_cache.db", "r") db = fs.open("lns_cache.db", "r")
db_contents = db.readAll() db_contents = db.readAll()
@ -28,17 +23,15 @@ function load_cache()
end end
end end
function save_cache() function _save_cache()
db = fs.open("lns_cache.db", "w") db = fs.open("lns_cache.db", "w")
db.write(textutils.serialize(cache)) db.write(textutils.serialize(cache))
db.close() db.close()
end end
function lns_lookup(remote_hostname) function server()
local data = { local lns_server_id = settings.get("lns_client.lns_server")
["action"] = "lookup", local allow_dynamic_lns_hosts = settings.get("lns_client.allow_dynamic_lns_hosts")
["hostname"] = remote_hostname
}
if allow_dynamic_lns_hosts then if allow_dynamic_lns_hosts then
lns_server_id = rednet.lookup("lns", lns_server) lns_server_id = rednet.lookup("lns", lns_server)
@ -48,7 +41,22 @@ function lns_lookup(remote_hostname)
end end
end end
rednet.send(lns_server_id, data, "lns") return lns_server_id
end
function lookup(remote_hostname)
_load_cache()
if cache[remote_hostname] ~= nil then
return cache[remote_hostname]
end
local data = {
["action"] = "lookup",
["hostname"] = remote_hostname
}
rednet.send(server(), data, "lns")
while true do while true do
id, msg = rednet.receive("lns") id, msg = rednet.receive("lns")
@ -56,43 +64,16 @@ function lns_lookup(remote_hostname)
if msg == nil then if msg == nil then
return nil return nil
else else
cache[remote_hostname] = msg
_save_cache()
return msg return msg
end end
end end
end end
end end
function clear_cache()
local action = args[1]
if action == nil then
io.write("Usage: lns <action>\n")
return
end
if action == "lookup" then
load_cache()
local hostname = args[2]
if hostname == nil then
io.write("Usage: lns lookup <hostname>\n")
return
end
if cache[hostname] ~= nil then
io.write(cache[hostname])
return
end
local result = lns_lookup(hostname)
if result ~= nil then
io.write(result)
cache[hostname] = result
save_cache()
end
elseif action == "clear" then
cache = {} cache = {}
save_cache() _save_cache()
print("Cleared cache!") print("Cleared cache!")
end end

22
lib/lum.lua Normal file
View File

@ -0,0 +1,22 @@
local target_branch = "main"
local bin_path = "/bin/"
local lib_path = "/lib/"
local startup_path = "/startup/"
function download_lua(remote_path, local_path)
response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil)
if response.getResponseCode() == 200 then
file = fs.open(local_path, "w")
file.write(response.readAll())
file.close()
end
else
io.write("Failed to update " .. program_name .. ".lua\n")
io.write(response)
end
end
function install(package)
io.write("Installing " .. package .. "\n")
download_lua("packages/" .. package .. ".lua", bin_path .. package .. ".lua")
end

19
lum.lua Normal file
View File

@ -0,0 +1,19 @@
local args = { ... }
local action = args[1]
if action == nil then
print("Usage: lum <install:remove>")
return
end
if action == "install" then
package = args[2]
if package == nil then
print("Usage: lum install <package>")
return
end
lum.install(package)
io.write("Done!\n")

View File

@ -1,27 +0,0 @@
local args = { ... }
local target_branch = "main"
local bin_path = "/bin/"
local program_name = args[1]
if program_name == nil then
print("Usage: lupdate <program_name>")
return
end
response = http.get("https://gitea.layla.gg/layla/computercraft/raw/branch/" .. target_branch .. "/" .. program_name .. ".lua", nil)
io.write("Updating " .. program_name .. ".lua\n")
if response.getResponseCode() == 200 then
file = fs.open(bin_path .. program_name .. ".lua", "w")
file.write(response.readAll())
file.close()
io.write("Updated " .. program_name .. ".lua\n")
else
io.write("Failed to update " .. program_name .. ".lua\n")
io.write(response)
end

8
packages/dig.lua Normal file
View File

@ -0,0 +1,8 @@
local args = { ... }
if args[1] == nil then
print("Usage: dig <hostname>")
return
end
os.write(lns.lookup(args[1]) .. "\n")

View File

@ -5,15 +5,6 @@ hostname = io.read()
io.write("Enter the modem location: ") io.write("Enter the modem location: ")
modem_location = io.read() modem_location = io.read()
io.write("Use default LNS Server? (y/n): ")
use_default = io.read()
lns_server = "default"
if use_default == "n" then
io.write("Enter the LNS Server Rednet Hostname: ")
lns_server = io.read()
end
rednet.open(modem_location) rednet.open(modem_location)
receiverID = rednet.lookup("lns", lns_server) receiverID = rednet.lookup("lns", lns_server)
current_time = os.time() current_time = os.time()

View File

@ -1,3 +1,12 @@
local bin_path = "/bin/" local bin_path = "/bin/"
shell.setPath(shell.path() .. ":" .. bin_path) local lib_path = "/lib/"
shell.setPath(shell.path() .. ":" .. bin_path)
-- Load libraries
for _, file in ipairs(fs.list(lib_path)) do
if not fs.isDir(lib_path .. file) then
os.loadAPI(lib_path .. file)
end
end