COMP 310
Fall 2012

Lec33:  Chat App API finalization

Home  Info  Owlspace  Java Resources  Eclipse Resources

 

IUser.receiveData() return type:  DataPacket

 

Well-Known Data Types

Change IChatRoom.getUsers()?

What should the return type of this method be? 

 

Starting up the ChatApp application

Remember that in order to chat, your ChatApp program must be BOTH a RMI client AND a RMI server!

Things that need to get done (NOT A COMPLETE LIST!):

 

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?

Identifying IUsers

Question:   Is the UUID value tied to the instance of the IUser 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 IUser implementation, 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

 

 

 


© 2012 by Stephen Wong