Advertisement
sammie287

PowergridClientSide.lua

Aug 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 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 pullArray = {}
  6. local DNSAddress = "9955471e-b28f-4001-9d66-79c28c75be2d"
  7. local address = "0190b137-6ed4-421c-89f0-755193b71c7f"
  8. local copyArray = {}
  9.  
  10. local port = 70
  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.   modem.close(70)
  24.   if modem.isOpen(70) == false then
  25.     print("Successfully closed port 70, closing program...")
  26.   else
  27.     print("Could not close port 70, please contact admin.")
  28.   end
  29.   os.exit()
  30. end
  31.  
  32. function EventPull(timeout)
  33.   local type, _, foreignAddress, port, distance, message = event.pull(timeout)
  34.   local pullArray = {}
  35.   if type == "key_down" then
  36.     if keyboard.isAltDown() then
  37.       Quit()
  38.     end
  39.   elseif type == "modem_message" then
  40.     pullArray[1] = type
  41.     pullArray[2] = foreignAddress
  42.     pullArray[3] = port
  43.     pullArray[4] = distance
  44.     pullArray[5] = message
  45.     --print("Message has been received from DNS on port " .. port)
  46.   end
  47.   return pullArray
  48. end
  49.  
  50. function SendACK(remoteAddress, port)
  51.   modem.send(remoteAddress, port, "ACK")
  52.   --print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
  53. end
  54.  
  55. local function SendTimeout(remoteAddress, port, message, timeout)
  56.   local timeoutPull = {}
  57.   while true do
  58.     modem.send(remoteAddress, port, message)
  59.     timeoutPull = EventPull(timeout)
  60.     if timeoutPull[1] ~= nil then
  61.       break
  62.     end
  63.   end
  64. end
  65.  
  66. OpenModem(port)
  67. while true do
  68.   pullArray = EventPull(timeout)
  69.   if pullArray[1] == "modem_message" then
  70.     SendACK(pullArray[2], pullArray[3])
  71.     if copyArray[1] ~= pullArray[5] then
  72.       print(pullArray[5])
  73.     end
  74.     copyArray[1] = pullArray[5]
  75.     pullArray = {}
  76.   end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement