Skip to content

Commit

Permalink
type and retr commands implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
pReya committed Nov 23, 2016
1 parent 55343dd commit 3dbe74d
Showing 1 changed file with 147 additions and 1 deletion.
148 changes: 147 additions & 1 deletion src/ftpServer/Worker.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package ftpServer;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
Expand All @@ -11,6 +15,11 @@

public class Worker extends Thread
{

private enum transferType {
ASCII, BINARY
}

private enum userStatus {
NOTLOGGEDIN, ENTEREDUSERNAME, LOGGEDIN
}
Expand All @@ -32,6 +41,7 @@ private enum userStatus {
private Socket dataConnection;
private PrintWriter dataOutWriter;
private int dataPort = 1026;
private transferType transferMode = transferType.ASCII;


// user properly logged in?
Expand Down Expand Up @@ -171,7 +181,7 @@ private boolean executeCommand(String c) throws IOException
break;

case "RETR":
handleRetr();
handleRetr(args);
break;

case "MKD":
Expand All @@ -181,6 +191,10 @@ private boolean executeCommand(String c) throws IOException
case "RMD":
handleRmd(args);
break;

case "TYPE":
handleType(args);
break;

default:
sendMsgToClient("501 Unknown command");
Expand Down Expand Up @@ -609,6 +623,23 @@ private void handleRmd(String args)

}

private void handleType(String mode)
{
if(mode.toUpperCase().equals("A"))
{
transferMode = transferType.ASCII;
sendMsgToClient("200 OK");
}
else if(mode.toUpperCase().equals("I"))
{
transferMode = transferType.BINARY;
sendMsgToClient("200 OK");
}
else
sendMsgToClient("504 Not OK");;

}

private String parseExtendedArguments(String extArg)
{
String[] splitArgs = extArg.split("\\|");
Expand All @@ -621,6 +652,121 @@ private String parseExtendedArguments(String extArg)

}

private void handleRetr(String file)
{
File f = new File(currDirectory + fileSeparator + file);
if(!f.exists())
{
sendMsgToClient("550 Not ok");
}

else
{
BufferedOutputStream fout = null;
BufferedInputStream fin = null;

// Binary mode
if (transferMode == transferType.BINARY)
{
sendMsgToClient("150 Opening binary mode data connection for requested file " + f.getName());

try
{
//create streams
fout = new BufferedOutputStream(dataConnection.getOutputStream());
fin = new BufferedInputStream(new FileInputStream(f));
}
catch (Exception e)
{
System.out.println("Could not create file streams");
}

System.out.println("Starting file transmission of " + f.getName());

// write file with buffer
byte[] buf = new byte[1024];
int l = 0;
try
{
while ((l = fin.read(buf,0,1024)) != -1)
{
fout.write(buf,0,l);
}
}
catch (IOException e)
{
System.out.println("Could not read from or write to file streams");
e.printStackTrace();
}

//close streams
try
{
fin.close();
fout.close();
} catch (IOException e)
{
System.out.println("Could not close file streams");
e.printStackTrace();
}


System.out.println("Completed file transmission of " + f.getName());

sendMsgToClient("226 File transfer successful. Closing data connection.");

}

// ASCII mode
else
{
sendMsgToClient("150 Opening ASCII mode data connection for requested file " + f.getName());

BufferedReader rin = null;
PrintWriter rout = null;

try
{
rin = new BufferedReader(new FileReader(f));
rout = new PrintWriter(dataConnection.getOutputStream(),true);

}
catch (IOException e)
{
System.out.println("Could not create file streams");
}

String s;

try
{
while((s = rin.readLine()) != null)
{
rout.println(s);
}
} catch (IOException e)
{
System.out.println("Could not read from or write to file streams");
e.printStackTrace();
}

try
{
rout.close();
rin.close();
} catch (IOException e)
{
System.out.println("Could not close file streams");
e.printStackTrace();
}
sendMsgToClient("226 File transfer successful. Closing data connection.");
}

}
closeDataConnection();

}



}

0 comments on commit 3dbe74d

Please sign in to comment.