Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

File Server Problem In this problem we will take our solution to JH2 called FileOperations and we will make it work across a network. From

File Server 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. 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: 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"); } } 

My Soultion to JH2 File Operations

import java.io.*; import java.util.*; public class FileOperations { StringTokenizer parseCommand; String fileForAction = ""; File path; public void delete() { boolean returnCode = false; if (path.exists()) { System.out.println("Deleting: " + path.getAbsolutePath()); returnCode = path.delete(); } else { System.out.println("File does not exist: " + path); } if (returnCode == true) { System.out.println("Successful Deletion"); } else { System.out.println("Failed to Delete."); } printSeperator(); // code for handling the delete command // Make sure you check the return code from the // File delete method to print out success/failure status } public void rename(ArrayList commandArguments) { File newPath = new File(commandArguments.get(0)); boolean returnCode = false; if (path.exists()) { System.out.println("Renaming: " + path.getAbsolutePath()); returnCode = path.renameTo(newPath); System.out.println(newPath); } else { System.out.println("File does not exist."); } if (returnCode == true) { System.out.println("Successful Rename"); } else { System.out.println("Failed to Rename."); } printSeperator(); } public void list() { if (path.exists()) { if (path.isDirectory()) { System.out.println("Listing: " + path.getAbsolutePath()); for (int i = 0; i < path.list().length; i++) { System.out.println(path.list()[i]); } } else System.out.println("Not a directory:" + path); } else { System.out.println("Directory does not exist."); } printSeperator(); } public void size() { if (path.exists()) { System.out.println("Size for: " + path.getAbsolutePath()); System.out.println("Size in bytes: " + path.length()); } else { System.out.println("File does not exist."); } printSeperator(); } public void mkdir() { boolean returnCode = path.mkdir(); if (returnCode == true) { System.out.println("Successful directory creation."); } else { System.out.println("Failed to create directory"); } } public void createFile(ArrayList commandArguments) { try { PrintStream printer = new PrintStream(new FileOutputStream(path)); for (int i = 0; i < commandArguments.size(); i++) { printer.println(commandArguments.get(i)); } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("Created file for: " + path.getAbsolutePath()); printSeperator(); } } public void printFile() { Scanner fileIn = null; try { fileIn = new Scanner(new FileInputStream((path))); while (fileIn.hasNextLine()) { System.out.println(fileIn.nextLine()); } System.out.println("Printed file for: " + path.getAbsolutePath()); } catch (FileNotFoundException e) { System.out.println("Error. File not found." + e); } finally { if (fileIn != null) fileIn.close(); printSeperator(); } } public void lastModified() { if (path.exists() && path.isFile()) { System.out.println("Last modified for: " + path.getAbsolutePath()); System.out.println(new Date(path.lastModified())); } else { System.out.println("File does not exist."); } printSeperator(); } void printSeperator() { System.out.println("************************"); } void printUsage() { // process the "?" command String[] validCommands = {"?", "quit", "delete", "rename", "size", "lastModified", "list", "printFile", "createfile", "mkdir", "quit"}; for (int i = 0; i < validCommands.length; i++) { System.out.println(validCommands[i]); } printSeperator(); } public void processCommandLine(String line) { try { StringTokenizer parseCommand = new StringTokenizer(line); String command = parseCommand.nextToken(); System.out.println("Processing: " + line); ArrayList commandArguments = new ArrayList<>(); while (parseCommand.hasMoreTokens()) { commandArguments.add(parseCommand.nextToken()); } if (commandArguments.size() != 0) { fileForAction = commandArguments.get(0); path = new File(fileForAction); commandArguments.remove(0); } switch (command) { case "?": printUsage(); break; case "createFile": createFile(commandArguments); break; case "printFile": printFile(); break; case "lastModified": lastModified(); break; case "size": size(); break; case "rename": rename(commandArguments); break; case "mkdir": mkdir(); break; case "delete": delete(); break; case "list": list(); break; case "quit": System.out.println("Bye."); return; default: System.out.println("Invalid command: " + line); printSeperator(); break; } } catch (NullPointerException e) { System.out.println("File argument is missing. Exception is: " + e); printSeperator(); } catch (NoSuchElementException e) { System.out.println("Command is missing. Exception is: " + e); printSeperator(); } } void processCommandFile(String commandFile) { Scanner fileIn = null; File commandFileToRead = new File(commandFile); String marker = "====================================================================="; try { fileIn = new Scanner(new FileInputStream((commandFile))); System.out.println(marker); System.out.println("Starting command file = " + commandFileToRead.getAbsolutePath()); System.out.println(marker); while (fileIn.hasNextLine()) { processCommandLine(fileIn.nextLine()); } System.out.println(marker); System.out.println("Ending command file = " + commandFileToRead.getAbsolutePath()); System.out.println(marker); } catch (FileNotFoundException e) { System.out.println("Error. File not found." + e); } finally { if (fileIn != null) fileIn.close(); } } public static void main(String[] args) { FileOperations fo = new FileOperations(); for (int i = 0; i < args.length; i++) { System.out.println(" ============ Processing " + args[i] + " ======================= "); fo.processCommandFile(args[i]); } System.out.println("Done with file_operations.FileOperations"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

What are the main objectives of Inventory ?

Answered: 1 week ago

Question

Explain the various inventory management techniques in detail.

Answered: 1 week ago