Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local fs = require("filesystem")
- local modem = component.modem
- local keyboard = require("keyboard")
- local serverAddress = "9955471e-b28f-4001-9d66-79c28c75be2d"
- local modemStrength = 5000
- local fileName
- local computerName
- local sendSignal
- local serverResponse
- local GUID
- local chosenPort = 42
- local addComputerPort = 50
- local retrieveGUIDPort = 60
- local timeOut = 15
- local firstArray = {}
- local secondArray = {}
- local decision
- local filePath = "/home/"
- local function OpenModem(port1, port2, port3, strength)
- modem.open(port1)
- modem.open(port2)
- modem.open(port3)
- modem.setStrength(strength)
- if modem.isOpen(port1) and modem.isOpen(port2) and modem.isOpen(port3) then
- print("Ports have been opened.")
- else
- print("Could not open ports, closing program.")
- end
- end
- local function Quit()
- modem.close(42)
- modem.close(50)
- modem.close(60)
- if modem.isOpen(42) == false and modem.isOpen(50) == false and modem.isOpen(60) == false then
- print("Ports have been closed. Exiting program.")
- os.exit()
- else
- print("Could not close ports.")
- os.exit()
- end
- end
- local function EventPull(timeOut)
- 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 recieved from DNS on port " .. port)
- end
- return pullArray
- end
- local function SendACK(remoteAddress, port)
- modem.send(remoteAddress, port, "ACK")
- print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
- end
- local function SendTimeout(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
- local function AddComputer(name, serverAddress, port, timeOut)
- SendTimeout(serverAddress, port, name, timeOut)
- print("Computer has been successfully added to the DNS under the name " .. name)
- end
- local function AddDashes(guid)
- return guid:sub(1,8) .. "-" .. guid:sub(9,12) .. "-" .. guid:sub(13,16) .. "-" .. guid:sub(17,20) .. "-" .. guid:sub(21,34)
- end
- local function RemoveDashes(guid)
- return guid:sub(1,8) .. guid:sub(10,13) .. guid:sub(15,18) .. guid:sub(20,23) .. guid:sub(25,38)
- end
- local function SaveID(GUID, filePath, fileName)
- file = io.open(filePath .. fileName, "w")
- file:write(GUID)
- file:close()
- print("GUID has been saved to " .. filePath .. fileName)
- end
- OpenModem(chosenPort, addComputerPort, retrieveGUIDPort, modemStrength)
- print("Press ALT to exit.")
- while true do
- print("Enter '1' to get a GUID or '2' to add this computer to the DNS.")
- decision = tostring(io.read())
- os.sleep(1)
- if decision == "1" then
- print("Enter the computer's name of the GUID you desire. The format is Name#, such as Matt1 or Luke2.")
- fileName = io.read()
- os.sleep(1)
- SendTimeout(serverAddress, retrieveGUIDPort, fileName, timeOut)
- firstArray = EventPull(timeOut)
- SendACK(serverAddress, retrieveGUIDPort)
- if tostring(firstArray[5]) ~= "500" then
- GUID = firstArray[5]
- print("The GUID of the requested computer is...")
- print(GUID)
- SaveID(GUID, filePath, fileName)
- Quit()
- else
- print(fileName .. " does not exist as typed in the DNS server.")
- end
- elseif decision == "2" then
- print("Type the name you want to give this computer. It should be your name with a number at the end, such as Luke1 or Luke2")
- computerName = io.read()
- os.sleep(1)
- AddComputer(computerName, serverAddress, addComputerPort, timeOut)
- Quit()
- else
- print("Not a decision, closing program.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement