Advertisement
alexhorner

Network Management Server

Jun 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. --DevelopedLogic's Network Management Server (NMS)
  2. --This is the equivalent of a real DNS server, however may become much more
  3.  
  4. os.loadAPI("/dniconf")
  5. modemSide = dniconf.modemSide
  6. DNIPort = dniconf.DNIPort
  7. networkName = dniconf.networkName
  8.  
  9. modem = peripheral.wrap(modemSide)
  10.  
  11. if modem.isWireless() then
  12.     error("YOU MAY NOT USE NMS ON A WIRELESS NETWORK AS TOO MANY MODEM MESSAGES PASS BY!")
  13. end
  14.  
  15. if not fs.exists("/domains") then
  16.     fs.makeDir("/domains")
  17.     print("Created domain directory")
  18. end
  19.  
  20. if not fs.exists("/ids") then
  21.     fs.makeDir("/ids")
  22.     print("Created id directory")
  23. end
  24.  
  25. termW, termH = term.getSize()
  26.  
  27. term.setPaletteColor(colours.blue, 0, 0, 1)
  28.  
  29. term.clear()
  30. term.setCursorPos(1,1)
  31.  
  32. topBar = window.create(term.current(), 1, 1, termW, 1, true)
  33. bottomBar = window.create(term.current(), 1, termH, termW, 1, true)
  34. centre = window.create(term.current(), 1, 2, termW, termH-2, true)
  35.  
  36. topBar.setBackgroundColour(colours.lightGrey)
  37. topBar.setTextColour(colours.blue)
  38. topBar.clear()
  39. bottomBar.setTextColour(colours.blue)
  40. bottomBar.setBackgroundColour(colours.lightGrey)
  41. bottomBar.clear()
  42. centre.setTextColour(colours.lightGrey)
  43. centre.setBackgroundColour(colours.blue)
  44. centre.clear()
  45.  
  46. topBar.setCursorPos(1,1)
  47. topBar.write("Network Management Server - "..networkName)
  48. bottomBar.setCursorPos(1,1)
  49. bottomBar.write("Run nmsconfig to manage domains")
  50.  
  51. term.redirect(centre)
  52.  
  53. modem.open(DNIPort)
  54.  
  55. function stok(data)
  56.     if type(data) == "string" then
  57.         return true
  58.     else
  59.         return false
  60.     end
  61. end
  62.  
  63. function numok(data)
  64.     if type(data) == "number" then
  65.         return true
  66.     else
  67.         return false
  68.     end
  69. end
  70.  
  71. function fail(gotoid)
  72.     print("Fail from "..gotoid)
  73.     print()
  74.     data = {gotoid, "failed"}
  75.     modem.transmit(DNIPort, DNIPort, textutils.serialise(data))
  76. end
  77.  
  78. function pass(val, gotoid)
  79.     print("Pass from "..gotoid)
  80.     print()
  81.     data = {gotoid, val}
  82.     modem.transmit(DNIPort, DNIPort, textutils.serialise(data))
  83. end
  84.  
  85. function getdomain(id, goto)
  86.     print(goto.." Requested "..id)
  87.     if not fs.exists("/domains/"..id) then
  88.         fail(goto)
  89.     else
  90.         file = fs.open("/domains/"..id, "r")
  91.         pass(file.readLine(), goto)
  92.         file.close()
  93.     end
  94. end
  95.  
  96. function getid(domain, goto)
  97.     print(goto.." Requested "..domain)
  98.     if not fs.exists("/ids/"..domain) then
  99.         fail(goto)
  100.     else
  101.         file = fs.open("/ids/"..domain, "r")
  102.         pass(file.readLine(), goto)
  103.         file.close()
  104.     end
  105. end
  106.  
  107. function run(msg)
  108.     data = textutils.unserialise(msg)
  109.     if data ~= nil and #data == 4 and stok(data[1]) and stok(data[2]) and stok(data[3]) and numok(data[4]) and data[1] == "nmsreq" then
  110.         if data[2] == "domain" then
  111.             getdomain(data[3], data[4])
  112.         elseif data[2] == "id" then
  113.             getid(data[3], data[4])
  114.         end
  115.     end
  116. end
  117.  
  118. while true do
  119.     message = { os.pullEvent("modem_message") }
  120.     run(message[5])
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement