Class NetworkHandlerSocket

java.lang.Object
it.polimi.ingsw.am13.client.network.socket.NetworkHandlerSocket
All Implemented Interfaces:
NetworkHandler

public class NetworkHandlerSocket extends Object implements NetworkHandler
This is the socket implementation of NetworkHandler. For each method, it creates the corresponding MsgCommand and then sends it to the server.
  • Field Details

    • out

      private final ObjectOutputStream out
      The output stream in which the messages are written
    • latestPlayer

      private PlayerLobby latestPlayer
      It is the player corresponding to this instance of the class (the latest one, in the sense that is updated if a player leaves a room and then creates, joins or reconnects to another)
    • pingThread

      private Thread pingThread
      Thread sending periodically pings to server
  • Constructor Details

    • NetworkHandlerSocket

      public NetworkHandlerSocket(Socket socket, View view)
      Initialize the output stream and start the server response handler
      Parameters:
      socket - the socket which is used to communicate with the server
      view - the user interface
  • Method Details

    • flushReset

      private void flushReset() throws IOException
      Flush and reset the network output stream
      Throws:
      IOException - if an IO exception has occured
    • sendMessage

      private void sendMessage(MsgCommand messageCommand)
      It sends the message to the server, by writing it to out, and then flushing and resetting out.
      Parameters:
      messageCommand - the message that has 0to be sent to the server
    • getPlayer

      public PlayerLobby getPlayer()
      Specified by:
      getPlayer in interface NetworkHandler
      Returns:
      The current valid player this class is associated to. Null if no valid player is currently associated.
    • getRooms

      public void getRooms()
      Get the existing rooms
      Specified by:
      getRooms in interface NetworkHandler
    • createRoom

      public void createRoom(String chosenNickname, Token token, int players)
      Create a room with the passed parameters
      Specified by:
      createRoom in interface NetworkHandler
      Parameters:
      chosenNickname - of the player who is creating the room
      token - of the player who is creating the room
      players - of the room that is being created
    • joinRoom

      public void joinRoom(String chosenNickname, Token token, int gameId)
      Make the player join the specified room
      Specified by:
      joinRoom in interface NetworkHandler
      Parameters:
      chosenNickname - the nickname the client wants to join the room with
      token - chosen by the player who wants to join the room
      gameId - of the game that the client wants to join
    • reconnect

      public void reconnect(String nickname, Token token)
      Reconnect the passed player to an existing game he was previously part of
      Specified by:
      reconnect in interface NetworkHandler
      Parameters:
      nickname - of the player who wants to reconnect
      token - of the player who wants to reconnect
    • leaveRoom

      public void leaveRoom()
      Make the player leave the room he is currently part of
      Specified by:
      leaveRoom in interface NetworkHandler
    • playStarter

      public void playStarter(Side side)
      Play the starter card
      Specified by:
      playStarter in interface NetworkHandler
      Parameters:
      side - on which the starter card should be played
    • choosePersonalObjective

      public void choosePersonalObjective(CardObjectiveIF card)
      Choose the personal objective card
      Specified by:
      choosePersonalObjective in interface NetworkHandler
      Parameters:
      card - the chosen personal objective
    • playCard

      public void playCard(CardPlayableIF card, Coordinates coords, Side side)
      Play a card according to the parameters
      Specified by:
      playCard in interface NetworkHandler
      Parameters:
      card - the player wants to play
      coords - where the card should be played
      side - on which the card should be played
    • pickCard

      public void pickCard(CardPlayableIF card)
      Pick the passed card
      Specified by:
      pickCard in interface NetworkHandler
      Parameters:
      card - the player wants to pick
    • ping

      private void ping()
      Ping the server
    • startPing

      public void startPing()
      Starts the thread sending pings to server
      Specified by:
      startPing in interface NetworkHandler
    • stopPing

      public void stopPing()
      Stops the thread sending pings to server
      Specified by:
      stopPing in interface NetworkHandler
    • sendChatMessage

      public void sendChatMessage(List<PlayerLobby> receivers, String text)
      Send a chat message to the receivers (either a single player, or all the players)
      Specified by:
      sendChatMessage in interface NetworkHandler
      Parameters:
      receivers - of the chat message
      text - of the chat message