コード例 #1
0
ファイル: Server.java プロジェクト: jasonchenliang/DBLike
  public static void main(String[] args) {

    System.out.println("hello server");
    init();

    if (args.length > 0) {
      if (args[0].equalsIgnoreCase("active")) {
        startServerMode();
      }
      if (args[0].equalsIgnoreCase("join")) {
        join();
      }
      if (args[0].equalsIgnoreCase("leave")) {
        leave();
      }

    } else {
      System.out.println("looing for existing active server...");
      findActive();
      if (remoteServer == null) {
        startServerMode();

      } else {
        startClientMode();
      }
    }
  }
コード例 #2
0
ファイル: Server.java プロジェクト: jasonchenliang/DBLike
  private static void electNewActive() {
    remoteServer = null;
    init();

    int total = clientModeConnection.getServerCount();
    System.out.println(total);
    int tempActiveIndex = (activeServerIndex + 1) % total;
    int wait = clientModeConnection.getWaitTime() * 1000;
    System.out.println("Active Server " + activeServerIndex + " failed, electing new server");
    while (tempActiveIndex != selfIndex) {
      System.out.println("trying to connect to server" + tempActiveIndex);
      try {
        Thread.sleep(wait);
      } catch (InterruptedException ex) {
      }
      remoteServer = clientModeConnection.connectTo(tempActiveIndex);
      if (remoteServer != null) {
        break;
      } else {
        tempActiveIndex = (tempActiveIndex + 1) % total;
      }
    }
    if (remoteServer == null) {
      findActive();
      if (remoteServer == null) {
        startServerMode();
      } else {
        startClientMode();
      }
    } else {
      startClientMode();
    }
  }