FileDocCategorySizeDatePackage
RMICollaboratorImpl.javaAPI DocExample4062Tue Jan 20 22:29:02 GMT 1998dcj.util.Collaborative

RMICollaboratorImpl

public class RMICollaboratorImpl extends UnicastRemoteObject implements RMICollaborator
Source 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 booleanbroadcast(java.lang.String tag, java.lang.String msg)

    boolean success = false;
    if (mediator != null) {
      success = mediator.broadcast(getIdentity(), tag, msg);
    }
    return success;
  
public booleanbroadcast(java.lang.String tag, java.lang.Object data)

    boolean success = false;
    if (mediator != null) {
      success = mediator.broadcast(getIdentity(), tag, data);
    }
    return success;
  
public booleanconnect(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 IdentitygetIdentity()

 return id; 
public static voidmain(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 booleannotify(java.lang.String tag, java.lang.Object data, Identity src)

    System.out.println("Got message: \"" + tag + " " + data + "\""
                       + " from " + src.getName());
    return true;
  
public booleannotify(java.lang.String tag, java.lang.String msg, Identity src)

    System.out.println("Got message: \"" + tag + " " + msg + "\""
                       + " from " + src.getName());
    return true;
  
public booleansend(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 booleansend(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;