Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local fs = require("filesystem")
- local term = require("term")
- local md = component.modem
- local event = require("event")
- --0 for send 1 for receive
- ftpMode = 0
- discoveryAddr = "DIS"
- ftpAddr = "FTP"
- ftpPort = 0
- term.clear()
- --file send function
- function sendFile(filename,dest,port)
- local numPackets = 1
- local filepath = ("/home/ftpDir/" .. tostring(filename))
- file = io.open(filepath,"r")
- local filesize = fs.size(filepath)
- md.send(dest,port,"File Incoming")
- os.sleep(0.5)
- md.send(dest,port,filename)
- print("Sending file")
- local contents = file : read(filesize)
- md.send(dest,port,contents)
- file : close()
- print("File sent!")
- end
- --file receive function
- function receiveFile()
- local numPackets = 1
- local _,_,from,port,_,message = event.pull("modem_message")
- local filename = message
- print("Received filename: "..filename)
- local filePath = ("/home/ftpDir/" .. filename)
- local fileContents = ""
- print("File Receiving: 0/"..numPackets)
- local _,_,from,port,_,message = event.pull("modem_message")
- fileContents = (fileContents .. tostring(message))
- print("Received 1/"..numPackets)
- file = io.open(filePath,"w")
- print("Writing contents")
- file : write(fileContents)
- file : close()
- end
- --Open discovery port
- print("OPEN DISCOVERY")
- md.open(1)
- --get DISCOVERY ADDRESS
- print("GET DISCOVERY")
- md.broadcast(1,"DISCOVERY")
- repeat
- local _,_,from,port,_,message = event.pull("modem_message")
- message = tostring(message)
- from = tostring(from)
- discoveryAddr = from
- until(message == "DISCOVERY_RECEIVE")
- print("GOT DISCOVERY: " .. discoveryAddr)
- --Get FTP info from DISCOVERY
- print("GET FTP INFO")
- md.send(discoveryAddr, 1, "FTP_INFO")
- local _,_,from,port,_,message = event.pull("modem_message")
- print("Receiving ".. tostring(message) .. " packets.")
- local _,_,from,port,_,message = event.pull("modem_message")
- ftpPort = tonumber(message)
- if ftpPort == nil then
- print("No FTP Port registered!")
- else
- print("GOT FTP PORT: "..ftpPort)
- end
- local _,_,from,port,_,message = event.pull("modem_message")
- ftpAddr = tostring(message)
- if ftpPort == nil then
- print("No FTP Address registered!")
- else
- print("GOT FTP ADDRESS: " .. ftpAddr)
- end
- md.send(discoveryAddr,1,"FTP_LOG")
- --Open FTP Port and Close DISCOVERY
- md.close(1)
- md.open(ftpPort)
- --check for /home/ftp/
- dExists = fs.isDirectory("/home/ftpDir/")
- if dExists == true then
- print("/home/ftp exists!")
- end
- if dExists == false then
- print("Creating directory at /home/ftpDir/ ...")
- fs.makeDirectory("/home/ftpDir/")
- print("Done!")
- end
- --Begin main part
- term.clear()
- print("INIT COMPLETE!")
- term.write("Send or Receive >")
- term.setCursor(18,2)
- term.setCursorBlink(true)
- mode = tostring(io.read())
- term.setCursorBlink(false)
- if (mode == "send") or (mode == "Send") or (mode == "s") then
- ftpMode = 0
- elseif (mode == "receive") or (mode == "Receive") or (mode == "r") then
- ftpMode = 1
- else
- print("Illegal input!")
- os.sleep(1)
- os.exit()
- end
- if ftpMode == 0 then
- print("Possible files:")
- for file in fs.list("/home/ftpDir/") do
- print(file)
- if file == nil then break end
- end
- print("")
- print("Select file:")
- fileSelect = io.read()
- md.broadcast(ftpPort,"SEND_REQUEST")
- local _,_,from,port,_,message = event.pull("modem_message")
- if message == "Accept" then
- print("Request Accept at "..from)
- sendFile(fileSelect,from,ftpPort,1)
- end
- end
- if ftpMode == 1 then
- print("Receive files:")
- local _,_,from,port,_,message = event.pull("modem_message")
- message = tostring(message)
- if message == "SEND_REQUEST" then
- print("Send Request incoming")
- print("Press Y to accept")
- local wasYPressed = io.read()
- if (wasYPressed == "y") or (wasYPressed == "Y") then
- md.send(from,port,"Accept")
- local _,_,from,port,_,message = event.pull("modem_message")
- message = tostring(message)
- if message == "File Incoming" then receiveFile() end
- print("Done!")
- end
- end
- end
- md.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement