Class RMIClient
java.lang.Object
it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient
- All Implemented Interfaces:
IClient
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ClientControllerClient-side controller that receives and applies incoming game events.private ExecutorServiceFuture executor used for timer Runningprivate final StringHostname or IP address of the RMI server.private final StringLocal IP address advertised to the RMI runtime so callbacks are routed correctly.private ScheduledExecutorServiceScheduler that fires ping() every 3000L ms.private final intPort of the RMI registry.private booleantruewhile the heartbeat loop is active; set tofalseon disconnect.private IGameServerRemote stub obtained from the RMI registry after a successful connection.private StringUsername of the currently connected player; set duringconnect(String, int). -
Constructor Summary
ConstructorsConstructorDescriptionRMIClient(ClientController controller, String host, int port, String myIp) Constructs an RMI client and initializes its connection parameters. -
Method Summary
Modifier and TypeMethodDescriptionConnects to the RMI server and starts the heartbeat loop.private voidTears down the connection.voiddrawLowerBuildingCard(String playerUsername, int pos) Requests to draw a building card from the lower list.voiddrawLowerTribeCard(String playerUsername, int pos) Requests to draw a tribe card from the lower list.voiddrawUpperBuildingCard(String playerUsername, int pos) Requests to draw a building card from the upper list.voiddrawUpperTribeCard(String playerUsername, int pos) Requests to draw a tribe card from the upper list.voidNotifies the server of a voluntary disconnection via RMI, then stops the keep-alive scheduler.voidRequests to skip the turn.voidslotChoice(String playerUsername, int pos) Used to perform the slot choice action for the specified player at the specified position.private voidStarts sending periodic pings to the server.voidtotemChoice(String playerUsername, String totem) Sends a totem choice to the server for the specified player via RMI.
-
Field Details
-
host
Hostname or IP address of the RMI server. -
port
private final int portPort of the RMI registry. -
stub
Remote stub obtained from the RMI registry after a successful connection. -
controller
Client-side controller that receives and applies incoming game events. -
myIp
Local IP address advertised to the RMI runtime so callbacks are routed correctly. -
username
Username of the currently connected player; set duringconnect(String, int). -
running
private volatile boolean runningtruewhile the heartbeat loop is active; set tofalseon disconnect. -
pingSender
Scheduler that fires ping() every 3000L ms. -
executor
Future executor used for timer Running
-
-
Constructor Details
-
RMIClient
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
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). -
startHeartbeat
private void startHeartbeat()Starts sending periodic pings to the server.Mirrors the
ScheduledExecutorServiceinTCPClient.heartbeatLoop()that writesPINGevery 3 s. OnRemoteExceptionthe server is considered gone anddisconnect()is called — mirrors the behavior onSocketTimeoutException/IOExceptionin the TCP version. -
disconnect
private void disconnect()Tears down the connection. MirrorsTCPClient.disconnect(): stops the heartbeat and notifies the view. -
drawUpperTribeCard
Requests to draw a tribe card from the upper list. Creates a NetworkEvent and sends it through the network client.- Specified by:
drawUpperTribeCardin interfaceIClient- Parameters:
playerUsername- the name of the player performing the actionpos- the index of the card to draw
-
drawLowerTribeCard
Requests to draw a tribe card from the lower list. Creates a NetworkEvent and sends it through the network client.- Specified by:
drawLowerTribeCardin interfaceIClient- Parameters:
playerUsername- the name of the player performing the actionpos- the index of the card to draw
-
drawUpperBuildingCard
Requests to draw a building card from the upper list. Creates a NetworkEvent and sends it through the network client.- Specified by:
drawUpperBuildingCardin interfaceIClient- Parameters:
playerUsername- the name of the player performing the actionpos- the index of the card to draw
-
drawLowerBuildingCard
Requests to draw a building card from the lower list. Creates a NetworkEvent and sends it through the network client.- Specified by:
drawLowerBuildingCardin interfaceIClient- Parameters:
playerUsername- the name of the player performing the actionpos- the index of the card to draw
-
skipTurn
-
slotChoice
Used to perform the slot choice action for the specified player at the specified position.- Specified by:
slotChoicein interfaceIClient- Parameters:
playerUsername- the name of the player performing the actionpos- the index of the selected slot
-
totemChoice
Sends a totem choice to the server for the specified player via RMI.- Specified by:
totemChoicein interfaceIClient- 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:
notifyDisconnectionin interfaceIClient
-