Esempio n. 1
0
  /**
   * Dispatches a message to the appropriate receiver.
   *
   * <p>It will buffer the message under the following conditions: 1) The MessageReceiver is not yet
   * registered. 2) The MessageReceiver is a PastryAppl, and localNode.isReady() == false
   *
   * @param msg the message.
   * @return true if message could be dispatched, false otherwise.
   */
  public boolean dispatchMessage(Message msg) {
    if (msg.getDestination() == 0) {
      Logger logger =
          localNode.getEnvironment().getLogManager().getLogger(MessageDispatch.class, null);
      if (logger.level <= Logger.WARNING)
        logger.logException(
            "Message " + msg + "," + msg.getClass().getName() + " has no destination.",
            new Exception("Stack Trace"));
      return false;
    }
    // NOTE: There is no safety issue with calling localNode.isReady() because this is on the
    // PastryThread, and the only way to set a node ready is also on the ready thread.
    PastryAppl mr = (PastryAppl) addressBook.get(Integer.valueOf(msg.getDestination()));

    if (mr == null) {
      if ((logger.level <= Logger.FINE) || (localNode.isReady() && (logger.level <= Logger.INFO))) {
        logger.log(
            "Dropping message "
                + msg
                + " because the application address "
                + msg.getDestination()
                + " is unknown.");
      }
      return false;
    } else {
      mr.receiveMessage(msg);
      return true;
    }
  }
Esempio n. 2
0
 /**
  * Stop listening for messages of the given type with the given listener.
  *
  * @param m specify message type and template we're listening for
  * @param listener destination for received messages
  */
 public void deregisterListener(Message template, MessageListener listener) {
   Integer amType = new Integer(template.amType());
   Vector vec = (Vector) templateTbl.get(amType);
   if (vec == null) {
     throw new IllegalArgumentException(
         "No listeners registered for message type "
             + template.getClass().getName()
             + " (AM type "
             + template.amType()
             + ")");
   }
   msgTemplate mt = new msgTemplate(template, listener);
   // Remove all occurrences
   while (vec.removeElement(mt)) ;
   if (vec.size() == 0) templateTbl.remove(amType);
 }
  public void handleMessageData(int length, MVByteBuffer messageData, AgentInfo agentInfo) {
    if (length == -1 || messageData == null) {
      if ((agentInfo.flags & MessageAgent.DOMAIN_FLAG_TRANSIENT) != 0) {
        Log.info("Lost connection to '" + agentInfo.agentName + "' (transient)");
        agents.remove(agentInfo.socket);
        agentNames.remove(agentInfo.agentName);
        messageIO.removeAgent(agentInfo);
      } else Log.info("Lost connection to '" + agentInfo.agentName + "'");

      try {
        agentInfo.socket.close();
      } catch (java.io.IOException ex) {
        Log.exception("close", ex);
      }
      agentInfo.socket = null;
      // ## clear buffers
      // ## keep agentInfo?  to preserve agentId?
      return;
    }

    Message message = (Message) MarshallingRuntime.unmarshalObject(messageData);

    MessageType msgType = message.getMsgType();
    if (Log.loggingDebug)
      Log.debug(
          "handleMessageData from "
              + agentInfo.agentName
              + ","
              + message.getMsgId()
              + " type="
              + msgType.getMsgTypeString()
              + " len="
              + length
              + " class="
              + message.getClass().getName());

    try {
      if (message instanceof AllocNameMessage)
        handleAllocName((AllocNameMessage) message, agentInfo.socket);
      else if (message instanceof AwaitPluginDependentsMessage)
        handleAwaitPluginDependents((AwaitPluginDependentsMessage) message, agentInfo.socket);
      else if (message instanceof PluginAvailableMessage)
        handlePluginAvailable((PluginAvailableMessage) message, agentInfo.socket);
      else
        Log.error(
            "Unsupported message from "
                + agentInfo.agentName
                + ","
                + message.getMsgId()
                + " type="
                + msgType.getMsgTypeString()
                + " len="
                + length
                + " class="
                + message.getClass().getName());
    } catch (java.io.IOException e) {
      Log.error(
          "IO error on message from "
              + agentInfo.agentName
              + ","
              + message.getMsgId()
              + " type="
              + msgType.getMsgTypeString()
              + " len="
              + length
              + " class="
              + message.getClass().getName());
    }
  }