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.

ChatServer Contains a main method and all other code related to the server.
ClientGui Contains a main method for starting the chat client and all other code related to the interface.
ClientModel Contains code for tracking what an individual client knows about the chatroom and for encoding/decoding messages to update this state.
ClientConnection Contains 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 send method will take a request provided by ClientModel and 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 “ok n”, in which case send should return null; or it could respond with “err n X”, in which case send should return the string X.

    The send method 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 call send virtually 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 PrintWriter instance variable out — and then use flush on the PrintWriter so that whatever is buffered in memory is sent to the server immediately.

  • The run method 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 to processMessage.

  • Upon receiving a ok or err message, the processMessage method should inform the send method that sent the message. For other types of messages, processMessage should forward it to the ClientModel's processMessage method.

You can use any concurrency facilities packaged with Java to use to complete this assignment.