76 lines
1.6 KiB
Lua
76 lines
1.6 KiB
Lua
|
|
modem_location = "right"
|
|
|
|
|
|
rednet.host("lns", "default")
|
|
rednet.open(modem_location)
|
|
|
|
local data = {}
|
|
|
|
|
|
function log(str)
|
|
io.write("[" .. os.time() .. "] " .. str .. "\n")
|
|
end
|
|
|
|
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()
|
|
request = msg
|
|
|
|
if request.action == nil or request.hostname == nil then
|
|
rednet.send(client_id, "invalid request")
|
|
end
|
|
|
|
if request.action == "lookup" then
|
|
|
|
-- check if hostname in data
|
|
if request.hostname == nil then
|
|
rednet.send(client_id, nil)
|
|
log("Sent nil to " .. client_id)
|
|
else
|
|
rednet.send(client_id, data[request.hostname])
|
|
log("Sent " .. request.hostname .. " to " .. client_id)
|
|
end
|
|
end
|
|
|
|
if request.action == "reload" then
|
|
load_data()
|
|
rednet.send(client_id, "ok")
|
|
log("Reloaded data")
|
|
end
|
|
|
|
if request.action == "register" then
|
|
data[request.hostname] = client_id
|
|
log("Registered " .. request.hostname .. " as " .. client_id)
|
|
save_data()
|
|
rednet.send(client_id, "ok")
|
|
end
|
|
end |