Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

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

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 ...

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 ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

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 arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

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 ...

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 ...

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 ...

Task - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

  • 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