views:

462

answers:

1

Can anyone tell me how to find available agent containers through java code? I am using the JADE agent framework and I have figured out how to create new containers but not find existing containers (so that agents can be deployed in them).

+1  A: 

There are two ways of doing this, depending on whether you want to receive the information via an ongoing service or the current snapshot in a message.

To get a snapshot of the IDs of the currently available agent containers, send a Request message to the Agent Management Service (AMS) and wait for its reply. Using the JADE Management Ontology and the QueryPlatformLocationsAction term, the sending and receiving methods would be:

private void queryAMS() throws CodecException, OntologyException {
    QueryPlatformLocationsAction query = new QueryPlatformLocationsAction();
    Action action = new Action(myAgent.getAID(), query);

    ACLMessage message = new ACLMessage(ACLMessage.REQUEST);
    message.addReceiver(myAgent.getAMS());
    message.setLanguage(FIPANames.ContentLanguage.FIPA_SL);
    message.setOntology(JADEManagementOntology.getInstance().getName());
    myAgent.getContentManager().fillContent(message, action);
    myAgent.send(message);
}
private void listenForAMSReply() throws UngroundedException, CodecException, 
OntologyException {
    ACLMessage receivedMessage = myAgent.blockingReceive(MessageTemplate
            .MatchSender(myAgent.getAMS()));
    ContentElement content = myAgent.getContentManager().extractContent(
        receivedMessage);

    // received message is a Result object, whose Value field is a List of
    // ContainerIDs
    Result result = (Result) content;
    List listOfPlatforms = (List) result.getValue();

    // use it
    Iterator iter = listOfPlatforms.iterator();
    while (iter.hasNext()) {
        ContainerID next = (ContainerID) iter.next();
        System.out.println(next.getID());
    }
}

To get this information as an ongoing service, and to receive the ContainerID of each container as it registers with the AMS, create a Behaviour that extends the AMSSubscriber. Register a handler for the AddedContainer event and you will be able to access the ContainerID of the newly available container:

public class AMSListenerBehaviour extends AMSSubscriber {
@Override
public void installHandlers(Map handlersTable) {
    handlersTable.put(AddedContainer.NAME, addedContainerHandler);
}

public final class AddedContainerHandler implements EventHandler {
@Override
public void handle(Event ev) {
    AddedContainer event = (AddedContainer) ev;
    ContainerID addedContainer = event.getContainer();
    System.out.println(addedContainer.getID());
}

Hope this helps,

Russ

DoctorRuss