Writing a multi-threaded chat-room server


This assignment states you to write a multi-threaded chat-room server. The first practical assignment asked you to design and implement some data structures which would be helpful for this task; for this assignment, you will write the code which will implement a working chat-room server. Most of this code comprises setting up sockets to handle communication between the chat-room and the logged-in users and by using the input and output streams of such sockets to send and receive messages. Such messages should be in a specific format, so that client software can present the messages in a meaningful way. The format of the messages is explained in the chat-room protocol below.

Chat-room Server:

A chat-room server permits remote users to connect across a network to the chat-room. Once they are connected, they can send messages to the chat-room: any message sent to the chat-room is broadcast to all connected users. Additionally, the chat-room server broadcasts notifications when a new chatter connects to the chat-room, and when an existing chatter leaves the chat-room. The chat-room server uses the given protocol for input/output:

A) Whenever a remote client connects to the server, the client must send a single, non-empty line of text that will be the name of the remote user in the chat-room. The server will then broadcast to all chatters in the room a string of the form " 0 " +   n, where n  is the name of chatter who has just joined.

B) The remote client has now "entered" the chat-room, and can send messages to the chat-room. The client can send two types of message:

i) A message to be broadcast: this might comprise of several lines. Each line must start with the prefix "2"; the end of the message is pointed out by a line consisting of just "3" (For illustration, to send a message "Hi\ neveryone", the remote client must send three lines:
" 2 H i \n2everyone \n 3\n"). The server then broadcasts one line of text of the form "2" + n , where n  is the name of the chatter who has sent the message; the server then sends all the lines of the message text (each line starting with " 2 ") and finally, a line comprising of just the text "3".

ii) A logout notification: this comprises of a single line starting with "1"; this points out that the remote client is leaving the chat-room and their session ends. The server broadcasts a string of the form "1" + n, where n is the name of the chatter who has just left.

If the remote client sends any input that does not conform to this protocol, the server ends the session.

One way to implement the functionality of the chat-room server would be to use the given classes:

A) Chatter - an instance of this class represents a single logged-in remote client. Each time a client connects to the server, an instance of Chatter is created; this instance will handle all interactions with that user. The class must listen for input from the client, and use the suitable ChatterList methods to broadcast the messages. In more detail, class Chatter must implement interface Runnable; when a client connects to the server, a Chatter instance must be used to handle all the communication between the server and that remote client; this Chatter will run in its own Thread.

B) ChatterList - this maintains a list of Chatter instances that represent the currently connected remote clients, and sends messages to all logged-in users, as in the 1st assignment. ChatroomServer - the top-level class which implements a server; this class has a main method that sets up a ChatterList to maintain a list of Chatter instances and then sets up a server socket which accepts incoming connections. Whenever a connection comes in from a remote client, an instance of Chatter is created that will handle all interactions with the client.

In addition, you must provide some means of shutting down the server. When the server is shut down, all Sockets and I/O streams must be closed, and all Threads should be ended.

The Chatter class would need several modifications to the class written for the 1st practical assignment:

A) Instances would require to store (in a field) the Socket instance which represents the connection to the remote client.

Instances would require running in a separate thread of computation (as a "session-handler" class). The class must therefore implement Runnable. The run () method must:

1) Set up the input and output streams from the Socket; then

2) Read a single line of input from the remote client: this will be the chatter's name; then

3) Add itself to the chat-room's list of chatters (see method connect (Chatter) from the first assignment; the Chatter will therefore require a reference to the ChatterList instance created in the main method of class ChatroomServer); then

4) Repeatedly read input from the remote client, and take the appropriate action:

i) If the input is a message to be sent to all users of the chat-room, the ChatterList instance should broadcast the message to all chatters (see method broadcast (String) from the first assignment);

ii) If the input is a logout notification, then end the session (see method l e a v e (Chatter) from the first assignment) and close the connection to the remote client.

B) The sendToUser (String) method must be implemented so that the given string is sent to the remote client, by using the output stream from the Socket.

Implementing the ChatroomServer class and making the above modifications to the Chatter class would give a working chat-room server. Though, the result would be instead inefficient. Every time a remote client connects to the server, a new Thread instance is made to handle the session with the remote client; when the remote client disconnects from the client, the Thread dies, however still exists in the JVM's memory. If the server has a lot of traffic, the JVM's memory will soon be cluttered with unused, dead threads. One solution would be to use method newCachedThreadPool ( ) in class java. util. concurrent . Executors, which gives an extensible pool of reusable threads.

For the same reason, it would be desirable to reduce the number of instances of other classes created by the server. Recall that the linked list in class ChatterList uses the class Node. When Chatter is added to the list, a new Node instance is created to store the Chatter. As we're trying not to clutter the JVM's memory with unused objects, care should be taken not to create unnecessary nodes. One solution would be to "merge" the Node and Chatter classes; to be more precise, move the "pointers" to the Chatter class and not use the node class at all. Chatters then have a field that stores the next Chatter in the list; you'll probably need a setTail(Chatter) method.

Likewise, to reduce the number of Chatter instances created, we might have the server maintain a list of "dead" but re-usable chatters (just as ChatterList maintains a list of "live" chatters). When a remote client connects to the chat-room, a dead chatter will be taken from this list (if the list is empty, a new Chatter should be created), and "initialized": this means that class Chatter needs a new method, initialize (Socket), that will instantiate the Socket field to the given parameter; once initialized, the Chatter session is run in a thread, as before.

One way of achieving this would be to modify the ChatterList class, so that it stores two lists: a linked list of live Chatters, as before; and a list of dead Chatters. It would also require a method, getChatter ( ), to return the first Chatter in the dead Chatter list (or a new Chatter instance if the dead Chatter list.

One further issue to be addressed is thread-safety. While the server is running, there will be one Chatter thread for each user in the chat-room; all of these threads call methods from one ChatterList instance. You should make sure that interference cannot arise in your solution.

Tasks:

The following is a suggested plan for completing this assignment (you do not need to follow this plan):

1) Modify the Chatter class so it stores a pointer to the next Chatter in whichever list it finds itself (null if there is no next Chatter).

2) Modify the ChatterList class so it uses class Chatter instead of class Node, and maintains two lists: a live Chatter list, and a dead Chatter list (you can either use your own solution to the first assignment, or use the model solution). Add method getChatter(), and modify the leave(Chatter) method so that the removed Chatter is added to the dead Chatter list.

3) Implement the server functionality in class ChatroomServer.

4) Make the other necessary changes to class Chatter.

5) Make sure your solution is thread-safe.

6) Make sure your solution is fully documented, and that your comments explain why your solution is thread-safe.

7) Submit your solution using the departmental submission system. You should submit all your Java source-code files. Typically, this might be Chatter.java, ChatterList.java, and ChatroomServer.java, but you might structure your classes in different ways, for example by using inner classes.

IMPORTANT NOTICE:

Setting up a server socket can compromise network security. If you test your solution on the departmental network, you MUST:

i) Only run your server program under Linux (e.g., in an Exceed session);
ii) Only set up your server on your allocated port number.

The departmental technical support team has allocated a port number to each student taking this module; the list of allocated port numbers is available here. You should set up your server socket on host "localhost" at the port number you have been assigned.

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Writing a multi-threaded chat-room server
Reference No:- TGS0422

Expected delivery within 24 Hours