Class TCPClient
java.lang.Object
it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient
- All Implemented Interfaces:
IClient
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate SocketSocket TCPprivate final ClientControllerClient game's controllerprivate InputStreamInput stream ofheartbeatSocket, used to readPONGbytes.private OutputStreamOutput stream ofheartbeatSocket, used to writePINGbytes.private final intTCP port dedicated to heartbeat communication.private SocketSocket used exclusively for heartbeat ping/pong exchange.private final StringIP address of the server to connect toprivate final intTCP portprivate static final intByte value sent by the client to signal it is still alive.private static final intByte value expected from the server in response to aPING.private booleantruewhile the connection is active; set tofalseon disconnect.private ObjectInputStreamInput stream used receive objects from the serverprivate ObjectOutputStreamOutput stream used to send objects to the server -
Constructor Summary
ConstructorsConstructorDescriptionTCPClient(ClientController controller, String hostname, int mainPort, int heartbeatPort) Constructs a TCP client and initializes its connection parameters. -
Method Summary
Modifier and TypeMethodDescriptionStarts the TCP connection with the server.private voidDisconnects the player and cleans model and controllerprivate voiddoEvent(NetworkEvent event) Sends aNetworkEventto the server.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.private voidvoidNotifies the server of a voluntary disconnection by sending a sentinel value on the heartbeat channel, then closes the connection.private voidListens continuously for incoming objects from the servervoidRequests to skip drawing turn.voidslotChoice(String playerUsername, int pos) Used to perform the slot choice action for the specified player at the specified position.voidtotemChoice(String playerUsername, String totems) Sends a totem choice event to the server for the specified player.
-
Field Details
-
PING
private static final int PINGByte value sent by the client to signal it is still alive.- See Also:
-
PONG
-
communicationSocket
Socket TCP -
socketReceive
Input stream used receive objects from the server -
socketSend
Output stream used to send objects to the server -
controller
Client game's controller -
hostname
IP address of the server to connect to -
running
private boolean runningtruewhile the connection is active; set tofalseon disconnect. -
mainPort
private final int mainPortTCP port -
heartbeatPort
private final int heartbeatPortTCP port dedicated to heartbeat communication. -
heartbeatSocket
Socket used exclusively for heartbeat ping/pong exchange. -
heartbeatOut
Output stream ofheartbeatSocket, used to writePINGbytes. -
heartbeatIn
Input stream ofheartbeatSocket, used to readPONGbytes.
-
-
Constructor Details
-
TCPClient
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
Starts the TCP connection with the server. Sends anAddPlayerevent, if the server responds with-1, the connection is refused and the method returnsfalse. Otherwise, a listener thread is started. -
heartbeatLoop
private void heartbeatLoop()Sends aPINGbyte to the server every 3 seconds and waits for aPONGreply. If no reply arrives within 5000L ms, the server is considered unreachable anddisconnect()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 aNetworkEventflagged as an error, it is printed. - If the received object is a validNetworkEvent, it is applied to the game controller. - If the received object is aMiniModel, the controller's model is set. -
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
Requests to skip drawing turn. This action is available only when the player cannot draw any tribe card, but still can buy some buildings. -
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
-
doEvent
Sends aNetworkEventto the server.- Parameters:
event- The NetworkEvent to send.
-
totemChoice
Sends a totem choice event to the server for the specified player.- Specified by:
totemChoicein interfaceIClient- 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:
notifyDisconnectionin interfaceIClient
-