diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..a21ca5d 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -1,3 +1,8 @@ +/* +* This is the edited version of the ClientConsole class for the Assignment1. +* All edits made specifically for the question number will be added as a comment +*/ + // 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 @@ -40,12 +45,14 @@ public class ClientConsole implements ChatIF * * @param host The host to connect to. * @param port The port to connect on. + * new edit #1 E6 a) + * @param loginID The client login ID */ - 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) { @@ -62,35 +69,66 @@ public ClientConsole(String host, int port) * 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)); + public void accept(){ + boolean loginStatus = true; + try{ + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; - - while (true) - { + while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + //new edit #2 E6 a) + if(message.contains("#")){ + if (message.equals("#quit")){ + System.exit(0); + }else if(message.equals("#gethost")){ + System.out.println(client.getHost()); + }else if (message.equals("#getport")){ + System.out.println(client.getPort()); + }else if (message.equals("#logoff")){ + loginStatus = false; + client.handleMessageFromClientUI(client.getLogin()+" has logged off"); + System.out.println("the connection has closed."); + }else if (message.equals("#login")){ + if(loginStatus==true){ + System.out.println("You are logged in"); + } + loginStatus = true; + }else if (message.contains("#sethost")){ + if (loginStatus==false){ + client.setHost(message.split(" ")[1]); + }else{ + System.out.println("Cannot set host while logged in."); + } + }else if (message.contains("#setport")){ + if (loginStatus==false) { + String tmp =message.split(" ")[1]; + int tmp1 = Integer.parseInt(tmp); + client.setPort(tmp1); + }else{ + System.out.println("Cannot set port while logged in."); + } + }else{ + System.out.println("This function does not exist."); + } + }else{ + if(loginStatus==true){ + client.handleMessageFromClientUI(message); + }else{ + System.out.println("You are logged out, please log in."); + } + } } - } - catch (Exception ex) - { - System.out.println - ("Unexpected error while reading from console!"); + }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) - { + public void display(String message){ System.out.println("> " + message); } @@ -101,22 +139,30 @@ public void display(String message) * This method is responsible for the creation of the Client UI. * * @param args[0] The host to connect to. + * new edit#3 E6a) */ - public static void main(String[] args) - { + public static void main(String[] args){ String host = ""; + String loginID=""; int port = 0; //The port number - - try - { - host = args[0]; - } - catch(ArrayIndexOutOfBoundsException e) - { + try{ + 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){ + //new edit #2 E5b) + port = DEFAULT_PORT; + } + //new edit #3 E5b) + if (args.length !=0){ + loginID = args[0]; + ClientConsole chat=new ClientConsole(loginID,host,port); + chat.accept(); //Wait for console data + }else{ + System.out.println("You didn't login, please login."); + } } -} -//End of ConsoleChat class +} \ No newline at end of file diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..ca94d28 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -1,9 +1,17 @@ +/* +* This is the edited version of the EchoServer class for the Assignment1. +* All edits made specifically for the question number will be added as a comment +*/ + // This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com import java.io.*; import ocsf.server.*; +//new edit #1 E6 b) +import ocsf.server.*; +import common.ChatIF; /** * This class overrides some of the methods in the abstract @@ -23,17 +31,25 @@ public class EchoServer extends AbstractServer * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; - + + /** + *new edit #2 E6b) + *Instance variables + */ + ChatIF serverUI; + //Constructors **************************************************** /** * Constructs an instance of the echo server. * * @param port The port number to connect on. + * new edit #3 E6 b) line 48, line 51 */ - public EchoServer(int port) + public EchoServer(int port, ChatIF serverUI) { super(port); + this.serverUI = serverUI; } @@ -44,33 +60,80 @@ public EchoServer(int port) * * @param msg The message received from the client. * @param client The connection from which the message originated. + * new edit #4 E6 b) */ - public void handleMessageFromClient - (Object msg, ConnectionToClient client) + public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + String tmp = msg.toString(); + if (tmp.contains("#login")){ + System.out.println(msg); + this.sendToAllClients("Server MSG>: "+tmp.split(" ")[1]+"."); + client.setInfo("LoginID", tmp.split(" ")[1]); + }else{ + System.out.println("Message received: " + msg + " from " + client); + this.sendToAllClients(client.getInfo("Login ID ")+""+msg); + } } /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. + * new edit #5 E6 b) */ protected void serverStarted() { - System.out.println - ("Server listening for connections on port " + getPort()); + System.out.println ("Server listening for connections on port " + getPort()); + try{ + listen(); + }catch(IOException exception){ + System.out.println("Error"); + } + } + + +/** +* new edit #1 E5c) +* This method shows if the client has made a connection with the server +* @param client the client that is trying to make the connetion +* new edit #7 E6 b) line 100 +*/ + protected void clientConnected(ConnectionToClient client){ + System.out.println("A client is connected: "+client.getInfo("Login ID")); + } + +/** +* new edit #2 E5c) +* This method shows if the client has disconnected with the server +* @param client the client that has disconnected with the server +* new edit #8 E6 b) line 109 +*/ + synchronized protected void clientDisconnected(ConnectionToClient client){ + System.out.println("A client is disconnected: "+client.getInfo("Login ID")); } +/** +* new edit #3 E5c) +* This method is called whenever an exception is thrown +* @param client the client that had the exception +* new edit #9 E6 b) line 119 +*/ + synchronized protected void clientException(ConnectionToClient client, Throwable exception){ + System.out.println("Client error: "+client.getInfo("Login ID")); + } + + + /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. + * new edit #6 E6 b) */ protected void serverStopped() { - System.out.println - ("Server has stopped listening for connections."); + stopListening(); + System.out.println("Server has stopped listening for connections."); } +} //Class methods *************************************************** @@ -81,7 +144,7 @@ protected void serverStopped() * @param args[0] The port number to listen on. Defaults to 5555 * if no argument is entered. */ - public static void main(String[] args) +/* public static void main(String[] args) { int port = 0; //Port to listen on @@ -105,5 +168,5 @@ public static void main(String[] args) System.out.println("ERROR - Could not listen for clients!"); } } -} -//End of EchoServer class +}*/ +//End of EchoServer class \ No newline at end of file diff --git a/code/simplechat1/SEG2105 A1 Testcases.pdf b/code/simplechat1/SEG2105 A1 Testcases.pdf new file mode 100644 index 0000000..f7c55ff Binary files /dev/null and b/code/simplechat1/SEG2105 A1 Testcases.pdf differ diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..1e573e1 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,83 @@ +/* +* This is the ServerConsole calss for the Assignment1. +* Helps Echoserver access multiple methods and variables specific to the class +*/ + +import java.io.*; +import client.*; +import common.*; + +public class ServerConsole implements ChatIF { + private EchoServer server; + final public static int DEFAULT_PORT = 5555; + public ServerConsole(int port) { + server = new EchoServer(port,this); + } + public void display(String message) { + server.sendToAllClients("ServerMSG> " + message); + } + public void handleMessageFromConsole(String message){ + display(message); + } + public void accept(){ + try{ + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); + String message; + boolean status = true; + while (true) { + message = fromConsole.readLine(); + if(message.contains("#")){ + if(message.equals("#quit")){ + System.exit(0); + }else if(message.equals("#stop")){ + server.serverStopped(); + status = false; + }else if(message.equals("#start")){ + server.serverStarted(); + status= true; + }else if(message.equals("#close")) { + server.close(); + }else if(message.contains("#setport")){ + if(status==false){ + String temp =message.split(" ")[1]; + int temp1 = Integer.parseInt(temp); + server.setPort(temp1); + }else{ + System.out.println("Cannot set port while connections are made"); + } + }else if (message.equals("#getport")) { + System.out.println(server.getPort()); + }else{ + System.out.println("This function does not exist."); + } + }else{ + handleMessageFromConsole(message); + } + } + } + catch (Exception ex){ + System.out.println("Unexpected error while reading from console!"); + } +} + +public EchoServer get(){ + return server; +} + +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 server = new ServerConsole(port); + try{ + server.get().listen(); //Start listening for connections + server.accept(); + } + catch (Exception ex){ + System.out.println("ERROR - Could not listen for clients!"); + } +} +} diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..49f6d6e 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -1,3 +1,8 @@ +/* +* This is the edited version of the ChatClient class for the Assignment1. +* All edits made specifically for the question number will be added as a comment +*/ + // 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 @@ -26,23 +31,35 @@ public class ChatClient extends AbstractClient * the display method in the client. */ ChatIF clientUI; + //new edit #1 E7a) + String loginId; //Constructors **************************************************** +/* +* This is the edited version of the ChatClient class for the Assignment1. +* All edits made specifically for the question number will be added as a comment +*/ + /** * Constructs an instance of the chat client. * * @param host The server to connect to. * @param port The port number to connect on. * @param clientUI The interface type variable. + * + * new edit #2 E7a) line 57, line 62 + * @param loginId The login ID of the client + * */ - 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 = loginId; openConnection(); } @@ -72,12 +89,44 @@ public void handleMessageFromClientUI(String message) } catch(IOException e) { - clientUI.display - ("Could not send message to server. Terminating client."); + clientUI.display ("Could not send message to server. Terminating client."); quit(); } } - + +/** +* new edit #1 E5 a) +* This method lets the client know that the server has shut down +* and will be quitting/stopping the connection +*/ +protected void connectionClosed(){ + System.out.println("The server has closed, will quit connection shortly"); +} + +/** +*new edit #3 E7a) +*This method returns the loginID of the client +*/ +public String getLogin(){ + return loginId; +} + +/** +*new edit #4 E7b) +*This method sends the loginID to the server. +*/ +public void sendLogin(){ + handleMessageFromClientUI("#login: "+getLogin()); +} + +/** +*new edit #5 E7b) +*This method lets the server/client know that a connection has been established +*/ +protected void connectionEstablish(){ + sendLogin(); +} + /** * This method terminates the client. */