Class TCPClient

java.lang.Object
it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient
All Implemented Interfaces:
IClient

public class TCPClient extends Object implements IClient
Client TCP. Sends and receives messages with the TCP server.
  • Field Details

    • PING

      private static final int PING
      Byte value sent by the client to signal it is still alive.
      See Also:
    • PONG

      private static final int PONG
      Byte value expected from the server in response to a PING.
      See Also:
    • communicationSocket

      private Socket communicationSocket
      Socket TCP
    • socketReceive

      private ObjectInputStream socketReceive
      Input stream used receive objects from the server
    • socketSend

      private ObjectOutputStream socketSend
      Output stream used to send objects to the server
    • controller

      private final ClientController controller
      Client game's controller
    • hostname

      private final String hostname
      IP address of the server to connect to
    • running

      private boolean running
      true while the connection is active; set to false on disconnect.
    • mainPort

      private final int mainPort
      TCP port
    • heartbeatPort

      private final int heartbeatPort
      TCP port dedicated to heartbeat communication.
    • heartbeatSocket

      private Socket heartbeatSocket
      Socket used exclusively for heartbeat ping/pong exchange.
    • heartbeatOut

      private OutputStream heartbeatOut
      Output stream of heartbeatSocket, used to write PING bytes.
    • heartbeatIn

      private InputStream heartbeatIn
      Input stream of heartbeatSocket, used to read PONG bytes.
  • Constructor Details

    • TCPClient

      public TCPClient(ClientController controller, String hostname, int mainPort, int heartbeatPort)
      Constructs a TCP client and initializes its connection parameters.
      Parameters:
      controller - the client controller.
      hostname - the IP address or hostname of the server.
      mainPort - the main TCP port of the server.
      heartbeatPort - the TCP port used for heartbeat communication.
  • Method Details

    • connect

      public ErrorType connect(String user, int proposedNPlayers)
      Starts the TCP connection with the server. Sends an AddPlayer event, if the server responds with -1, the connection is refused and the method returns false. Otherwise, a listener thread is started.
      Specified by:
      connect in interface IClient
      Parameters:
      user - The username of the player
      proposedNPlayers - The desired number of players for the game
      Returns:
      null if the connection is successful, GENERIC_ERROR otherwise.
    • heartbeatLoop

      private void heartbeatLoop()
      Sends a PING byte to the server every 3 seconds and waits for a PONG reply. If no reply arrives within 5000L ms, the server is considered unreachable and disconnect() is called.
    • disconnect

      private void disconnect()
      Disconnects the player and cleans model and controller
    • receiveMessage

      private void receiveMessage()
      Listens continuously for incoming objects from the server. - If the received object is a NetworkEvent flagged as an error, it is printed. - If the received object is a valid NetworkEvent, it is applied to the game controller. - If the received object is a MiniModel, the controller's model is set.
    • drawUpperTribeCard

      public void drawUpperTribeCard(String playerUsername, int pos)
      Requests to draw a tribe card from the upper list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawUpperTribeCard in interface IClient
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • drawLowerTribeCard

      public void drawLowerTribeCard(String playerUsername, int pos)
      Requests to draw a tribe card from the lower list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawLowerTribeCard in interface IClient
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • drawUpperBuildingCard

      public void drawUpperBuildingCard(String playerUsername, int pos)
      Requests to draw a building card from the upper list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawUpperBuildingCard in interface IClient
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • drawLowerBuildingCard

      public void drawLowerBuildingCard(String playerUsername, int pos)
      Requests to draw a building card from the lower list. Creates a NetworkEvent and sends it through the network client.
      Specified by:
      drawLowerBuildingCard in interface IClient
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the card to draw
    • skipTurn

      public void skipTurn(String playerUsername)
      Requests to skip drawing turn. This action is available only when the player cannot draw any tribe card, but still can buy some buildings.
      Specified by:
      skipTurn in interface IClient
      Parameters:
      playerUsername - the name of the player performing the action
    • slotChoice

      public void slotChoice(String playerUsername, int pos)
      Used to perform the slot choice action for the specified player at the specified position.
      Specified by:
      slotChoice in interface IClient
      Parameters:
      playerUsername - the name of the player performing the action
      pos - the index of the selected slot
    • doEvent

      private void doEvent(NetworkEvent event)
      Sends a NetworkEvent to the server.
      Parameters:
      event - The NetworkEvent to send.
    • totemChoice

      public void totemChoice(String playerUsername, String totems)
      Sends a totem choice event to the server for the specified player.
      Specified by:
      totemChoice in interface IClient
      Parameters:
      playerUsername - the username of the player choosing the totem.
      totems - the name of the chosen totem.
    • notifyDisconnection

      public void notifyDisconnection()
      Notifies the server of a voluntary disconnection by sending a sentinel value on the heartbeat channel, then closes the connection.
      Specified by:
      notifyDisconnection in interface IClient