Package info.walnutstreet.vs.ps04.member

Source Code of info.walnutstreet.vs.ps04.member.P2PMember

/**
*
*/
package info.walnutstreet.vs.ps04.member;

import info.walnutstreet.vs.ps02.exception.SystemConsoleNotFoundException;
import info.walnutstreet.vs.ps04.conf.PropertyHandler;
import info.walnutstreet.vs.ps04.member.P2PMemberProtocol.P2PProtocolListener;

import java.io.Console;
import java.io.IOException;
import java.rmi.NotBoundException;
import java.util.Scanner;

/**
* @author Christoph Gostner
* @version 0.1
*
*/
public class P2PMember {
 
  static {
    PropertyHandler.loadProperties();
  }

  private Console console;
 
  /**
   * Constructor.
   * This will be the member witch creates the bootstrap node.
   *  
   * @throws IOException
   * @throws SystemConsoleNotFoundException
   */
  public P2PMember() throws IOException, SystemConsoleNotFoundException {
    super();
   
    this.console = System.console();
    if (this.console == null)
      throw new SystemConsoleNotFoundException();
  }
 
  /**
   * Constructor.
   *
   * @param hostname The host of the bootstrap node
   * @throws IOException
   * @throws SystemConsoleNotFoundException
   * @throws NotBoundException
   */
  public P2PMember(String hostname) throws IOException, SystemConsoleNotFoundException, NotBoundException {
    this();
   
    P2PMemberController.getInstance().setBootstrap(hostname);
  }

  /**
   * Member input loop
   */
  public void memberConversation() {
    while (true) {
      String commandLine = this.console.readLine(System.getProperty(P2PMember.class.getName() + ".prompt")).trim().toUpperCase();
      if ("QUIT".equals(commandLine)) {
        System.exit(0);
        return;
      }
     
      try {
        P2PMemberProtocol p2pcommand = Enum.valueOf(P2PMemberProtocol.class, commandLine);
        p2pcommand.execute(this.console, this.getListener());
      } catch (IllegalArgumentException e) {
        this.console.printf("Invalid command ...\n");
      }
    }
  }

  /**
   * Get the listener for error handling.
   *
   * @return Listener to handle errors
   */
  private P2PProtocolListener getListener() {
    return new P2PMemberProtocol.P2PProtocolListener() {
      @Override
      public void exception(Exception e) {
        e.printStackTrace();
      }
    };
  }
 
  /**
   * Main.
   *
   * @param args
   * @throws SystemConsoleNotFoundException
   * @throws IOException
   * @throws NotBoundException
   */
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Wish you to be the bootstrap node? (y/N) ");
    String answer = scanner.nextLine();
 
    try {
      P2PMember member = null;
      if (answer.equals(System.getProperty(P2PMember.class.getName() + ".bootstrap.answer")))
        member = new P2PMember();
      else
        member = new P2PMember(System.getProperty(P2PMember.class.getName() + ".hostname"));
      member.memberConversation();
    } catch (Exception e) {
      System.err.println("I wonder if there will be cake ...");
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of info.walnutstreet.vs.ps04.member.P2PMember

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.