Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------
- --[ Global varibales ]--
- -----------------------------
- local protocolRequest = "FTP_request"
- local protocolData = "FTP_data"
- local protocolArrived = "FTP_arrived"
- local messageContinue = "continue"
- local messageStop = "stop"
- local args = {...}
- local debug = true
- local modemSide
- -----------------------------
- --[ Utility Functions ]--
- -----------------------------
- local function getWirelessModemSide()
- local sides = rs.getSides()
- local side = nil
- for i = 1, #sides do
- if peripheral.getType(sides[i]) == "modem" then
- if peripheral.wrap(sides[i]).isWireless() then
- side = sides[i]
- end
- end
- end
- return side
- end
- -----------------------------
- --[ Main Functions ]--
- -----------------------------
- local function send(targetID, filePath, code)
- -- Checks the targetID variable
- if tonumber(targetID) == nil then
- if debug then print("The ID argument must be a number.") end
- return
- else
- if tonumber(targetID) < 0 then
- if debug then print("The ID argument cannot be lower than 0.") end
- return
- end
- end
- -- Checks the filePath variable
- if not fs.exists(filePath) then
- if debug then print("The file path you specified does not exist.") end
- return
- end
- local file = fs.open(filePath, "r")
- local fileContent = file.readAll()
- file.close()
- -- Sends a request and waits for confirmation of arrival
- local senderID, message, protocol = nil
- local i = 0
- while senderID == nil and i < 10 do
- rednet.send(tonumber(targetID), code, protocolRequest)
- senderID, message, protocol = rednet.receive(protocolArrived, 1)
- i = i + 1
- end
- if senderID == nil and i == 10 then
- if debug then print("We didn't receive a response, not sending any data.") end
- return
- elseif message == messageStop then
- if debug then print("The codes didn't match, not sending any data.") end
- return
- end
- -- Sends the file content and waits for confirmation of arrival
- local senderID, message, protocol = nil
- local i = 0
- while senderID == nil and i < 10 do
- rednet.send(tonumber(targetID), fileContent, protocolData)
- senderID, message, protocol = rednet.receive(protocolArrived, 1)
- i = i + 1
- end
- if i == 10 then
- if debug then print("We didn't receive a response, the data may have not arrived.") end
- return
- end
- end
- local function receive(time, code)
- -- Checks the time variable
- if tonumber(time) == nil then
- if debug then print("The time argument must be a number.") end
- return
- else
- if tonumber(time) < 0 then
- if debug then print("The time argument cannot be lower than 0.") end
- return
- end
- end
- -- Starts the timer
- local timerID
- if tonumber(time) > 0 then
- timerID = os.startTimer(tonumber(time))
- end
- local fileCount = 0
- while true do
- -- Waits for the next event
- local event, p1, p2, p3 = os.pullEvent()
- if event == "rednet_message" and p3 == protocolRequest then
- if p2 == code then
- -- Sends a message to confirm arrival
- rednet.send(p1, messageContinue, protocolArrived)
- -- Waits for the file content, saves it to a file (if the codes match) and sends a message to confirm arrival
- local senderID, message, protocol = rednet.receive(protocolData, 10)
- rednet.send(senderID, messageStop, protocolArrived)
- local file = fs.open("download" .. fileCount, "w")
- file.write(message)
- file.close()
- if debug then print("A file from computer " .. senderID .. " arrived and was saved in the path \"download" .. fileCount .. "\".") end
- fileCount = fileCount + 1
- else
- -- Sends a message to confirm arrival
- rednet.send(p1, messageStop, protocolArrived)
- if debug then print("A file from computer " .. p1 .. " arrived and was discarded, because the codes didn't match.") end
- end
- elseif event == "key" then
- -- Stops receiving files if the user presses any button
- if debug then print("The user pressed a key, stopped receiving files.") end
- return
- elseif event == "timer" and p1 == timerID then
- -- Stops receiving files if the timer went off
- if debug then print("The time has passed, stopped receiving files.") end
- return
- end
- end
- end
- -----------------------------
- --[ Program ]--
- -----------------------------
- -- Opens a wireless modem
- modemSide = getWirelessModemSide()
- if modemSide == nil then
- if debug then print("Please attach a wireless modem to this computer.") end
- return
- else
- rednet.open(modemSide)
- end
- -- Runs main program
- if args[1] == "send" and args[2] ~= nil and args[3] ~= nil and args[4] ~= nil then
- send(args[2], args[3], args[4])
- elseif args[1] == "send" and args[2] ~= nil and args[3] ~= nil then
- send(args[2], args[3], "")
- elseif args[1] == "receive" and args[2] ~= nil and args[3] ~= nil then
- receive(args[2], args[3])
- elseif args[1] == "receive" and args[2] ~= nil then
- receive(args[2], "")
- else
- print("Usage (<> required, [] optional):")
- print(" FTP send <ID> <file path> [code]")
- print(" sends <file path> to <ID> (with [code])")
- print(" FTP receive <time> [code]")
- print(" receives files for <time> in seconds and saves")
- print(" them (if they are sent with the right [code])")
- print(" (set <time> to 0 for indefinite file receiving)")
- end
- -- Closes the wireless modem
- rednet.close(modemSide)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement