diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..0245bd2 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -2,9 +2,11 @@ // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com +package simplechat1; + import java.io.*; -import client.*; -import common.*; +import simplechat1.client.*; +import simplechat1.common.*; /** * This class constructs the UI for a chat client. It implements the @@ -41,17 +43,17 @@ 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); + //System.out.println(host+" :"+ port); + client= new ChatClient(loginid, host, port, this); } catch(IOException exception) { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); + System.out.println("Error: Can't setup connection! Terminating client."); + //System.exit(1); } } @@ -91,32 +93,57 @@ public void accept() */ public void display(String message) { - System.out.println("> " + message); + //Objeect id=client.getInfo("loginId"); + 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. + * @param args[0] login id + * @param args[1] host + * @param args[2] port */ public static void main(String[] args) { + String loginId =""; + int port = 0; String host = ""; - int port = 0; //The port number + + + try { + loginId = args[0]; + + } catch (Exception e) { + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(0); + } + try { - host = args[0]; + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + try{ + port = Integer.valueOf(args[2]); + } + catch(ArrayIndexOutOfBoundsException e){ + port = DEFAULT_PORT; + } + //System.out.println(loginId); + // System.out.println(host); + //System.out.println(port); + ClientConsole chat= new ClientConsole(loginId,host, port); chat.accept(); //Wait for console data + } + } -//End of ConsoleChat class + +//End of ConsoleChat class \ No newline at end of file diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..6053b89 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -1,7 +1,9 @@ // 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 +package simplechat1; +import simplechat1.common.*; import java.io.*; import ocsf.server.*; @@ -15,95 +17,240 @@ * @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; - + //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 ************************************************ - + /** * 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) - { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + (Object msg, ConnectionToClient client) { + + String message = msg.toString(); + + String[] idArray = message.split(" "); + + + if (idArray[0].equals("#login")) { + //System.out.println("h00"); + try { + if (client.getInfo("loginId") != null) { + client.sendToClient("Error - You has already logined."); + } else { + client.setInfo("loginId", idArray[1]); + //System.out.println("h01"); + System.out.println("Message received: " + msg + " from " + client.getInfo("loginId")); + //System.out.println("h02"); + client.setInfo("loginId", idArray[1]); + //System.out.println("h03"); + System.out.println(client.getInfo("loginId") + " has logged on"); + this.sendToAllClients(client.getInfo("loginId") + " has logged on"); + } + } catch(IOException e){ + + } + // + } + else{ + if (client.getInfo("loginId") == null){ + try { + client.close(); + } + catch (IOException e) {} + } + else{ + //System.out.println(client.getInfo("loginId")); + //System.out.println(String.valueOf(client.getInfo("loginId "))); + System.out.println + ("Message received: " + msg + " from " + client.getInfo("loginId")); + this.sendToAllClients(client.getInfo("loginId")+"> "+msg); + } + } } - + + /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. */ - protected void serverStarted() - { + protected void serverStarted() { System.out.println - ("Server listening for connections on port " + getPort()); + ("Server listening for connections on port " + getPort()); } - + /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. */ - protected void serverStopped() - { + protected void serverStopped() { System.out.println - ("Server has stopped listening for connections."); + ("Server has stopped listening for connections."); + } + + //protected void serverClosed(){System.out.println("Server offline");} + + public void commandMethod(String message) throws IOException { + + //seperates the command from the arguments (if pertinent - for #sethost and #setport) + + String[] argumentsArray = message.split(" "); + switch (argumentsArray[0]) { + //System.out.println("123"); + + //#quitCauses the server to quit gracefull + case "#quit": + + stopListening(); + + System.out.println("The server quits."); + sendToAllClients("The server quits."); + System.exit(0); + break; + + //#stop Causes the server to stop listening for new clients. + case "#stop": + + //System.out.println("Server has stopped listening for connections."); + sendToAllClients("WARNING - Server has stopped listening for connections."); + stopListening();break; + + + //#close Causes the server not only to stop listening for new clients, + // but also to disconnect all existing clients + case "#close": + try { + sendToAllClients("WARNING - The server has stopped listening for connections"); + sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!"); + close(); + } catch (IOException e) { + } + System.out.println("Server offline"); + break; + + + //#setport Calls the setPort method in the server. + // Only allowed if the server is close + case "#setport": + if (!isListening() && getNumberOfClients() == 0) { + try { + setPort(Integer.parseInt(argumentsArray[1])); + System.out.println("port now is " + getPort()); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("Please use the following format: #setport "); + } + } else { + System.out.println("The server is open. Please close the server first"); + System.out.println("You can use #close to close it"); + } + break; + + //#start Causes the server to start listening for new clients. + // Only valid if the server is stopped + case "#start": + listen(); + break; + + //#getport Displays the current port number + case "#getport": + System.out.println("The port number is: " + Integer.toString(getPort())); + break; + + //if not the command has be setted + default: + System.out.println("This is not a acceptable command"); + System.out.println("Please use one of the following command:"); + System.out.println("#quit, #stop, #close, #setport, #start, #getport"); + break; + } + + } + + /** + * This method handles all data coming from the UI + * + * @param message The message from the UI. + */ + public void handleMessageFromServerUI(String message) { + //Checks if the message is start with "#" which is a command + + String[] argumentsArray = message.split(" "); + if (argumentsArray[0].subSequence(0, 1).equals("#")) { + try { + commandMethod(message); + } catch (Exception e) { + + } + } else { + + sendToAllClients("SERVER MSG> " + message); + //Any message originating from the end-user of the server + // should be prefixed by the string "SERVER MSG> " + System.out.println("SERVER MSG> " + message); + } + } + + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); + } + + + protected synchronized void clientDisconnected(ConnectionToClient client) { + sendToAllClients(client.getInfo("loginId") + " has disconnected."); + System.out.println(client.getInfo("loginId") + " has disconnected."); + } + + + protected synchronized void clientException(ConnectionToClient client, Throwable exception) { + clientDisconnected(client); } - //Class methods *************************************************** - + /** - * This method is responsible for the creation of + * 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) - { + public static void main(String[] args) { int port = 0; //Port to listen on - try - { + try { port = Integer.parseInt(args[0]); //Get port from command line - } - catch(Throwable t) - { + } catch (Throwable t) { port = DEFAULT_PORT; //Set port to 5555 } - + EchoServer sv = new EchoServer(port); - - try - { + try { sv.listen(); //Start listening for connections - } - catch (Exception ex) - { + } 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 new file mode 100644 index 0000000..19ea9b3 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,95 @@ +package simplechat1; + +import java.io.*; +import simplechat1.client.*; +import simplechat1.common.*; + + +//some part copy from ClientConsole + +public class ServerConsole 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; + EchoServer server; + + public ServerConsole(int port) { + //System.out.println("00"); + server = new EchoServer(port); + //System.out.println(port); + try { + server.listen(); + //System.out.println("02"); + } catch (Exception e) { + System.out.println("error: Can not listen form EchoServer"); + System.exit(0); + } + } + //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(); + server.handleMessageFromServerUI(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] port + */ + public static void main(String[] args) { + int port = 0; + try { + port = Integer.valueOf(args[0]); + } catch (ArrayIndexOutOfBoundsException e) { + port = DEFAULT_PORT; + } + ServerConsole chat = new ServerConsole(port); + chat.accept(); //Wait for console data + + } +} + diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..6247475 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -2,10 +2,10 @@ // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com -package client; +package simplechat1.client; import ocsf.client.*; -import common.*; +import simplechat1.common.*; import java.io.*; /** @@ -25,11 +25,12 @@ 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 loginId; //Constructors **************************************************** - + /** * Constructs an instance of the chat client. * @@ -38,12 +39,29 @@ 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; - openConnection(); + try { + //System.out.println("0"); + //openConnection(); + this.loginId = loginid; + handleMessageFromClientUI("#login " + loginId); + //sendToServer("#login " + loginId); + /*this.loginId = loginId; + System.out.println("1"); + + System.out.println("2"); + + */ + //sendToServer("#login " + loginId); + //System.out.println("2"); + //openConnection(); + } catch (Exception e) { + System.out.println("Cannot open connection. Awaiting command."); + } } @@ -59,36 +77,133 @@ public void handleMessageFromServer(Object msg) clientUI.display(msg.toString()); } + protected void connectionClosed() { + //System.out.println("Client terminates."); + + } + + protected void connectionException(Exception exception) { + quit(); + } + + /** * This method handles all data coming from the UI * * @param message The message from the UI. */ - public void handleMessageFromClientUI(String message) - { - try - { - sendToServer(message); + public void handleMessageFromClientUI(String message) { + + String[] argumentsArray = message.split(" "); + //System.out.println(argumentsArray[0]); + + if(argumentsArray[0].subSequence(0, 1).equals("#")) { + switch (argumentsArray[0]) { + case "#quit": + quit(); + break; + + case "#logoff": + if (isConnected()) { + try { + closeConnection(); + System.out.println("Client log off "); + } catch (IOException e) { + System.out.println("Client log off fail"); + } + } + break; + + case "#sethost": + if (isConnected()) { + clientUI.display("Only allowed if the client is logged off"); + return; + } + try { + String host = argumentsArray[1]; + setHost(host); + System.out.println("Host set to: " + host); + } catch (Exception e) { + clientUI.display("Invalid host."); + } + break; + + case "#setport": + if (isConnected()) { + clientUI.display("Only allowed when the client is logged off"); + return; + } + try { + int port = Integer.parseInt(argumentsArray[1]); + setPort(port); + System.out.println("port set to: " + port); + } catch (Exception e) { + clientUI.display("Invalid port."); + } + break; + + case "#login": + //System.out.println("l0"); + if (isConnected()) { + // System.out.println("l1"); + clientUI.display("Only allowed if the client is not already connected."); + return; + } else { + try { + //System.out.println("l21"); + openConnection(); + sendToServer("#login " + loginId); + //System.out.println(loginId); + //System.out.println("l22"); + + //System.out.println("l23"); + + } catch (IOException e) { + clientUI.display("Cannot open connection. Awaiting command."); + //System.exit(0); + } + } + break; + + case "#gethost": + clientUI.display("current host: " + getHost()); + break; + + case "#getport": + clientUI.display("current port: " + getPort()); + break; + + default: + clientUI.display("wrong command"); + break; + } } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); - quit(); + else{ + try { + sendToServer(message); + } catch (IOException e) { + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } } } /** * This method terminates the client. */ - public void quit() - { - try - { - closeConnection(); + public void quit() { + if (isConnected()) { + try { + closeConnection(); + } catch (IOException e) { + } } - catch(IOException e) {} + System.out.println("Abnormal termination of connection."); System.exit(0); } + + + } //End of ChatClient class diff --git a/code/simplechat1/common/ChatIF.java b/code/simplechat1/common/ChatIF.java index 45b04d4..4d9ed35 100644 --- a/code/simplechat1/common/ChatIF.java +++ b/code/simplechat1/common/ChatIF.java @@ -2,7 +2,7 @@ // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com -package common; +package simplechat1.common; /** * This interface implements the abstract method used to display