FileDocCategorySizeDatePackage
RMIMediatorImpl.javaAPI DocExample5102Sun Oct 31 15:36:14 GMT 1999dcj.util.Collaborative

RMIMediatorImpl

public class RMIMediatorImpl extends UnicastRemoteObject implements RMIMediator
Source code from "Java Distributed Computing", by Jim Farley. Class: RMIMediatorImpl Example: 9-12 Description: Implementation of the RMI-based mediator.

Fields Summary
Hashtable
clients
Vector
idList
Constructors Summary
public RMIMediatorImpl()


      
    super();
  
Methods Summary
public booleanbroadcast(Identity from, java.lang.String mtag, java.lang.String msg)

    System.out.println("Broadcasting...");
    boolean success = true;
    Enumeration ids;
    synchronized (clients) {
      ids = clients.keys();
    }
    RMICollaborator target = null;
    while (ids.hasMoreElements()) {
      Identity i = (Identity)ids.nextElement();
      synchronized (clients) {
        target = (RMICollaborator)clients.get(i);
      }
      synchronized (target) {
        if (target == null ||
            !target.notify(mtag, msg, from)) {
          success = false;
        }
      }
    }
    return success;
  
public booleanbroadcast(Identity from, java.lang.String mtag, java.lang.Object data)

    boolean success = true;
    Enumeration ids;
    synchronized (clients) {
      ids = clients.keys();
    }
    RMICollaborator target = null;
    while (ids.hasMoreElements()) {
      Identity i = (Identity)ids.nextElement();
      synchronized (clients) {
        target = (RMICollaborator)clients.get(i);
      }
      synchronized (target) {
        if (target == null ||
            !target.notify(mtag, data, from)) {
          success = false;
        }
      }
    }
    return success;
  
protected RMICollaboratorgetMember(Identity i)

    Enumeration ids;
    synchronized (clients) {
      ids = clients.keys();
    }
    RMICollaborator c = null;
    Identity tmp;
    while (c == null && ids.hasMoreElements()) {
       tmp = (Identity)ids.nextElement();
       if (tmp.equals(i)) {
         synchronized (clients) {
           c = (RMICollaborator)clients.get(tmp);
         }
       }
    }
    return c;
  
public java.util.VectorgetMembers()

    synchronized (idList) {
      return (Vector)idList.clone();
    }
  
public static voidmain(java.lang.String[] argv)

    // Install a security manager
    System.setSecurityManager(new RMISecurityManager());

    try {
      String name = "TheMediator";
      System.out.println("Registering RMIMediatorImpl as \""
                         + name + "\"");
      RMIMediatorImpl mediator = new RMIMediatorImpl();
      System.out.println("Created mediator, binding...");
      Naming.rebind(name, mediator);
      System.out.println("Remote mediator ready...");
    }
    catch (Exception e) {
      System.out.println("Caught exception while registering: "
                         + e);
    }
  
public IdentitynewMember()

    int max = -1;
    boolean found = true;
    Enumeration enum = idList.elements();
    // Find max identity num given out thus far
    while (enum.hasMoreElements()) {
      Identity id = (Identity)enum.nextElement();
      int i = id.getId();
      if (i > max) {
        max = i;
      }
    }
    // New identity 
    Identity newId = new Identity(max + 1);
    synchronized (idList) {
      idList.addElement(newId);
    }
    return newId;
  
public booleanregister(Identity i, RMICollaborator c)

    System.out.println("Registering member " + i.getId()
                       + " as " + c.getIdentity().getName());
    clients.put(i, c);
    return true;
  
public booleanremove(Identity i)

    boolean success = true;
    synchronized (idList) {
      synchronized (clients) {
        if (idList.removeElement(i) && clients.remove(i) != null) {
          success = true;
        }
        else {
          success = false;
        }
      }
    }
    return success;
  
public booleansend(Identity to, Identity from, java.lang.String mtag, java.lang.String msg)

    boolean success = false;
    RMICollaborator c = getMember(to);
    synchronized (c) {
      if (c != null) {
        success = c.notify(mtag, msg, from);
      }
    }

    return success;
  
public booleansend(Identity to, Identity from, java.lang.String mtag, java.lang.Object data)

    boolean success = false;
    RMICollaborator c = getMember(to);
    synchronized (c) {
      if (c != null) {
        success = c.notify(mtag, data, from);
      }
    }
    return success;