FileDocCategorySizeDatePackage
RMIPeerImpl.javaAPI DocExample6389Tue Oct 06 23:23:44 BST 1998jdc.patterns.peer

RMIPeerImpl

public class RMIPeerImpl extends UnicastRemoteObject implements RMIPeer

Fields Summary
protected Hashtable
mPeers
Table of currently connected Peers.
protected Hashtable
mIdentities
protected Identity
mIdentity
The networked identity of this peer. Subclasses implementing this abstract class will need to provide a means for initializing this identity, such that the identity is unique in the distributed context.
public static final String
HOST_PROP
Static host property name. The host specifier includes any required port specifier, if the default RMI port is not in use.
public static final String
REGNAME_PROP
Property for RMI registry name of remote peer.
protected String
mRegName
Name under which this peer is registered.
Constructors Summary
public RMIPeerImpl()
Default constructor. Leave identity uninitialized, to indicate no identity.

  
               
      
    mIdentity = new Identity();
    register();
  
public RMIPeerImpl(String name)

    super();
    mRegName = name;
    mIdentity = new Identity();
    register();
  
Methods Summary
public voidaddPeer(RMIPeer p)
Add a peer to our list.

    // Store peer by id in one table, and store id by peer in another
    Identity id = p.getIdentity();
    mPeers.put(id, p);
    mIdentities.put(p, id);
  
public booleanbroadcast(Message msg)
Broadcast the message to all currently connected peers.

    boolean success = true;
    try {
      Enumeration pEnum = getPeers();
      while (pEnum.hasMoreElements()) {
	RMIPeer p = (RMIPeer)pEnum.nextElement();
	
	try {
	  if (!send(msg, p.getIdentity())) {
	    success = false;
	  }
	}
	catch (java.rmi.RemoteException ce) {
	  System.out.println("Lost connection to peer");
	  try { removePeer(p); } catch (Exception e) { e.printStackTrace(); }
	}
      }
    }
    catch (RemoteException re) {
      System.out.println("broadcast failed: ");
      re.printStackTrace();
    }
    return success;
  
public booleanconnect(java.util.Properties connProps)
Connect to a peer given the connection properties. This RMI-based peer requires the host and registry name of the remote peer object.

    Identity id = new Identity();
    id.setName((String)connProps.getProperty(REGNAME_PROP));
    id.setProperty(HOST_PROP, (String)connProps.getProperty(HOST_PROP));
    return connect(id);
  
public booleanconnect(Identity peerID)
Connect to a peer given the connection properties and identity of the peer. Subclasses can choose to implement this using both arguments, or ignoring one in favor of the other (connection properties vs. properties pulled from the remote peer identifier).

    boolean success = false;
    String host = (String)peerID.getProperty(HOST_PROP);
    String regName = peerID.getName();
    // If no host specified, assume a local peer
    if (host == null) {
      host = "localhost";
    }

    // If no registry name, then fail right here
    if (regName == null || regName.trim().length() == 0) {
      System.out.println("RMIPeerImpl.connect: No registry name in identity.");
      return false;
    }

    String rmiURL = "rmi://" + host + "/" + regName;
    try {
      RMIPeer p = (RMIPeer)Naming.lookup(rmiURL);
      addPeer(p);
      p.addPeer(this);
      success = true;
    }
    catch (Exception re) {
      re.printStackTrace();
    }

    return success;
  
public IdentitygetIdentity()

    return mIdentity;
  
public RMIPeergetPeer(Identity i)
Get the peer identified by the given identity.

    Enumeration e = mPeers.keys();
    RMIPeer p = null;
    while (e.hasMoreElements()) {
      Object o = e.nextElement();
      if (i.equals(o)) {
	p = (RMIPeer)mPeers.get(o);
	break;
      }
    }
    
    return p;
  
public java.util.EnumerationgetPeers()
Get list of Peers

    return mPeers.elements();
  
public voidhandle(Message m, RMIPeer p)
Handle an incoming message.

    System.out.println("Got message: \"" + m.getId() + "\", body: \"" +
		       m.getBody() + "\" from " + p.getIdentity().getName());
  
protected voidregister()

    try {
      // Bind this peer to our name in the local registry
      Naming.rebind(mRegName, this);
      // Set our identity properties accordingly
      // The "name" is the name used in the registry.
      getIdentity().setName(mRegName);
      // The "host" property includes the local host name and the port
      // on which the RMI registry is listening on the local host.
      getIdentity().setProperty(HOST_PROP,
				InetAddress.getLocalHost().getHostName() +
				":" + Registry.REGISTRY_PORT);
    }
    catch (Exception e) {
      System.out.println("Error registering peer \"" + mRegName + "\":");
      e.printStackTrace();
    }
  
public voidremovePeer(RMIPeer p)
Remove a peer from our list.

    Identity id = (Identity)mIdentities.get(p);
    mPeers.remove(id);
    mIdentities.remove(p);
  
public booleansend(Message msg, Identity peer)
Send the message to the identified peer.

    boolean success = false;
    RMIPeer p = null;
    try {
      p = (RMIPeer)getPeer(peer);
      p.handle(msg, this);
      success = true;
    }
    catch (java.rmi.ConnectException ce) {
      System.out.println("Lost connection to peer.");
      try { removePeer(p); } catch (Exception e) { e.printStackTrace(); }
    }
    catch (Exception e) {
      System.out.println("Error sending message:" );
      e.printStackTrace();
    }

    return success;
  
public voidsetIdentity(Identity i)

    mIdentity = i;