From 60e8821d3180e0bf783f7a2f21da9232fd5e33e9 Mon Sep 17 00:00:00 2001 From: ah110 <67201388+ah110@users.noreply.github.com> Date: Sat, 20 Jun 2020 18:18:04 -0400 Subject: [PATCH 1/3] E5(a,b,c) server and client recognizes connect and disconnect --- code/.DS_Store | Bin 0 -> 6148 bytes code/simplechat1/ClientConsole.java | 3 +++ code/simplechat1/EchoServer.java | 10 ++++++++++ code/simplechat1/client/ChatClient.java | 11 +++++++++++ 4 files changed, 24 insertions(+) create mode 100644 code/.DS_Store diff --git a/code/.DS_Store b/code/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b77f88e08f17ab3ce9090e39744bd0da28a977fb GIT binary patch literal 6148 zcmeH~K?=e^3`G;|LU7Zi%h`AUZ!n0SpcfDn-AD_HuIK3fWP)IIEh0aV{7Gg)>05Lf z5z+PSuo7uSWCk~trG=3x@xC+1b{XuyJ79K1T}(S!u1*@b}wNMJ-@TJzTK|1JE}{6A`8N&+PC zX9Tp_belC^D(=>|*R%RAs Date: Sun, 21 Jun 2020 06:02:51 -0400 Subject: [PATCH 2/3] final added ServerConsole Updated ChatClient and ClientConsole and EchoServer --- .DS_Store | Bin 0 -> 6148 bytes code/.DS_Store | Bin 6148 -> 6148 bytes code/simplechat1/ClientConsole.java | 94 ++++++++++++++++----- code/simplechat1/EchoServer.java | 25 +++++- code/simplechat1/ServerConsole.java | 105 ++++++++++++++++++++++++ code/simplechat1/client/ChatClient.java | 35 +++++++- 6 files changed, 231 insertions(+), 28 deletions(-) create mode 100644 .DS_Store create mode 100644 code/simplechat1/ServerConsole.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..42a17cf2a85585d9e663d95ef374656ef90e43e6 GIT binary patch literal 6148 zcmeHK!A=4(5N!dqV2m70nOg4xIZzgN>paynbBAeirz#>t^>~H8F`33%t zGi@OfH1TAN%p}t{ozArB+fAosjPb@OC^MF4j0sT0R2G^q1jkXABxOB_9Ov-I!@l2B zKJ?#~qRH_W8NhFsVK+>%fOS59Kfm7#hjFR&&Qs~k+WLkNIg#7SUAtX1c4IFd4IAFz z5__j!-1V(}rc`fkd;WRY8MpE~M=FZFFzO6+au{?VMKKVtyT2MLPkSWFD+qXQbeKH_)= z5d~~~OCU;%j>W_vjDT>H3TRTfePVEv4t{Cl9E*uTlg_wa8RoGobNfQ!dUfzi9nQF8 zkXmAZ7+7Q=ZMu1^|Ht3I{}+>}M+^`H|B3;gX}Zk@Y{}NvrOjcjm7r}<6pTv@&Qrk9 hr5Iwd6c<3XfM22k=vYh)f(L{y0-6SDh=E^a-~*bzP>28k literal 0 HcmV?d00001 diff --git a/code/.DS_Store b/code/.DS_Store index b77f88e08f17ab3ce9090e39744bd0da28a977fb..eec4db243f27718e857a54a8b7fdd3a122a08d97 100644 GIT binary patch delta 108 zcmZoMXfc=|&e%S&P>hv>fq{WzVxb5p6OaJ{OcMjF8JQ;PNQ-kb6f{m6o5DZh{0g9qsU=q0C{l~ AMgRZ+ delta 74 zcmZoMXfc=|&Zs)EP?(W%VvjT@69WV=P7JW#_&}X~;)Br5>>L6djH(+ierKM{FQO>O S2vm^(QwF3PHam(OW(EL}a}Y)V diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index b02a185..e0d2834 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,18 +41,12 @@ 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 host, int port, String userId) { - try - { - client= new ChatClient(host, port, this); - } - catch(IOException exception) - { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); - } + + client= new ChatClient(host, port,this, userId); + + } @@ -66,14 +60,57 @@ public void accept() { try { - BufferedReader fromConsole = - new BufferedReader(new InputStreamReader(System.in)); + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; - while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + String[] cmd = message.split(" "); + if(message.charAt(0)==('#')){ + if(message.equals("#quit")){ + System.out.println("client quit"); + client.quit(); + } + else if((cmd[0].equals("#setport"))||(cmd[0].equals("#sethost"))){ + if (client.isConnected()){ + System.out.println("The client is already connected."); + } + else{ + if(cmd[0].equals("#sethost")){ + client.setHost(cmd[1]); + System.out.println("Host set to :"+cmd[1]); + } + else{ + System.out.println("Port set to :"+cmd[1]); + client.setPort(Integer.parseInt(cmd[1])); + } + } + } + else if(message.equals("#logoff")){ + client.closeConnection(); + } + else if(message.equals("#login")){ + if (client.isConnected()){ + System.out.println("The client is already connected."); + } + else{ + client.openConnection(); + client.handleMessageFromClientUI( client.getuserId()+" has logged on"); + } + } + else if(message.equals("#getport")){ + System.out.println(client.getPort()); + } + else if(message.equals("#gethost")){ + System.out.println(client.getHost()); + } + else{ + System.out.println("invaild command"); + } + } + else{ + client.handleMessageFromClientUI(message); + } } } catch (Exception ex) @@ -91,7 +128,7 @@ public void accept() */ public void display(String message) { - System.out.println("> " + message); + System.out.println(client.getuserId()+"> " + message); } @@ -107,18 +144,33 @@ public static void main(String[] args) { String host = ""; int port = 0; //The port number - + String userId = ""; try { - host = args[0]; - port = Integer.parseInt(arg[1]); + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; - port = DEFAULT_PORT; + } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + try + { + port = Integer.parseInt(args[2]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = DEFAULT_PORT; + } + try + { + userId = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + userId = ""; + } + ClientConsole chat= new ClientConsole(host, port,userId); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index be3c08c..0427a75 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -48,7 +48,23 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); + try{ + String a =msg.toString(); + String cmd[]=a.split(" "); + if (cmd[0].equals("#login")) { + if(client.getInfo("userId")==null){ + client.setInfo("userId",cmd[1]); + this.sendToAllClients(client.getInfo("userId") + " has logged on."); + } + else{ + client.sendToClient("A user has already logged on"); + } + } + } + catch(Exception e){ + + } + System.out.println("Message received: " + msg + " from " + client.getInfo("userId")+" "+client); this.sendToAllClients(msg); } @@ -73,7 +89,7 @@ protected void serverStopped() } protected void clientConnected(ConnectionToClient client){ - System.out.println("client is connected :"+client.toString()); + System.out.println(client.getInfo("userId") + " has logged on."); } protected void clientDisconnected(ConnectionToClient client){ System.out.println("client is disconnected"+client.toString()); @@ -94,7 +110,6 @@ protected void clientException(ConnectionToClient client,Throwable exception){ public static void main(String[] args) { int port = 0; //Port to listen on - try { port = Integer.parseInt(args[0]); //Get port from command line @@ -105,6 +120,7 @@ public static void main(String[] args) } EchoServer sv = new EchoServer(port); + try { @@ -114,6 +130,9 @@ public static void main(String[] args) { System.out.println("ERROR - Could not listen for clients!"); } + ServerConsole sc = new ServerConsole(sv); + sc.accept(); + } } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..ba58310 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,105 @@ + +import java.io.*; +import common.*; + +public class ServerConsole implements ChatIF +{ + //Class variables ************************************************* + + private EchoServer server; + + + //Constructors **************************************************** + + /** + * Constructs an instance of the ClientConsole UI. + * + * @param port The port to connect on. + */ + public ServerConsole(EchoServer server){ + + this.server = server; + } + + + + //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(); + if(message.charAt(0)==('#')){ + if(message.equals("#quit")){ + System.out.println("server quit"); + server.close(); + } + else if(message.equals("#setport")){ + if (server.isListening()){ + System.out.println("The serveris already connected."); + } + else{ + String[] cmd = message.split(" "); + try + { + server.setPort(Integer.parseInt(cmd[1])); + } + catch(NumberFormatException e) + { + System.out.println("invaild input"); + } + } + } + else if(message.equals("#stop")){ + server.sendToAllClients("server stop listening"); + server.stopListening(); + } + else if(message.equals("#close")){ + server.sendToAllClients("server stop listening and stop connecting"); + server.close(); + } + else if(message.equals("#start")){ + if (server.isListening()){ + System.out.println("The serveris already connected."); + } + else{ + server.listen(); + } + } + else if(message.equals("#getport")){ + System.out.println(server.getPort()); + } + } + server.sendToAllClients("Server MSG> " + 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); + } +} +//End of ConsoleChat class diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index f56af26..1cccdc3 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -25,7 +25,8 @@ public class ChatClient extends AbstractClient * The interface type variable. It allows the implementation of * the display method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + private String userId; //Constructors **************************************************** @@ -38,12 +39,29 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException + public ChatClient(String host, int port, ChatIF clientUI, String userId) { super(host, port); //Call the superclass constructor this.clientUI = clientUI; - openConnection(); + this.userId = userId; + + try + { + openConnection(); + + if(userId==""){ + System.out.println("ERROR - No login ID specified. Connection aborted."); + quit(); + } + else{ + System.out.println(userId+" has logged on."); + } + } + catch(IOException exception) + { + System.out.println("Cannot open connection. Awaiting command."); + } + } @@ -72,6 +90,7 @@ public void handleMessageFromClientUI(String message) } catch(IOException e) { + clientUI.display ("Could not send message to server. Terminating client."); quit(); @@ -101,5 +120,13 @@ public void connectionException(Exception exception){ public void connectionClosed() { System.out.println("Connection Closed"); } + public String getuserId(){ + return userId; + } + public void setuserId(String userId){ + this.userId = userId; + } + + } //End of ChatClient class From 49e234faddd058733ebc024c386ec0e3eba378a4 Mon Sep 17 00:00:00 2001 From: ah110 <67201388+ah110@users.noreply.github.com> Date: Sun, 21 Jun 2020 06:54:41 -0400 Subject: [PATCH 3/3] add testcase student information inside testcase.txt --- .DS_Store | Bin 6148 -> 6148 bytes code/simplechat1/ClientConsole.java | 4 +- testcase.txt | 138 ++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 testcase.txt diff --git a/.DS_Store b/.DS_Store index 42a17cf2a85585d9e663d95ef374656ef90e43e6..102b7e9aa65065a21cae2dd6bd91bbd5dc91cd07 100644 GIT binary patch delta 21 ccmZoMXffEZoQcE8(m+SS$kbr-dZtiO07t|I761SM delta 21 ccmZoMXffEZoQXr%#7sxQ*xYFIdZtiO07t6^7XSbN diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index e0d2834..f64cddd 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -109,7 +109,7 @@ else if(message.equals("#gethost")){ } } else{ - client.handleMessageFromClientUI(message); + client.handleMessageFromClientUI(client.getuserId()+"> "+message); } } } @@ -128,7 +128,7 @@ else if(message.equals("#gethost")){ */ public void display(String message) { - System.out.println(client.getuserId()+"> " + message); + System.out.println("> " + message); } diff --git a/testcase.txt b/testcase.txt new file mode 100644 index 0000000..0f8b23b --- /dev/null +++ b/testcase.txt @@ -0,0 +1,138 @@ +Student name: Andy Huang +Student number: 300117248 +Email: chuan11@gmail.com +getInfo return null don't know why + + +Case 2001 Server startup check with default arguments +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServer +Server listening for connections on port 5555 + +Case 2002 Client startup check without a login +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole +ERROR - No login ID specified. Connection aborted. +Connection Closed + +Case 2003 Client startup check with a login and without a server +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole andyhuang +Cannot open connection. Awaiting command. + +Case 2004 Client connection with default arguments +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConle andyhuang +andyhuang has logged on. + +Case 2005 Client Data transfer and data echo +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConle andyhuang +andyhuang has logged on. +dfigdiyr +andyhuang> dfigdiyr + +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServer +Server listening for connections on port 5555 +null has logged on. +Message received: dfigdiyr from null localhost (127.0.0.1) + +Case 2006 Multiple local connections +Server: +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServer +Server listening for connections on port 5555 +null has logged on. +Message received: andyhuang> fgudskf from null localhost (127.0.0.1) +null has logged on. +Message received: Wenzhema> rgew from null localhost (127.0.0.1) +Message received: Wenzhema> efegewr from null localhost (127.0.0.1) +sdkjashbfjkds +Client Andy: +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole andyhuang +andyhuang has logged on. +fgudskf +> andyhuang> fgudskf +> Wenzhema> rgew +> Wenzhema> efegewr +> Server MSG> sdkjashbfjkds +Client Wen: +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole Wenzhema +Wenzhema has logged on. +rgew +> Wenzhema> rgew +efegewr +> Wenzhema> efegewr +> Server MSG> sdkjashbfjkds + +Case 2007 Server termination command check +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServer +Server listening for connections on port 5555 +#quit +server quit +Server has stopped listening for connections. + +Case 2008 server stop check +Server: ^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServer +Server listening for connections on port 5555 +null has logged on. +#stop +Server has stopped listening for connections. +Message received: Wenzhema> hbjk from null localhost (127.0.0.1) +#start +Server listening for connections on port 5555 +null has logged on. + +Client: +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole Wenzhema +Wenzhema has logged on. +> server stop listening +> Server MSG> #stop + +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole andyhuang +andyhuang has logged on. + +Case 2009 close command check +Server listening for connections on port 5555 +null has logged on. +#close +client is disconnectednull +client is disconnectednull +Server has stopped listening for connections. + +The server closed, quit connection +Connection Closed + +start command check + +#start +Server listening for connections on port 5555 +null has logged on + +Case 10 Client termination command check +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole Wenzhema +Wenzhema has logged on. +#quit +client quit +Connection Closed + +Case 11 client logoff check +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole Wenzhema +Wenzhema has logged on. +#logoff +Connection Closed + +Case 12 client set command check +owners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConsole Wenzhema +Cannot open connection. Awaiting command. +#setport 1234 +Port set to :1234 +#sethost hello +Host set to :hello + +Case 13 Starting a server on a non-default port +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServe1234 +Server listening for connections on port 1234 + +Case 14 Connecting a client to a non-default host or port +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' EchoServe1234 +Server listening for connections on port 1234 +null has logged on. + +^Cowners-MacBook-Pro:simplechat1 owner$ java -classpath '../../code:.' ClientConle Wenzhema localhost 1234 +Wenzhema has logged on. +