Package it.polimi.ingsw.eriantys.server
Class ClientConnection
java.lang.Object
it.polimi.ingsw.eriantys.server.ClientConnection
This class represents a single client-server connection.
It exposes methods which allow to read from and write to the connection's I/O streams.
-
Constructor Summary
ConstructorsConstructorDescriptionClientConnection(Server server, Socket socketToClient) Constructs a new instance ofClientConnectionwith the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptiongetGame()A getter for the game whichthisrefers to.booleanA getter to know if the client is inside a game lobby.voidping()Sends aPingmessage to the client approximately every 2.5 seconds in order to ensure that the connection is working, disconnecting the client if an I/O error occurs.voidread()Continuously checks for new messages being sent by the client through the connection socket's input stream and handles them according to the game phase, disconnecting the client if an I/O error occurs or if the client is unresponsive to ping messages.voidSets the connection's game reference to the specifiedGame.voidsetJoinedLobby(boolean joinedLobby) Sets thejoinedLobbymember variable to the specified value.voidsetRunning(boolean running) Sets therunningmember variable to the specified value.voidWrites the specifiedMessageto the connection socket's output stream, disconnecting the client if an I/O error occurs.
-
Constructor Details
-
ClientConnection
Constructs a new instance ofClientConnectionwith the specified parameters.- Parameters:
server- the game serversocketToClient- the socket between the server and this connection's client- Throws:
IOException- if an error occurs when retrieving the input or output stream
-
-
Method Details
-
setRunning
public void setRunning(boolean running) Sets therunningmember variable to the specified value.- Parameters:
running- the desired value
-
getGame
A getter for the game whichthisrefers to.- Returns:
- the game which the connection refers to (if there is one), or
nullotherwise.
-
setGame
Sets the connection's game reference to the specifiedGame.- Parameters:
game- the connection's newGamereference
-
hasJoinedLobby
public boolean hasJoinedLobby()A getter to know if the client is inside a game lobby.- Returns:
trueif and only if the client associated withthishas joined a game lobby
-
setJoinedLobby
public void setJoinedLobby(boolean joinedLobby) Sets thejoinedLobbymember variable to the specified value.- Parameters:
joinedLobby- the desired value
-
read
public void read()Continuously checks for new messages being sent by the client through the connection socket's input stream and handles them according to the game phase, disconnecting the client if an I/O error occurs or if the client is unresponsive to ping messages. -
write
Writes the specifiedMessageto the connection socket's output stream, disconnecting the client if an I/O error occurs.- Parameters:
message- the message to be written and sent to the client
-
ping
public void ping()Sends aPingmessage to the client approximately every 2.5 seconds in order to ensure that the connection is working, disconnecting the client if an I/O error occurs.
-