Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shell.setPath(shell.path() .. ":/bin")
- rednet.open("right")
- print("DNS server started")
- local dnsTable = {}
- local function saveToFile(filename)
- local file = fs.open(filename, 'w')
- if file then
- file.write(textutils.serialize(dnsTable))
- file.close()
- print("DNS table saved to " .. filename)
- else
- print("Error: unable to open file for saving")
- end
- end
- local function loadFromFile(filename)
- if fs.exists(filename) then
- local file = fs.open(filename, 'r')
- if file then
- local content = file.readAll()
- dnsTable = textutils.unserialise(content) or {}
- file.close()
- print("DNS table loaded from " .. filename)
- else
- print("Error: Unable to open file for loading")
- end
- else
- print("No existing DNS table found, starting fresh")
- end
- end
- local function registerComputer(name, id)
- dnsTable[id] = name
- print("Registation successful")
- print("PC: " .. name .. " ID: " .. id)
- saveToFile("dns_table.txt")
- return true
- end
- local function getAllComputers()
- return dnsTable
- end
- loadFromFile("dns_table.txt")
- while true do
- local senderID, message, protocol = rednet.receive()
- print("Something query")
- if type(message) == "string" then
- local data = textutils.unserialize(message)
- if protocol == "dns_register" then
- if data and data.name and data.id then
- registerComputer(data.name, data.id)
- rednet.send(senderID, "Registation has been successful", "dns_ack")
- end
- elseif protocol == "dns_list" then
- print("Protocol dns_list")
- local allComputers = getAllComputers()
- local listMessage = textutils.serialize(allComputers)
- rednet.send(senderID, listMessage, "dns_list_response")
- end
- elseif type(message) == "table" then
- if protocol == "log" then
- print("Protocol log")
- local file = fs.open("teleport.log", "a")
- if file then
- file.write(senderID .. ";" ..message.id .. ";" .. message.name .. "\n")
- file.close()
- else
- print("Something strange with file")
- end
- end
- else
- rednet.send(senderID, "something strange", "dns_list_response")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment