Advanced Java Programming - Exercise 5 |
This exercise consists of one programming assignment.
Exercise Targets |
Client-Server Application |
In this exercise you will gradually create a Chat Room application.
Our ChatRoom, and basically every chat room, can be divided into two separate units that communicate with each other. The first is of course the chat-server, which is responsible for the connections and serves as a router for messages between all the users in the chat room. The second is the user, and as we refer to him - the client. The client is the initiator of the connection. When a client wants to get inside the chat room he will have to ask for a connection from the chat server. In order to make the server understand the client and vise versa, we need to create a language between them, and that language is referred to as the Protocol. Every message that is being send or received must go through that protocol and the protocol decides what to do with it. Of course, both the server and the client must talk with the same protocol. All of these little details will be explained later on.
What do you need to do?
We divided this exercise into two parts. In the first part you will create a ChatRoomClient, and in the second you will create a ChatRoomServer. To make it a bit easier we supply you our own solution of the 2 parts as an executable jars, and we also supply some of the classes already implemented. See our demo for the client and server in the demo directory. Also check the src directory which include all the given implemented files. Test the complete application. When you create your ChatRoomClient you can test it with the supplied ChatRoomServer (server.jar). If you decide to start implementing the ChatRoomServer, you can test it with our ChatRoomClient (client.jar). During your work on this project we will make it a bit more interesting by opening a server in one of our computers and publish our IP address so we could chat online.
Step 1 - ChatRoomClient |
In this step you will build a ChatRoomClient program. Your client will connect to a ChatRoomServer so that you may chat with other students in the class. Since a chat client is nothing without some basic user interface we supply you a ChatRoomClientGUI (download). The ChatRoomClientGUI is our main class. As we mentioned before, the client needs a common protocol with the server. This ChatRoomProtocol (download) will be used by both sides (client and server) and every side uses the methods it needs. Notice that the protocol is a singleton, so to create one you need to call the static method getInstance(). The default values the ChatRoomClient uses as remote IP and port are: IP = 127.0.0.1 (localhost), Port = 9090. These are the defaults and the user should be able to change them by typing new values into a dialog box that appears when the client starts. Download and add to your project the implementation of the JDialog box: ConnectionInfoDialog . You don't need to do much with this one, just notice that the main method calls the dialog on startup and then creates the ChatRoomClient with the given parameters.
How does the Client talk?
Since all of the conversation between the client and the server is textual we need to have some indicator in the text to indicate the services we want to receive from the other side. To do so we reserved the first character in every line we send. This character is an integer that represents a certain command. Who decides what to write there and how to process it, is of course, the ChatRoomProtocol.
When a client wants to connect the ChatRoomServer he should open a Socket connection to the server. The next step is to send a request for connection to the server by invoking the protocol method createConnectionRequest() and sending the returned string to the server. If everything goes well on the server side, the server will send the client a response. In this response he will ask the client to send his user name, and again, the client will turn to the protocol and call the createUserNameResponse(name), but this time, only after turning to the GUI to request some user input about his name. In general, every response from the server should go through the protocol, and the protocol will invoke methods on the client side. In this case, the only demands the server has are to send him your user name and to display a normal chat room message.
Guidelines and Technical Details
Step 2 - ChatRoomServer |
In this step you will build the ChatRoomServer that serves all the ChatRoomClients. Again, we supply you the GUI of the server in ChatRoomServerUI (download) , ConnectionInformationTableModel (download)(gui component we used with the server UI) and the protocol in ChatRoomProtocol (download). In this case, the ChatRoomServerUI is our main class. By default, the server works on port 9090, but you should add the option to configure a different port with command line parameters.
How the Server talks?
Just like on the client side, the server always consult the protocol in every step of the way, but this time, the diversity of commands is a bit higher as you can see in the protocol's handleClientMessage(). In your implementation you should limit the number of users that can receive services from the server. When your server reach the limit, a user that is trying to connect will have to wait until some other user disconnect. When a connection is accepted you should handle this connection in a different thread of a ChatRoomConnection. The communication between the server and a specific user should go through the connection thread that links between them.
Guidelines and Technical Details
Have fun!
Solution |
Will be available after submission.
Submission Instructions |
The submission of your exercise should include:
Good Luck |