diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..79fa656 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,18 +41,9 @@ 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 host, int port, String id) { - try - { - client= new ChatClient(host, port, this); - } - catch(IOException exception) - { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); - } + client= new ChatClient(host, port, id, this); } @@ -72,14 +63,21 @@ public void accept() while (true) { - message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + message = fromConsole.readLine(); + // For commands passed + if (message.equals("#sethost")) { + // User didn't enter full command for changing host + System.out.println("No host specified. Using default host..."); + client.handleMessageFromClientUI("#sethost localhost"); + } + // No commands passed. Just plain messages + else + client.handleMessageFromClientUI(message); } } catch (Exception ex) { - System.out.println - ("Unexpected error while reading from console!"); + System.out.println("Could not read from console!"); } } @@ -87,11 +85,28 @@ public void accept() * This method overrides the method in the ChatIF interface. It * displays a message onto the screen. * - * @param message The string to be displayed. + * @param message The string fto be displayed. */ public void display(String message) { - System.out.println("> " + message); + if (message.contains("#server")) { + // Remove #server and display server's maessage + int index = message.indexOf('#'); + String mes = message.substring(0, index); + System.out.println("SERVER MSG> " + mes); + } + else { + // Split the message and print: loginID> msg (messages) + if (message.contains(" has loged on")) { + String[] mes = message.split(" ", 2); + System.out.println(mes[0] + " " + mes[1]); + } + else { + String[] mes = message.split(" ", 2); + System.out.println(mes[0] + "> " + mes[1]); + } + } + } @@ -100,22 +115,47 @@ 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] Login-id of the user to connect to a server. */ public static void main(String[] args) { + String clientID = ""; String host = ""; int port = 0; //The port number try { - host = args[0]; + // Given that only clientID argument is mandatory, check if user entered additional arguments + if (args.length == 3) { + clientID = args[0]; + host = args[1]; + port = Integer.parseInt(args[2]); + + } + else if (args.length == 2) { + // Determine the type of argument. Assume that second argument is port argument + clientID = args[0]; + port = Integer.parseInt(args[1]); + host = "localhost"; + } + else + clientID = args[0]; + host = "localhost"; + port = DEFAULT_PORT; } catch(ArrayIndexOutOfBoundsException e) { - host = "localhost"; + System.out.println("ERROR - No login ID specified. Connection aborted."); + System.exit(1); } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + // The assumption was wrong. + catch (NumberFormatException e) + { + // Use the default port + port = DEFAULT_PORT; + host = args[1]; + } + ClientConsole chat= new ClientConsole(host, port, clientID); chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..939908d 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,6 +4,7 @@ import java.io.*; import ocsf.server.*; +import common.*; /** * This class overrides some of the methods in the abstract @@ -17,13 +18,6 @@ */ public class EchoServer extends AbstractServer { - //Class variables ************************************************* - - /** - * The default port to listen on. - */ - final public static int DEFAULT_PORT = 5555; - //Constructors **************************************************** /** @@ -36,6 +30,14 @@ public EchoServer(int port) super(port); } + //Instance variables ********************************************** + + /** + * The interface type variable. It allows the implementation of + * the display method in the server. + */ + ChatIF serverUI; + //Instance methods ************************************************ @@ -48,10 +50,108 @@ public EchoServer(int port) public void handleMessageFromClient (Object msg, ConnectionToClient client) { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + String mes = (String) msg; + // Don't display the command to the server interface and send it to other clients + if (mes.contains("#login")) + { + // Log the user into the system + int ind = mes.indexOf(" "); // Start of identification string + int len = mes.length(); // End of identiification string + String ID = mes.substring(ind+1, len); + client.setInfo("Login ID", ID); + + // Indicate who connected to the administrator + System.out.println("A new client is attempting to connect to the server."); + System.out.println("Message received #login " + client.getInfo("Login ID") + " from null."); + System.out.println(client.getInfo("Login ID") + " has logged on."); + try { + // Add > so that client's console does not mistake the login as a message + client.sendToClient(client.getInfo("Login ID") + " has logged on."); + } + catch (IOException e) { + System.out.println("Could not notify the client!"); + } + + + } + else { + System.out.println("Message received: " + msg + " from " + client.getInfo("Login ID")); + this.sendToAllClients(client.getInfo("Login ID") + " " + msg); + } } - + + /** + * This method reads user input's from the console + * + * @param in Input from the console + */ + public void handleInputFromServerConsole(String input) + { + // Check if message is a command + if (input.charAt(0) == '#') { + // Split the message into two + String[] mes = input.split(" ", 2); + // Select appropriate function + switch(mes[0]) { + case "#close": + try { + close(); + } + catch(IOException e) + { + System.out.println(": " + e); + } + break; + case "#stop": + stopListening(); + break; + case "#quit": + try { + close(); + System.exit(0); + } + catch(IOException e) + { + System.out.println(": " + e); + } + break; + case "#setport": + try { + setPort(Integer.parseInt(mes[1])); + } + catch (Exception e) { + System.out.println("No port specified. Using default port..."); + setPort(5555); + } + break; + case "#start": + try { + listen(); + } + catch(IOException e) + { + System.out.println(": " + e); + } + break; + case "#getport": + System.out.println(getPort()); + break; + default: + System.out.println("Command not valid!"); + break; + } + } + else + { + + // Echo to the server console + System.out.println("SERVER MESSAGE> " + input); + // Send to the clients + input += ("#server"); // Add identifier + this.sendToAllClients(input); + } + } + /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. @@ -61,49 +161,19 @@ 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."); + sendToAllClients("WARNING - 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!"); - } + protected void serverClosed() + { + System.out.println("Server is closed"); + sendToAllClients("WARNING - The server has stopped listening for connections. SERVER SHUTTING DOWN! DISCONNECTING!"); } + } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..7ef191a --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,102 @@ +import common.*; +import java.io.*; + +public class ServerConsole implements ChatIF { + //Instance variables ********************************************** + + /** + * The instance of the server that created this EchoServer. + */ + EchoServer sv; + + //Constructors **************************************************** + + /** + * Constructs an instance of the ServerConsole UI. + * + * @param port The port that server listens to. + */ + public ServerConsole(int port) + { + sv = new EchoServer(port); + } + + //Instance methods ************************************************ + /** + * This method overrides the method in the ChatIF interface. It + * displays a message onto the screen. + * + * @param message The string fto be displayed. + */ + public void display(String message) + { + System.out.println("SERVER MSG> " + message); + } + + /** + * This method allows the user of console for commands and + * messages to the client + */ + public void console() { + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String input; + sv.listen(); + + while (true) + { + input = fromConsole.readLine(); + sv.handleInputFromServerConsole(input); + } + } + catch (Exception ex) + { + System.out.println + ("Unexpected error while reading from console!"); + } + } + + //Class variables ************************************************* + + /** + * The default port to listen on. + */ + final public static int DEFAULT_PORT = 5555; + + //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 + } + + ServerConsole serv = new ServerConsole(port); + + try + { + serv.console(); // Allow access to server console + } + 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..f523686 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -25,7 +25,10 @@ public class ChatClient extends AbstractClient * The interface type variable. It allows the implementation of * the display method in the client. */ - ChatIF clientUI; + ChatIF clientUI; + + // Variable to store login id + String loginID; //Constructors **************************************************** @@ -38,12 +41,23 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException +// Don't try to connect immediately + public ChatClient(String host, int port, String ID, ChatIF clientUI) { super(host, port); //Call the superclass constructor this.clientUI = clientUI; - openConnection(); + this.loginID = ID; + + // Try to open connection + try { + openConnection(); + // Send the server command for logging in the user + sendToServer("#login " + ID); + } + catch(IOException exception) + { + System.out.println("Cannot open connection. Awaiting command."); + } } @@ -56,7 +70,7 @@ public ChatClient(String host, int port, ChatIF clientUI) */ public void handleMessageFromServer(Object msg) { - clientUI.display(msg.toString()); + clientUI.display((String)msg); } /** @@ -68,16 +82,83 @@ public void handleMessageFromClientUI(String message) { try { - sendToServer(message); + // Check if message is a command + if (message.charAt(0) == '#') { + // Split the message into two + String[] mes = message.split(" ", 4); + if (mes[0].equals("#login") && mes.length == 2) { + // Open the connection if it is still closed + if (!(isConnected())) + openConnection(); + sendToServer(message); + } + else { + // Select appropriate function + switch(mes[0]) { + case "#quit": + // Break the connection and close the client + closeConnection(); + System.exit(0); // Indicate succesful exit + break; + case "#logoff": + System.out.println(loginID + " has disconnected."); + // Break the connection + closeConnection(); + break; + case "#sethost": + // Send the argument of the command to the function + if (mes[1].equals("")) { + setHost("localhost"); + System.out.println("Host set to: " + getHost()); + } + else { + setHost(mes[1]); + System.out.println("Host set to: " + getHost()); + } + break; + case "#setport": + // Convert argument of the command to int and pass it + try { + setPort(Integer.parseInt(mes[1])); + System.out.println("Port set to: " + getPort()); + } + catch (Exception e) { + System.out.println("No port supplied. Using default...."); + setPort(5555); + System.out.println("Port set to: " + getPort()); + } + break; + case "#login": + // Open connection only if connection is closed + if (!(isConnected())) { + openConnection(); + sendToServer("#login " + loginID); + } + else + System.out.println("You must logoff first before login"); + break; + case "#gethost": + System.out.println(getHost()); + break; + case "#getport": + System.out.println(getPort()); + break; + default: + System.out.println("Command not valid!"); + break; + } + } + } + else + sendToServer(message); } catch(IOException e) { clientUI.display - ("Could not send message to server. Terminating client."); - quit(); + ("Cannot open connection. Awaiting command."); } - } - + } + /** * This method terminates the client. */