Advertisement
abstract_abstract

dns_server.lua

Oct 7th, 2024 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.11 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.     dnsTable[name] = id
  9.     print("Registation successful")
  10.     print("PC: " .. name .. " ID: " .. id)
  11. end
  12.  
  13.  
  14. local function isNameRegistered(name)
  15.     return dnsTable[name] ~= nil
  16. end
  17.  
  18.  
  19. local function getComputerID(name)
  20.     return dnsTable[name]
  21. end
  22.  
  23.  
  24. local function getAllComputers()
  25.     return dnsTable
  26. end
  27.  
  28.  
  29. while true do
  30.     local senderID, message, protocol = rednet.receive()  
  31.     local data = textutils.unserialise(message)
  32.    
  33.     if protocol == "dns_register" then
  34.         if data and data.name and data.id then
  35.             registerComputer(data.name, data.id)
  36.             rednet.send(senderID, "Registation has been successful", "dns_ack")
  37.         else
  38.             rednet.send(SenderID, "Registration fail", "dns_ack")
  39.         end    
  40.     elseif protocol == "dns_list" then
  41.         local allComputers = getAllComputers()
  42.         local listMessage = textutils.serialize(allComputers)
  43.         rednet.send(senderID, listMessage, "dns_list_response")
  44.     end    
  45. end
  46.  
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement