Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("right")
- print("DNS server started")
- local dnsTable = {}
- local function saveToFile(fulename)
- 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 loadlFromFile(filename)
- if fs.exists(filename) then
- local file = fs.open(fileopen, '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)
- if dnsTable[name] then
- print("Registration failed: Name already registred")
- return false
- end
- dnsTable[name] = id
- 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()
- local data = textutils.unserialise(message)
- if protocol == "dns_register" then
- if data and data.name and data.id then
- if registerComputer(data.name, data.id) then
- rednet.send(senderID, "Registation has been successful", "dns_ack")
- else
- rednet.send(senderID, "Registration failed: Name already registered", "dns_ack")
- end
- end
- elseif protocol == "dns_list" then
- local allComputers = getAllComputers()
- local listMessage = textutils.serialize(allComputers)
- rednet.send(senderID, listMessage, "dns_list_response")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement