Title: File Server
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

File Server Homework Problem

In this problem we will take our solution to JH2 called FileOperations and we will make it work across a network. From this we will have in effect the beginnings of a minimal File Server. You can start from your solution or you can start from the published answer to JH2.

I would recommend that you use 2 work spaces and 2 instances of Eclipse to develop this .... one for the Client side and one for the FileServer side.

Note that all of our FileOperations commands were a one line command. However, the output for a given command could be many lines. To make this work, we need invent the following protocol:

- Our FileClient software will send one command line to the File Server.
- The File Server will take the command and send back as many lines as it wishes.
- When a command is done, our File Server will send back an empty line which is effectively a call to:

somePrintStream.println( );

Client Code

- Include EchoClient.java in your project.
- Create a class called FileClient which extends EchoClient. (We did this in EchoObjectClient in "Networking Part2"). DO NOT MODIFY EchoClient.java. All of your work can be done in FileClient.
- Override the "communicate" method. A lot of the code will stay the same. The main thing that happens is that you will keep reading from the Server socket until you get a String that has zero length.
- When you print out the output received from your Server, I would like you to start each line with "received from server:" So for example the output from an list command might be:


received from server: list .
received from server: Listing files for C:\Users\chasselb.000\Desktop\Archives\ClassWork\workspace_java\JH5\.
received from server: .classpath
received from server: .project
received from server: .settings
received from server: bin
received from server: cmd.txt
received from server: src


Server Code

- To start out, include the following in your project: ConnectListener.java, MotherServer.java, EchoServer.java.
- Take your solution to FileOperations (or use the JH2 published answer) and rename it to FileServer.
- FileServer should extend EchoServer. Do all of your work in this class and DO NOT MODIFY ConnectListener.java, MotherServer.java, or EchoServer.java.
- Create a PrintStream instance variable in FileServer. I called mine outputPs.
- Every place where your old FileOperations did a System.out.println(.....), my code was changed to:
outputPS.println(......)
- After these changes, you shouldn't need to change most of your methods: getFile, getNextToken, printUsage, rename, delete, list, etc.
- Your processCommandLine will be the same as for JH2, except that you will want send out a blank line just before it returns. THIS IS VERY IMPORTANT or else your client won't know when a command has finished.
- You will not use the main in FileOperations, nor the processCommandFile methods from your previous JH2 solution.
- Instead, you will override the processStream method in EchoServer. Your main will be similar to the main that you see in EchoServer (minor changes).
- It is useful to have the Server code print out to the screen each command that it is processes. This is useful for debug purposes.

Summary of the above Server changes:




public class FileServer extends EchoServer
{
    StringTokenizer parseCommand;
    PrintStream outputPs=null;

    // Other than changing the calls System.out.println(....) to outputPs.println(....), the following routines should need no work:
    public void delete().....
    public void rename().....
    public void list().....
    public void size().....
    public void lastModified().....
    public void mkdir().....
    public void createFile().....
    public void printFile().....
    void printUsage().....
    private String getNextToken().....
    public File getFile().....

    public boolean processCommandLine(String line).....
    // Additionally the processCommandLine routine should do 2 things:
    1.) echo the command to be processed to the Screen
    2.) Send a blank line to outputPs just before this routine returns


  You can eliminate the processCommandFile and main methods and replace them with:
  1.) Overriding processStream from EchoServer
  2.) A main that is similar to the main in EchoServer

  Your processStream method should do the following:

    void processStream(InputStream is, OutputStream os)
    {

        Create a Scanner from is, and a PrintStream from os.
        Set outputPs (instance variable) equal to the PrintStream

        Read lines as long as the hasNextLine method of the Scanner class returns true.
        Send the String read to processCommandLine and check the return code.
        If processCommandLine returns false we need to exit.
        Make sure you always close your Scanner and PrintStream classes.

    }


    public static void main(String[] args) {

        FileServer fs = new FileServer();
        fs.monitorServer();
        System.out.println("Exitting FileServer");

    }
}


In your Jing Video, I want you to have your Client and Server screens showing in the 2 Eclipse windows. Execute the following commands:


?
createFile f.txt pink red green blue
printFile f.txt
lastModified f.txt
size f.txt
rename f.txt g.txt
mkdir dir
list  .
delete g.txt
list .
badcommand


Last Updated: April 2, 2014 9:21 AM