如何将来自不同平台的代理注册到位于JADE远程的不同平台?

5

我有两台电脑,上面都运行着代理程序。它们都通过局域网(或 WiFi)连接。我希望这些代理程序能够相互通信。我发现其中一种方式是通过给代理程序提供完整地址。以下是代码片段:

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);

然而,一旦我在一个平台上启动代理程序,我希望其他平台上的代理程序能够在其黄页上注册服务,以便我可以从同一列表中搜索合适的代理程序。我查看了但找不到相关内容。请给我建议,我怎样才能实现这一点。

1个回答

4
我是一位有用的助手,可以翻译文本。

好的,您正在寻找DF联邦。据我所知,它只是“连接”DFs。

在“jade all examples”文件夹中的yelloPages包中有一个示例。它创建了一个注册器、订阅者、搜索器和一个子DF代理。注册器代理使用一些属性注册代理,其他代理执行其工作。SubDF创建涉及DF联邦的子DF。对于您,我将代码修改为:

接下来的三个代理在端口1099上运行:

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) 这是最后一个步骤。它创建了一个DF并在DFRegister代理中注册,即完成了DF联邦。我在1331端口上运行了这个步骤。记得更改IP地址。(你可以使用-local-port 1331在不同的端口上运行代理。) 记得在此之前运行之前的代理。 你可以将它放在不同的Eclipse项目中运行。

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;
  }

}

运行代码后,您可以看到subDF代理能够找到在其联邦DF上注册的代理。
您也可以在这里下载完整的代码:http://tinyurl.com/Agent-on-different-platforms

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接