diff --git a/code/simplechat1/ClientConsole.java b/code/simplechat1/ClientConsole.java index c9bb4e9..fb9fe0e 100644 --- a/code/simplechat1/ClientConsole.java +++ b/code/simplechat1/ClientConsole.java @@ -41,17 +41,16 @@ 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) { + client= new ChatClient(loginid,host, port, this); try { - client= new ChatClient(host, port, this); - } + client.openConnection(); + } catch(IOException exception) { - System.out.println("Error: Can't setup connection!" - + " Terminating client."); - System.exit(1); + System.out.println("Cannot open connection. Awaiting command."); } } @@ -73,7 +72,62 @@ public void accept() while (true) { message = fromConsole.readLine(); - client.handleMessageFromClientUI(message); + + //hml #command + char messagecheck = message.charAt(0); + if (messagecheck == '#'){ + String[] cmessage = message.split(" "); + String command = cmessage[0]; + System.out.println(command); + if (command.equals("#quit")){ + System.out.println("The client has quit"); + client.quit(); + } + else if (command.equals("#logoff")){ + client.closeConnection(); + } + else if (command.equals("#sethost")){ + if (client.isConnected()){ + System.out.println("The client is still connected."); + } + else{ + String newhost = cmessage[1]; + client.setHost(newhost); + System.out.println("Host set to " + newhost); + } + } + else if (command.equals("#setport")){ + if (client.isConnected()){ + System.out.println("The client is still connected."); + } + else{ + int newport = Integer.parseInt(cmessage[1]); + client.setPort(newport); + System.out.println("Port set to " + newport); + } + } + else if (command.equals("#login")){ + if (client.isConnected()){ + System.out.println("The client is still connected."); + } + else{ + client.openConnection(); + client.handleMessageFromClientUI("#login " + client.getloginid()); + } + } + else if (command.equals("#gethost")){ + System.out.println(client.getHost()); + } + else if (command.equals("#getport")){ + System.out.println(client.getPort()); + } + else{ + System.out.println("Wrong command."); + } + } + else{ + client.handleMessageFromClientUI(message); + } } } catch (Exception ex) @@ -106,16 +160,45 @@ public static void main(String[] args) { String host = ""; int port = 0; //The port number + String id = ""; try { - host = args[0]; + id = args[0]; + } + catch(ArrayIndexOutOfBoundsException e) + { + id = ""; + } + + try + { + host = args[1]; } catch(ArrayIndexOutOfBoundsException e) { host = "localhost"; } - ClientConsole chat= new ClientConsole(host, DEFAULT_PORT); + + try + { + port = Integer.parseInt(args[2]); + } + catch(Throwable e) + { + port = DEFAULT_PORT; + } + + ClientConsole chat= new ClientConsole(id, host, port); + + if(chat.client.isConnected()){ + if (chat.client.getloginid().equals("")){ + System.out.println("ERROR - No login ID specified. Connection aborted."); + chat.client.quit(); + } + chat.client.handleMessageFromClientUI("#login "+chat.client.getloginid()); + } + chat.accept(); //Wait for console data } } diff --git a/code/simplechat1/EchoServer.java b/code/simplechat1/EchoServer.java index d4f3a1a..eee9c39 100644 --- a/code/simplechat1/EchoServer.java +++ b/code/simplechat1/EchoServer.java @@ -4,7 +4,6 @@ import java.io.*; import ocsf.server.*; - /** * This class overrides some of the methods in the abstract * superclass in order to give more functionality to the server. @@ -24,6 +23,8 @@ public class EchoServer extends AbstractServer */ final public static int DEFAULT_PORT = 5555; + private boolean sc = false; + //Constructors **************************************************** /** @@ -47,9 +48,29 @@ public EchoServer(int port) */ public void handleMessageFromClient (Object msg, ConnectionToClient client) - { - System.out.println("Message received: " + msg + " from " + client); - this.sendToAllClients(msg); + { + try + { + String idcommand = (String)(msg); + String[] aidcommand = idcommand.split(" "); + if(aidcommand[0].equals("#login")) + { + if (client.getInfo("loginid")==null){ + String idname = aidcommand[1]; + client.setInfo("loginid",idname); + this.sendToAllClients(client.getInfo("loginid") + " has logged on."); + } + else{ + client.sendToClient("Error, wrong command."); + client.close(); + } + } + } + catch(Exception ex){ + } + System.out.println("Message received: " + msg + " from " + client.getInfo("loginid")); + Object echmsg = (String)(client.getInfo("loginid")) + " " + (String)(msg); + this.sendToAllClients(echmsg); } /** @@ -71,7 +92,22 @@ protected void serverStopped() System.out.println ("Server has stopped listening for connections."); } - + //hml + protected void clientConnected(ConnectionToClient client) { + System.out.println("A new client is attempting to connect to the server."); + System.out.println(client.getInfo("loginid") + " has logged on."); + } + + synchronized protected void clientDisconnected(ConnectionToClient client) { + System.out.println(client.getInfo("loginid") + " has disconnected."); + } + + protected void serverClosed(){ + sc = true; + } + protected boolean serverisClosed(){ + return sc; + } //Class methods *************************************************** /** @@ -95,7 +131,10 @@ public static void main(String[] args) } EchoServer sv = new EchoServer(port); - + ServerConsole serverco = new ServerConsole(sv); + + + try { sv.listen(); //Start listening for connections @@ -104,6 +143,7 @@ public static void main(String[] args) { System.out.println("ERROR - Could not listen for clients!"); } + serverco.accept(); } } //End of EchoServer class diff --git a/code/simplechat1/ServerConsole.java b/code/simplechat1/ServerConsole.java new file mode 100644 index 0000000..ee9bef8 --- /dev/null +++ b/code/simplechat1/ServerConsole.java @@ -0,0 +1,70 @@ +import common.*; +import java.io.*; +public class ServerConsole implements ChatIF{ + EchoServer server; + public ServerConsole(EchoServer server){ + this.server = server; + } + public void display(String message) + { + System.out.println("> " + message); + } + public void accept(){ + try + { + BufferedReader fromConsole = + new BufferedReader(new InputStreamReader(System.in)); + String message; + + while (true){ + message = fromConsole.readLine(); + char messagecheck = message.charAt(0); + if (messagecheck == '#'){ + String[] cmessage = message.split(" "); + String command = cmessage[0]; + System.out.println(command); + if (command.equals("#quit")){ + System.out.println("The server has quit"); + try + { + server.close(); + } + catch(IOException e) {} + System.exit(0); + } + else if (command.equals("#stop")){ + server.stopListening(); + server.sendToAllClients("WARNING - Server has stopped listening for connections."); + } + else if (command.equals("#close")){ + server.sendToAllClients("WARNING - The server has stopped listening for connections"+ + "\n"+"SERVER SHUTTING DOWN! DISCONNECTING!"); + server.close(); + } + else if (command.equals("#setport")){ + if (!server.serverisClosed()){ + System.out.println("The server is still connected."); + } + else{ + int newport = Integer.parseInt(cmessage[1]); + server.setPort(newport); + } + } + else if (command.equals("#start")){ + if(server.isListening()){ + System.out.println("The server is still listening") + } + else{ + server.listen(); + } + } + } + server.sendToAllClients("SERVER MSG> " + message); + } + } + catch(Exception ex){ + System.out.println + ("Unexpected error while reading from console!"); + } + } +} \ No newline at end of file diff --git a/code/simplechat1/client/ChatClient.java b/code/simplechat1/client/ChatClient.java index fe1401e..4cf80fa 100644 --- a/code/simplechat1/client/ChatClient.java +++ b/code/simplechat1/client/ChatClient.java @@ -26,7 +26,7 @@ public class ChatClient extends AbstractClient * the display method in the client. */ ChatIF clientUI; - + private String loginid; //Constructors **************************************************** @@ -38,12 +38,12 @@ public class ChatClient extends AbstractClient * @param clientUI The interface type variable. */ - public ChatClient(String host, int port, ChatIF clientUI) - throws IOException + public ChatClient(String loginid, String host, int port, ChatIF clientUI) + { super(host, port); //Call the superclass constructor this.clientUI = clientUI; - openConnection(); + this.loginid = loginid; } @@ -72,12 +72,25 @@ public void handleMessageFromClientUI(String message) } catch(IOException e) { - clientUI.display - ("Could not send message to server. Terminating client."); + clientUI.display("Could not send message to server. Terminating client."); quit(); } } - + //hml + protected void connectionClosed() { + System.out.println("The connetion is closed"); + } + + protected void connectionException(Exception exception) { + System.out.println("Abnormal termination of connection."); + } + public void setloginid(String id){ + this.loginid = id; + } + public String getloginid(){ + return loginid; + } + /** * This method terminates the client. */