Пример #1
0
  /**
   * An agent on the FrontEnd has sent a message. Note that the NotFoundException here is referred
   * to the sender and indicates an inconsistency between the FrontEnd and the BackEnd
   */
  public void messageOut(ACLMessage msg, String sender) throws NotFoundException, IMTPException {
    // Check whether the sender exists
    AID id = new AID(sender, AID.ISLOCALNAME);

    synchronized (frontEndSynchLock) {
      AgentImage image = getAgentImage(id);
      if (image == null) {
        if (synchronizing) {
          // The image is not yet there since the front-end is synchronizing.
          // Buffer the message. It will be delivered as soon as the
          // FrontEnd synchronization process completes
          postponeAfterFrontEndSynch(msg, sender);
          return;
        } else {
          throw new NotFoundException("No image for agent " + sender + " on the BackEndContainer");
        }
      }
    }

    int size;
    if (msg.hasByteSequenceContent()) {
      size = msg.getByteSequenceContent().length;
    } else {
      size = msg.getContent() != null ? msg.getContent().length() : 0;
    }
    myLogger.log(
        Logger.INFO,
        getID()
            + " - Delivering OUT message "
            + ACLMessage.getPerformative(msg.getPerformative())
            + ", size="
            + size);
    handleSend(msg, id, false);
  }
Пример #2
0
  /**
   * Dispatch a message to an agent in the FrontEnd. If this method is called by a thread that is
   * serving a message sent by an agent in the FrontEnd too, nothing is done as the dispatch has
   * already taken place in the FrontEnd (see messageOut()).
   */
  public boolean postMessageToLocalAgent(ACLMessage msg, AID receiverID) {

    // Try first in the LADT
    boolean found = super.postMessageToLocalAgent(msg, receiverID);
    if (found) {
      return found;
    } else {
      // The receiver must be in the FrontEnd
      AgentImage image = (AgentImage) agentImages.get(receiverID);
      if (image != null) {
        if (agentImages.containsKey(msg.getSender()) && isExplicitReceiver(msg, receiverID)) {
          // The message was sent by an agent living in the FrontEnd. The
          // receiverID (living in the FrontEnd too) has already received
          // the message.
          // The second part of the condition ensures that, if the
          // message was not directly sent to the receiver (e.g. it was sent to a topic
          // or an alias), message delivery occurs normally.
          // FIXME: This does not take into account that an agent not living
          // in the FrontEnd may send a message on behalf of an agent living
          // in the FrontEnd.
          return true;
        }

        try {
          // Forward the message to the FrontEnd
          int size;
          if (msg.hasByteSequenceContent()) {
            size = msg.getByteSequenceContent().length;
          } else {
            size = msg.getContent() != null ? msg.getContent().length() : 0;
          }
          myLogger.log(
              Logger.INFO,
              getID()
                  + " - Delivering IN message "
                  + ACLMessage.getPerformative(msg.getPerformative())
                  + ", size="
                  + size);
          myFrontEnd.messageIn(msg, receiverID.getLocalName());
          handlePosted(receiverID, msg);
          return true;
        } catch (NotFoundException nfe) {
          System.out.println("WARNING: Missing agent in FrontEnd");
          return false;
        } catch (IMTPException imtpe) {
          System.out.println("WARNING: Can't deliver message to FrontEnd");
          return false;
        }
      } else {
        // Agent not found
        System.out.println("WARNING: Agent " + receiverID + " not found on BackEnd container");
        return false;
      }
    }
  }