Fields Summary |
---|
protected Hashtable | mPeersTable of currently connected Peers. |
protected Hashtable | mIdentities |
protected Identity | mIdentityThe 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_PROPStatic 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_PROPProperty for RMI registry name of remote peer. |
protected String | mRegNameName under which this peer is registered. |
Methods Summary |
---|
public void | addPeer(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 boolean | broadcast(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 boolean | connect(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 boolean | connect(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 Identity | getIdentity()
return mIdentity;
|
public RMIPeer | getPeer(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.Enumeration | getPeers()Get list of Peers
return mPeers.elements();
|
public void | handle(Message m, RMIPeer p)Handle an incoming message.
System.out.println("Got message: \"" + m.getId() + "\", body: \"" +
m.getBody() + "\" from " + p.getIdentity().getName());
|
protected void | register()
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 void | removePeer(RMIPeer p)Remove a peer from our list.
Identity id = (Identity)mIdentities.get(p);
mPeers.remove(id);
mIdentities.remove(p);
|
public boolean | send(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 void | setIdentity(Identity i)
mIdentity = i;
|