next up previous contents
Next: E Listing von CommonAppletHelpers Up: No Title Previous: C Listing der IDL-Schnittstelle

D Listing des RegionManagementAgenten

package __MASA_PACKAGE_thisagent;

import __MASA_PACKAGE_agentSystem__(*);
import __MASA_PACKAGE_agent__(*);
import __MASA_PACKAGE_CfMAF__(*);
import __MASA_PACKAGE__(tools.NameWrapper);
import __MASA_PACKAGE__(tools.Debug);
import __MASA_PACKAGE__(tools.GlobalConstants);

import java.io.*;
import java.util.*;

import org.omg.CosEventComm.*;
import org.omg.CosEventChannelAdmin.*;
import org.omg.CosNaming.*;



/**
 * This is the class defining the RegionManagementAgent.
 * <br>The RegionManagementAgent is a mobile agent which primary task it is to provide methods
 * that allow applets to communicate with objects and CORBA services. In particular these are 
 * other agent systems, the CORBA Name Service and the CORBA Event Service, which are often not located
 * on the same machines as the applets. The RegionManagementAgent therefore acts as a proxy for these applets.
 *
 *      @see MobileAgent
 *      @author Stefan Gerber
 */

public class RegionManagementAgentMobileAgent extends MobileAgent
    implements RegionManagementAgentOperations {

    transient private org.omg.CosNaming.NamingContext agent_context; // CORBA naming context for agents
    transient private org.omg.CosNaming.NamingContext AS_context;    //CORBA naming context for agent systems

    
    //constructor
    public RegionManagementAgentMobileAgent() {
        super();
        //get_agent_context();  //doesn't work here, because _initContext = null at this moment
    }

    public void cleanUp() throws Throwable {
        System.err.println("RegionManagementAgentMobileAgent.cleanUp()");
    }

    public void checkSerialization() 
        throws CouldNotMigrate {
    }


    /**
     * Calls methods to fetch the naming contexts of agents/agent systems.
     * <br>This method is called, when the agent is started, also after migration.
     */

    public void run() {
        get_agent_context();
        get_agentsystem_context();
    }


    /**
     * Fetches the naming context containing the agents of the different agent systems.
     * <br>The naming context is saved into agent_context. This method is only for internal use.
     */

    void get_agent_context() 
    {
        //get context of agents
        try {
            NameComponent comp1 = new NameComponent("Agent", "");
            NameComponent name[] = {comp1};
            agent_context = NamingContextHelper.narrow(_initContext.resolve(name));
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("\nfailed :" + e.toString());    
        }
        
    }


    /**
     * Fetches the naming context containing the different agent systems.
     * <br>The naming context is saved into AS_context. This method is only for internal use.
     */

    void get_agentsystem_context() 
    {
        //Get context of the agent systems
        try {
            NameComponent comp1 = new NameComponent("AgentSystemService", "");
            NameComponent comp2 = new NameComponent("4", "");
            NameComponent comp3 = new NameComponent("mnm", "");
            NameComponent name[] = {comp1, comp2, comp3};
            AS_context = NamingContextHelper.narrow(_initContext.resolve(name));
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("failed :" + e.toString());      
        }
    }

    /**
     * Returns the AgentSystemService object of an agent system.
     * <br>This method is internally used by migrate_agent() and get_AS_URL()
     *
     * @param as name of agent system
     * @return AgentSystemService object of agent system specified by "as"
     **/

    AgentSystemService get_agentsystemservice(String as) {
        //get_agentsystem_context();

        AgentSystemService ass = null;
        
        try {
            //resolve AgentSystemService-Object of as
            NameComponent comp1 = new NameComponent(as, "");
            NameComponent name[] = {comp1};
            ass = AgentSystemServiceHelper.narrow(AS_context.resolve(name));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ass;
    }


    /**
     * Returns a list of all agents in an agent system.
     * <br>This list is determined using the NamingContext object agent_context.
     *
     * @param as name of the agent system
     * @return String[] containing the list of agent names
     */

    public String[] list_agent_names(String as) {
        //get the naming context from the CORBA name service
        //get_agent_context();

        //get name context of agent system "as"
        org.omg.CosNaming.NamingContext agentsystem_context = null;
        try {
            NameComponent comp1 = new NameComponent(as, "");
            NameComponent name[] = {comp1};
            agentsystem_context = NamingContextHelper.narrow(agent_context.resolve(name));
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("\n\nfailed :" + e.toString());  
        }

        BindingListHolder bl_holder = new BindingListHolder();
        BindingIteratorHolder bi_holder = new BindingIteratorHolder();
        agentsystem_context.list(1000000, bl_holder, bi_holder);
        Binding[] agent_bindings = bl_holder.value;

        System.err.println("list_agent_names: agent_bindings.length = "+agent_bindings.length);
        String agent_names[] = new String[agent_bindings.length];
        for (int i=0; i<agent_bindings.length; i++) {
            agent_names[i] = (String) agent_bindings[i].binding_name[agent_bindings[i].binding_name.length-1].id;
        }
        return agent_names;
    }


    /**
     * Transfers an agent to another agent system.
     * <br>The agent specified by "agent_identity"  is transferred to the
     * agent system with the name provided by the parameter "target". The
     * parameter "as" is the name of the source agent system.
     *
     * @param agent_identity name of agent
     * @param target target agent system
     * @param as source agent system
     */

    public void migrate_agent(String agent_identity, String target, String as) {
        AgentSystemService ass = null;

        try {
            ass = get_agentsystemservice(as);

            NameWrapper nw= new NameWrapper(agent_identity);

            // connect to agent to transfer
            org.omg.CORBA.Object obj = ass.connectToAgent(nw._name);
            Migration agent = MigrationHelper.narrow(obj);
            // connect to destination agent system
            agent.migrateToAgentSystem("AgentSystemService/4/mnm/"+ target);
            System.err.println("\n\ntransferAgent: agent "+ agent_identity + " transferred to "+ target);
        }
        catch(Exception e1){
            if (e1 instanceof org.omg.CORBA.BAD_PARAM) { }
            else {
                e1.printStackTrace();
                System.err.println("transferAgent failed: "+e1.toString()+"\n"+e1.getMessage()+"\n");
            }
        }
    }


    /**
     * Returns a String that contains the URL of an agent system.
     *
     * @param as name of the agent system
     * @return String containing the URL of the agent system
     */

    public String get_AS_URL(String as) {
        AgentSystemService ass = get_agentsystemservice(as);
        return ass.getURL();
    }



  /**
   * Terminates an agent system.
   * <br>This method simply gets a reference to the AgentSystemService instance of agent system "as"
   * and calls its terminate_agent_system() method.
   *
   * @param as name of the agent system
   */

  public void terminate_agent_system(String as) {
    try {
      //connect to agent system specified by "as"
      AgentSystemService ass = get_agentsystemservice(as);
      //terminate agent system
      ass.terminate_agent_system();
    } catch (Exception e) {
      e.printStackTrace();
      System.err.println("\nRegionManagementAgent: terminate agent system "+as+" failed!");
      if (e instanceof TerminateFailed) 
        System.err.println(e.toString()+"\n"+e.getMessage());
    }
  }

}



Copyright Munich Network Management Team