RMICollaboratorImplpublic class RMICollaboratorImpl extends UnicastRemoteObject implements RMICollaboratorSource code from "Java Distributed Computing", by Jim Farley.
Class: RMICollaboratorImpl
Example: 9-11
Description: Implementation of the RMI-based collaborator. |
Fields Summary |
---|
protected Identity | id | protected RMIMediator | mediator |
Constructors Summary |
---|
public RMICollaboratorImpl(String name, String host, String mname)
id = new Identity(0);
id.setName(name);
Properties p = new Properties();
p.put("host", host);
p.put("mediatorName", mname);
connect(p);
| public RMICollaboratorImpl(String name)
id = new Identity(0);
id.setName(name);
|
Methods Summary |
---|
public boolean | broadcast(java.lang.String tag, java.lang.String msg)
boolean success = false;
if (mediator != null) {
success = mediator.broadcast(getIdentity(), tag, msg);
}
return success;
| public boolean | broadcast(java.lang.String tag, java.lang.Object data)
boolean success = false;
if (mediator != null) {
success = mediator.broadcast(getIdentity(), tag, data);
}
return success;
| public boolean | connect(java.util.Properties p)
boolean success = false;
String host = p.getProperty("host");
String mName = p.getProperty("mediatorName");
if (host != null && mName != null) {
try {
String url = "rmi://" + host + "/" + mName;
System.out.println("looking up " + url);
mediator = (RMIMediator)Naming.lookup(url);
System.out.println("Got mediator " + mediator);
Identity newId = mediator.newMember();
mediator.register(newId, this);
newId.setName(id.getName());
id = newId;
success = true;
}
catch (Exception e) {
e.printStackTrace();
success = false;
}
}
return success;
| public Identity | getIdentity() return id;
| public static void | main(java.lang.String[] argv)
// Install a security manager
System.setSecurityManager(new RMISecurityManager());
try {
String name = argv[0];
String host = argv[1];
String mname = argv[2];
Properties props = new Properties();
props.put("host", host);
props.put("mediatorName", mname);
RMICollaboratorImpl c = new RMICollaboratorImpl(name);
if (c.connect(props)) {
System.out.println("Got mediator...");
c.broadcast("msg", "hello world");
}
}
catch (Exception e) {
System.out.println("Caught exception:");
e.printStackTrace();
}
| public boolean | notify(java.lang.String tag, java.lang.Object data, Identity src)
System.out.println("Got message: \"" + tag + " " + data + "\""
+ " from " + src.getName());
return true;
| public boolean | notify(java.lang.String tag, java.lang.String msg, Identity src)
System.out.println("Got message: \"" + tag + " " + msg + "\""
+ " from " + src.getName());
return true;
| public boolean | send(java.lang.String tag, java.lang.String msg, Identity dst)
boolean success = false;
if (mediator != null) {
success = mediator.send(dst, getIdentity(), tag, msg);
}
return success;
| public boolean | send(java.lang.String tag, java.lang.Object data, Identity dst)
boolean success = false;
if (mediator != null) {
success = mediator.send(dst, getIdentity(), tag, data);
}
return success;
|
|