Advertisement
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 homePath = "/home/"
- local messageArray = {}
- local brokenMessageArray = {}
- local maxSize
- local port = 21
- local timeout = 5
- 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()
- modem.close(21)
- if modem.isOpen(21) == false then
- print("Successfully closed port 21, closing program...")
- else
- print("Could not close port 21, please contact admin.")
- 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)
- --return pullArray
- 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 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 OpenFile(fileName)
- local path = homePath .. fileName
- local file = io.open(path, "r")
- local fileSize, _ = file:seek("end")
- file:seek("set")
- fileString = file:read(fileSize)
- file:close()
- return fileString
- end
- local function PacketBreak(inputString)
- local tempArray = {}
- tempArray[3] = "This is to stop stupid LUA errors"
- maxSize = (modem.maxPacketSize()) - 2 --subtraction of 2 is to account for overhead of message
- stringLength = string.len(inputString)
- if stringLength > maxSize then
- print("File is too long, splitting into multiple files...")
- stringOne = string.sub(inputString, 1, math.floor(stringLength/2))
- stringTwo = string.sub(inputString, (math.floor(stringLength/2)) + 1, stringLength)
- tempArray[1] = stringOne
- tempArray[2] = stringTwo
- end
- return tempArray
- end
- local args = {...} --send/recieve, who to send to, filename
- if args[1] == nil then error("Provide function of the program (send/recieve).") end
- if args[2] == nil then error("Provide who you are sending this file to or receive from.") end
- OpenModem(port)
- if args[1] == "send" then
- if args[3] == nil then error("Provide the file name.") end
- print("Preparing to send file...")
- local sendString = OpenFile(args[3])
- if sendString == nil then
- print("The target file is empty. Choose another.")
- Quit()
- end
- brokenMessageArray = PacketBreak(sendString)
- if brokenMessageArray[1] == nil then
- SendTimeout(OpenFile(args[2]), port, sendString, timeout)
- print("File sent to " .. args[2] .. " over port " .. port)
- SendTimeout(OpenFile(args[2]), port, "DONE", timeout)
- else
- for i, loopString in ipairs(brokenMessageArray) do
- SendTimeout(OpenFile(args[2]), port, loopString, timeout)
- end
- SendTimeout(OpenFile(args[2]), port, "DONE", timeout)
- end
- elseif args[1] == "receive" then
- if args[3] == nil then error("Provide a name for the file.") end
- print("Preparing to receive file at path " .. args[3])
- messageArray = EventPull(timeout)
- SendACK(messageArray[2], messageArray[3])
- local receivedFile = io.open(homePath .. args[3], "w")
- receivedFile:write(messageArray[5])
- receivedFile:flush()
- receivedFile:close()
- local finished = false
- while finished == false do
- messageArray = EventPull(timeout)
- SendACK(messageArray[2], messageArray[3])
- if messageArray[5] == "DONE" then
- finished = true
- else
- local receivedFile = io.open(homePath .. args[3], "a")
- receivedFile:write(messageArray[5])
- receivedFile:flush()
- receivedFile:close()
- end
- end
- print("File " .. args[3] .. " has been saved with the path " .. homePath .. args[3])
- end
- Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement