Package pong.common

Examples of pong.common.Message


  /**
   * This method gets a message from the server and performs an appropriate action
   */
  private void getMessage() {
    Message message = null;
    try {
      message = (Message) input.readObject();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      System.exit(1);
    } catch (IOException e) {
      Debugger.printLine("Error receiving message");
      if (!this.isInterrupted()) {
        onServerDisconnect();
      }
      return;
    }

    Debugger.printLine(message.getMessageType().name() + " message received" + " time "
        + System.currentTimeMillis());
    if (message.getMessageType() == Message.Type.POSITIONS) {
      this.eventsHandler.objectPositionsReceivedEvent((Position[]) message.getContent());
      sendMovementInformation();
    } else if (message.getMessageType() == Message.Type.MATCH_BEGINS) {
      final Integer roundsNumber = (Integer) message.getContent();
      this.eventsHandler.matchBeginsEvent(roundsNumber);
    } else if (message.getMessageType() == Message.Type.ROUND_BEGINS) {
      this.eventsHandler.roundBeginsEvent();
    } else if (message.getMessageType() == Message.Type.ROUND_END) {
      final Boolean won = (Boolean) message.getContent();
      this.eventsHandler.roundEndEvent(won);
    } else if (message.getMessageType() == Message.Type.OTHER_DISCONNECTED) {
      this.eventsHandler.otherDisconnectedWhilePlayingEvent();
    } else if (message.getMessageType() == Message.Type.YOUR_PLAYER_INDEX) {
      final Integer playerIndex = (Integer) message.getContent();
      this.eventsHandler.playerIndexReceivedEvent(playerIndex);
    }
    assert message.getMessageType() == Message.Type.ARE_YOU_ALIVE;
  }
View Full Code Here


  /**
   * This method sends information about player movement
   */
  private void sendMovementInformation() {
    final Message message = new Message(Message.Type.MOVEMENT,
        this.eventsHandler.movementInformationRequestedEvent());
    try {
      this.output.writeObject(message);
      this.output.reset();
    } catch (IOException e) {
View Full Code Here

  /**
   * This method tell the client the other player disconnected asynchronously.
   */
  public synchronized void otherDisconnectedWhilePlayingEvent() {
    final Message m = new Message(Message.Type.OTHER_DISCONNECTED, null);
    this.messagesQueue.add(m);
  }
View Full Code Here

   *            the number of rounds for the match
   */
  public synchronized void matchBeginsEvent(final int roundNumber) {
    Debugger.printLine("ServerProtocolThread.matchBeginsEvent called for player "
        + this.playerIndex);
    final Message m = new Message(Message.Type.MATCH_BEGINS, Integer.valueOf(roundNumber));
    this.messagesQueue.add(m);
  }
View Full Code Here

   *
   * @param won
   *            true or false
   */
  public synchronized void roundEndEvent(final Boolean won) {
    final Message m = new Message(Message.Type.ROUND_END, won);
    this.messagesQueue.add(m);
  }
View Full Code Here

   * This method tell the client the round begins asynchronously.
   */
  public synchronized void roundBeginsEvent() {
    Debugger.printLine("ServerProtocolThread.roundBeginsEvent called for player "
        + this.playerIndex);
    final Message m = new Message(Message.Type.ROUND_BEGINS, null);
    this.messagesQueue.add(m);
  }
View Full Code Here

  /**
   * This method sends messages that were to be sent asynchronously
   */
  private void sendMessagesOnQueue() {
    while (this.messagesQueue.size() != 0) {
      final Message m = this.messagesQueue.remove();
      sendMessage(m);
      if (m.getMessageType() == Message.Type.OTHER_DISCONNECTED
          || m.getMessageType() == Message.Type.ROUND_END
          || m.getMessageType() == Message.Type.MATCH_BEGINS) {
        this.roundRunning = false;
      } else if (m.getMessageType() == Message.Type.ROUND_BEGINS) {
        this.roundRunning = true;
      }
    }
  }
View Full Code Here

   * This method sends the player index to the client.
   */
  private void sendPlayerIndex() {
    Debugger.printLine("ServerProtocolThread.sendPlayerIndex called for player "
        + this.playerIndex);
    final Message m = new Message(Message.Type.YOUR_PLAYER_INDEX, playerIndex);
    sendMessage(m);
  }
View Full Code Here

   */
  private void sendPositionInformation() {
    final Position[] pos = server.getObjectsPositions();
    // Debugger.printLine("ServerProtocolThread.sendPositions called");
    // Debugger.printLine("Player 1 position " + pos[1].getY());
    final Message m = new Message(Message.Type.POSITIONS, pos);
    sendMessage(m);
  }
View Full Code Here

    if (this.isInterrupted()) {
      return;
    }
    Debugger.printLine("ServerProtocolThread.getMovementInformation called for player"
        + this.playerIndex + " time " + System.currentTimeMillis());
    Message message = null;
    try {
      message = (Message) this.input.readObject();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    } catch (IOException e) {
      onDisconnect();
      return;
    }
    assert (message != null);
    assert (message.getMessageType() == Message.Type.MOVEMENT);
    this.player.setMovementInformation((PlayerMovementInformation) message.getContent());
    Debugger.printLine("Movement information received for player" + this.playerIndex + " time "
        + System.currentTimeMillis());
  }
View Full Code Here

TOP

Related Classes of pong.common.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.