From d33faa9ad934444a34635036ca3a7a62ab7bce95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=BF=81=E7=A3=8A?= Date: Sun, 31 May 2020 19:53:08 -0400 Subject: [PATCH 1/2] This is the phase 2 of simplechat. --- code/simplechat1/ServerConsole.java | 110 ++++++++++++++++++++++++++++ 1 file changed, 110 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..894b498 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,110 @@ +import java.io.*; +import ocsf.server.*; +import common.*; + + +public class ServerConsole implements ChatIF { + + //Class variables ************************************************* + + /** + * The default port to listen on. + */ + final public static int DEFAULT_PORT = 5555; + + //Instance variables ********************************************** + + /** + * The instance variable of the echoserver. + */ + EchoServer server; + + + //Constructors **************************************************** + + /** + * Constructs an instance of the ServerConsole UI. + * + * @param server The server to connect to. + */ + 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 server's message handler. + */ + public void accept() { + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true) + { + message = fromConsole.readLine(); + server.handleMessageFromServer(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); + } + + + //Class methods *************************************************** + + + /** + * 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. + */ + public static void main(String[] args) + { + int port = 0; //Port to listen on + + try + { + port = Integer.parseInt(args[0]); //Get port from command line + } + catch(Throwable t) + { + port = DEFAULT_PORT; //Set port to 5555 + } + + EchoServer sv = new EchoServer(port); + + try + { + sv.listen(); //Start listening for connections + } + catch (Exception ex) + { + System.out.println("ERROR - Could not listen for clients!"); + } + + ServerConsole chat = new ServerConsole(sv); + chat.accept(); + } +} From 1b7d3fb0f1996796859dd7f1745d899788f9f131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E7=BF=81=E7=A3=8A?= Date: Sun, 31 May 2020 21:29:04 -0400 Subject: [PATCH 2/2] This is phase 2 of the simple chat program. --- README.txt | 265 ++++++++++++++++++++++++ code/simplechat1/ClientConsole.java | 68 +++--- code/simplechat1/EchoServer.java | 208 +++++++++++++++---- code/simplechat1/ServerConsole.java | 2 +- code/simplechat1/client/ChatClient.java | 125 +++++++++-- 5 files changed, 583 insertions(+), 85 deletions(-) create mode 100644 README.txt diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..50940e2 --- /dev/null +++ b/README.txt @@ -0,0 +1,265 @@ +Student name: Wenglei Wu +Student number: 300136767 +Email address: wwu077@uottawa.ca + +Testcase 2001 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 + + +Testcase 2002 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole +Error: Login ID is not provided. Quit client. +wuwenglei@wuwengleideMacBook-Pro simplechat1 % + + + +Testcase 2003 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole abcde +Error: Can't setup connection! Terminating client. +wuwenglei@wuwengleideMacBook-Pro simplechat1 % + + +Testcase 2004 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client abcde connects. +New login from localhost (127.0.0.1), loginID: abcde + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole abcde +Connected! + + +Testcase 2005 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client abcde connects. +New login from localhost (127.0.0.1), loginID: abcde +Message received: Hello world! from localhost (127.0.0.1), loginID: abcde + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole abcde +Connected! +Hello world! +> abcde: Hello world! + + +Testcase 2006 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Tom connects. +New login from localhost (127.0.0.1), loginID: Tom +Client Alice connects. +New login from localhost (127.0.0.1), loginID: Alice +Client Jim connects. +New login from localhost (127.0.0.1), loginID: Jim +Message received: I am Tom. from localhost (127.0.0.1), loginID: Tom +Message received: I am Alice. from localhost (127.0.0.1), loginID: Alice +Message received: I am Jim. from localhost (127.0.0.1), loginID: Jim +This is the server receiving message from Tom, Alice, and Jim. +SERVER MSG> This is the server receiving message from Tom, Alice, and Jim. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Tom +Connected! +I am Tom. +> Tom: I am Tom. +> Alice: I am Alice. +> Jim: I am Jim. +> SERVER MSG> This is the server receiving message from Tom, Alice, and Jim. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Alice +Connected! +> Tom: I am Tom. +I am Alice. +> Alice: I am Alice. +> Jim: I am Jim. +> SERVER MSG> This is the server receiving message from Tom, Alice, and Jim. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Jim +Connected! +> Tom: I am Tom. +> Alice: I am Alice. +I am Jim. +> Jim: I am Jim. +> SERVER MSG> This is the server receiving message from Tom, Alice, and Jim. + + +Testcase 2007 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +#quit +Server has closed. +wuwenglei@wuwengleideMacBook-Pro simplechat1 % + + +Testcase 2008 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +#stop +Server has stopped listening for connections. +Server has stopped listening for connections. +Message received: Hello, I'm Sun. from localhost (127.0.0.1), loginID: Sun +#start +Server listening for connections on port 5555 +Client Alice connects. +New login from localhost (127.0.0.1), loginID: Alice +Client Jim connects. +New login from localhost (127.0.0.1), loginID: Jim + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +> [Warning] Server has stopped listening for connections. +Hello, I'm Sun. +> [Warning] Server has stopped listening for connections. +> Sun: Hello, I'm Sun. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Alice +Connected! + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Jim +Connected! + + +Testcase 2009 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +#stop +Server has stopped listening for connections. +Server has stopped listening for connections. +#close +Client Sun disconnects. +Server has closed. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +> [Warning] Server has stopped listening for connections. +SERVER SHUTTING DOWN! DISCONNECTING! +Abnormal termination of connection. +Connection closed! +wuwenglei@wuwengleideMacBook-Pro simplechat1 % + + +Testcase 2009 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +#close +Client Sun disconnects. +Server has stopped listening for connections. +Server has closed. +#start +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +Message received: This is a connect after a close and a start of the server. from localhost (127.0.0.1), loginID: Sun + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +SERVER SHUTTING DOWN! DISCONNECTING! +Abnormal termination of connection. +Connection closed! +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +This is a connect after a close and a start of the server. +> Sun: This is a connect after a close and a start of the server. + + +Testcase 2010 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +Client Sun disconnected. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +#quit +Connection closed! +wuwenglei@wuwengleideMacBook-Pro simplechat1 % + + +Testcase 2011 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +Client Sun disconnected. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +#logoff +Connection closed! + + +Testcase 2012 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +Client Sun disconnected. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +#logoff +Connection closed! +#sethost localhost2 +Host set to: localhost2 +#setport 1234 +Port set to: 1234 + + +Testcase 2013 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole 1234 +Server listening for connections on port 1234 + + +Testcase 2017 + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ServerConsole +Server listening for connections on port 5555 +Client Sun connects. +New login from localhost (127.0.0.1), loginID: Sun +Client Alice connects. +New login from localhost (127.0.0.1), loginID: Alice +Client Jim connects. +New login from localhost (127.0.0.1), loginID: Jim +Client Sun disconnected. +Client Alice disconnected. + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Sun +Connected! +> Alice has connected. +> Jim has connected. +#quit +Connection closed! +wuwenglei@wuwengleideMacBook-Pro simplechat1 % + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Alice +Connected! +> Jim has connected. +> Sun has disconnected. +#logoff +Connection closed! + +wuwenglei@wuwengleideMacBook-Pro simplechat1 % java ClientConsole Jim +Connected! +> Sun has disconnected. +> Alice has disconnected. diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..34a5140 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -1,6 +1,6 @@ // This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source -// license found at www.lloseng.com +// license found at www.lloseng.com import java.io.*; import client.*; @@ -9,30 +9,30 @@ /** * This class constructs the UI for a chat client. It implements the * chat interface in order to activate the display() method. - * Warning: Some of the code here is cloned in ServerConsole + * Warning: Some of the code here is cloned in ServerConsole * * @author François Bélanger - * @author Dr Timothy C. Lethbridge + * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganière * @version July 2000 */ -public class ClientConsole implements ChatIF +public class ClientConsole implements ChatIF { //Class variables ************************************************* - + /** * The default port to connect on. */ final public static int DEFAULT_PORT = 5555; - + //Instance variables ********************************************** - + /** * The instance of the client that created this ConsoleChat. */ ChatClient client; - + //Constructors **************************************************** /** @@ -41,13 +41,13 @@ 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 + try { - client= new ChatClient(host, port, this); - } - catch(IOException exception) + client= new ChatClient(loginID, host, port, this); + } + catch(IOException exception) { System.out.println("Error: Can't setup connection!" + " Terminating client."); @@ -55,28 +55,28 @@ public ClientConsole(String host, int port) } } - + //Instance methods ************************************************ - + /** - * This method waits for input from the console. Once it is + * This method waits for input from the console. Once it is * received, it sends it to the client's message handler. */ - public void accept() + public void accept() { try { - BufferedReader fromConsole = + BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; - while (true) + while (true) { message = fromConsole.readLine(); client.handleMessageFromClientUI(message); } - } - catch (Exception ex) + } + catch (Exception ex) { System.out.println ("Unexpected error while reading from console!"); @@ -89,33 +89,47 @@ public void accept() * * @param message The string to be displayed. */ - public void display(String message) + public void display(String message) { System.out.println("> " + message); } - + //Class methods *************************************************** - + /** * This method is responsible for the creation of the Client UI. * * @param args[0] The host to connect to. */ - public static void main(String[] args) + public static void main(String[] args) { + String loginID = ""; String host = ""; int port = 0; //The port number + try { + loginID = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) { + System.err.println("Error: Login ID is not provided. Quit client. "); + System.exit(1); + } 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; + } + ClientConsole chat= new ClientConsole(loginID, host, port); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..0ad9489 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -1,12 +1,14 @@ // This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source -// license found at www.lloseng.com +// license found at www.lloseng.com import java.io.*; import ocsf.server.*; +import common.*; +import java.util.LinkedList; /** - * This class overrides some of the methods in the abstract + * This class overrides some of the methods in the abstract * superclass in order to give more functionality to the server. * * @author Dr Timothy C. Lethbridge @@ -15,30 +17,50 @@ * @author Paul Holden * @version July 2000 */ -public class EchoServer extends AbstractServer +public class EchoServer extends AbstractServer { //Class variables ************************************************* - + /** * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; - + + //Instance variables ************************************************* + + /** + * This is a boolean variable indicating whether the server has stopped. + */ + private boolean serverStopped; + + /** + * This is a boolean variable indicating whether the server has closed + */ + private boolean serverClosed; + + /** + * This is to store all the clients who have ever made connections with the + * server, to help printing information of disconnction when all clients + * have already disconnected. + */ + LinkedList clients; + //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); + clients = new LinkedList(); } - + //Instance methods ************************************************ - + /** * This method handles any messages received from the client. * @@ -48,10 +70,51 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + try { + String message = (String) msg; + if (message.length() != 0) { + if (message.substring(0,1).equals("#")) { + if (message.length() >= 7) { + if (message.substring(1,6).equals("login")) { + if (client.getInfo("loginID") == null) { + client.setInfo("loginID", message.substring(7)); + System.out.println("Client "+client.getInfo("loginID")+" connects. "); + System.out.println("New login from "+client+", loginID: "+message.substring(7)); + for (ConnectionToClient cli: clients) { + cli.sendToClient(message.substring(7)+" has connected."); + } + clients.add(client); + client.sendToClient("#successfullogin"); + } else { + client.sendToClient("[Error] You have already logged in, loginID: "+client.getInfo("loginID")); + } + } else if (message.substring(1,7).equals("logoff")) { + clients.remove(client); + System.out.println("Client "+client.getInfo("loginID")+" disconnected. "); + for (ConnectionToClient cli: clients) { + cli.sendToClient(message.substring(7)+" has disconnected."); + } + } else { + System.err.println("[Error] Invalid command expression from client! "); + } + } + } else { + if (client.getInfo("loginID") == null) { + client.sendToClient("[Error] Missing login command with loginID, terminate connection! "); + client.close(); + } else { + if (serverStopped) { + client.sendToClient("[Warning] Server has stopped listening for connections. "); + } + System.out.println("Message received: " + msg + " from " + client+", loginID: "+client.getInfo("loginID")); + this.sendToAllClients(client.getInfo("loginID")+": "+msg); + } + } + } + } + catch (IOException e) {} } - + /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. @@ -60,8 +123,10 @@ protected void serverStarted() { System.out.println ("Server listening for connections on port " + getPort()); + serverStopped = false; + serverClosed = false; } - + /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. @@ -70,39 +135,102 @@ protected void serverStopped() { System.out.println ("Server has stopped listening for connections."); + serverStopped = true; } - - //Class methods *************************************************** - + /** - * 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. + * This method overrides the one in the superclass. Called + * when the server is closed. */ - public static void main(String[] args) - { - int port = 0; //Port to listen on + protected void serverClosed() { + System.out.println("Server has closed. "); + serverClosed = true; + } + - try - { - port = Integer.parseInt(args[0]); //Get port from command line + /** + * This method handles all data that comes in from the server. + * + * @param msg The message from the server. + */ + public void handleMessageFromServer(Object msg) { + try { + String message = (String) msg; + if (message.length() != 0) { + if (message.substring(0,1).equals("#")) { + handleCommandMessage(message.substring(1)); + } else { + this.sendToAllClients("SERVER MSG> "+message); + System.out.println("SERVER MSG> "+message); + } + } } - catch(Throwable t) - { - port = DEFAULT_PORT; //Set port to 5555 + catch (IOException e) {} + } + + /** + * This is a help method to handle commands. + * @param command The command message from the server sonsole without #. + */ + private void handleCommandMessage(String command) throws IOException { + if (command.equals("quit")) { + existingClientDisconnected(clients); + this.sendToAllClients("#servershutdown"); + close(); + System.exit(0); + } else if (command.equals("stop")) { + this.sendToAllClients("[Warning] Server has stopped listening for connections. "); + serverStopped(); + stopListening(); + } else if (command.equals("close")) { + existingClientDisconnected(clients); + this.sendToAllClients("#servershutdown"); + close(); + } else if (command.equals("start")) { + if (serverStopped) { + listen(); + } else { + System.err.println("[Error] The server is not stopped! "); + } + } else if (command.equals("getport")) { + System.out.println(getPort()); + } else if (command.length() >= 9) { + if (command.substring(0,8).equals("setport ")) { + if (serverClosed) { + try { + setPort(Integer.parseInt(command.substring(8))); + } + catch (Exception e){ + System.err.println("[Error] Invalid command expression! "); + } + } else { + System.err.println("[Error] The server is not closed! "); + } + } else { + System.err.println("[Error] Invalid command expression! "); + } + } else { + System.err.println("[Error] Invalid command expression! "); } - - EchoServer sv = new EchoServer(port); - - try - { - sv.listen(); //Start listening for connections - } - catch (Exception ex) - { - System.out.println("ERROR - Could not listen for clients!"); + } + + /** + * This is to printing information of disconnction when all clients + * have already disconnected with the help of variable clients. + * + * The purpose of setting this method is that when a client is quit, the + * connection is broken, which result in a null value when originally + * calling clientDisconnected method. + * + * @param clients A linkedlist storing all clients who have ever made + * connections with the server, no matter they are currently connected or not. + */ + private void existingClientDisconnected(LinkedList clients) { + for (ConnectionToClient cli: clients) { + try { + System.out.println("Client "+cli.getInfo("loginID")+" disconnects. "); + } + catch (Exception e) {} } } } diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java index 894b498..6832c4e 100644 --- a/code/simplechat1/ServerConsole.java +++ b/code/simplechat1/ServerConsole.java @@ -75,7 +75,7 @@ public void display(String message) /** * This method is responsible for the creation of - * the server instance (there is no UI in this phase). + * the server instance (there is a UI in this phase). * * @param args[0] The port number to listen on. Defaults to 5555 * if no argument is entered. diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..c690887 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -1,6 +1,6 @@ // This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source -// license found at www.lloseng.com +// license found at www.lloseng.com package client; @@ -20,16 +20,21 @@ public class ChatClient extends AbstractClient { //Instance variables ********************************************** - + /** - * The interface type variable. It allows the implementation of + * The interface type variable. It allows the implementation of * the display method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + + /** + * This variable stores loginID that the user entered. + */ + private String loginID; + - //Constructors **************************************************** - + /** * Constructs an instance of the chat client. * @@ -37,38 +42,55 @@ public class ChatClient extends AbstractClient * @param port The port number to connect on. * @param clientUI The interface type variable. */ - - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException + + public ChatClient(String loginID, String host, int port, ChatIF clientUI) + throws IOException { super(host, port); //Call the superclass constructor + this.loginID = loginID; this.clientUI = clientUI; openConnection(); + sendToServer("#login "+loginID); } - + //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()); + String message = (String) msg; + if (message.equals("#servershutdown")) { + System.err.println("SERVER SHUTTING DOWN! DISCONNECTING!"); + System.err.println("Abnormal termination of connection."); + quit(); + } else if (message.equals("#successfullogin")) { + System.out.println("Connected!"); + } else { + clientUI.display(msg.toString()); + } } /** - * This method handles all data coming from the UI + * This method handles all data coming from the UI * - * @param message The message from the UI. + * @param message The message from the UI. */ public void handleMessageFromClientUI(String message) { try { - sendToServer(message); + if (message.length() != 0) { + if (message.substring(0,1).equals("#")) { + handleCommandMessage(message.substring(1)); + } else { + sendToServer(message); + } + } } catch(IOException e) { @@ -77,7 +99,7 @@ public void handleMessageFromClientUI(String message) quit(); } } - + /** * This method terminates the client. */ @@ -90,5 +112,74 @@ public void quit() catch(IOException e) {} System.exit(0); } + + /** + * This method is called when the connection to the server is closed. + */ + public void connectionClosed() { + System.err.println("Connection closed! "); + } + + + /** + * This is the getter method of loginID. + */ + public String getLoginID() { + return loginID; + } + + /** + * This is a help method to handle commands. + * @param command The command message from the client sonsole without #. + */ + private void handleCommandMessage(String command) throws IOException { + if (command.equals("quit")) { + sendToServer("#logoff "+loginID); + quit(); + } else if (command.equals("logoff")) { + try + { + sendToServer("#logoff "+loginID); + closeConnection(); + } + catch(IOException e) {} + } else if (command.equals("login")) { + if (! isConnected()) { + openConnection(); + sendToServer("#login "+loginID); + } else { + System.err.println("[Error] The client is already connected! "); + } + } else if (command.equals("gethost")) { + System.out.println(getHost()); + } else if (command.equals("getport")) { + System.out.println(getPort()); + } else if (command.length() >= 9) { + if (command.substring(0,8).equals("sethost ")) { + if (! isConnected()) { + setHost(command.substring(8)); + System.out.println("Host set to: "+command.substring(8)); + } else { + System.err.println("[Error] The client is not logged off! "); + } + } else if (command.substring(0,8).equals("setport ")) { + if (! isConnected()) { + try { + setPort(Integer.parseInt(command.substring(8))); + System.out.println("Port set to: "+command.substring(8)); + } + catch (Exception e){ + System.err.println("[Error] Invalid command expression! "); + } + } else { + System.err.println("[Error] The client is not logged off! "); + } + } else { + System.err.println("[Error] Invalid command expression! "); + } + } else { + System.err.println("[Error] Invalid command expression! "); + } + } } //End of ChatClient class