Class ClientHandler
java.lang.Object
it.polimi.ingsw.gc14.Network.TCP.Server.ClientHandler
- All Implemented Interfaces:
Runnable
Handles the TCP connection with a single client.
Each instance runs on a dedicated thread and is responsible for receiving
NetworkEvent from its client and adding them
to the actionQueue.
It is also responsible to send events and game model updates back to the client.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BlockingQueue<NetworkEvent> Queue containing the events to be applied to the game modelprivate final List<ClientHandler> Shared list of all client handlers.private final SocketThe TCP socketprivate final ObjectInputStreamInput stream used to receive objects from the clientprivate final LimitedMap<String, Boolean> Maps each connected player's username to their connection state (true = connected).private final ObjectOutputStreamOutput stream used to send objects to the clientprivate booleantruewhile the handler is actively reading from the socket; set tofalseon disconnect.private final StringThe username of the connected client on this handler -
Constructor Summary
ConstructorsConstructorDescriptionClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, LimitedMap<String, Boolean> playersMap, BlockingQueue<NetworkEvent> actionQueue) Class constructor that initializes the attributes. -
Method Summary
Modifier and TypeMethodDescriptionvoidDisconnects the client from the server.Returns the username associated with this client.voidnotifyEvent(NetworkEvent event) Sends aNetworkEventto the client.voidnotifyMiniModel(MiniModel game) Sends the current game model to this client.voidrun()Listens for incomingNetworkEvents from the client and adds them to theactionQueue.
-
Field Details
-
username
The username of the connected client on this handler -
running
private volatile boolean runningtruewhile the handler is actively reading from the socket; set tofalseon disconnect. -
clientSocket
The TCP socket -
in
Input stream used to receive objects from the client -
out
Output stream used to send objects to the client -
clientHandlers
Shared list of all client handlers. This handler removes itself from the list when disconnected. -
limitedMap
Maps each connected player's username to their connection state (true = connected). -
actionQueue
Queue containing the events to be applied to the game model
-
-
Constructor Details
-
ClientHandler
public ClientHandler(String username, Socket clientSocket, ObjectOutputStream out, ObjectInputStream in, List<ClientHandler> clientHandlers, LimitedMap<String, Boolean> playersMap, BlockingQueue<NetworkEvent> actionQueue) Class constructor that initializes the attributes.- Parameters:
username- the username associated with the client.clientSocket- the socket representing the client's TCP connection.out- the output stream used to send data to the client.in- the input stream used to receive data from the client.clientHandlers- the shared list of all active client handlers.playersMap- the shared map containing the connected players.actionQueue- the queue containing incoming events.
-
-
Method Details
-
getUsername
Returns the username associated with this client.- Returns:
- the username associated with this client.
-
run
public void run()Listens for incomingNetworkEvents from the client and adds them to theactionQueue. Upon disconnection, this handler removes itself fromclientHandlers. -
notifyEvent
Sends aNetworkEventto the client.- Parameters:
event- The network event to send to the client.
-
notifyMiniModel
Sends the current game model to this client.- Parameters:
game- The current state of the game to send to the client.
-
disconnect
public void disconnect()Disconnects the client from the server.The client handler is stopped and removed from the list of active handlers. The player is marked as disconnected, a
DisconnectedPlayerevent is added to the action queue, and the associated socket is closed.
-