diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..102b7e9 Binary files /dev/null and b/.DS_Store differ diff --git a/code/.DS_Store b/code/.DS_Store new file mode 100644 index 0000000..eec4db2 Binary files /dev/null and b/code/.DS_Store differ diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..f64cddd 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(client.getuserId()+"> "+message); + } } } catch (Exception ex) @@ -91,7 +128,7 @@ public void accept() */ public void display(String message) { - System.out.println("> " + message); + System.out.println("> " + message); } @@ -101,21 +138,39 @@ public void display(String message) * This method is responsible for the creation of the Client UI. * * @param args[0] The host to connect to. + * arg[1] port */ public static void main(String[] args) { String host = ""; int port = 0; //The port number - + String userId = ""; try { - host = args[0]; + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; + } - 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 d4f3a1a..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); } @@ -71,6 +87,16 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } + + protected void clientConnected(ConnectionToClient client){ + System.out.println(client.getInfo("userId") + " has logged on."); + } + protected void clientDisconnected(ConnectionToClient client){ + System.out.println("client is disconnected"+client.toString()); + } + protected void clientException(ConnectionToClient client,Throwable exception){ + System.out.println("client is now disconnected"); + } //Class methods *************************************************** @@ -84,7 +110,6 @@ protected void serverStopped() public static void main(String[] args) { int port = 0; //Port to listen on - try { port = Integer.parseInt(args[0]); //Get port from command line @@ -95,6 +120,7 @@ public static void main(String[] args) } EchoServer sv = new EchoServer(port); + try { @@ -104,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 fe1401e..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(); @@ -90,5 +109,24 @@ public void quit() catch(IOException e) {} System.exit(0); } + + + //print message when server disconnect + public void connectionException(Exception exception){ + System.out.println("The server closed, quit connection"); + quit(); + } + //when connection closed print message + 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 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. +