Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local modem = component.modem
- local keyboard = require("keyboard")
- local DNSAddress = "9955471e-b28f-4001-9d66-79c28c75be2d"
- local getGUIDPort = 60
- local addComputerPort = 50
- --local pullArray = {}
- --local port = 1 --TEST VALUE, PUT SOMETHING HERE
- --local timeout = 15
- function OpenModem(port)
- modem.open(port)
- if modem.isOpen(port) then
- print("Successfully opened port " .. port)
- else
- print("Could not open port " .. port .. ", closing server")
- end
- end
- function Quit()
- for i = 1,100 do
- if modem.isOpen(i) then
- modem.close(i)
- print("Closing port " .. i)
- end
- i = i + 1
- end
- os.exit()
- end
- function EventPull(timeout)
- for i=1,10 do
- local type, _, foreignAddress, port, distance, message = event.pull(timeout)
- local pullArray = {}
- if type == "key_down" then
- if keyboard.isAltDown() then
- Quit()
- end
- elseif type == "modem_message" then
- pullArray[1] = type
- pullArray[2] = foreignAddress
- pullArray[3] = port
- pullArray[4] = distance
- pullArray[5] = message
- print("Message has been received from DNS on port " .. port)
- end
- if pullArray[1] ~= nil then
- return pullArray
- end
- i = i + 1
- end
- end
- function SendACK(remoteAddress, port)
- modem.send(remoteAddress, port, "ACK")
- print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
- end
- local function SendMessage(remoteAddress, port, message, timeout)
- local timeoutPull = {}
- while true do
- modem.send(remoteAddress, port, message)
- timeoutPull = EventPull(timeout)
- if timeoutPull[1] ~= nil then
- break
- end
- end
- end
- function ReadFile(filePath)
- local file = io.open(filePath, "r")
- local fileSize, _ = file:seek("end")
- file:seek("set")
- local fileString = file:read(fileSize)
- file:close()
- return fileString
- end
- function SaveFile(name, message)
- local filePath = "/home/" .. name
- local file = io.open(filePath, "w")
- file:write(message)
- file:close()
- print("File has been saved with path " .. filePath)
- end
- function GetGUID(name, timeout)
- SendMessage(DNSAddress, getGUIDPort, name, timeout)
- local GUIDArray = {}
- GUIDArray = EventPull(timeout)
- SendACK(GUIDArray[2], GUIDArray[3])
- SaveFile(name, GUIDArray[5])
- return GUIDArray[5]
- end
- function AddComputer(name, timeout)
- SendMessage(DNSAddress, addComputerPort, name, timeout)
- local check = GetGUID(name, timeout)
- for k,v in component.list(modem) do
- local address = k
- end
- if check == address then
- print("Computer has been added to the DNS under the name " .. name)
- else
- print("Error while adding computer, contact admin.")
- end
Add Comment
Please, Sign In to add comment