From 695c2224a56ffbaa8f4a75c8cec7b89b43e108d0 Mon Sep 17 00:00:00 2001 From: Layla Manley Date: Sat, 7 Oct 2023 23:36:37 -0400 Subject: [PATCH] iteration --- lns_server.lua | 19 +++++++++++-------- register_lns.lua | 9 ++++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lns_server.lua b/lns_server.lua index 6549dfb..f403186 100644 --- a/lns_server.lua +++ b/lns_server.lua @@ -38,27 +38,30 @@ load_data() while true do client_id, msg = rednet.receive() + request = textutils.unserialize(msg) - -- split string msg into action and hostname with ':' as a delimiter using string.unpack - local action, hostname = split(msg, ":") + if request.action == nil or request.hostname == nil then + rednet.send(client_id, "invalid request") + return + end - if action == "lookup" then + if request.action == "lookup" then -- check if hostname in data - if data[hostname] == nil then + if request.hostname == nil then rednet.send(client_id, "not found") else - rednet.send(client_id, data[hostname]) + rednet.send(client_id, data[request.hostname]) end end - if action == "reload" then + if request.action == "reload" then load_data() rednet.send(client_id, "ok") end - if action == "register" then - data[hostname] = client_id + if request.action == "register" then + data[request.hostname] = client_id save_data() rednet.send(client_id, "ok") end diff --git a/register_lns.lua b/register_lns.lua index 90a3b39..1fdb2af 100644 --- a/register_lns.lua +++ b/register_lns.lua @@ -17,7 +17,14 @@ end rednet.open(modem_location) receiverID = rednet.lookup("lns", lns_server) current_time = os.time() -rednet.send(receiverID, "register:" .. hostname) + + +data = { + [action] = "register", + [hostname] = hostname +} + +rednet.send(receiverID, textutils.serialize(data)) while true do id, msg = rednet.receive()