diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..209225a 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,11 +41,11 @@ 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); + client= new ChatClient(loginid, host, port, this); } catch(IOException exception) { @@ -100,22 +100,45 @@ public void display(String message) /** * This method is responsible for the creation of the Client UI. * - * @param args[0] The host to connect to. + * @param args[0] The login ID of the client. + * @param args[1] The host to connect to. + * @param args[2] The port to connect on. */ public static void main(String[] args) { + String loginid = ""; String host = ""; - int port = 0; //The port number + int port = 0; try { - host = args[0]; + loginid = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + System.out.println("You must provide a valid login ID."); + System.exit(0); + } + + try + { + 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..82edb8a 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -3,6 +3,8 @@ // license found at www.lloseng.com import java.io.*; + +import common.*; import ocsf.server.*; /** @@ -17,93 +19,191 @@ */ 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) - { - super(port); - } - - - //Instance methods ************************************************ - - /** - * This method handles any messages 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); - } - - /** - * This method overrides the one in the superclass. Called - * when the server starts listening for connections. - */ - protected void serverStarted() - { - System.out.println - ("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() - { - System.out.println - ("Server has stopped listening for connections."); - } - - //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!"); - } - } + //Class variables ************************************************* + + /** + * The default port to listen on. + */ + final public static int DEFAULT_PORT = 5555; + + ChatIF server; + boolean serverStatus; + + //Constructors **************************************************** + + /** + * Constructs an instance of the echo server. + * + * @param port The port number to connect on. + */ + public EchoServer(int port) + { + super(port); + } + + //Instance methods ************************************************ + + /** + * This method handles any messages received from the client. + * + * @param message The message received from the client. + * @param client The connection from which the message originated. + */ + public void handleMessageFromClient + (Object message, ConnectionToClient client) + { + if(((String) message).charAt(0) == '#') { + if((boolean)(client.getInfo("connected"))){ + String[] input = ((String)message).split(" ", 0); + client.setInfo("connected", false); + + //check to see if message is login message + if(input[0].equals("#login")) { + client.setInfo("login", input[1]); + } + else { + try { + client.sendToClient("You must log in to start session."); + client.close(); + } + catch(IOException e){ + System.exit(0); + } + } + } + else{ + if(((String)message).startsWith("#login")){ + try{ + client.sendToClient("You are already logged in."); + } + catch(IOException e){ + System.exit(0); + } + } + } + + System.out.println("Message received: " + message + " from " + client.getInfo("login")); + this.sendToAllClients("["+ client.getInfo("login") + "] " + message); + } + } + + public void processServerMessage(String message) throws IOException { + //create string array to handle setHost and setPort + String[] input = message.split(" ", 2); + + if(message.charAt(0) == '#') { + try { + switch (input[0]){ + case "#quit": System.exit(0); + break; + + case "#stop": stopListening(); + break; + + case "#close": close(); + break; + + case "#setport": + if(!serverStatus) { + setPort(Integer.parseInt(input[1])); + } + break; + + case "#start": + if(!isListening()) { + this.listen(); + } + break; + + case "#getport": + server.display("Port: "+ getPort()); + break; + + default: + throw new IOException("Invalid Command"); + + } + } + catch(IOException e) { + e.printStackTrace(); + } + } + else { + server.display("SERVER MSG>" + message); + sendToAllClients("SERVER MSG>" + message); + } + + } + + /** + * This method overrides the one in the superclass. Called + * when the server starts listening for connections. + */ + protected void serverStarted() + { + System.out.println + ("Server listening for connections on port " + getPort()); + serverStatus = true; + } + + /** + * This method overrides the one in the superclass. Called + * when the server stops listening for connections. + */ + protected void serverStopped() + { + System.out.println + ("Server has stopped listening for connections."); + serverStatus = false; + } + + //handle client connection and disconnection + protected void clientConnected(ConnectionToClient client) { + System.out.println("The client, " + client + ", has successfully connected."); + client.setInfo("connected", true); + } + + protected void clientDisconnected(ConnectionToClient client) { + System.out.println("The client, " + client + ", has successfully disconnected."); + } + + protected void clientException(ConnectionToClient client, Throwable exception) { + clientDisconnected(client); + } + + //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!"); + } + } + } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..6a910cc --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,76 @@ + +import java.io.*; + +import common.*; + +public class ServerConsole implements ChatIF { + + EchoServer server; + final public static int DEFAULT_PORT = 5555; + + //Constructors **************************************************** + + public ServerConsole() { + server = new EchoServer(DEFAULT_PORT); + } + + public ServerConsole(int port) { + server = new EchoServer(port); + } + + //Instance methods ************************************************ + + public void display(String message) { + System.out.println(message); + } + + //Class methods ************************************************ + + public static void main(String[] args) + { + int port = 0; + + try + { + port = Integer.parseInt(args[0]); + } + catch(Throwable t) + { + port = DEFAULT_PORT; + } + + ServerConsole console = new ServerConsole(port); + + try + { + console.server.listen(); + } + catch (Exception ex) + { + System.out.println("ERROR."); + } + + + try { + console.server.listen(); + } + catch (IOException e) { + e.printStackTrace(); + } + + try { + BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + + while (true){ + String message = input.readLine(); + console.server.processServerMessage(message); + } + } + catch (IOException error) + { + System.out.println("ERROR."); + System.exit(0); + } + } + +} diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..3895363 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -26,6 +26,7 @@ public class ChatClient extends AbstractClient * the display method in the client. */ ChatIF clientUI; + String loginid; //Constructors **************************************************** @@ -38,12 +39,14 @@ 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.loginid = loginid; this.clientUI = clientUI; openConnection(); + sendToServer("#login <" + loginid + ">"); } @@ -66,18 +69,96 @@ public void handleMessageFromServer(Object msg) */ public void handleMessageFromClientUI(String message) { - try - { - sendToServer(message); - } - catch(IOException e) - { - clientUI.display - ("Could not send message to server. Terminating client."); - quit(); - } + //check if message is a command + if(message.charAt(0) == '#') { + try{ + String[] input = message.split(" ", 0); + switch (input[0]) { + + case "#quit": quit(); + break; + + case "#logoff" : closeConnection(); + break; + + case "#sethost" : + if(!isConnected()) { + setHost(input[1]); + } + else { + throw new IOException("You must logoff first before setting the host."); + } + break; + + case "#setport": + if(!isConnected()) { + setPort(Integer.parseInt(input[1])); + } + else { + throw new IOException("You must logoff first before setting the port."); + } + break; + + case "#login": + if(!isConnected()) { + openConnection(); + } + else { + throw new IOException("You have not logged out."); + } + break; + + case "#gethost": + clientUI.display("Host: "+ getHost()); + break; + + case "#getport": + clientUI.display("Port: " + getPort()); + break; + + default: + throw new IOException("Invalid Command"); + } + } + catch(IOException e) { + System.exit(0); + } + } + else { + try + { + sendToServer(message); + } + catch(IOException e) + { + clientUI.display + ("Could not send message to server. Terminating client."); + quit(); + } + } } + /** + * This method closes the connection to the server. + * + * @param quit The decision to close connection. + */ + protected void connectionClosed(boolean quit) { + if (quit) { + System.exit(0); + } + } + + /** + * This method responds to the shutdown of the server. + * + * @param exception The exception to be handled before closing. + */ + protected void connectionException(Exception exception) { + System.out.println("The connection to the server is now closing."); + connectionClosed(true); + } + /** * This method terminates the client. */ diff --git a/code/ocsf/client/AbstractClient.java b/code/simplechat1/ocsf/client/AbstractClient.java similarity index 100% rename from code/ocsf/client/AbstractClient.java rename to code/simplechat1/ocsf/client/AbstractClient.java diff --git a/code/ocsf/client/AdaptableClient.java b/code/simplechat1/ocsf/client/AdaptableClient.java similarity index 100% rename from code/ocsf/client/AdaptableClient.java rename to code/simplechat1/ocsf/client/AdaptableClient.java diff --git a/code/ocsf/client/ObservableClient.java b/code/simplechat1/ocsf/client/ObservableClient.java similarity index 100% rename from code/ocsf/client/ObservableClient.java rename to code/simplechat1/ocsf/client/ObservableClient.java diff --git a/code/simplechat1/ocsf/index.html b/code/simplechat1/ocsf/index.html new file mode 100644 index 0000000..13025a0 --- /dev/null +++ b/code/simplechat1/ocsf/index.html @@ -0,0 +1,22 @@ + + +Installing and Running OCSF + + + +

To install OCSF, simply compile all the .java files in the client and +server directories.

+ +

OCSF is a Framework, so this directory contains no main program. To +learn how it works, consult the book +"Object-Oriented Software Engineering: Practical Software Development +using UML and Java" by Lethbridge and Laganière.

+ +

To use OCSF, import the "ocsf.client" or "ocsf.server" package in your +application code. Make sure that the ocsf directory is in your classpath +when you compile your application.

+ +

Back to the source code page.

+ + + diff --git a/code/ocsf/server/AbstractServer.java b/code/simplechat1/ocsf/server/AbstractServer.java similarity index 100% rename from code/ocsf/server/AbstractServer.java rename to code/simplechat1/ocsf/server/AbstractServer.java diff --git a/code/ocsf/server/AdaptableServer.java b/code/simplechat1/ocsf/server/AdaptableServer.java similarity index 100% rename from code/ocsf/server/AdaptableServer.java rename to code/simplechat1/ocsf/server/AdaptableServer.java diff --git a/code/ocsf/server/ConnectionToClient.java b/code/simplechat1/ocsf/server/ConnectionToClient.java similarity index 100% rename from code/ocsf/server/ConnectionToClient.java rename to code/simplechat1/ocsf/server/ConnectionToClient.java diff --git a/code/ocsf/server/ObservableOriginatorServer.java b/code/simplechat1/ocsf/server/ObservableOriginatorServer.java similarity index 100% rename from code/ocsf/server/ObservableOriginatorServer.java rename to code/simplechat1/ocsf/server/ObservableOriginatorServer.java diff --git a/code/ocsf/server/ObservableServer.java b/code/simplechat1/ocsf/server/ObservableServer.java similarity index 100% rename from code/ocsf/server/ObservableServer.java rename to code/simplechat1/ocsf/server/ObservableServer.java diff --git a/code/ocsf/server/OriginatorMessage.java b/code/simplechat1/ocsf/server/OriginatorMessage.java similarity index 100% rename from code/ocsf/server/OriginatorMessage.java rename to code/simplechat1/ocsf/server/OriginatorMessage.java