예제 #1
0
    /* (non-Javadoc)
     * @see com.jme3.network.MessageListener#messageReceived(java.lang.Object, com.jme3.network.Message)
     */
    @Override
    public void messageReceived(HostedConnection source, Message message) {
      if (message instanceof StringMessage) {
        // do something with the message
        StringMessage stringMessage = (StringMessage) message;

        final String msg =
            "Server received '" + stringMessage.getMessage() + "' from client #" + source.getId();
        System.out.println(msg);
        logger.log(Level.INFO, msg, stringMessage);
      } else if (message instanceof ChatMessage) {
        ChatMessage chatMessage = (ChatMessage) message;
        ChatMessage aPlayerSays =
            new ChatMessage(chatMessage.getPlayerID(), chatMessage.getMessage());

        gameServer.broadcast(Filters.notEqualTo(source), aPlayerSays);
        final String chatMsg =
            "#" + chatMessage.getPlayerID() + " says " + chatMessage.getMessage();
        logger.log(Level.INFO, chatMsg, chatMessage);
      } else if (message instanceof PlayerLeavingMessage) {
        PlayerLeavingMessage plm = (PlayerLeavingMessage) message;
        Player p = plm.getPlayer();
        p.setLeaver(true);
        ServerHub.getInstance().removePlayer(p);
        gameServer.getConnection(source.getId()).close("You wanted to leave! Shame on you...");
        gameServer.broadcast(Filters.notEqualTo(source), plm);
      }
    }
예제 #2
0
  /**
   * Respond player.
   *
   * @param p the p
   * @param connecting the connecting flag
   */
  public void respondPlayer(Player p, boolean connecting) {
    if (!connectedPlayers.containsKey(p)) {
      return;
    }
    if (connecting) {
      boolean isHost = p.isHost();
      HostedConnection hc = connectedPlayers.get(p);
      checkPlayersName(p);
      PlayerAcceptedMessage joiningPlayer =
          new PlayerAcceptedMessage(p, ServerHub.getPlayers(), isHost, true);
      hc.setAttribute("PlayerObject", p);
      hc.setAttribute("PlayerID", p.getID());
      hc.setAttribute("PlayerName", p.getName());

      gameServer.broadcast(Filters.equalTo(hc), joiningPlayer);
      final String connectMsg = "#" + p.getID() + "/" + p.getName() + " joined server.";
      logger.log(Level.INFO, connectMsg, joiningPlayer);
      // Collection<HostedConnection> connections = gameServer.getConnections();

      for (Map.Entry<Player, HostedConnection> entrySet : connectedPlayers.entrySet()) {
        Player player = entrySet.getKey();
        HostedConnection connection = entrySet.getValue();

        if (player != null && !player.equals(p) && connection != null) {
          isHost = player.isHost();
          PlayerAcceptedMessage otherPlayer =
              new PlayerAcceptedMessage(p, ServerHub.getPlayers(), isHost, false);
          gameServer.broadcast(Filters.equalTo(connection), otherPlayer);
          final String othersMsg =
              "Told #"
                  + player.getID()
                  + "/"
                  + player.getName()
                  + " that #"
                  + p.getID()
                  + "/"
                  + p.getName()
                  + " joined server.";
          logger.log(Level.INFO, othersMsg, joiningPlayer);
        }
      }
    } else {
      HostedConnection hc = connectedPlayers.get(p);
      PlayerLeavingMessage plm = new PlayerLeavingMessage(p);

      gameServer.broadcast(Filters.notEqualTo(hc), plm);
      final String leavingMsg = "#" + p.getID() + "/" + p.getName() + " left server.";
      logger.log(Level.INFO, leavingMsg, plm);

      connectedPlayers.remove(p);
    }
  }
예제 #3
0
    /* (non-Javadoc)
     * @see com.jme3.network.MessageListener#messageReceived(java.lang.Object, com.jme3.network.Message)
     */
    @Override
    public void messageReceived(HostedConnection source, Message message) {
      if (message instanceof PlanetActionMessage) {
        PlanetActionMessage clientMessage = (PlanetActionMessage) message;
        PlanetActionMessage serverMessage =
            new PlanetActionMessage(
                clientMessage.getClientTime(),
                System.currentTimeMillis(),
                clientMessage.getActionName(),
                clientMessage.getPlayerID(),
                clientMessage.getPlayerState(),
                clientMessage.getPlanetID());

        gameServer.broadcast(Filters.notEqualTo(source), serverMessage);

        final String planetActionMsg =
            "Client@"
                + new Date(clientMessage.getClientTime()).toString()
                + " | "
                + clientMessage.getActionName()
                + " from #"
                + clientMessage.getPlayerID()
                + "/"
                + clientMessage.getPlayerState().name
                + " moves ships to planet #"
                + clientMessage.getPlanetID();
        logger.log(Level.INFO, planetActionMsg, serverMessage);
      } else if (message instanceof GeneralActionMessage) {
        GeneralActionMessage clientMessage = (GeneralActionMessage) message;

        GeneralActionMessage serverMessage =
            new GeneralActionMessage(
                clientMessage.getActionName(),
                clientMessage.getSender(),
                clientMessage.getSenderState(),
                clientMessage.getReciever(),
                clientMessage.getRecieverState());

        gameServer.broadcast(Filters.notEqualTo(source), serverMessage);
        //                final String generalMsg =
        //                        clientMessage.getActionName() + " from #"
        //                        + clientMessage.getSender()
        //                        + "/" + clientMessage.getSenderState().name
        //                        + " to #" + clientMessage.getReciever()
        //                        + "/" + clientMessage.getRecieverState().name;
        logger.log(Level.INFO, serverMessage.getActionName(), serverMessage);
      }
    }
예제 #4
0
  /**
   * Syncronize level.
   *
   * @param tpf the tpf
   */
  public void syncronizeLevel(float tpf) {
    if (serverState != ServerState.INGAME) {
      return;
    }

    LevelActionMessage message =
        new LevelActionMessage(seed, System.currentTimeMillis(), DeathmatchGameplay.getGameTick());
    this.gameServer.broadcast(Filters.notEqualTo(host), message);
  }
예제 #5
0
  public ServerTest() throws IOException, InterruptedException {
    DiffClassRegistration.registerClasses();
    Serializer.registerClass(GameStateMessage.class);

    myServer = Network.createServer(6143);
    diffHandler = new ServerDiffHandler<GameStateMessage>(myServer);
    myServer.start();

    myServer.addConnectionListener(
        new ConnectionListener() {

          @Override
          public void connectionRemoved(Server server, HostedConnection conn) {
            System.out.println("Connection closed from " + conn.getAddress());
          }

          @Override
          public void connectionAdded(Server server, HostedConnection conn) {
            System.out.println("New connection from " + conn.getAddress());
          }
        });

    List<Float> or = Arrays.asList(new Float[] {0.5f, 0.6f, 0.7f});

    while (true) {
      if (myServer.hasConnections()) {
        List<Float> newPos =
            new ArrayList<Float>(Arrays.asList(new Float[] {(float) serverTicks, 8.0f, 3.0f}));

        GameStateMessage newMessage = new GameStateMessage("test", newPos, or, (byte) 0);

        /* Dispatch same message to all clients */
        diffHandler.dispatchMessage(myServer, Filters.in(myServer.getConnections()), newMessage);

        // send a message that is old (id=1), see what happens
        // myServer.broadcast(new LabeledMessage((short)1, newMessage));

        /* Check if the connection is lagging badly */
        for (HostedConnection conn : myServer.getConnections()) {
          if (diffHandler.getLag(conn) > 20) {
            System.out.println("Client " + conn.getAddress() + " is lagging badly.");
          }
        }
      }
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      serverTicks++;
    }
  }