Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// This file contains material supporting section 3.7 of the textbook: // Object Oriented Software Engineering and is issued under the open-source // license found

image text in transcribed

// This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com import java.io.*; import client.*; import common.*; /** * This class constructs the UI for a chat client. It implements the * chat interface in order to activate the display() method. * Warning: Some of the code here is cloned in ServerConsole * * @author François Bélanger * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganière * @version July 2000 */ public class ClientConsole implements ChatIF { //Class variables ************************************************* /** * The default port to connect on. */ final public static int DEFAULT_PORT = 5555; //Instance variables ********************************************** /** * The instance of the client that created this ConsoleChat. */ ChatClient client; //Constructors **************************************************** /** * Constructs an instance of the ClientConsole UI. * * @param host The host to connect to. * @param port The port to connect on. */ public ClientConsole(String host, int port) { try { client= new ChatClient(host, port, this); } catch(IOException exception) { System.out.println("Error: Can't setup connection!" + " Terminating client."); System.exit(1); } } //Instance methods ************************************************ /** * This method waits for input from the console. Once it is * received, it sends it to the client's message handler. */ public void accept() { try { BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; while (true) { message = fromConsole.readLine(); client.handleMessageFromClientUI(message); } } catch (Exception ex) { System.out.println ("Unexpected error while reading from console!"); } } /** * This method overrides the method in the ChatIF interface. It * displays a message onto the screen. * * @param message The string to be displayed. */ public void display(String message) { System.out.println("> " + message); } //Class methods *************************************************** /** * This method is responsible for the creation of the Client UI. * * @param args[0] The host to connect to. */ public static void main(String[] args) { String host = ""; int port = 0; //The port number try { host = args[0]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); chat.accept(); //Wait for console data } } //End of ConsoleChat class 
3. Currently, the client simply sends to the server everything the end-user types. When the server receives these messages it simply echoes them to all clients. Add a mechanism so that the user of the client can type commands that perform special functions. Each command should start with the '#' symbol - in fact, anything that starts with that symbol should be considered a command. You should implement the following commands: (a) #quit cause the client to terminate gracefully. Make sure the connec- tion to the server is terminated before exiting the program. (b) #logoff causes the client to disconnect from the server, but not quit. (c) #sethost calls the setHost method in the client. Only al- lowed if the client is logged off; displays an error message otherwise. (d) #setport calls the setPort method in the client, with the same constraints as #sethost. (e) #login causes the client to connect to the server. Only allowed if the client is not already connected; display an error message otherwise. (f) #gethost displays the current host name. (8) #getport displays the current port number. 3. Currently, the client simply sends to the server everything the end-user types. When the server receives these messages it simply echoes them to all clients. Add a mechanism so that the user of the client can type commands that perform special functions. Each command should start with the '#' symbol - in fact, anything that starts with that symbol should be considered a command. You should implement the following commands: (a) #quit cause the client to terminate gracefully. Make sure the connec- tion to the server is terminated before exiting the program. (b) #logoff causes the client to disconnect from the server, but not quit. (c) #sethost calls the setHost method in the client. Only al- lowed if the client is logged off; displays an error message otherwise. (d) #setport calls the setPort method in the client, with the same constraints as #sethost. (e) #login causes the client to connect to the server. Only allowed if the client is not already connected; display an error message otherwise. (f) #gethost displays the current host name. (8) #getport displays the current port number

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Generative Artificial Intelligence For Project Management With Aws

Authors: Timothy Krimmel

1st Edition

B0CQV9KWB8, 979-8872627197

More Books

Students also viewed these Databases questions