diff --git a/README.md b/README.md index f063b24..1b51468 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,5 @@ Some relevant websites related to this repository: * Student solutions to exercises (first edition): http://highered.mheducation.com/sites/0077097610/student_view0/student_solutions.html Professors may obtain solutions to exercises including code by contacting the authors. They must show a web presence confirming their email address and that they are indeed an academic at that same domain. + +Ahmed Abdulwahab 300124243 aabdu170@uottawa.ca diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..26657fc 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,14 +41,19 @@ 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 id, String host, int port) { try { client= new ChatClient(host, port, this); + try { + client.sendToServer("#login "+id); + }catch(IOException e){ + System.out.println("Could login to server. Terminating."); + quit(); + } } - catch(IOException exception) - { + catch(IOException exception) { System.out.println("Error: Can't setup connection!" + " Terminating client."); System.exit(1); @@ -104,19 +109,45 @@ public void display(String message) */ public static void main(String[] args) { + String id = ""; String host = ""; int port = 0; //The port number + try{ + id = args[0]; + }catch(ArrayIndexOutOfBoundsException e){ + System.out.println("user id required."); + System.exit(0); + } + + try{ + host = args[1]; + }catch(ArrayIndexOutOfBoundsException e){ + host = "localhost"; + } try { - host = args[0]; + port = Integer.parseInt(args[1]); } catch(ArrayIndexOutOfBoundsException e) { - host = "localhost"; + port = 5555; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + ClientConsole chat = new ClientConsole(id, host, port); chat.accept(); //Wait for console data } + + public void quit(){ + /*try{ + closeConnection(); + } + catch(IOException e) { + + }*/ + System.exit(0); +} + + } //End of ConsoleChat class diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..167e0df 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 client.ChatClient; import ocsf.server.*; /** @@ -45,11 +47,17 @@ 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) - { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + public void handleMessageFromClient(Object msg, ConnectionToClient client){ + String message = msg.toString(); + if(message.charAt(0) =='#'){ + if(message.substring(1,6).equals("login")){ + System.out.println("xD"); + client.setInfo("id",message.substring(6)); + } + + } + System.out.println("Message received: " + message + " from " + client.getInfo("id")); + this.sendToAllClients(client.getInfo("id") + ": " + message); } /** @@ -72,8 +80,45 @@ protected void serverStopped() ("Server has stopped listening for connections."); } - //Class methods *************************************************** - + protected void clientConnected(ConnectionToClient client) { + System.out.println("A Client has connected to the server !"); + } + synchronized protected void clientDisconnected(ConnectionToClient client) { + System.out.println("A Client has disconnected from the server ."); + } + + //Class methods *************************************************** + public void handleMessageFromServerUI(String message){ + if(message.charAt(0) == '#'){ + if(message.substring(1,5).equals("quit")){ + System.exit(0); + }else if(message.substring(1,5).equals("stop")){ + stopListening(); + }else if(message.substring(1,6).equals("close")){ + stopListening(); + }else if(message.substring(1,6).equals("start")){ + if(!isListening()){ + try{ + listen(); + } catch(Exception exception){ + System.out.println("friggin error xD"); + } + }else{ + System.out.println("Server already listening"); + } + }else if(message.substring(1,8).equals("setport")){ + if(isListening()){ + System.out.println("Error: yous already connected foo"); + }else{ + setPort(Integer.parseInt(message.substring(9))); + } + }else if(message.substring(1,8).equals("getport")){ + System.out.println(getPort()); + } + }else{ + this.sendToAllClients("SERVER MSG > "+message); + } + } /** * This method is responsible for the creation of * the server instance (there is no UI in this phase). diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..fb43b4b --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,129 @@ +// 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 client.*; +import common.*; + +/** + * 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 + * + * @author François Bélanger + * @author Dr Timothy C. Lethbridge + * @author Dr Robert Laganière + * @version July 2000 + */ +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 ConsoEchoServerleChat. + */ + EchoServer server; + + + //Constructors **************************************************** + + /** + * Constructs an instance of the ClientConsole UI. + * + * @param host The host to connect to. + * @param port The port to connect on. + */ + public ServerConsole(int port) + { + try + { + server= new EchoServer(port); + server.listen(); + } + catch(Exception exception) + { + System.out.println("Error: Can't setup connection!" + + " Terminating client."); + System.exit(1); + } + } + + + //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("> " + 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) + { + int port = 0; //The port number + try + { + port = Integer.parseInt(args[0]); + } + catch(ArrayIndexOutOfBoundsException e) + { + port = 5555; + } + + ServerConsole sv = new ServerConsole(port); + try{ + sv.accept(); + }catch (Exception ex){ + } + } + + public void quit(){ + System.exit(0); + } +} +//End of ConsoleChat class