示例#1
0
  @Test
  public void testRegisterMainServer() throws RemoteException {
    String currentIp = null;
    try {
      currentIp = InetAddress.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    int port = 7623;
    IpAddressInfo mainServerIpAddressInfo = new IpAddressInfo(currentIp, port);

    int dimension = 5;

    ServerUtil.startGameServer(dimension, mainServerIpAddressInfo);
    //		ServerResponse response = serverRemote.joinGame(new Player("AV", 1));
    //		assertEquals(response.getResponseState(), ResponseState.NO_ENOUGH_PALYER);

  }
  @Override
  public void run() {
    Thread thisThread = Thread.currentThread();
    timer = AUTO_PING_TIME;
    blinker = thisThread;
    while (blinker == thisThread) {
      try {
        //	logger.log(Level.INFO, "to ping main server in " + timer + " secs");
        Thread.sleep(1000);
        timer--;
        if (timer <= 0) {
          timer = AUTO_PING_TIME;
          mainServerRemote.autoPinging();
        }
      } catch (InterruptedException e) {
        logger.log(Level.SEVERE, e.toString());
      } catch (RemoteException e) {
        GameSessionManager sessionManager = GameSessionManager.getInstance();
        // remove current main server Ip address from set..
        logger.log(
            Level.INFO,
            "Main server down. Set "
                + sessionManager.getThisAddress().toString()
                + " as main server");

        ServerUtil.removeAddressFromMap(
            sessionManager.getMainServerAddress(), sessionManager.getIpAddressInfoMap());
        sessionManager.setBackUpServer(false);
        sessionManager.setMainServer(true);
        sessionManager.setMainServerAddress(sessionManager.getThisAddress());
        GameSessionManager.getInstance().setServerStub(new MainServerRemoteProxy(owner));

        // start backup listener
        new BackupServerRunningListener(owner).start();
        // yeah! I am main server now! no need to ping others again! send of this thread..
        stopRun();
      }
    }
  }