Package info.walnutstreet.vs.ps01.client

Source Code of info.walnutstreet.vs.ps01.client.Client

/**
*
*/
package info.walnutstreet.vs.ps01.client;

import info.walnutstreet.vs.ps01.common.Log;
import info.walnutstreet.vs.ps01.common.commands.Command;
import info.walnutstreet.vs.ps01.common.exception.InvalidUserInput;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
* @author Christoph Gostner, Romedius Weiss, Christian Bitschnau
* @version 0.1
*
*/
public class Client {
 
  public final int SERVER_PORT = 12345;
 
  private Socket socket;
  private PrintWriter printWriter;
  private BufferedReader bufferedReader;
  private BufferedReader stdIn;

  /**
   * Constructor.
   *
   * @param address The server's address.
   * @throws IOException
   */
  public Client(String address) throws IOException {
    this.socket = new Socket(address, SERVER_PORT);
    this.printWriter = new PrintWriter(this.socket.getOutputStream(), true);
        this.bufferedReader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        this.stdIn = new BufferedReader(new InputStreamReader(System.in));
  }
 
  /**
   * Start the client's work.
   * Read from command line the commands, send them to the server
   * and wait for the answer.
   */
  public void startClient() {
    String fromUser;
   
    try {
      while (true) {
        fromUser = stdIn.readLine();
        try {
          Command command = Command.parseInput(fromUser);
          printWriter.println(fromUser);
         
          if (command.isQuit())
            break;
         
          System.out.println("----------------------------- START ----------------------------");
          this.handleServerAnswer(command);
          System.out.println("----------------------------- END ------------------------------");
         
        } catch (InvalidUserInput e) {
          Log.error("Invalid command, please try again.");
        }
      }
   
      this.printWriter.close();
      this.bufferedReader.close();
      this.stdIn.close();
      this.socket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 
  /**
   * Handle the server's answer to a client's command.
   *
   * @param command The client's command to react to.
   */
  private void handleServerAnswer(Command command) {
    String abortKey = command.getAbortKey();
    String fromServer;
    PrintStream printStream = null;
   
    if (command.isCopy()) {
      try {
        printStream = new PrintStream(new FileOutputStream(command.getParameter(0)));
      } catch (FileNotFoundException e) {
        Log.error("Can't create file!");
        return;
      } catch (IndexOutOfBoundsException e) {
        // if we get here the parser method of the command is wrong.
        e.printStackTrace();
      }
    } else if (command.isHead() || command.isList()) {
      printStream = System.out;
    }
   
    try {
      while ((fromServer = this.bufferedReader.readLine()) != null) {
        if (abortKey.equals(fromServer)) {
          break;
        }
        printStream.println(fromServer);
      }
    } catch (IOException e) {
      Log.error("Can't read from server - could it be down?");
    }
   
    if (command.isCopy())
      printStream.close();
  }
 
  /**
   * Main.
   *
   * @param args IP
   * @throws IOException
   */
  public static void main(String[] args)  {
    if (args.length != 1) {
      Log.error("Please submit the server's hostname or IP address!");
      System.exit(-1);
    }
    try {
      Client client = new Client(args[0]);
      client.startClient();
    } catch (IOException e) {
      Log.error("Can't create connection to the server.");
    }
  }
}
TOP

Related Classes of info.walnutstreet.vs.ps01.client.Client

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.