Assignment 3: Chat room
Due: 5:00pm, Friday, October 11. Value: 40 pts.
In this assignment, you'll complete the portion of a chat client program that handles communication with a server. The program consists of four files.
ChatServerContains a mainmethod and all other code related to the server.ClientGuiContains a mainmethod for starting the chat client and all other code related to the interface.ClientModelContains code for tracking what an individual client knows about the chatroom and for encoding/decoding messages to update this state. ClientConnectionContains code for communicating with the server.
In testing this program, you'll want to ensure that the ChatServer
program is running in the background before you start ClientGui.
You'll likely stop and start ClientGui multiple
times during debugging, but it should be OK to simply keep ChatServer
running in the background.
The ClientConnection class contains the only code
you will modify for this assignment, and the file containing it is the only
one you should submit.
The constructor should establish a connection to the server and start a thread to process messages received from the server.
The
sendmethod will take a request provided byClientModeland send it to the server. Each message sent to the server should be preceded by an message identifier, which is a unique integer n. (You can simply number each message starting from 0.) The method should then await a server response: The server could respond with “okn”, in which casesendshould returnnull; or it could respond with “errn X”, in which casesendshould return the string X.The
sendmethod should not read this directly from the server, but rather it will wait for the reading thread. Please note that it should be possible for two threads to callsendvirtually simultaneously; your method should accommodate this except for necessary synchronization. The server responses could easily arrive out of order.Note: To send information to the server, print the message to the
PrintWriterinstance variableout— and then useflushon thePrintWriterso that whatever is buffered in memory is sent to the server immediately.The
runmethod is complete. It is the code executed by the thread started in the constructor, and it repeatedly reads and process lines read from the server. It breaks each line into three pieces — the operation code, the first argument (which could be the message ID to which the server is responding), and the second argument (which could be the error message). It then passes these toprocessMessage.Upon receiving a
okorerrmessage, theprocessMessagemethod should inform thesendmethod that sent the message. For other types of messages,processMessageshould forward it to theClientModel'sprocessMessagemethod.
You can use any concurrency facilities packaged with Java to use to complete this assignment.

