Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("right")
- print("DNS server started")
- local dnsTable = {}
- 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)
- return true
- end
- local function getComputerID(name)
- return dnsTable[name]
- end
- local function getAllComputers()
- return dnsTable
- end
- 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