Class RMIClient

java.lang.Object
it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient
All Implemented Interfaces:
IClient

public class RMIClient extends Object implements IClient
Client RMI. Uses the methods exposed by the server RMI.
  • Field Details

    • host

      private final String host
      Hostname or IP address of the RMI server.
    • port

      private final int port
      Port of the RMI registry.
    • stub

      private IGameServer stub
      Remote stub obtained from the RMI registry after a successful connection.
    • controller

      private final ClientController controller
      Client-side controller that receives and applies incoming game events.
    • myIp

      private final String myIp
      Local IP address advertised to the RMI runtime so callbacks are routed correctly.
    • username

      private String username
      Username of the currently connected player; set during connect(String, int).
    • running

      private volatile boolean running
      true while the heartbeat loop is active; set to false on disconnect.
    • pingSender

      private ScheduledExecutorService pingSender
      Scheduler that fires ping() every 3000L ms.
    • executor

      private ExecutorService executor
      Future executor used for timer Running
  • Constructor Details

    • RMIClient

      public RMIClient(ClientController controller, String host, int port, String myIp)
      Constructs an RMI client and initializes its connection parameters.
      Parameters:
      controller - the client controller associated with this RMI client.
      host - the hostname or IP address of the RMI server.
      port - the port used to connect to the RMI registry.
      myIp - the IP address of the client.
  • Method Details

    • connect

      public ErrorType connect(String username, int proposedNPlayers)
      Connects to the RMI server and starts the heartbeat loop.

      Mirrors TCPClient.connect(): after a successful join the heartbeat channel is opened (here: a scheduler is started instead of opening a second socket).

      Specified by:
      connect in interface IClient
      Parameters:
      username - the username chosen by the player.
      proposedNPlayers - the desired number of players proposed by this client.
      Returns:
      true if the connection is established successfully, false otherwise.
    • startHeartbeat

      private void startHeartbeat()
      Starts sending periodic pings to the server.

      Mirrors the ScheduledExecutorService in TCPClient.heartbeatLoop() that writes PING every 3 s. On RemoteException the server is considered gone and disconnect() is called — mirrors the behavior on SocketTimeoutException / IOException in the TCP version.

    • disconnect

      private void disconnect()
      Tears down the connection. Mirrors TCPClient.disconnect(): stops the heartbeat and notifies the view.
    • 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 the 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
    • totemChoice

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

      public void notifyDisconnection()
      Notifies the server of a voluntary disconnection via RMI, then stops the keep-alive scheduler.
      Specified by:
      notifyDisconnection in interface IClient