FileDocCategorySizeDatePackage
MessageCollaborator.javaAPI DocExample4089Tue Jan 20 22:25:54 GMT 1998dcj.util.Collaborative

MessageCollaborator

public class MessageCollaborator extends Object implements Collaborator
Source code from "Java Distributed Computing", by Jim Farley. Class: MessageCollaborator Example: 9-7 Description: A colaborator that communicates using message-passing.

Fields Summary
MessageHandler
handler
Identity
id
String
name
Constructors Summary
public MessageCollaborator(String n)


     
    initHandler();
    name = n;
  
public MessageCollaborator(String host, int port, String n)

    initHandler();
    name = n;
    Properties p = new Properties();
    p.put("host", host);
    p.put("port", String.valueOf(port));
    connect(p);
  
Methods Summary
public booleanbroadcast(java.lang.String tag, java.lang.String msg)

    boolean success = false;
    Message m = new Message("broadcast");
    m.addArg(getIdentity());
    m.addArg(tag);
    m.addArg(msg);
    System.out.println("mc: Sending broadcast message \"" + tag + "\"");
    success = handler.sendMsg(m);
    System.out.println("mc: success = " + success);
    return success;
  
public booleanbroadcast(java.lang.String tag, java.lang.Object data)

    boolean success = true;
    Message m = new Message("broadcast");
    m.addArg(getIdentity());
    m.addArg(tag);
    m.addArg("#OBJ");
    m.addArg(data);
    success = handler.sendMsg(m);
    return success;
  
public booleanconnect(java.util.Properties p)

    boolean success = false;

    String host = p.getProperty("host");
    String itmp = p.getProperty("port");
    if (host != null && itmp != null) {
      try {
        int port = Integer.parseInt(itmp);
        // Make a socket connection to the mediator.
        Socket mConn = new Socket(host, port);
        int pid = handler.addAgent(mConn.getInputStream(),
                                  mConn.getOutputStream());
        System.out.println("Got socket to Mediator, id = "
                           + id + "...");
        // The mediator should send us an identity in a message...
        Message imsg = handler.readMsg(pid);
        System.out.println("Got message with id = " + imsg.messageID());
        if (imsg.messageID().compareTo("identity") == 0) {
          id = (Identity)imsg.getArg(0);
          id.setName(name);
          System.out.println("Got identity from mediator, id = "
                             + id.getId() + "...");
          success = true;
        }
        else {
          handler.removeAgent(pid);
          success = false;
        }
      }
      catch (Exception e) {
        success = false;
      }
    }
    else {
      success = false;
    }
    return success;
  
public IdentitygetIdentity()

 return id; 
protected voidinitHandler()

    handler.addMessageType(new CollaboratorMessage(this));
  
public booleannotify(java.lang.String tag, java.lang.String msg, Identity src)

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

    System.out.println("Received \"" + tag + "\" object \""
                       + data + "\" from " + src.getName());
    return true;
  
public booleansend(java.lang.String tag, java.lang.String msg, Identity dst)

    boolean success = false;
    Message m = new Message("send");
    m.addArg(getIdentity());
    m.addArg(dst);
    m.addArg(tag);
    m.addArg(msg);
    success = handler.sendMsg(m);

    return success;
  
public booleansend(java.lang.String tag, java.lang.Object data, Identity dst)

    boolean success = false;
    Message m = new Message("send");
    m.addArg(getIdentity());
    m.addArg(dst);
    m.addArg(tag);
    m.addArg("#OBJ");
    m.addArg(data);
    success = handler.sendMsg(m);
    return success;