COMP 310
Spring 2018

Lec33:  Chat App API finalization

Home  Info  Canvas   Java Resources  Eclipse Resources  Piazza

 

Today will be spent having the groups give their final presentations to the class covering their changes since the last presentation with an emphasis on how they are addressing the feedback they received.    Remember that each group is trying to sell their API proposal to the class in order to win the API competition!

 

Adding Yourself to a Chat Room

  What are the steps to add yourself to a chat room?   Do you add yourself (your IUser stub) locally and then send a message to everyone to add you to their chat rooms or vice versa?

Be careful!  What if you are already in the room?

 

 

UPDATE:   In the latest version of  Java 8, RMI stub objects support .equals() and .hashCode() such that different instances of stubs that refer back to the same RMI Server objects will properly be comparable as equal and will generate the same hash code.   Thus, RMI Remote stubs can be used directly as keys in hash tables, etc. without any need for proxying or other techniques as described below.    Please see the "Comparing DeserializedObjects" page in the Java Resources site. 

Identifying message receiver stubs:

Question:   Is the UUID value tied to the instance of the RMI server object associated with the stub or is it tied to the instance of the ChatApp, i.e. the human operator?    That is, if one's model instantiates more than one instance its own message receiver object, is that a different user or the same user?     Is this an API issue or not?

 

Overriding equals() and hashCode() in a proxy class to enable standard equality tests:

	private class ProxyUser implements IUser, Serializable {
				
		/**
		 * The decoree
		 */
		private IUser stub;
		
		// --- Other methods and fields elided -----------

		/**
		 * Overridden equals() method to compare UUID's
		 * @return  Equal if UUID's are equal.  False otherwise.
		 */
		@Override
		public boolean equals(Object other){
			if(other instanceof IUser) { // make sure that other object is an IUser
				try {
					// Equality of IUsers is same as equality of UUID's.
					return stub.getUUID().equals(((IUser)other).getUUID());
				} catch (RemoteException e) {
					// Deal with the exception without throwing a RemoteException.
					System.err.println("ProxyUser.equals(): error getting UUID: "+ e);
					e.printStackTrace();
					// Fall through and return false
				}
			}
			return false;
		}		

		/**
		 * Overridden hashCode() method to create a hashCode from that is hashCode of the UUID since
		 * equality is based on equality of UUID.
		 * @return a hashCode of the UUID.	
		 */
		@Override
		public int hashCode(){
			try {
				// hashCode is shorter than UUID, but Java spec says that if two objects are equal then
				// their hashCodes must also be equal, which will be true here since equals() is based on 
				// UUID equality.  Java does NOT require that unequal entities have unequal hashCodes. 
				return stub.getUUID().hashCode();
			} catch (RemoteException e) {
				// Deal with the exception without throwing a RemoteException.
				System.err.println("ProxyStub.hashCode(): Error calling remote method on IUser stub: "+e);
				e.printStackTrace();
				return super.hashCode();  // return some sort of hashCode
			}
		}
	}

 

 

Java How-To's

 

 

 


© 2018 by Stephen Wong