FileDocCategorySizeDatePackage
MessageMediator.javaAPI DocExample3298Tue Jan 20 22:24:12 GMT 1998dcj.util.Collaborative

MessageMediator

public class MessageMediator extends Object implements Runnable, Mediator
Source code from "Java Distributed Computing", by Jim Farley. Class: MessageMediator Example: 9-5 Description: A mediator that uses message-passing for its communication.

Fields Summary
MessageHandler
mhandler
ServerSocket
socket
int
port
Constructors Summary
public MessageMediator(int p)


     
    initHandler();
    port = p;
  
public MessageMediator()

    initHandler();
  
Methods Summary
public booleanbroadcast(Identity from, java.lang.String mtag, java.lang.Object o)

    Message msg = new Message(mtag);
    msg.addArg(from);
    msg.addArg(o);
    return mhandler.sendMsg(msg);
  
public booleanbroadcast(Identity from, java.lang.String mtag, java.lang.String s)

    System.out.println("mm: Broadcasting message \"" + mtag + s + "\"");
    Message msg = new Message(mtag);
    msg.addArg(from);
    msg.addArg(s);
    return mhandler.sendMsg(msg);
  
public java.util.VectorgetMembers()

    Vector members = new Vector();
    Vector ids = mhandler.getAgentIds();
    Enumeration e = ids.elements();
    while (e.hasMoreElements()) {
      Integer id = (Integer)e.nextElement();
      Identity i = new Identity(id.intValue());
      members.addElement(i);
    }
    return members;
  
protected voidinitHandler()

    // Add the mediator message "prototype" to the handler
    Message m = new MediatorMessage(this);
    mhandler.addMessageType(m);
  
public IdentitynewMember()

    int id = mhandler.nextAgentId();
    Identity i = new Identity(id);
    return i;
  
public booleanremove(Identity i)

    int id = i.getId();
    boolean success = mhandler.removeAgent(id);
    return success;
  
public voidrun()

    // Make the server socket
    try {
      socket = new ServerSocket(port);
    }
    catch (IOException e) {
      System.out.println("Failed to bind to port " + port);
      return;
    }

    System.out.println("Mediator running on port " + port);

    // Listen for new clients...
    while (true) {
      try {
        Socket clientConn = socket.accept();
        Identity i = newMember();
        mhandler.addAgent(i.getId(), clientConn.getInputStream(),
                          clientConn.getOutputStream());
        System.out.println("Got new connection...");
        Message imsg = new Message("identity");
        imsg.addArg(i);
        mhandler.sendMsg(imsg, i.getId());
      }
      catch (Exception e) {}
    }
  
public booleansend(Identity to, Identity from, java.lang.String mtag, java.lang.Object o)

    Message msg = new Message(mtag);
    msg.addArg(from);
    msg.addArg(o);
    return mhandler.sendMsg(msg, to.getId());
  
public booleansend(Identity to, Identity from, java.lang.String mtag, java.lang.String s)

    boolean success = false;
    Message msg = new Message(mtag);
    msg.addArg(from);
    msg.addArg(s);
    return mhandler.sendMsg(msg, to.getId());