From c64f949c32e2f4d928bbe9b04f5f6461704169ee Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 7 Jun 2020 20:06:19 -0400 Subject: [PATCH 01/17] Update ChatClient.java 5a done --- code/simplechat1/client/ChatClient.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..fc35c5e 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -78,6 +78,19 @@ public void handleMessageFromClientUI(String message) } } + public void connectionClosed(){ + clientUI.display("The server has shut down, quitting..."); +} + + /* @param exception + * the exception raised. + */ + public void connectionException(Exception exception) { + + System.out.Println(exception); + quit(); +} + /** * This method terminates the client. */ @@ -85,6 +98,7 @@ public void quit() { try { + { closeConnection(); } catch(IOException e) {} From 5c02a272fb13fa0da1cb2f9e0a78fe1db1999610 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 11:16:44 -0400 Subject: [PATCH 02/17] Update EchoServer.java E5c --- code/simplechat1/EchoServer.java | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..70b1cd0 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -71,7 +71,37 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } - + /** + * This method overrides the one in superclass. called + * each time a new client connection is accepted. + * @param client the connection connected to the client. + */ + protected void clientConnected(ConnectionToClient client) { + + } + + /** + * This method overrides the one in superclass. called + * each time a client disconnects. + * @param client the connection with the client. + */ + synchronized protected void clientDisconnected(ConnectionToClient client) { + System.out.println(client + "disconnect to the server."); +this.sendToAllClients(client.getInfo("loginID") + "disconnected from the server."); + } + + /** + * This method overrides the one in superclass. alled each time an exception is thrown in a + * ConnectionToClient thread. + * + * @param client the client that raised the exception. + * @param Throwable the exception thrown. + */ + synchronized protected void clientException( + ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("loginID") + " disconnected from the server."); + this.sendToAllClients(client.getInfo("loginID") + " disconnected from the server."); +} //Class methods *************************************************** /** From 9ada9f3150fde50819e11b0a6979f7dca0bde1be Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 12:01:57 -0400 Subject: [PATCH 03/17] Create ServerConsole.java added ServerConsole.java --- code/simplechat1/ServerConsole.java | 117 ++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 code/simplechat1/ServerConsole.java diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..2a484fb --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,117 @@ +// 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 common.ChatIF; +import java.io.*; +import client.*; +import common.*; +/** + * This class constructs the UI for a chat server, followed ClientConsole.java. + * It implements the chatIF 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 ServerConsole implements ChatIF { + +//Instance variables ********************************************** + + /** + * The instance of the client that created this ConsoleChat. + */ + EchoServer echoServer; + + +//Constructors **************************************************** + + /** + * Constructs an instance of the ServerConsole UI. + * + * @param port The port to connect on. + */ + public ServerConsole(int port) + { + echoServer = new EchoServer(port,this); + try + { + echoServer.listen(); + } + catch(IOException exception) + { + System.out.println("Could not listen for clients!" + + " 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("SERVER MSG> " + message); + } + + + //Class methods *************************************************** + + /** + * This method is responsible for the creation of the Server UI. + * + * @param args[0] The port to listen to. + */ + public static void main(String[] args) + { + + int port = 0; //The port number + + try + { + port = Integer.parseInt(args[0]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + + ServerConsole sv = new ServerConsole(port); + sv.accept(); + +} +//End of ServerConsole class + + + From e3b989bf7fe1f090220861b74e762d34eb868f26 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 16:43:28 -0400 Subject: [PATCH 04/17] update update ChatClient, EchoServer --- code/simplechat1/ClientConsole.java | 35 +++++-- code/simplechat1/EchoServer.java | 133 +++++++++++++++++------- code/simplechat1/client/ChatClient.java | 98 +++++++++++++---- 3 files changed, 206 insertions(+), 60 deletions(-) diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..15e0a16 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,11 +41,11 @@ 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) + public ClientConsole(String loginid,String host, int port) { try { - client= new ChatClient(host, port, this); + client= new ChatClient(loginid, host, port, this); } catch(IOException exception) { @@ -100,23 +100,44 @@ public void display(String message) /** * This method is responsible for the creation of the Client UI. * - * @param args[0] The host to connect to. + * @param args[0] The login id + * @param args[1] The host to connect to. + * @param args[2] The port to connect to. */ public static void main(String[] args) { String host = ""; int port = 0; //The port number - + String loginid=""; + try{ + loginid=args[0]; + } + catch(Exception e){ + System.out.println("You need to specify login ID. Terminating..."); + System.exit(0); + } + try { - host = args[0]; + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data + + + try + { + port = Integer.parseInt(args[2]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + + ClientConsole cc= new ClientConsole(loginid,host, port); + cc.accept(); } } //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 70b1cd0..9a706ee 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,7 @@ import java.io.*; import ocsf.server.*; +import common.*; /** * This class overrides some of the methods in the abstract @@ -23,6 +24,7 @@ public class EchoServer extends AbstractServer * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; + ChatIF serverUI; //Constructors **************************************************** @@ -31,9 +33,10 @@ public class EchoServer extends AbstractServer * * @param port The port number to connect on. */ - public EchoServer(int port) + public EchoServer(int port, ChatIF serverUI) { super(port); + this.serverUI=serverUI; } @@ -48,9 +51,88 @@ 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)msg; + String loginid=message.substring(11); + + if (message.substring(0,10)=="#login ID: "){ + if(client.getInfo("loginid")==null){ + client.setInfo("loginid",loginid); + try{ + client.sendToClient( client.getInfo("loginid") +" logged in"); + }catch(IOException e){} + }else{ + try{ + client.sendToClient("You have already logged in!"); + }catch(IOException e){ + } + + } + }else{ + if(client.getInfo("loginid")==null){ + try{ + client.sendToClient( "create a login ID first"); + client.close(); + }catch(IOException e){} + } + System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); + + this.sendToAllClients(client.getInfo("loginid") + "> " +msg); } + + public void handleMessageFromServerUI(String message){ + try{ + if(message.startsWith("#")){ + switch(message.substring(0,7)){ + case "#close": + serverClosed(); + break; + case "#stop": + serverStopped(); + break; + case "#quit": + close(); + serverUI.display("server quits..."); + break; + case "#setport": + if(!isListening()){ + try{ + int port = Integer.parseInt(message.substring(9)); + setPort(port); + serverUI.display("Port is set to: "+ port); + }catch(Exception e) { + serverUI.display("invalid port, please try again"); + return; + } + }else{ + serverUI.display("You need to close server before set port"); + return; + } + break; + case "#start": + if(!isListening()){ + try{ + listen(); + serverUI.display("^^ logging you in ...^^"); + }catch(Exception e) { + serverUI.display("Exception occurred when log in. please try again"); + return; + } + }else{ + serverUI.display("You need to close server before start server"); + return; + } + break; + case "#getport": + serverUI.display("Your current port is: " + getPort()); + break; + } }else + serverUI.display(message); + this.sendToAllClients("SERVER MSG> " + message); + }catch(IOException e){ + serverUI.display("Could not send message from server. Terminating server."); + System.exit(1); + } + } /** * This method overrides the one in the superclass. Called @@ -62,6 +144,16 @@ protected void serverStarted() ("Server listening for connections on port " + getPort()); } + /** + * This method overrides the one in the superclass. + * Called called when the server is closed. + */ + + protected void serverClosed() + { + System.out.println("Server has closed."); + } + /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. @@ -71,37 +163,7 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } - /** - * This method overrides the one in superclass. called - * each time a new client connection is accepted. - * @param client the connection connected to the client. - */ - protected void clientConnected(ConnectionToClient client) { - - } - - /** - * This method overrides the one in superclass. called - * each time a client disconnects. - * @param client the connection with the client. - */ - synchronized protected void clientDisconnected(ConnectionToClient client) { - System.out.println(client + "disconnect to the server."); -this.sendToAllClients(client.getInfo("loginID") + "disconnected from the server."); - } - - /** - * This method overrides the one in superclass. alled each time an exception is thrown in a - * ConnectionToClient thread. - * - * @param client the client that raised the exception. - * @param Throwable the exception thrown. - */ - synchronized protected void clientException( - ConnectionToClient client, Throwable exception) { - System.out.println(client.getInfo("loginID") + " disconnected from the server."); - this.sendToAllClients(client.getInfo("loginID") + " disconnected from the server."); -} + //Class methods *************************************************** /** @@ -124,7 +186,8 @@ public static void main(String[] args) port = DEFAULT_PORT; //Set port to 5555 } - EchoServer sv = new EchoServer(port); + ServerConsole sc = new ServerConsole(port); + sc.accept(); try { diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fc35c5e..4dabf88 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -26,7 +26,7 @@ public class ChatClient extends AbstractClient * the display method in the client. */ ChatIF clientUI; - + String loginid; //Constructors **************************************************** @@ -38,12 +38,14 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) + public ChatClient(String loginid, String host, int port, ChatIF clientUI) throws IOException { super(host, port); //Call the superclass constructor this.clientUI = clientUI; + this.loginid="Guest"; openConnection(); + sendToServer("#login ID: " + loginid); } @@ -66,9 +68,70 @@ public void handleMessageFromServer(Object msg) */ public void handleMessageFromClientUI(String message) { - try - { + try { + if(message.startsWith("#")){ + switch(message){ + case "#quit": + quit(); + case "#logoff": + quit(); + case "#sethost": + if(!isConnected()){ + try{ + String host = message.substring(0); + setHost(host); + System.out.println("Host is set to: "+host); + }catch(Exception e) { + clientUI.display("invalid host, please try again"); + return; + } + }else{ + clientUI.display("You need to log off before set host"); + return; + } + break; + case "#setport": + if(!isConnected()){ + try{ + int port =Integer.parseInt(message.substring(0)); + setPort(port); + System.out.println("Port is set to: "+ port); + }catch(Exception e) { + clientUI.display("invalid port, please try again"); + return; + } + }else{ + clientUI.display("You need to log off before set port"); + return; + } + break; + case "#login": + if(!isConnected()){ + try{ + openConnection(); + clientUI.display("^^ logging you in ...^^"); + }catch(Exception e) { + clientUI.display("Exception occurred when log in. please try again"); + return; + } + }else{ + clientUI.display("You already logged in."); + return; + } + break; + case "#gethost": + clientUI.display("Your current host is: " + getHost()); + break; + case "#getport": + clientUI.display("Your current port is: " + getPort()); + break; + } + }else + + sendToServer(message); + + } catch(IOException e) { @@ -77,20 +140,20 @@ public void handleMessageFromClientUI(String message) quit(); } } - public void connectionClosed(){ - clientUI.display("The server has shut down, quitting..."); -} - - /* @param exception - * the exception raised. - */ - public void connectionException(Exception exception) { - - System.out.Println(exception); - quit(); -} - + clientUI.display("The server has shut down, quitting..."); + } + + /* @param exception + * the exception raised. + */ + public void connectionException(Exception exception) { + + clientUI.display("Exception occured when connecting, quitting..."); + quit(); + } + + /** * This method terminates the client. */ @@ -98,7 +161,6 @@ public void quit() { try { - { closeConnection(); } catch(IOException e) {} From 4eee7ba26411d7c4d3456e5846a665812ebbae44 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 17:02:55 -0400 Subject: [PATCH 05/17] update fix error --- code/simplechat1/EchoServer.java | 2 +- code/simplechat1/ServerConsole.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 9a706ee..77962c7 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -78,7 +78,7 @@ public EchoServer(int port, ChatIF serverUI) this.sendToAllClients(client.getInfo("loginid") + "> " +msg); } - + } public void handleMessageFromServerUI(String message){ try{ if(message.startsWith("#")){ diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java index 2a484fb..0c130a3 100644 --- a/code/simplechat1/ServerConsole.java +++ b/code/simplechat1/ServerConsole.java @@ -64,7 +64,7 @@ public void accept() while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + echoServer.handleMessageFromServerUI(message); } } catch (Exception ex) @@ -104,12 +104,13 @@ public static void main(String[] args) } catch(ArrayIndexOutOfBoundsException e) { - port = DEFAULT_PORT; + port = 5555; } ServerConsole sv = new ServerConsole(port); sv.accept(); +} } //End of ServerConsole class From 4a3caccf679a2dfd3b669e081a0f91d45de93455 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 17:05:36 -0400 Subject: [PATCH 06/17] update fix error --- code/simplechat1/EchoServer.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 77962c7..4e96c25 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -188,15 +188,22 @@ public static void main(String[] args) ServerConsole sc = new ServerConsole(port); sc.accept(); - - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { - System.out.println("ERROR - Could not listen for clients!"); + } + @Override + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); } + + @Override + protected synchronized void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); + + } + @Override + protected synchronized void clientException(ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); } } //End of EchoServer class From 25ecb8e2770c4fdcb07222816d05609b35aeee5a Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 17:35:55 -0400 Subject: [PATCH 07/17] Update EchoServer.java fix bracket --- code/simplechat1/EchoServer.java | 38 +++++++++++++++++--------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 4e96c25..5c2f378 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -53,7 +53,7 @@ public EchoServer(int port, ChatIF serverUI) { String message=(String)msg; String loginid=message.substring(11); - + System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); if (message.substring(0,10)=="#login ID: "){ if(client.getInfo("loginid")==null){ client.setInfo("loginid",loginid); @@ -138,6 +138,7 @@ public void handleMessageFromServerUI(String message){ * This method overrides the one in the superclass. Called * when the server starts listening for connections. */ + @Override protected void serverStarted() { System.out.println @@ -148,16 +149,32 @@ protected void serverStarted() * This method overrides the one in the superclass. * Called called when the server is closed. */ - + @Override protected void serverClosed() { System.out.println("Server has closed."); } - + @Override + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); + } + + @Override + protected synchronized void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); + + } + @Override + protected synchronized void clientException(ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); + } /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. */ + @Override protected void serverStopped() { System.out.println @@ -189,21 +206,6 @@ public static void main(String[] args) ServerConsole sc = new ServerConsole(port); sc.accept(); } - @Override - protected void clientConnected(ConnectionToClient client) { - System.out.println("A new client is attempting to connect to the server."); - } - - @Override - protected synchronized void clientDisconnected(ConnectionToClient client) { - System.out.println(client.getInfo("loginid") + " has disconnected."); - sendToAllClients(client.getInfo("loginid") + " has disconnected."); - } - @Override - protected synchronized void clientException(ConnectionToClient client, Throwable exception) { - System.out.println(client.getInfo("loginid") + " has disconnected."); - sendToAllClients(client.getInfo("loginid") + " has disconnected."); - } } //End of EchoServer class From f11d5f849189f864c0b21263075fc26e0c1ba6a5 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 18:20:37 -0400 Subject: [PATCH 08/17] fix update --- code/simplechat1/ClientConsole.java | 8 ++-- code/simplechat1/EchoServer.java | 61 +++++++++++++++---------- code/simplechat1/ServerConsole.java | 3 +- code/simplechat1/client/ChatClient.java | 15 +++++- 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index 15e0a16..673d9d5 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -5,7 +5,7 @@ import java.io.*; import client.*; import common.*; - +import java.util.Scanner; /** * This class constructs the UI for a chat client. It implements the * chat interface in order to activate the display() method. @@ -107,7 +107,7 @@ public void display(String message) public static void main(String[] args) { String host = ""; - int port = 0; //The port number + int port ; //The port number String loginid=""; try{ loginid=args[0]; @@ -115,6 +115,7 @@ public static void main(String[] args) catch(Exception e){ System.out.println("You need to specify login ID. Terminating..."); System.exit(0); + return; } try @@ -126,6 +127,7 @@ public static void main(String[] args) host = "localhost"; } + Scanner in = new Scanner(System.in); try { @@ -133,7 +135,7 @@ public static void main(String[] args) } catch(ArrayIndexOutOfBoundsException e) { - port = DEFAULT_PORT; + port = in.nextInt(); } ClientConsole cc= new ClientConsole(loginid,host, port); diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 5c2f378..80433fd 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -5,6 +5,7 @@ import java.io.*; import ocsf.server.*; import common.*; +import java.util.Scanner; /** * This class overrides some of the methods in the abstract @@ -24,7 +25,7 @@ public class EchoServer extends AbstractServer * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; - ChatIF serverUI; + //Constructors **************************************************** @@ -33,10 +34,10 @@ public class EchoServer extends AbstractServer * * @param port The port number to connect on. */ - public EchoServer(int port, ChatIF serverUI) + public EchoServer(int port) { super(port); - this.serverUI=serverUI; + } @@ -80,7 +81,7 @@ public EchoServer(int port, ChatIF serverUI) } } public void handleMessageFromServerUI(String message){ - try{ + if(message.startsWith("#")){ switch(message.substring(0,7)){ case "#close": @@ -90,21 +91,25 @@ public void handleMessageFromServerUI(String message){ serverStopped(); break; case "#quit": - close(); - serverUI.display("server quits..."); + try{ + close();} + catch(IOException e){ + + } + System.out.println("server quits..."); break; case "#setport": if(!isListening()){ try{ int port = Integer.parseInt(message.substring(9)); setPort(port); - serverUI.display("Port is set to: "+ port); + System.out.println("Port is set to: "+ port); }catch(Exception e) { - serverUI.display("invalid port, please try again"); + System.out.println("invalid port, please try again"); return; } }else{ - serverUI.display("You need to close server before set port"); + System.out.println("You need to close server before set port"); return; } break; @@ -112,25 +117,27 @@ public void handleMessageFromServerUI(String message){ if(!isListening()){ try{ listen(); - serverUI.display("^^ logging you in ...^^"); + System.out.println("^^ logging you in ...^^"); }catch(Exception e) { - serverUI.display("Exception occurred when log in. please try again"); + System.out.println("Exception occurred when log in. please try again"); return; } }else{ - serverUI.display("You need to close server before start server"); + System.out.println("You need to close server before start server"); return; } break; case "#getport": - serverUI.display("Your current port is: " + getPort()); + System.out.println("Your current port is: " + getPort()); break; - } }else - serverUI.display(message); + default: + System.out.println("Invalid command"); + break; + } + }else { + System.out.println(message); this.sendToAllClients("SERVER MSG> " + message); - }catch(IOException e){ - serverUI.display("Could not send message from server. Terminating server."); - System.exit(1); + } } @@ -193,19 +200,27 @@ protected void serverStopped() public static void main(String[] args) { int port = 0; //Port to listen on - + Scanner in = new Scanner(System.in); + System.out.println("Please Enter a Port Number: "); try { port = Integer.parseInt(args[0]); //Get port from command line } catch(Throwable t) { - port = DEFAULT_PORT; //Set port to 5555 + port = in.nextInt(); } - ServerConsole sc = new ServerConsole(port); - sc.accept(); - } + 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 index 0c130a3..5a8a1e9 100644 --- a/code/simplechat1/ServerConsole.java +++ b/code/simplechat1/ServerConsole.java @@ -4,7 +4,6 @@ import common.ChatIF; import java.io.*; -import client.*; import common.*; /** * This class constructs the UI for a chat server, followed ClientConsole.java. @@ -35,7 +34,7 @@ public class ServerConsole implements ChatIF { */ public ServerConsole(int port) { - echoServer = new EchoServer(port,this); + echoServer = new EchoServer(port); try { echoServer.listen(); diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index 4dabf88..f1e05f9 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -44,8 +44,12 @@ public ChatClient(String loginid, String host, int port, ChatIF clientUI) super(host, port); //Call the superclass constructor this.clientUI = clientUI; this.loginid="Guest"; - openConnection(); + try + {openConnection(); sendToServer("#login ID: " + loginid); + }catch(IOException e){ + System.out.println("wait..."); + } } @@ -141,7 +145,14 @@ public void handleMessageFromClientUI(String message) } } public void connectionClosed(){ - clientUI.display("The server has shut down, quitting..."); + try { + if (!isConnected()) { + closeConnection(); + } + } catch (IOException e) { + connectionException(e); + clientUI.display("The server has shut down, quitting..."); + } } /* @param exception From c64c3c12dbe156f9f44f581ebb18b101faeb4f12 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 18:29:03 -0400 Subject: [PATCH 09/17] Update ClientConsole.java --- code/simplechat1/ClientConsole.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index 673d9d5..f42e218 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -113,7 +113,7 @@ public static void main(String[] args) loginid=args[0]; } catch(Exception e){ - System.out.println("You need to specify login ID. Terminating..."); + System.out.println("ERROR - No login ID specified. Connection aborted."); System.exit(0); return; } From ccc75f04577316af33b18c420b6e59f250a50389 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 18:35:24 -0400 Subject: [PATCH 10/17] fix message --- code/simplechat1/client/ChatClient.java | 2 +- testlog.txt | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 testlog.txt diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index f1e05f9..64c9f8a 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -48,7 +48,7 @@ public ChatClient(String loginid, String host, int port, ChatIF clientUI) {openConnection(); sendToServer("#login ID: " + loginid); }catch(IOException e){ - System.out.println("wait..."); + System.out.println("Cannot open connection. Awaiting command."); } } diff --git a/testlog.txt b/testlog.txt new file mode 100644 index 0000000..161001a --- /dev/null +++ b/testlog.txt @@ -0,0 +1,16 @@ +/** +* name: Xia(Cynthia) Sheng +* student number: 300091655 +* email: xshen061@uottawa.ca +**/ + +Testcase 2001: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. EchoServer +Server listening for connections on port 5555 + + +Testcase 2002: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole +ERROR - No login ID specified. Connection aborted. + +Testcase 2003: \ No newline at end of file From 5a37462ce5040a1f8dd3a61a0b2c5aaa346984ee Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 18:48:52 -0400 Subject: [PATCH 11/17] Update ChatClient.java --- code/simplechat1/client/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index 64c9f8a..5c3340c 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -43,7 +43,7 @@ public ChatClient(String loginid, String host, int port, ChatIF clientUI) { super(host, port); //Call the superclass constructor this.clientUI = clientUI; - this.loginid="Guest"; + this.loginid=loginid; try {openConnection(); sendToServer("#login ID: " + loginid); From 60ba40cf375c1cc5fec3c84a987cbdef6b167ecc Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 19:37:18 -0400 Subject: [PATCH 12/17] Update ChatClient.java --- code/simplechat1/client/ChatClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index 5c3340c..a31ac8c 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -151,7 +151,7 @@ public void connectionClosed(){ } } catch (IOException e) { connectionException(e); - clientUI.display("The server has shut down, quitting..."); + clientUI.display("Abnormal termination of connection."); } } @@ -160,7 +160,7 @@ public void connectionClosed(){ */ public void connectionException(Exception exception) { - clientUI.display("Exception occured when connecting, quitting..."); + clientUI.display("Abnormal termination of connection."); quit(); } From 3d596db344598dcfbd4511634c37e0261d0622eb Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 20:00:18 -0400 Subject: [PATCH 13/17] Update ChatClient.java --- code/simplechat1/client/ChatClient.java | 1 + 1 file changed, 1 insertion(+) diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index a31ac8c..2d7f203 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -173,6 +173,7 @@ public void quit() try { closeConnection(); + clientUI.display("client quits"); } catch(IOException e) {} System.exit(0); From b3687e67ab1396998e2cdf047b8913ff28a15508 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Sun, 14 Jun 2020 20:17:56 -0400 Subject: [PATCH 14/17] Update testlog.txt --- testlog.txt | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 143 insertions(+), 1 deletion(-) diff --git a/testlog.txt b/testlog.txt index 161001a..9e71871 100644 --- a/testlog.txt +++ b/testlog.txt @@ -13,4 +13,146 @@ Testcase 2002: C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole ERROR - No login ID specified. Connection aborted. -Testcase 2003: \ No newline at end of file +Testcase 2003: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole abc +Cannot open connection. Awaiting command... +Wait for client input) + +Testcase 2004: +server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc has logged on. + +client: +abc logged in. + +Testcase 2005: +server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +> Message received: Hello! from abc + +client: +abc logged in. +Hello +>Hello! + + +Testcase 2006: +Server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +A new client is attempting to connect to the server. +Message received: #login xyz from localhost (127.0.0.1) +xyz logged in. +Message received: xyz from xyz +Message received: abc from abc +serveruser +SERVER MSG>serveruser + +client: +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole abc +abc logged in. +xyz> xyz +abc +abc>abc +SERVER MSG> serveruser + + +C:\Users\xiash\Lloseng\Lloseng\code\simplechat1>java -classpath ..\..\code;. ClientConsole xyz +xyz logged in. +abc> abc +xyz +abc>xyz +SERVER MSG> serveruser + + +Testcase 2007: +server: +Server listening for connections on port 5555 +#quit +The server quits. +Server has stopped listening for connections. + +Testcase 2008: +Server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +#stop +Server has stopped listening for connections. +Message received: Hello! from abc +#start +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login xyz from localhost (127.0.0.1) +xyz logged in. +#quit +Server has stopped listening for connections. +> abc has disconnected from the server. +> abc has disconnected from the server. + +client: +abc logged in. +Hello! +abc> Hello! +>Abnormal termination of connection. + +xyz logged in +>Abnormal termination of connection. + + +testcase 2009: +server: +Server listening for connections on port 5555 +A new client is attempting to connect to the server. +Message received: #login abc from localhost (127.0.0.1) +abc logged in. +#stop +Server has stopped listening for connections. +#close +abc has disconnected from the server. + + +client: +abc logged in. +WARNING - Server has stopped listening for connections. +SERVER SHUTTING DOWN! DISCONNECTING! +> Abnormal termination of connection. + + +testcase 2010: +client: +#quit +Client quits. + + +testcase 2011: +client: +#logoff +Client quits. +#quit +Client quits. + +testcase 2013: +server: +Server listening for connections on port 1234 +#quit +server quits... +Server has stopped listening for connections. + + + + + + + + + From b7d2981f9eff22444174c08866cbe70f8c101c45 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Wed, 17 Jun 2020 22:05:46 -0400 Subject: [PATCH 15/17] Mixed branch lose lines of code when merge personal branchs --- code/simplechat1/EchoServer.java | 38 ++++++++++++++--------------- code/simplechat1/ServerConsole.java | 5 +++- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index 80433fd..d7145de 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -53,42 +53,38 @@ public EchoServer(int port) (Object msg, ConnectionToClient client) { String message=(String)msg; - String loginid=message.substring(11); - System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); - if (message.substring(0,10)=="#login ID: "){ + String[] mes = message.split(" "); + try + { + if (mes[0].equals("#login")){ if(client.getInfo("loginid")==null){ + String loginid = mes[1]; client.setInfo("loginid",loginid); - try{ - client.sendToClient( client.getInfo("loginid") +" logged in"); - }catch(IOException e){} + this.sendToAllClients( client.getInfo("loginid") +" logged in"); + }else{ - try{ - client.sendToClient("You have already logged in!"); - }catch(IOException e){ - } + client.sendToClient("Error, please try again!"); + client.close(); } - }else{ - if(client.getInfo("loginid")==null){ - try{ - client.sendToClient( "create a login ID first"); - client.close(); - }catch(IOException e){} } + }catch(Exception ex){} System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); this.sendToAllClients(client.getInfo("loginid") + "> " +msg); - } + } public void handleMessageFromServerUI(String message){ if(message.startsWith("#")){ switch(message.substring(0,7)){ case "#close": + sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); serverClosed(); break; case "#stop": serverStopped(); + sendToAllClients("WARNING - Server has stopped listening for connections."); break; case "#quit": try{ @@ -200,18 +196,18 @@ protected void serverStopped() public static void main(String[] args) { int port = 0; //Port to listen on - Scanner in = new Scanner(System.in); - System.out.println("Please Enter a Port Number: "); + try { port = Integer.parseInt(args[0]); //Get port from command line } catch(Throwable t) { - port = in.nextInt(); + port = DEFAULT_PORT; //Set port to 5555 } EchoServer sv = new EchoServer(port); + ServerConsole sc = new ServerConsole(sv); try { @@ -221,6 +217,8 @@ public static void main(String[] args) { System.out.println("ERROR - Could not listen for clients!"); } + sc.accept(); } + } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java index 5a8a1e9..0b5deeb 100644 --- a/code/simplechat1/ServerConsole.java +++ b/code/simplechat1/ServerConsole.java @@ -4,7 +4,6 @@ import common.ChatIF; import java.io.*; -import common.*; /** * This class constructs the UI for a chat server, followed ClientConsole.java. * It implements the chatIF interface in order to activate the display() method. @@ -46,6 +45,10 @@ public ServerConsole(int port) System.exit(1); } } + + public ServerConsole(EchoServer server){ + this.echoServer = server; + } //Instance methods ************************************************ /** From e4c590a0cc2898924d20bf757e2e828e0aabb7d3 Mon Sep 17 00:00:00 2001 From: xshen061 Date: Wed, 17 Jun 2020 22:40:49 -0400 Subject: [PATCH 16/17] Update EchoServer.java --- code/simplechat1/EchoServer.java | 280 +++++++++++++++---------------- 1 file changed, 133 insertions(+), 147 deletions(-) diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d7145de..2178b67 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -8,8 +8,8 @@ import java.util.Scanner; /** - * This class overrides some of the methods in the abstract - * superclass in order to give more functionality to the server. + * 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 * @author Dr Robert Laganière @@ -17,208 +17,194 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer -{ - //Class variables ************************************************* - +public class EchoServer extends AbstractServer { + // Class variables ************************************************* + /** * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; - - //Constructors **************************************************** - + // 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); } - - //Instance methods ************************************************ - + // Instance methods ************************************************ + /** * This method handles any messages received from the client. * - * @param msg The message received from the client. + * @param msg The message received from the client. * @param client The connection from which the message originated. */ - public void handleMessageFromClient - (Object msg, ConnectionToClient client) - { - String message=(String)msg; + public void handleMessageFromClient(Object msg, ConnectionToClient client) { + String message = (String) msg; String[] mes = message.split(" "); - try - { - if (mes[0].equals("#login")){ - if(client.getInfo("loginid")==null){ - String loginid = mes[1]; - client.setInfo("loginid",loginid); - this.sendToAllClients( client.getInfo("loginid") +" logged in"); + try { + if (mes[0].equals("#login")) { + if (client.getInfo("loginid") == null) { + String loginid = mes[1]; + client.setInfo("loginid", loginid); + this.sendToAllClients(client.getInfo("loginid") + " logged in"); - }else{ + } else { client.sendToClient("Error, please try again!"); client.close(); + } } + } catch (Exception ex) { } - }catch(Exception ex){} System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); - - this.sendToAllClients(client.getInfo("loginid") + "> " +msg); - + + this.sendToAllClients(client.getInfo("loginid") + "> " + msg); + } - public void handleMessageFromServerUI(String message){ - - if(message.startsWith("#")){ - switch(message.substring(0,7)){ - case "#close": - sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); - serverClosed(); - break; - case "#stop": - serverStopped(); - sendToAllClients("WARNING - Server has stopped listening for connections."); - break; - case "#quit": - try{ - close();} - catch(IOException e){ - - } - System.out.println("server quits..."); - break; - case "#setport": - if(!isListening()){ - try{ - int port = Integer.parseInt(message.substring(9)); - setPort(port); - System.out.println("Port is set to: "+ port); - }catch(Exception e) { - System.out.println("invalid port, please try again"); - return; - } - }else{ - System.out.println("You need to close server before set port"); - return; - } - break; - case "#start": - if(!isListening()){ - try{ - listen(); - System.out.println("^^ logging you in ...^^"); - }catch(Exception e) { - System.out.println("Exception occurred when log in. please try again"); - return; - } - }else{ - System.out.println("You need to close server before start server"); - return; - } - break; - case "#getport": - System.out.println("Your current port is: " + getPort()); - break; - default: - System.out.println("Invalid command"); - break; - } - }else { - System.out.println(message); - this.sendToAllClients("SERVER MSG> " + message); + public void handleMessageFromServerUI(String message) { + + if (message.charAt(0)=='#') { + switch (message.substring(0)) { + case "#close": + sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); + serverClosed(); + break; + case "#stop": + serverStopped(); + sendToAllClients("WARNING - Server has stopped listening for connections."); + break; + case "#quit": + try { + close(); + } catch (IOException e) { + + } + System.out.println("server quits..."); + break; + case "#setport": + if (!isListening()) { + try { + int port = Integer.parseInt(message.substring(9)); + setPort(port); + System.out.println("Port is set to: " + port); + } catch (Exception e) { + System.out.println("invalid port, please try again"); + return; + } + } else { + System.out.println("You need to close server before set port"); + return; + } + break; + case "#start": + if (!isListening()) { + try { + listen(); + System.out.println("^^ logging you in ...^^"); + } catch (Exception e) { + System.out.println("Exception occurred when log in. please try again"); + return; + } + } else { + System.out.println("You need to close server before start server"); + return; + } + break; + case "#getport": + System.out.println("Your current port is: " + getPort()); + break; + default: + System.out.println("Invalid command"); + break; } + } else { + System.out.println(message); + this.sendToAllClients("SERVER MSG> " + message); + } - + } + /** - * This method overrides the one in the superclass. Called - * when the server starts listening for connections. + * This method overrides the one in the superclass. Called when the server + * starts listening for connections. */ @Override - protected void serverStarted() - { - System.out.println - ("Server listening for connections on port " + getPort()); + protected void serverStarted() { + System.out.println("Server listening for connections on port " + getPort()); } - + /** - * This method overrides the one in the superclass. - * Called called when the server is closed. + * This method overrides the one in the superclass. Called called when the + * server is closed. */ - @Override - protected void serverClosed() - { + @Override + protected void serverClosed() { System.out.println("Server has closed."); } + @Override - protected void clientConnected(ConnectionToClient client) { - System.out.println("A new client is attempting to connect to the server."); - } + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); + } + + @Override + protected synchronized void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); - @Override - protected synchronized void clientDisconnected(ConnectionToClient client) { - System.out.println(client.getInfo("loginid") + " has disconnected."); - sendToAllClients(client.getInfo("loginid") + " has disconnected."); - } + @Override protected synchronized void clientException(ConnectionToClient client, Throwable exception) { - System.out.println(client.getInfo("loginid") + " has disconnected."); - sendToAllClients(client.getInfo("loginid") + " has disconnected."); + System.out.println(client.getInfo("loginid") + " has disconnected."); + sendToAllClients(client.getInfo("loginid") + " has disconnected."); } + /** - * This method overrides the one in the superclass. Called - * when the server stops listening for connections. + * This method overrides the one in the superclass. Called when the server stops + * listening for connections. */ @Override - protected void serverStopped() - { - System.out.println - ("Server has stopped listening for connections."); + protected void serverStopped() { + System.out.println("Server has stopped listening for connections."); } - - //Class methods *************************************************** - + + // Class methods *************************************************** + /** - * This method is responsible for the creation of - * the server instance (there is no UI in this phase). + * 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 - * if no argument is entered. + * @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 + 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); ServerConsole sc = new ServerConsole(sv); - - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { + + try { + sv.listen(); // Start listening for connections + } catch (Exception ex) { System.out.println("ERROR - Could not listen for clients!"); } sc.accept(); } - + } -//End of EchoServer class +// End of EchoServer class From da632c5609d105de499cfe3960076850f3d38edd Mon Sep 17 00:00:00 2001 From: xshen061 Date: Thu, 18 Jun 2020 09:05:56 -0400 Subject: [PATCH 17/17] fix bug --- code/simplechat1/ClientConsole.java | 5 +- code/simplechat1/client/ChatClient.java | 230 ++++++++++++------------ 2 files changed, 111 insertions(+), 124 deletions(-) diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index f42e218..78a05eb 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -5,7 +5,6 @@ import java.io.*; import client.*; import common.*; -import java.util.Scanner; /** * This class constructs the UI for a chat client. It implements the * chat interface in order to activate the display() method. @@ -126,8 +125,6 @@ public static void main(String[] args) { host = "localhost"; } - - Scanner in = new Scanner(System.in); try { @@ -135,7 +132,7 @@ public static void main(String[] args) } catch(ArrayIndexOutOfBoundsException e) { - port = in.nextInt(); + port = DEFAULT_PORT; } ClientConsole cc= new ClientConsole(loginid,host, port); diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index 2d7f203..a861581 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -9,174 +9,164 @@ import java.io.*; /** - * This class overrides some of the methods defined in the abstract - * superclass in order to give more functionality to the client. + * This class overrides some of the methods defined in the abstract superclass + * in order to give more functionality to the client. * * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganiè * @author François Bélanger * @version July 2000 */ -public class ChatClient extends AbstractClient -{ - //Instance variables ********************************************** - +public class ChatClient extends AbstractClient { + // Instance variables ********************************************** + /** - * The interface type variable. It allows the implementation of - * the display method in the client. + * The interface type variable. It allows the implementation of the display + * method in the client. */ - ChatIF clientUI; + ChatIF clientUI; String loginid; - - //Constructors **************************************************** - + + // Constructors **************************************************** + /** * Constructs an instance of the chat client. * - * @param host The server to connect to. - * @param port The port number to connect on. + * @param host The server to connect to. + * @param port The port number to connect on. * @param clientUI The interface type variable. */ - - public ChatClient(String loginid, String host, int port, ChatIF clientUI) - throws IOException - { - super(host, port); //Call the superclass constructor + + public ChatClient(String loginid, String host, int port, ChatIF clientUI) throws IOException { + super(host, port); // Call the superclass constructor this.clientUI = clientUI; - this.loginid=loginid; - try - {openConnection(); - sendToServer("#login ID: " + loginid); - }catch(IOException e){ + this.loginid = loginid; + try { + openConnection(); + sendToServer("#login ID: " + loginid); + } catch (IOException e) { System.out.println("Cannot open connection. Awaiting command."); } } - - //Instance methods ************************************************ - + // 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 { - if(message.startsWith("#")){ - switch(message){ - case "#quit": - quit(); - case "#logoff": - quit(); - case "#sethost": - if(!isConnected()){ - try{ - String host = message.substring(0); - setHost(host); - System.out.println("Host is set to: "+host); - }catch(Exception e) { - clientUI.display("invalid host, please try again"); - return; - } - }else{ - clientUI.display("You need to log off before set host"); - return; - } - break; - case "#setport": - if(!isConnected()){ - try{ - int port =Integer.parseInt(message.substring(0)); - setPort(port); - System.out.println("Port is set to: "+ port); - }catch(Exception e) { - clientUI.display("invalid port, please try again"); - return; - } - }else{ - clientUI.display("You need to log off before set port"); - return; - } - break; - case "#login": - if(!isConnected()){ - try{ - openConnection(); - clientUI.display("^^ logging you in ...^^"); - }catch(Exception e) { - clientUI.display("Exception occurred when log in. please try again"); - return; - } - }else{ - clientUI.display("You already logged in."); - return; - } - break; - case "#gethost": - clientUI.display("Your current host is: " + getHost()); - break; - case "#getport": - clientUI.display("Your current port is: " + getPort()); - break; - } - }else - - - sendToServer(message); + public void handleMessageFromClientUI(String message) { + try { + if (message.charAt(0) == '#') { + switch (message) { + case "#quit": + quit(); + case "#logoff": + quit(); + case "#sethost": + if (!isConnected()) { + try { + String host = message.substring(0); + setHost(host); + System.out.println("Host is set to: " + host); + } catch (Exception e) { + clientUI.display("invalid host, please try again"); + return; + } + } else { + clientUI.display("You need to log off before set host"); + return; + } + break; + case "#setport": + if (!isConnected()) { + try { + int port = Integer.parseInt(message.substring(0)); + setPort(port); + System.out.println("Port is set to: " + port); + } catch (Exception e) { + clientUI.display("invalid port, please try again"); + return; + } + } else { + clientUI.display("You need to log off before set port"); + return; + } + break; + case "#login": + if (!isConnected()) { + try { + openConnection(); + clientUI.display("^^ logging you in ...^^"); + } catch (Exception e) { + clientUI.display("Exception occurred when log in. please try again"); + return; + } + } else { + clientUI.display("You already logged in."); + return; + } + break; + case "#gethost": + clientUI.display("Your current host is: " + getHost()); + break; + case "#getport": + clientUI.display("Your current port is: " + getPort()); + break; + default: + System.out.println("Invalid command"); + break; + } + } else + sendToServer(message); - } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); + } catch (IOException e) { + clientUI.display("Could not send message to server. Terminating client."); quit(); } } - public void connectionClosed(){ + + public void connectionClosed() { try { - if (!isConnected()) { - closeConnection(); - } - } catch (IOException e) { + if (!isConnected()) { + closeConnection(); + } + } catch (IOException e) { connectionException(e); clientUI.display("Abnormal termination of connection."); } } - - /* @param exception - * the exception raised. - */ - public void connectionException(Exception exception) { - - clientUI.display("Abnormal termination of connection."); + + /* + * @param exception the exception raised. + */ + public void connectionException(Exception exception) { + + clientUI.display("Abnormal termination of connection."); quit(); } - - + /** * This method terminates the client. */ - public void quit() - { - try - { + public void quit() { + try { closeConnection(); clientUI.display("client quits"); + } catch (IOException e) { } - catch(IOException e) {} System.exit(0); } } -//End of ChatClient class +// End of ChatClient class