Question

I have two pc's on which i am running agents.Both are connected by LAN(or wifi). I want these agents to communicate. One of the ways i found is by giving agent's full addresses.Below is the code snippet.

AID a = new AID("A@192.168.14.51:1099/JADE",AID.ISGUID);
a.addAddresses("http://192.168.14.51:7778/acc");
msg.addReceiver(a);
send(msg);

however Once i start agents at one platform, i want the agents on other platform to be able to register services on its yellow pages so that i can search for appropriate agent from a list of same.I looked but could not find anything about it. Please give me suggestion on how i can achieve this.

Was it helpful?

Solution

Well, you are looking for DF federation. As far as I understand, it is nothing but 'connecting' DFs. There is an example in yelloPages package in 'jade all examples' folder. It creates register,subscriber,searcher and a subDF agent. registrer agent registers agent with soe property and other agents do their jobs. SubDF creates child DF which involves DF Federation. For you, I modified the code as this:

Next three agents run on port 1099 as:

1)

 package examples.yellowPages;

    import jade.core.Agent;
    import jade.core.AID;
    import jade.domain.DFService;
    import jade.domain.FIPAException;
    import jade.domain.FIPANames;
    import jade.domain.FIPAAgentManagement.DFAgentDescription;
    import jade.domain.FIPAAgentManagement.ServiceDescription;
    import jade.domain.FIPAAgentManagement.Property;

    /**
       This example shows how to register an application specific service in the Yellow Pages
       catalogue managed by the DF Agent so that other agents can dynamically discover it.
       In this case in particular we register a "Weather-forecast" service for 
       Italy. The name of this service is specified as a command line argument.
       @author Giovanni Caire - TILAB
     */
    public class DFRegisterAgent extends Agent {

      protected void setup() {
        String serviceName = "unknown";

        // Read the name of the service to register as an argument
        Object[] args = getArguments();
        if (args != null && args.length > 0) {
            serviceName = (String) args[0];
        }

        // Register the service
        System.out.println("Agent "+getLocalName()+" registering service \""+serviceName+"\" of type \"weather-forecast\"");
        try {
            DFAgentDescription dfd = new DFAgentDescription();
            dfd.setName(getAID());
            ServiceDescription sd = new ServiceDescription();
            sd.setName(serviceName);
            sd.setType("weather-forecast");
            // Agents that want to use this service need to "know" the weather-forecast-ontology
            sd.addOntologies("weather-forecast-ontology");
            // Agents that want to use this service need to "speak" the FIPA-SL language
            sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL);
            sd.addProperties(new Property("country", "Italy"));
            dfd.addServices(sd);

            DFService.register(this, dfd);
        }
        catch (FIPAException fe) {
            fe.printStackTrace();
        }
      } 
    }

2)

package examples.yellowPages;

import jade.core.Agent;
import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAAgentManagement.SearchConstraints;
import jade.util.leap.Iterator;

/**
   This example shows how to search for services provided by other agents 
   and advertised in the Yellow Pages catalogue managed by the DF agent.
   In this case in particular we search for agents providing a 
   "Weather-forecast" service.
   @author Giovanni Caire - TILAB
 */
public class DFSearchAgent extends Agent {

  protected void setup() {
    // Search for services of type "weather-forecast"
    System.out.println("Agent "+getLocalName()+" searching for services of type \"weather-forecast\"");
    try {
        // Build the description used as template for the search
        DFAgentDescription template = new DFAgentDescription();
        ServiceDescription templateSd = new ServiceDescription();
        templateSd.setType("weather-forecast");
        template.addServices(templateSd);

        SearchConstraints sc = new SearchConstraints();
        // We want to receive 10 results at most
        sc.setMaxResults(new Long(10));

        DFAgentDescription[] results = DFService.search(this, template, sc);
        if (results.length > 0) {
            System.out.println("Agent "+getLocalName()+" found the following weather-forecast services:");
            for (int i = 0; i < results.length; ++i) {
                DFAgentDescription dfd = results[i];
                AID provider = dfd.getName();
                // The same agent may provide several services; we are only interested
                // in the weather-forcast one
                Iterator it = dfd.getAllServices();
                while (it.hasNext()) {
                    ServiceDescription sd = (ServiceDescription) it.next();
                    if (sd.getType().equals("weather-forecast")) {
                        System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
                    }
                }
            }
        }   
        else {
            System.out.println("Agent "+getLocalName()+" did not find any weather-forecast service");
        }
    }
    catch (FIPAException fe) {
        fe.printStackTrace();
    }
  } 
}

3)

package examples.yellowPages;

import jade.core.Agent;
import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.SearchConstraints;
import jade.proto.SubscriptionInitiator;
import jade.lang.acl.ACLMessage;
import jade.util.leap.Iterator;

/**
   This example shows how to subscribe to the DF agent in order to be notified 
   each time a given service is published in the yellow pages catalogue.
   In this case in particular we want to be informed whenever a service of type
   "Weather-forecast" for Italy becomes available.
   @author Giovanni Caire - TILAB
 */
public class DFSubscribeAgent extends Agent {

  protected void setup() {
        // Build the description used as template for the subscription
        DFAgentDescription template = new DFAgentDescription();
        ServiceDescription templateSd = new ServiceDescription();
        templateSd.setType("weather-forecast");
        templateSd.addProperties(new Property("country", "Italy"));
        template.addServices(templateSd);

        SearchConstraints sc = new SearchConstraints();
        // We want to receive 10 results at most
        sc.setMaxResults(new Long(10));

        addBehaviour(new SubscriptionInitiator(this, DFService.createSubscriptionMessage(this, getDefaultDF(), template, sc)) {
            protected void handleInform(ACLMessage inform) {
            System.out.println("Agent "+getLocalName()+": Notification received from DF");
            try {
                    DFAgentDescription[] results = DFService.decodeNotification(inform.getContent());
                if (results.length > 0) {
                    for (int i = 0; i < results.length; ++i) {
                        DFAgentDescription dfd = results[i];
                        AID provider = dfd.getName();
                        // The same agent may provide several services; we are only interested
                        // in the weather-forcast one
                        Iterator it = dfd.getAllServices();
                        while (it.hasNext()) {
                            ServiceDescription sd = (ServiceDescription) it.next();
                            if (sd.getType().equals("weather-forecast")) {
                                System.out.println("Weather-forecast service for Italy found:");
                                System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
                            }
                        }
                    }
                }   
                System.out.println();
            }
            catch (FIPAException fe) {
                fe.printStackTrace();
            }
            }
        } );
  } 
}

4) This is the last one. It creates a DF and registers in DFRegister agent i.e. DF federation is done. I ran this on 1331 port. Remember to change IP addresses. (u can run agent on different port by using -local-port 1331. Remember to run previous agents before this. You can put it in different eclipse project and run it.

import jade.core.*;
import jade.core.behaviours.*;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.FIPAException;
import jade.domain.DFService;
import jade.domain.FIPANames;
import jade.util.leap.Iterator;

/**
This is an example of an agent that plays the role of a sub-df by 
automatically registering with a parent DF.
Notice that exactly the same might be done by using the GUI of the DF.
<p>
This SUBDF inherits all the functionalities of the default DF, including
its GUI.
@author Giovanni Rimassa - Universita` di Parma
@version $Date: 2003-12-03 17:57:03 +0100 (mer, 03 dic 2003) $ $Revision: 4638 $
*/

public class SubDF2 extends jade.domain.df {


  public void setup() {

   // Input df name
   int len = 0;
   byte[] buffer = new byte[1024];

   try {

//     AID parentName = getDefaultDF();
        AID parentName = new AID("df@10.251.216.135:1099/JADE");
        parentName.addAddresses("http://NikhilChilwant:7778/acc");

     //Execute the setup of jade.domain.df which includes all the default behaviours of a df 
     //(i.e. register, unregister,modify, and search).
     super.setup();

     //Use this method to modify the current description of this df. 
     setDescriptionOfThisDF(getDescription());

     //Show the default Gui of a df.
     super.showGui();

     DFService.register(this,parentName,getDescription());
     addParent(parentName,getDescription());
         System.out.println("Agent: " + getName() + " federated with default df.");


         DFAgentDescription template = new DFAgentDescription();
            ServiceDescription templateSd = new ServiceDescription();
            templateSd.setType("weather-forecast");
            templateSd.addProperties(new Property("country", "Italy"));
            template.addServices(templateSd);

            SearchConstraints sc = new SearchConstraints();
            // We want to receive 10 results at most
            sc.setMaxResults(new Long(10));
            DFAgentDescription[] results = DFService.search(this,parentName, template, sc);
        /*  if (results.length > 0) {*/
                System.out.println("SUB DF ***Agent "+getLocalName()+" found the following weather-forecast services:");
                for (int i = 0; i < results.length; ++i) {
                    DFAgentDescription dfd = results[i];
                    AID provider = dfd.getName();
                    // The same agent may provide several services; we are only interested
                    // in the weather-forcast one
                    Iterator it = dfd.getAllServices();
                    while (it.hasNext()) {
                        ServiceDescription sd = (ServiceDescription) it.next();
                        if (sd.getType().equals("weather-forecast")) {
                            System.out.println("- Service \""+sd.getName()+"\" provided by agent "+provider.getName());
                        }
                    }
                }/*}*/


                String serviceName = "unknown2";
                DFAgentDescription dfd = new DFAgentDescription();
                dfd.setName(getAID());
                ServiceDescription sd = new ServiceDescription();
                sd.setName(serviceName);
                sd.setType("weather-forecast2");
                // Agents that want to use this service need to "know" the weather-forecast-ontology
                sd.addOntologies("weather-forecast-ontology2");
                // Agents that want to use this service need to "speak" the FIPA-SL language
                sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL);
                sd.addProperties(new Property("country2", "Italy2"));
                dfd.addServices(sd);

                DFService.register(this, parentName,dfd);

    }catch(FIPAException fe){fe.printStackTrace();}
  }

  private DFAgentDescription getDescription()
  {
     DFAgentDescription dfd = new DFAgentDescription();
     dfd.setName(getAID());
     ServiceDescription sd = new ServiceDescription();
     sd.setName(getLocalName() + "-sub-df");
     sd.setType("fipa-df");
     sd.addProtocols(FIPANames.InteractionProtocol.FIPA_REQUEST);
     sd.addOntologies("fipa-agent-management");
     sd.setOwnership("JADE");
     dfd.addServices(sd);
     return dfd;
  }

}

After running the code you can see that, subDF agent is able to find agent which is registered on its federated DF.

You can download complete code here also: http://tinyurl.com/Agent-on-different-platforms

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top