abstract_abstract

dns_server.lua

Oct 8th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. print("DNS server started")
  4.  
  5. local dnsTable = {}
  6.  
  7. local function registerComputer(name, id)
  8.     if dnsTable[name] then
  9.         print("Registration failed: Name already registred")
  10.         return false
  11.     end
  12.    
  13.     dnsTable[name] = id
  14.     print("Registation successful")
  15.     print("PC: " .. name .. " ID: " .. id)
  16.  
  17.     return true
  18. end
  19.  
  20.  
  21. local function getComputerID(name)
  22.     return dnsTable[name]
  23. end
  24.  
  25.  
  26. local function getAllComputers()
  27.     return dnsTable
  28. end
  29.  
  30.  
  31. while true do
  32.     local senderID, message, protocol = rednet.receive()  
  33.     local data = textutils.unserialise(message)
  34.    
  35.     if protocol == "dns_register" then
  36.         if data and data.name and data.id then
  37.             if registerComputer(data.name, data.id) then
  38.                 rednet.send(senderID, "Registation has been successful", "dns_ack")
  39.             else
  40.                 rednet.send(senderID, "Registration failed: Name already registered", "dns_ack")
  41.             end
  42.         end    
  43.     elseif protocol == "dns_list" then
  44.         local allComputers = getAllComputers()
  45.         local listMessage = textutils.serialize(allComputers)
  46.         rednet.send(senderID, listMessage, "dns_list_response")
  47.     end    
  48. end
  49.  
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment