commit c2cef57703416abb710e5205b8806189c2e3dff4 Author: Layla Manley Date: Sat Oct 7 22:57:23 2023 -0400 iteration diff --git a/lupdate.lua b/lupdate.lua new file mode 100644 index 0000000..1fca2c6 --- /dev/null +++ b/lupdate.lua @@ -0,0 +1,19 @@ +local args = { ... } + +local program_name = args[1] +if program_name == nil then + print("Usage: lupdate ") + return +end + +http.get("https://raw.githubusercontent.com/justync7/cc-programs/master/" .. program_name .. ".lua", nil, function(response) + if response.getResponseCode() == 200 then + local file = fs.open(program_name .. ".lua", "w") + file.write(response.readAll()) + file.close() + print("Updated " .. program_name .. ".lua") + else + print("Failed to update " .. program_name .. ".lua") + end +end + diff --git a/messageboard/server.lua b/messageboard/server.lua new file mode 100644 index 0000000..51d28f8 --- /dev/null +++ b/messageboard/server.lua @@ -0,0 +1,11 @@ +rednet + +rednet.open("back") +while true do + id, msg = rednet.receive() + if msg == "get" then + rednet.send(id, message) + elseif msg == "set" then + id, msg = rednet.receive() + message = msg + end \ No newline at end of file diff --git a/name_sever/lns.lua b/name_sever/lns.lua new file mode 100644 index 0000000..791b971 --- /dev/null +++ b/name_sever/lns.lua @@ -0,0 +1,65 @@ + +modem_location = "back" + + +rednet.host("lns", "default") +rednet.open(modem_location) + +local data = {} + +function save_data() + db = fs.open("lns.db", "w") + db.write(textutils.serialize(data)) + db.close() +end + +function load_data() + if fs.exists("lns.db") then + db = fs.open("lns.db", "r") + db_contents = db.readAll() + db.close() + + data = textutils.unserialize(db_contents) + end +end + +function split (inputstr, sep) + if sep == nil then + sep = "%s" + end + local t={} + for str in string.gmatch(inputstr, "([^"..sep.."]+)") do + table.insert(t, str) + end + return t + end + +load_data() + +while true do + client_id, msg = rednet.receive() + + -- split string msg into action and hostname with ':' as a delimiter using string.unpack + local action, hostname = split(msg, ":") + + if action == "lookup" then + + -- check if hostname in data + if data[hostname] == nil then + rednet.send(client_id, "not found") + else + rednet.send(client_id, data[hostname]) + end + end + + if action == "reload" then + load_data() + rednet.send(client_id, "ok") + end + + if action == "register" then + data[hostname] = client_id + save_data() + rednet.send(client_id, "ok") + end +end \ No newline at end of file diff --git a/name_sever/register_lns.lua b/name_sever/register_lns.lua new file mode 100644 index 0000000..90a3b39 --- /dev/null +++ b/name_sever/register_lns.lua @@ -0,0 +1,34 @@ + +io.write("Enter the hostname: ") +hostname = io.read() + +io.write("Enter the modem location: ") +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) +receiverID = rednet.lookup("lns", lns_server) +current_time = os.time() +rednet.send(receiverID, "register:" .. hostname) + +while true do + id, msg = rednet.receive() + if msg == "ok" then + print("Registered with LNS Server Successfully!") + break + end + + -- timeout after 5 seconds + if os.time() - current_time > 5 then + print("Failed to register with LNS Server! Timeout!") + break + end +end \ No newline at end of file