Пример #1
0
  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
  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();
    }
  }
Пример #3
0
  private static void join() {
    System.out.println("Joining server group...");
    Properties newRMIconfig = localServer.getRmiSettings();
    findActive();
    if (remoteServer != null) {
      try {
        String tempselfName = remoteServer.serverJoin(newRMIconfig.getProperty("publicIP"));
        if (tempselfName != null) {
          selfName = tempselfName;
          newRMIconfig.setProperty("name", selfName);

          try {
            PropertyHandler.save(RMI_CONFIG_FILE, newRMIconfig);
            init();
          } catch (Exception ex) {
            System.out.println("save rmiserver config failed");
          }
          System.out.println("You have joined the server group of " + remoteServer.getServerName());
        } else {
          System.out.println("you are already in the group of " + remoteServer.getServerName());
        }
      } catch (RemoteException ex) {
        System.out.println("no active server");
        System.exit(1);
      }

      startClientMode();
    } else {
      System.out.println("no active server");
      System.exit(1);
    }
  }