Exemplo n.º 1
0
  boolean connectionOpened(ServerConnection connection) {
    if (this.connection != connection) {
      connection.close();
      return false;
    }

    // Login to the server
    AdminContent auth = new AdminContent(AdminContent.AUTH);
    auth.setAttribute("name", userName);
    auth.setAttribute("password", userPassword);
    auth.setAttribute("client.version", CLIENT_VERSION);
    Message msg = new Message(userName, Agent.ADMIN, auth);

    return connection.sendMessage(msg);
  }
Exemplo n.º 2
0
  private void handleAdminContent(AdminContent admin) {
    ServerConnection connection = this.connection;
    int type = admin.getType();

    if (admin.isError()) {
      // Failed to do whatever we tried to do
      if (type == AdminContent.AUTH) {
        // Failed to login
        showWarning(
            "Authentication Failed",
            "could not login as " + userName + ": " + admin.getErrorReason());
        requestQuit();

      } else {
        showWarning(
            "Request Failed",
            "Failed to "
                + admin.getTypeAsString(type)
                + ": "
                + admin.getErrorAsString(admin.getError())
                + " ("
                + admin.getErrorReason()
                + ')');
        // What should be done here? FIX THIS!!!
      }
    } else if (connection == null) {
      // Connection has closed => ignore message

    } else {
      switch (type) {
        case AdminContent.AUTH:
          {
            String serverVersion = admin.getAttribute("server.version");
            connection.setAuthenticated(true);
            if (ConfigManager.compareVersion(serverVersion, "0.8.13") >= 0) {
              connection.setTransportSupported(BinaryTransportWriter.SUPPORT_TABLES);
              // connection
              // .setTransportSupported(BinaryTransportWriter.SUPPORT_CONSTANTS);
            }
            if (ConfigManager.compareVersion(serverVersion, "0.9") < 0) {
              // This is an older version of the server that does not
              // immediately send out this information by itself
              requestServerTime();
              autoJoinSimulation(false);
            }
            break;
          }

        case AdminContent.SERVER_TIME:
          {
            long serverTime = admin.getAttributeAsLong("time", 0L);
            if (serverTime > 0) {
              serverTimeDiff = System.currentTimeMillis() - serverTime;
              formatter.setLogTime(serverTime);
            }
            break;
          }

        case AdminContent.NEXT_SIMULATION:
        case AdminContent.JOIN_SIMULATION:
          {
            long currentTime = getServerTime();
            long startTime = admin.getAttributeAsLong("startTime", 0L);
            long nextTime = 0L;
            if (startTime > 0) {
              int simulationID = admin.getAttributeAsInt("simulation", -1);
              String simText = simulationID >= 0 ? " " + simulationID : "";
              // Simulation has been joined or already existed (displayed
              // to user by StatusInfo). Create the agent immediately to
              // ensure it has enough time to setup.
              if (this.agentService == null && !createAgentInstance()) {
                showWarning("Agent Setup Failed", "could not setup the agent");
                requestQuit();
              }
              if (startTime > currentTime) {
                log.info(
                    "next simulation"
                        + simText
                        + " starts in "
                        + ((startTime - currentTime) / 1000)
                        + " seconds");
              } else {
                log.info("next simulation" + simText + " has already started");
              }
              nextTime = startTime - 15000L;

            } else if (autoJoinCount > 0 && (type == AdminContent.NEXT_SIMULATION)) {
              autoJoinSimulation(false);

            } else {
              nextTime = admin.getAttributeAsLong("nextTime", 0L);
              if (nextTime < currentTime) {
                nextTime = currentTime + 60 * (long) (20000 + Math.random() * 5000);
              }
            }

            if (nextTime > currentTime) {
              // Next time to check for next simulation
              long delay = nextTime - currentTime;
              if (delay > MIN_MILLIS_BEFORE_DISCONNECT) {
                // We should wait a while => disconnect for now
                long maxSleep = 60 * (long) (56000 + Math.random() * 1000);
                // Do not wait too long before checking in at the server
                // again in case games have been rescheduled.
                if (delay > maxSleep) {
                  delay = maxSleep;
                }

                log.info(
                    "[will reconnect in "
                        + (delay / 60000)
                        + " minutes, "
                        + ((delay / 1000) % 60)
                        + " seconds]");

                isAutoJoinPending = true;
                this.connection = new ServerConnection(this, delay);
                this.connection.open();
                connection.close();
              }
            }
            break;
          }

        default:
          log.warning("unhandled admin content: " + admin);
          break;
      }
    }
  }