#C12172. Simulated Chat Server
Simulated Chat Server
Simulated Chat Server
You are required to implement a simulated chat server. In this simulation, a number of events occur in a chat room. The events include client connections, messages sent by clients, and client disconnections. Your task is to process these events and print the corresponding broadcast messages to all clients.
For a message event, the server broadcasts a message in the following format: \[ \texttt{Client \{id\}: \{message\}} \]
For a disconnect event, the server broadcasts a disconnection message in the following format: \[ \texttt{Client \{id\} has disconnected.} \]
The connect
event simply registers the client and does not produce any broadcast message. It is guaranteed that each client connects before sending any messages or disconnecting.
inputFormat
The first line of input contains an integer T, representing the total number of events. The following T lines each contain an event. An event can be one of three types:
connect <client>
- The client with identifier<client>
connects to the server. No broadcast is generated.message <client> <message>
- The connected client sends a message. The server should broadcastClient <client>: <message>
.disconnect <client>
- The client disconnects. The server should broadcastClient <client> has disconnected.
Note: The message part in the message
event can contain spaces.
outputFormat
For each event that requires broadcasting (i.e. message
and disconnect
events), print the corresponding broadcast message on a new line following the formats specified in the problem description.
6
connect 1
connect 2
message 1 Hello everyone!
message 2 Hi 1!
disconnect 1
message 2 Bye!
Client 1: Hello everyone!
Client 2: Hi 1!
Client 1 has disconnected.
Client 2: Bye!
</p>