diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f741ba1 --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index a1c2a23..dd858b4 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..8d2d3f8 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Lloseng + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/TestCases.txt.docx b/TestCases.txt.docx new file mode 100644 index 0000000..49c59cc Binary files /dev/null and b/TestCases.txt.docx differ diff --git a/code/ocsf/client/AbstractClient.java b/code/ocsf/client/AbstractClient.java index e905636..db96dcd 100644 --- a/code/ocsf/client/AbstractClient.java +++ b/code/ocsf/client/AbstractClient.java @@ -1,8 +1,9 @@ +package ocsf.client; // 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 ocsf.client; + import java.io.*; import java.net.*; @@ -268,6 +269,8 @@ final public void run() { * attempting to reconnect. */ protected void connectionClosed() { + + } /** @@ -279,7 +282,10 @@ protected void connectionClosed() { * the exception raised. */ protected void connectionException(Exception exception) { + } + + /** * Hook method called after a connection has been established. The default diff --git a/code/ocsf/client/AdaptableClient.java b/code/ocsf/client/AdaptableClient.java index 143592e..c5803b7 100644 --- a/code/ocsf/client/AdaptableClient.java +++ b/code/ocsf/client/AdaptableClient.java @@ -1,8 +1,9 @@ +package ocsf.client; // This file contains material supporting section 6.13 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com -package ocsf.client; + import java.io.*; import java.net.*; diff --git a/code/ocsf/client/ObservableClient.java b/code/ocsf/client/ObservableClient.java index a816488..839b52d 100644 --- a/code/ocsf/client/ObservableClient.java +++ b/code/ocsf/client/ObservableClient.java @@ -1,8 +1,9 @@ +package ocsf.client; // This file contains material supporting section 6.13 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com -package ocsf.client; + import java.util.*; import java.io.*; diff --git a/code/ocsf/server/AbstractServer.java b/code/ocsf/server/AbstractServer.java index 0134b22..1e23d98 100644 --- a/code/ocsf/server/AbstractServer.java +++ b/code/ocsf/server/AbstractServer.java @@ -179,7 +179,7 @@ final synchronized public void close() throws IOException } /** - * Sends a message to every client connected to the server. + * s a message to every client connected to the server. * This is merely a utility; a subclass may want to do some checks * before actually sending messages to all clients. This method * can be overriden, but if so it should still perform the general @@ -404,7 +404,9 @@ protected void listeningException(Throwable exception) {} * connections. The default implementation does nothing. * The method may be overridden by subclasses. */ - protected void serverStarted() {} + protected void serverStarted() { + System.out.println("Welcome!"); + } /** * Hook method called when the server stops accepting @@ -419,7 +421,9 @@ protected void serverStopped() {} * overriden by subclasses. When the server is closed while still * listening, serverStopped() will also be called. */ - protected void serverClosed() {} + protected void serverClosed() { + System.out.println("GoodBye!"); + } /** * Handles a command sent from one client to the server. diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..12b3f5a 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,19 +41,24 @@ 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) { System.out.println("Error: Can't setup connection!" + " Terminating client."); System.exit(1); + } } + + //Instance methods ************************************************ @@ -64,17 +69,72 @@ public ClientConsole(String host, int port) */ public void accept() { + try { BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in)); String message; - + boolean log_in_out=true; while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); - } + char first = message.charAt(0); + if (first == '#') { + if (message.equals("#quit")) { + System.out.println("The client has quit"); + System.exit(1); + } + else if (message.equals("#logoff")) { + log_in_out = false; + client.handleMessageFromClientUI(client.getLogin()+" left the server"); + System.out.println("The connection has closed"); + } + else if (message.split(" ")[0].equals(("#sethost"))) { + if (log_in_out==false) { + client.setHost(message.split(" ")[1]); + System.out.println("Host set to: " + client.getHost()); + } + else { + System.out.println("You have to be logged out to set the host"); + } + } + else if (message.split(" ")[0].equals(("#setport"))) { + if (log_in_out==false) { + int holder = Integer.parseInt(message.split(" ")[1]); + client.setPort(holder); + System.out.println("Port set to: " + client.getPort()); + } + else { + System.out.println("You have to be logged out to set the port"); + } + } + else if (message.equals("#login")) { + if (log_in_out==true) { + System.out.println("You are logged in"); + } + else { + client.openConnection(); + log_in_out=true; + System.out.println("Welcome Back!"); + } + } + else if (message.equals("#gethost")) { + System.out.println(client.getHost()); + } + else if (message.equals("#getport")) { + System.out.println(client.getPort()); + } + else { + System.out.println("Not a proper command"); + } + } + else { + client.handleMessageFromClientUI(message); + } + + } + } catch (Exception ex) { @@ -101,22 +161,41 @@ public void display(String message) * This method is responsible for the creation of the Client UI. * * @param args[0] The host to connect to. + * @throws IOException */ public static void main(String[] args) { String host = ""; int port = 0; //The port number - - try - { - host = args[0]; + String log_in = ""; + if (args.length>0) { + log_in=args[0]; } - catch(ArrayIndexOutOfBoundsException e) - { - host = "localhost"; + else { + System.out.println("You must have a login ID"); + } + try { + host=args[1]; + } + catch (ArrayIndexOutOfBoundsException e) { + host="localhost"; + } + + try{ + port = Integer.parseInt(args[2]); + } + catch(ArrayIndexOutOfBoundsException e){ + port = DEFAULT_PORT; + } + + + ClientConsole chat= new ClientConsole(log_in, host, port); + chat.accept(); //Wait for console data + + //System.out.println(args[1]); + + } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); - chat.accept(); //Wait for console data } -} + //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..607ff2d 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,7 @@ import java.io.*; import ocsf.server.*; +import common.ChatIF; /** * This class overrides some of the methods in the abstract @@ -23,6 +24,7 @@ public class EchoServer extends AbstractServer * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; + ChatIF main_server_UI; //Constructors **************************************************** @@ -31,9 +33,11 @@ public class EchoServer extends AbstractServer * * @param port The port number to connect on. */ - public EchoServer(int port) - { - super(port); + + public EchoServer(int port) { + super(port); + + } @@ -45,9 +49,19 @@ public EchoServer(int port) * @param msg The message received from the client. * @param client The connection from which the message originated. */ - public void handleMessageFromClient - (Object msg, ConnectionToClient client) + public void handleMessageFromClient(Object msg, ConnectionToClient client) { + String holder=msg.toString(); + if (holder.contains("#login")) { + System.out.println(msg); + this.sendToAllClients("Welcome to the server " + holder.split(" ")[1]+"."); + client.setInfo("Login ID",holder.split(" ")[1]); + } + else { + System.out.println("This message has been received: " + msg + "from: " + client); + this.sendToAllClients(client.getInfo("Login ID")+": "+msg); + } + System.out.println("Message received: " + msg + " from " + client); this.sendToAllClients(msg); } @@ -58,18 +72,37 @@ public EchoServer(int port) */ protected void serverStarted() { + System.out.println ("Server listening for connections on port " + getPort()); + try { + listen(); + } + + catch(IOException exception) { + System.out.println("There is an error"); + } } /** * 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."); + protected void serverStopped(){ + stopListening(); + + this.sendToAllClients("Server stopped listeneing for connections" + getPort()); + + } + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client has connected to the server"); + } + protected void clientException(ConnectionToClient client, Throwable exception) { + System.out.println(client.getInfo("Login ID")+" has left"); + } + + public void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("Login ID")+" has left"); } //Class methods *************************************************** diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..ee8f8b9 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,103 @@ +import java.io.*; +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); + } + public void display(String message) { + server.sendToAllClients("SERVER MSG>: " + message); + } + public void handleMessageFromConsole(String message) { + display(message); + } + public void accept() { + + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + boolean server_status=true; + + while (true) + { + message = fromConsole.readLine(); + char first = message.charAt(0); + if (first == '#') {//do the functions base on # + if (message.equals("#quit")) { + System.out.println("The Server has quit"); + System.exit(1); + } + else if (message.equals("#stop")) { + server.serverStopped(); + server_status = false; + + } + else if (message.equals("#close")) { + server.close(); + server_status=false; + } + + else if (message.split(" ")[0].equals(("#setport"))) { + if (server_status==false) { + int holder = Integer.parseInt(message.split(" ")[1]); + server.setPort(holder); + } + else { + System.out.println("You can set port when connections are haulted"); + } + } + else if (message.equals("#start")) { + server.serverStarted(); + server_status=true + ; + } + + else if (message.equals("#getport")) { + System.out.println(server.getPort()); + } + else { + System.out.println("function does not exist"); + } + } + + else { + server.sendToAllClients("Server MSG>" + message); + } + + } + } + catch (Exception ex) { + System.out.println("Unexpected Error"); + } + } +public EchoServer get() { + return server; +} +public static void main (String [] args) { + int port =0; + try { + port = Integer.parseInt(args[0]); + } + catch (Throwable t) { + port = DEFAULT_PORT; + } + ServerConsole server; + server = new ServerConsole(port); + try { + server.get().listen(); + 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..f5bfe4c 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 log_in; //Constructors **************************************************** @@ -38,14 +39,16 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException - { - super(host, port); //Call the superclass constructor - this.clientUI = clientUI; - openConnection(); + + public ChatClient(String log_in, String host, int port, ChatIF clientUI) throws IOException { + + super(host, port); //Call the superclass constructor + this.clientUI = clientUI; + this.log_in=log_in; + openConnection(); + + } - //Instance methods ************************************************ @@ -90,5 +93,20 @@ public void quit() catch(IOException e) {} System.exit(0); } + protected void connectionException(Exception exception) { + quit(); + } + protected void connectionClosed() { + clientUI.display("Server has Shutdown"); + } + public String getLogin() { + return log_in; + } + public void sendLogin() { + handleMessageFromClientUI("#login " + getLogin()); + } + public void connectionEstablished() { + sendLogin(); + } } //End of ChatClient class