2

I want to get a list of all containers in the current platform. This question is similar, but the answer is obsolete and the method is by querying to the AMS agent. Is there any simpler way out than to communicate via ACL messages which I think is complex, there should be a way out to get a simple list of containers. Thanks for your help

Community
  • 1
  • 1
Purushottam
  • 626
  • 1
  • 8
  • 18

1 Answers1

5

You can achieve this by using the AMSSubscriber class and listen to the events when a container is added or removed. See sample code below:

public class myAgent extends Agent {

  private ArrayList<ContainerID> availableContainers = new ArrayList<ContainerID>();

  /**
   * Agent initializations
  **/
  protected void setup() {

    AMSSubscriber subscriber = new AMSSubscriber(){
      protected void installHandlers(Map handlers){
        EventHandler addedHandler = new EventHandler(){
          public void handle(Event event){
              AddedContainer addedContainer = (AddedContainer) event;
              availableContainers.add(addedContainer.getContainer());
          }
        };
    handlers.put(IntrospectionVocabulary.ADDEDCONTAINER,addedHandler);


        EventHandler removedHandler = new EventHandler(){
          public void handle(Event event){
              RemovedContainer removedContainer = (RemovedContainer) event;
              ArrayList<ContainerID> temp = new ArrayList<ContainerID>(availableContainers);
              for(ContainerID container : temp){
                  if(container.getID().equalsIgnoreCase(removedContainer.getContainer().getID()))
                      availableContainers.remove(container);
              }
          }
        };
        handlers.put(IntrospectionVocabulary.REMOVEDCONTAINER,removedHandler);
      }
    };
    addBehaviour(subscriber);
  }
}

Reference: 1) Developing multi-agent systems with JADE By Fabio Luigi Bellifemine, Giovanni Caire, Dominic Greenwood (page 111) 2) Jade API

Ravi Kadaboina
  • 8,384
  • 3
  • 27
  • 42
  • how do i get to know the list of available containers? It isnt allowing me to access the int size() since it is a private member. What to do? – Purushottam Feb 02 '12 at 16:29
  • add a public setter/getter to the ArrayList. For example: public int getAvailableContainersSize(){return availableContainers.size();} – Ravi Kadaboina Feb 02 '12 at 16:32
  • i am still not getting how to display the list of all containers, please could you help me where to place the code and what... – Purushottam Feb 02 '12 at 16:50
  • take a look at this [link](http://permalink.gmane.org/gmane.comp.java.jade.devel/329) you might get some idea on how to implement. – Ravi Kadaboina Feb 03 '12 at 06:41