Ask Programming Language Expert

This assignment states you to prepare 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 prepare 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 describeed 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 describe 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 ex 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.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9422

Have any Question? 


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As