cs343 assign01 - achanchan/cs343-assign01 GitHub Wiki

Welcome to the cs343-assign01 wiki!

  1. Copy the interfaces that you used in your application.
    

ChatInterface:

import java.rmi.Remote; import java.rmi.RemoteException;

public interface ChatInterface extends Remote{ public String helloTo() throws RemoteException; public String addClient(String username, ClientInterface newClient) throws RemoteException; public String list() throws RemoteException; public void quit(String username) throws RemoteException; public void sendMessage(String start, String dest, String message) throws RemoteException; }

ClientInterface:

import java.rmi.Remote; import java.rmi.RemoteException;

public interface ClientInterface extends Remote{ public void messageFromServer(String message) throws RemoteException;

}

  1. How did you enforce the unique username rule? What is the complexity (Big-Oh) of this process?
    

We had a Vector that held all of the usernames. When a user registers themselves, we would first check if the username was already in the Vector which is O(n).

  1. How is the server able to push a message to a specific client?
    

The server pushes a message to a specific client by looking up the client interface on the registry by the client's username. Then the server uses the messageFromServer method which prints message on the client's end.

  1. Did you design your code to handle any type of bug; such as network problems or user input errors?
    

a. If yes, mention two of these errors, and copy and paste below parts of your code, in which you handle them.

We designed our code to handle incorrect inputs from the user. If the user inputs something other than the commands that are available they will get an error message that tells them the commands that they are able to use and to try again.

               if (split_msg.length == 1 && split_msg[0].equals("list")){
                      System.out.println(stub.list());
                  }

            else if (split_msg.length == 1 && split_msg[0].equals("quit")){
                      stub.quit(username);
                      System.exit(0);
                  }

             else if (split_msg.length == 2 && split_msg[0].equals("register")){
                 username = split_msg[1];
                 ChatClient client = new ChatClient();
                 ClientInterface clientStub = (ClientInterface) UnicastRemoteObject.exportObject(client, 0);

                 System.out.println(stub.addClient(username, clientStub));
             }


              else if (split_msg[0].equals("send")){
                  String message = "";
                  for(int i = 2; i<split_msg.length-1; i++){
                      message+= split_msg[i] + " ";

                  }
                  message+= split_msg[split_msg.length-1];

                  stub.sendMessage(username, split_msg[1], message);
              }

              else {
                  System.out.println("You have entered an incorrect command."
                  + " Please try again! Here is a list of available commands"
                  + " for you to try:" +
                  "\n * register <username>: to join the system \n" +
                  " * list: view active IM clients \n" +
                  " * send <destination username> <message>: send an IM to a specific user \n" +
                  " * quit: close connection to server \n" +
                  ":-)");
              }

Additionally, we also accounted for if the user tries to send a message to someone that is not an active user. In this case, the user will get a message from the server that the message was unable to be sent and that they should try again.

public void sendMessage(String start, String dest, String message) throws RemoteException{ Registry registry = LocateRegistry.getRegistry();

	if (!users.contains(dest)){
	try{
		ClientInterface stub = (ClientInterface) registry.lookup(start);
		stub.messageFromServer("Unable to send message to " + dest
														+ ". Please try again!");
	}
	catch(Exception e){
		System.err.println("Exception :" + e.toString());
	}
}
else {
	try{
			ClientInterface stub = (ClientInterface) registry.lookup(dest);
		  stub.messageFromServer("message from " + start + ": " + message);
	}
	catch(Exception e){
		System.err.println("Exception :" + e.toString());
	}
}
⚠️ **GitHub.com Fallback** ⚠️