Advertisement
Guest User

Untitled

a guest
Feb 11th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. import jade.core.Profile;
  2. import jade.core.ProfileImpl;
  3. import jade.core.Runtime;
  4. import jade.wrapper.AgentContainer;
  5. import jade.wrapper.AgentController;
  6. import jade.wrapper.ContainerController;
  7. import jade.wrapper.StaleProxyException;
  8.  
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.List;
  13.  
  14.  
  15. public class Environment {
  16.    
  17.     private static String hostname = "127.0.0.1";
  18.     private static HashMap<String, ContainerController> containerList=new HashMap<String, ContainerController>();// container's name - container's ref
  19.     private static List<AgentController> agentList;// agents's ref
  20.     //private static Runtime rt;   
  21.  
  22.     public static void main(String[] args){
  23.        
  24.         if(args.length != 1) {
  25.             System.out.println("Wrong usage. Number of agents unspecified");
  26.             System.exit(1);
  27.         }
  28.        
  29.         int nAgents=Integer.parseInt(args[0]);
  30.        
  31.        
  32.        
  33.         //1), create the platform (Main container (DF+AMS) + containers + monitoring agents : RMA and SNIFFER)
  34.         emptyPlatform(containerList);
  35.  
  36.         //2) create agents and add them to the platform.
  37.         agentList=createAgents(containerList, nAgents);
  38.  
  39.         try {
  40.             System.out.println("Press a key to start the agents");
  41.             System.in.read();
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         }
  45.  
  46.         //3) launch agents
  47.         startAgents(agentList);
  48.  
  49.     }
  50.    
  51.     /**********************************************
  52.      *
  53.      * Methods used to create an empty platform
  54.      *
  55.      **********************************************/
  56.  
  57.     /**
  58.      * Create an empty platform composed of 1 main container and 3 containers.
  59.      *
  60.      * @return a ref to the platform and update the containerList
  61.      */
  62.     private static Runtime emptyPlatform(HashMap<String, ContainerController> containerList){
  63.  
  64.         Runtime rt = Runtime.instance();
  65.  
  66.         // 1) create a platform (main container+DF+AMS)
  67.         Profile pMain = new ProfileImpl(hostname, 8888, null);
  68.         System.out.println("Launching a main-container :"+pMain);
  69.         AgentContainer mainContainerRef = rt.createMainContainer(pMain); //DF and AMS are include
  70.  
  71.         // 2) create the containers
  72.         containerList.putAll(createContainers(rt));
  73.  
  74.         // 3) create monitoring agents : rma agent, used to debug and monitor the platform; sniffer agent, to monitor communications;
  75.         createMonitoringAgents(mainContainerRef);
  76.  
  77.         System.out.println("Plaform ok");
  78.         return rt;
  79.  
  80.     }
  81.  
  82.     /**
  83.      * Create the containers used to hold the agents
  84.      * @param rt The reference to the main container
  85.      * @return an Hmap associating the name of a container and its object reference.
  86.      *
  87.      * note: there is a smarter way to find a container with its name, but we go fast to the goal here. Cf jade's doc.
  88.      */
  89.     private static HashMap<String,ContainerController> createContainers(Runtime rt) {
  90.         String containerName;
  91.         ProfileImpl pContainer;
  92.         ContainerController containerRef;
  93.         HashMap<String, ContainerController> containerList=new HashMap<String, ContainerController>();//bad to do it here.
  94.  
  95.         System.out.println("Launching containers ...");
  96.  
  97.         containerName="container";
  98.         pContainer = new ProfileImpl(null, 8888, null);
  99.         System.out.println("Launching container "+pContainer);
  100.         containerRef = rt.createAgentContainer(pContainer); //ContainerController replace AgentContainer in the new versions of Jade.
  101.         containerList.put(containerName, containerRef);
  102.         System.out.println("Launching containers done");
  103.         return containerList;
  104.     }
  105.    
  106.     /**
  107.      * create the monitoring agents (rma+sniffer) on the main-container given in parameter and launch them.
  108.      *  - RMA agent's is used to debug and monitor the platform;
  109.      *  - Sniffer agent is used to monitor communications
  110.      * @param mc the main-container's reference
  111.      * @return a ref to the sniffeur agent
  112.      */
  113.     private static void createMonitoringAgents(ContainerController mc) {
  114.  
  115.         System.out.println("Launching the rma agent on the main container ...");
  116.         AgentController rma=null;
  117.  
  118.         try {
  119.             rma = mc.createNewAgent("rma", "jade.tools.rma.rma", new Object[0]);
  120.             rma.start();
  121.         } catch (StaleProxyException e) {
  122.             e.printStackTrace();
  123.             System.out.println("Launching of rma agent failed");
  124.         }
  125.  
  126.         System.out.println("Launching  Sniffer agent on the main container...");
  127.         AgentController snif=null;
  128.  
  129.         try {
  130.             snif= mc.createNewAgent("sniffeur", "jade.tools.sniffer.Sniffer",new Object[0]);
  131.             snif.start();
  132.  
  133.         } catch (StaleProxyException e) {
  134.             e.printStackTrace();
  135.             System.out.println("launching of sniffer agent failed");
  136.  
  137.         }      
  138.     }
  139.    
  140.     /**
  141.      *  Creates the agents and add them to the agentList.  agents are NOT started.
  142.      *@param containerList :Name and container's ref
  143.      *@param sniff : a ref to the sniffeur agent
  144.      *@return the agentList
  145.      */
  146.     private static List<AgentController> createAgents(HashMap<String, ContainerController> containerList, int nAgents) {
  147.         System.out.println("Launching agents...");
  148.         ContainerController c;
  149.         String agentName;
  150.         List<AgentController> agentList=new ArrayList<AgentController>(nAgents);
  151.         List<String> agentListName=new ArrayList<String>(nAgents);
  152.         for(int i = 0 ; i< nAgents ; i ++) {
  153.             agentListName.add(i, "Agent Explorer "+String.valueOf(i));
  154.         }
  155.         for(int i = 0 ; i<nAgents ; i++) {
  156.             c = containerList.get("container");
  157.             try {
  158.                 agentName=agentListName.get(i);
  159.                 Object[] objtab=new Object[]{agentListName};
  160.                 AgentController ag=c.createNewAgent(agentName,AgentExplorer.class.getName(),objtab);
  161.                 agentList.add(ag);
  162.                 System.out.println(agentName+" launched");
  163.             } catch (StaleProxyException e) {
  164.                 e.printStackTrace();
  165.             }
  166.         }
  167.         System.out.println("Agents launched...");
  168.         return agentList;
  169.     }
  170.    
  171.     /**
  172.      * Start the agents
  173.      * @param agentList
  174.      */
  175.     private static void startAgents(List<AgentController> agentList){
  176.  
  177.         System.out.println("Starting agents...");
  178.  
  179.         for(final AgentController ac: agentList){
  180.             try {
  181.                 ac.start();
  182.             } catch (StaleProxyException e) {
  183.                 e.printStackTrace();
  184.             }
  185.  
  186.         }
  187.         System.out.println("Agents started...");
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement