From 3c048b36bb6732fcf250fa60e13a897e811df61c Mon Sep 17 00:00:00 2001 From: Maryam El Marjany Date: Mon, 8 Jun 2020 17:04:48 -0400 Subject: [PATCH] Changed files --- code/simplechat1/ClientConsole.java | 115 ++++++++++------ code/simplechat1/EchoServer.java | 99 +++++++------ code/simplechat1/ServerConsole.java | 126 +++++++++++++++++ code/simplechat1/client/ChatClient.java | 176 +++++++++++++++++++----- 4 files changed, 402 insertions(+), 114 deletions(-) create mode 100644 code/simplechat1/ServerConsole.java diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..ba3330a 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -1,6 +1,6 @@ // 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 +// license found at www.lloseng.com import java.io.*; import client.*; @@ -9,30 +9,30 @@ /** * 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 + * Warning: Some of the code here is cloned in ServerConsole * * @author François Bélanger - * @author Dr Timothy C. Lethbridge + * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganière * @version July 2000 */ -public class ClientConsole implements ChatIF +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 **************************************************** /** @@ -41,42 +41,48 @@ public class ClientConsole implements ChatIF * @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); - } - } + public ClientConsole(String loginId,String host, int port) + { + try + { + + + + client= new ChatClient(loginId,host, port, this); + } + catch(IOException exception) + { + System.out.println("Cannot open connection. Awaiting command."); + + + + + + } + } + - //Instance methods ************************************************ - + /** - * This method waits for input from the console. Once it is + * This method waits for input from the console. Once it is * received, it sends it to the client's message handler. */ - public void accept() + public void accept() { try { - BufferedReader fromConsole = + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; - while (true) + while (true) { message = fromConsole.readLine(); client.handleMessageFromClientUI(message); } - } - catch (Exception ex) + } + catch (Exception ex) { System.out.println ("Unexpected error while reading from console!"); @@ -89,33 +95,60 @@ public void accept() * * @param message The string to be displayed. */ - public void display(String message) + 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) + public static void main(String[] args) { + String loginId=""; String host = ""; int port = 0; //The port number - try - { - host = args[0]; - } - catch(ArrayIndexOutOfBoundsException e) - { - host = "localhost"; + try{ + loginId=args[0]; + + }catch(ArrayIndexOutOfBoundsException exeception){ + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(0); } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + try + { + + + host=args[1]; + /////YOU'RE MODIFYING HERE! + if(args.length >2 ){ + + port=Integer.valueOf(args[2]); + } + else{ + port=DEFAULT_PORT; + + + } + + + } + + catch(ArrayIndexOutOfBoundsException e) + { + host = "localhost"; + port=DEFAULT_PORT; + /////YOU'RE MODIFYING HERE! + //port=DEFAULT_PORT; + } + ClientConsole chat= new ClientConsole(loginId,host, port); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..7f3a954 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -1,12 +1,13 @@ // 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 +// license found at www.lloseng.com import java.io.*; import ocsf.server.*; +import common.ChatIF; /** - * This class overrides some of the methods in the abstract + * This class overrides some of the methods in the abstract * superclass in order to give more functionality to the server. * * @author Dr Timothy C. Lethbridge @@ -15,30 +16,38 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer +public class EchoServer extends AbstractServer { //Class variables ************************************************* - + /** * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; - + ChatIF serverUI; + //Constructors **************************************************** - + /** * Constructs an instance of the echo server. * * @param port The port number to connect on. */ - public EchoServer(int port) + public EchoServer(int port) { super(port); } - + + public EchoServer(int port,ChatIF svUserInterface) + { + super(port); + serverUI = svUserInterface; + } + + //Instance methods ************************************************ - + /** * This method handles any messages received from the client. * @@ -48,10 +57,25 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + String message=String.valueOf(msg); + if(message.startsWith("#login")){ + if(client.getInfo( "#loginId" )!= null){ + System.out.println("You can't set your login Id twice!"); + }else{ + String iD= message.substring(10,message.length()-14); + client.setInfo("#loginId",(Object) iD); + System.out.println(msg); + } + this.sendToAllClients(client.getInfo("#loginId")+" has logged in"); + } + + +else{ + System.out.println("Message received:< " + msg + " >from< " + client.getInfo("#loginId")+">"); + this.sendToAllClients( "Message from "+ client.getInfo("#loginId")+":"+msg); + } } - + /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. @@ -61,7 +85,7 @@ protected void serverStarted() System.out.println ("Server listening for connections on port " + getPort()); } - + /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. @@ -71,39 +95,34 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } - + + + protected void clientConnected(ConnectionToClient client) { + + System.out.println("A new client is attempting to connect to the server."); + + //this.sendToAllClients(client.getInfo("#loginId")+"has logged in"); + } + synchronized protected void clientDisconnected( + ConnectionToClient client) { + System.out.println("Client has disconnected!"); + } + synchronized protected void clientException( + ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("#loginId")+" has disconnected"); + this.sendToAllClients(client.getInfo("#loginId")+" has disconnected"); + + } + //Class methods *************************************************** - + /** - * This method is responsible for the creation of + * This method is responsible for the creation of * the server instance (there is no UI in this phase). * - * @param args[0] The port number to listen on. Defaults to 5555 + * @param args[0] The port number to listen on. Defaults to 5555 * if no argument is entered. */ - public static void main(String[] args) - { - int port = 0; //Port to listen on - try - { - port = Integer.parseInt(args[0]); //Get port from command line - } - catch(Throwable t) - { - port = DEFAULT_PORT; //Set port to 5555 - } - - EchoServer sv = new EchoServer(port); - - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { - System.out.println("ERROR - Could not listen for clients!"); - } - } } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..9a19f2e --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,126 @@ + +import java.io.*; +import common.*; + +public class ServerConsole implements ChatIF{ + final public static int DEFAULT_PORT = 5555; + EchoServer server; + + public ServerConsole( int port) + { + try + { + server=new EchoServer(port,this); + server.listen(); + } + catch(IOException exception) + { + System.out.println("Error: Can't setup connection!" + + " Terminating !"); + System.exit(1); + } + } + public void accept() + { + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true) + { + message = fromConsole.readLine(); + //ADDING HERE!! + if(message.startsWith("#")){ + + switch(message){ + case "#quit": + System.exit(0); + break; + + case "#stop": + server.stopListening(); + server.sendToAllClients("WARNING - Server has stopped listening for connections."); + break; + + case "#close": + server.close(); + + break; + + case "#start": + if( server.isListening()){ + System.out.println("Server is already listening!"); + }else{ + server.listen(); + } + break; + + case "#getport": + System.out.println(server.getPort()); + break; + default: + if(message.startsWith("#setport")){ + String [] elements=message.split(" "); + if(server.isListening()){ + System.out.println("Please logoff before attempting to set port ");} + else{ + server.setPort(Integer.valueOf(elements[1])); + System.out.println("Port set to : "+ server.getPort()); + } + }else{ + System.out.println("Please enter a valid command!"); +}} + + } + else{ + + + this.display("SERVER MSG>" + message); + server.sendToAllClients("SERVER MSG>" + message);} + + + } + } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + } + + public void display(String message){ + System.out.println("> " + message); + } + + + + + public static void main(String[] args) + { + int port = 0; //Port to listen on + + try + { + port = Integer.parseInt(args[0]); //Get port from command line + } + catch(Throwable t) + { + port = DEFAULT_PORT; //Set port to 5555 + } + + ServerConsole sc = new ServerConsole(port); + + try + { + sc.server.listen(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients"); + } + sc.accept(); + } + +} diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..c106b88 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -1,6 +1,6 @@ // 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 +// license found at www.lloseng.com package client; @@ -20,16 +20,17 @@ public class ChatClient extends AbstractClient { //Instance variables ********************************************** - + /** - * The interface type variable. It allows the implementation of + * The interface type variable. It allows the implementation of * the display method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + String loginId; + - //Constructors **************************************************** - + /** * Constructs an instance of the chat client. * @@ -37,47 +38,144 @@ public class ChatClient extends AbstractClient * @param port The port number to connect on. * @param clientUI The interface type variable. */ - - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException - { - super(host, port); //Call the superclass constructor - this.clientUI = clientUI; - openConnection(); - } + public ChatClient(String loginId,String host, int port, ChatIF clientUI) + throws IOException + { + + super(host, port); //Call the superclass constructor + this.loginId=loginId; + this.clientUI = clientUI; + openConnection(); + sendToServer("#loginId: "+ this.loginId + " has logged in"); + + + } + public ChatClient(String host, int port) { + super(host, port); + + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + try{ String message= fromConsole.readLine(); + handleMessageFromClientUI(message); } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + } + + + + - //Instance methods ************************************************ - + /** * This method handles all data that comes in from the server. * * @param msg The message from the server. */ - public void handleMessageFromServer(Object msg) + public void handleMessageFromServer(Object msg) { clientUI.display(msg.toString()); } /** - * This method handles all data coming from the UI + * This method handles all data coming from the UI * - * @param message The message from the UI. + * @param message The message from the UI. */ - public void handleMessageFromClientUI(String message) - { - try - { - sendToServer(message); - } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); - quit(); - } - } - + public void handleMessageFromClientUI (String message) + { + if(message.charAt(0)=='#'){ + + switch(message){ + case "#quit": + this.quit(); + break; + + case "#logoff": + try{ + this.closeConnection(); + + + } + catch (IOException exception) { + System.out.println("ERROR!!"); + } + break; + + case "#gethost": + System.out.println(this.getHost()); + break; + + case "#getport": + System.out.println(getPort()); + break; + + + + + default: + + String [] elements=message.split(" "); + if(message.startsWith("#sethost")){ + if(!this.isConnected()){ + this.setHost(elements[1]); + System.out.println("Host set to : "+ this.getHost());} + else{ + System.out.println("Please logoff before attempting to set host"); + } + } else{ + if(message.startsWith("#setport")){ + if(!this.isConnected()){ + this.setPort(Integer.valueOf(elements[1])); + System.out.println("Port set to : "+ this.getPort());} + else{ + System.out.println("Please logoff before attempting to set port "); + } + } + + else{ + if(message.startsWith("#login")){ + + if(!this.isConnected()){ + try{ + this.loginId=elements[1]; + openConnection(); + sendToServer("#loginId: "+ this.loginId + " has logged in"); + + + }catch(IOException exception){ + System.out.println("ERROR!");} + } + else{ + System.out.println("Login only allowed if the client isn't already connected!"); + } + } + else{ + System.out.println("Please enter another command!");} + } + + break; + } + } + + } + + else{ + try + { + sendToServer(message); + } + catch(IOException e) + { + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } + } + } /** * This method terminates the client. */ @@ -90,5 +188,17 @@ public void quit() catch(IOException e) {} System.exit(0); } + public void connectionClosed(){ + System.out.print("The connection has been closed!"); + + + + } + public void connectionException(java.lang.Exception exception){ + System.out.println("Server has shut down connection!"); + quit(); + + + } } //End of ChatClient class