sammie287

NetworkAPI.lua

Aug 16th, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local modem = component.modem
  4. local keyboard = require("keyboard")
  5. local DNSAddress = "9955471e-b28f-4001-9d66-79c28c75be2d"
  6. local getGUIDPort = 60
  7. local addComputerPort = 50
  8.  
  9. --local pullArray = {}
  10. --local port = 1  --TEST VALUE, PUT SOMETHING HERE
  11. --local timeout = 15
  12.  
  13. function OpenModem(port)
  14.   modem.open(port)
  15.   if modem.isOpen(port) then
  16.     print("Successfully opened port " .. port)
  17.   else
  18.     print("Could not open port " .. port .. ", closing server")
  19.   end
  20. end
  21.  
  22. function Quit()
  23.   for i = 1,100 do
  24.     if modem.isOpen(i) then
  25.       modem.close(i)
  26.       print("Closing port " .. i)
  27.     end
  28.     i = i + 1
  29.   end
  30.   os.exit()
  31. end
  32.  
  33. function EventPull(timeout)
  34.   for i=1,10 do
  35.     local type, _, foreignAddress, port, distance, message = event.pull(timeout)
  36.     local pullArray = {}
  37.     if type == "key_down" then
  38.       if keyboard.isAltDown() then
  39.         Quit()
  40.       end
  41.     elseif type == "modem_message" then
  42.       pullArray[1] = type
  43.       pullArray[2] = foreignAddress
  44.       pullArray[3] = port
  45.       pullArray[4] = distance
  46.       pullArray[5] = message
  47.       print("Message has been received from DNS on port " .. port)
  48.     end
  49.     if pullArray[1] ~= nil then
  50.       return pullArray
  51.     end
  52.     i = i + 1
  53.   end
  54. end
  55.  
  56. function SendACK(remoteAddress, port)
  57.   modem.send(remoteAddress, port, "ACK")
  58.   print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
  59. end
  60.  
  61. local function SendMessage(remoteAddress, port, message, timeout)
  62.   local timeoutPull = {}
  63.   while true do
  64.     modem.send(remoteAddress, port, message)
  65.     timeoutPull = EventPull(timeout)
  66.     if timeoutPull[1] ~= nil then
  67.       break
  68.     end
  69.   end
  70. end
  71.  
  72. function ReadFile(filePath)
  73.   local file = io.open(filePath, "r")
  74.   local fileSize, _ = file:seek("end")
  75.   file:seek("set")
  76.   local fileString = file:read(fileSize)
  77.   file:close()
  78.   return fileString
  79. end
  80.  
  81. function SaveFile(name, message)
  82.   local filePath = "/home/" .. name
  83.   local file = io.open(filePath, "w")
  84.   file:write(message)
  85.   file:close()
  86.   print("File has been saved with path " .. filePath)
  87. end
  88.  
  89. function GetGUID(name, timeout)
  90.   SendMessage(DNSAddress, getGUIDPort, name, timeout)
  91.   local GUIDArray = {}
  92.   GUIDArray = EventPull(timeout)
  93.   SendACK(GUIDArray[2], GUIDArray[3])
  94.   SaveFile(name, GUIDArray[5])
  95.   return GUIDArray[5]
  96. end
  97.  
  98. function AddComputer(name, timeout)
  99.   SendMessage(DNSAddress, addComputerPort, name, timeout)
  100.   local check = GetGUID(name, timeout)
  101.   for k,v in component.list(modem) do
  102.     local address = k
  103.   end
  104.   if check == address then
  105.     print("Computer has been added to the DNS under the name " .. name)
  106.   else
  107.     print("Error while adding computer, contact admin.")
  108. end
Add Comment
Please, Sign In to add comment