Hitachi

VisiBroker Version 5 Borland(R) Enterprise Server VisiBroker(R) デベロッパーズガイド


13.3.2 スマートエージェントが認識するものをすべて検索

コードサンプル13-3および13-4は,スマートエージェントが認識するものをすべて検索する方法を示します。これにはまず,all_repository_ids()メソッドを呼び出して,既知のインタフェースをすべて取得します。次に,all_instances_descs()メソッドをインタフェースごとに呼び出して,インスタンス記述を取得します。

コードサンプル13-3 スマートエージェントが認識するものをすべて検索(C++)
#include "corba.h"
 
#include "locate_c.hh"
 
// USE_STD_NS is a define setup by VisiBroker to use 
// the std namespace if it exists
USE_STD_NS
 
int DisplaybyRepID(CORBA::ORB_ptr the_orb,
         ObjLocation::Agent_var the_agent,
         char * myRepId){
   ObjLocation::ObjSeq_var accountRefs;
   accountRefs = the_agent->all_instances(myRepId);
   cout << "Obtained " << accountRefs->length() << 
         "Account objects" << endl;
   for (CORBA::ULong i=0; i < accountRefs->length(); i++) {
      cout << "Stringified IOR for account #" << i << ":" 
            << endl;
      CORBA::String_var stringified_ior(
            the_orb->object_to_string(accountRefs[i]));
      cout << stringified_ior << endl;
      cout << endl;
   }
   return(1);
}
void PrintUsage(char * name) {
   cout << "\nUsage: \n" << endl;
   cout << "\t" << name << " [Rep ID]" << endl;
   cout << "\n\tWith no argument,
         finds and prints all objects" << endl;
   cout << "\tOptional rep ID searches for
         specific rep ID\n" << endl;
}
int main(int argc, char** argv) {
   char myRepId[255] = "";
   if (argc == 2) {
      if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "/?") || 
            !strcmp(argv[1], "-?") ){
         PrintUsage(argv[0]);
         exit(0);
      } else {
         strcpy(myRepId, argv[1]);
      }
   }
   else if (argc > 2) {
      PrintUsage(argv[0]);
      exit(0);
   }
   try {
      CORBA::ORB_ptr the_orb = CORBA::ORB_init(argc, argv);
      CORBA::Object_ptr obj = the_orb->
      resolve_initial_references("LocationService");
      if ( CORBA::is_nil(obj) ) {
         cout << "Unable to locate initial LocationService"
               << endl;
         return 0;
      }
      ObjLocation::Agent_var the_agent = 
            ObjLocation::Agent::_narrow(obj);
      ObjLocation::DescSeq_var descriptors;
 
      //Display stringified IOR for RepID requested and exit
      if (argc == 2) {
         DisplaybyRepID(the_orb, the_agent, myRepId);
         exit(0);
      }
      //Report all hosts running osagents
      ObjLocation::HostnameSeq_var HostsRunningAgents =
            the_agent->all_agent_locations();
      cout << "Located " << HostsRunningAgents->length() << 
            "Hosts running Agents" << endl;
      for (CORBA::ULong k=
            0; k<HostsRunningAgents->length(); k++){
         cout << "\tHost #" << (k+1) << ": " <<
               (const char*) HostsRunningAgents[k] << endl;
      }
      cout << endl;
 
      // Find and display all Repository Ids
      ObjLocation::RepositoryIdSeq_var repIds = 
            the_agent->all_repository_ids();
      cout << "Located " << repIds->length() << 
            " Repository Ids" << endl;
      for (CORBA::ULong j=0; j<repIds->length(); j++) {
         cout << "\tRepository ID #" << (j+1) << ": " << 
               repIds[j] << endl;
      }
      // Find all Object Descriptors for each Repository Id
      for (CORBA::ULong i=0; i < repIds->length(); i++) {
         descriptors = 
               the_agent->all_instances_descs(repIds[i]);
         cout << endl;
         cout << "Located " << descriptors->length()
               << " objects for " << (const char*) (repIds[i])
               << " (Repository Id #" << (i+1) << "):" <<endl;
         for (CORBA::ULong j=0; j < descriptors->length(); j++){
            cout << endl;
            cout << (const char*) repIds[i] << " #" 
                  << (j+1) << ":" << endl;
            cout << "\tInstance Name \t= 
                  " << descriptors[j].instance_name << endl;
            cout << "\tHost \t= 
                  " << descriptors[j].iiop_locator.host << endl;
            cout << "\tPort \t= 
                  " << descriptors[j].iiop_locator.port << endl;
            cout << "\tAgent Host \t=
                  " << descriptors[j].agent_hostname << endl;
            cout << "\tActivable \t="
               << (descriptors[j].activable?"YES":"NO") << endl;
         }
      }
   } catch (const CORBA::Exception& e) {
      cout << "CORBA Exception during execution of find_all: " 
            << e << endl;
      return 0;
   }
   return 1;
}
コードサンプル13-4 スマートエージェントが認識するものをすべて検索(Java)
// Find.java
public class Find {
public static void main(String[ ] args){
   try {
      // Initialize the ORB.
      org.omg.CORBA.ORB orb =
                       org.omg.CORBA.ORB.init(args,null);
      com.inprise.vbroker.ObjLocation.Agent agent = null;
      try {
         agent = 
         com.inprise.vbroker.ObjLocation.AgentHelper.narrow(
         orb.resolve_initial_references("LocationService"));
      } catch (org.omg.CORBA.ORBPackage.InvalidName e){
         System.out.println(
            "Not able to resolve references " +
            "for LocationService");
      System.exit(1);
      } catch (Exception e){
         System.out.println(
            "Not able to resolve references " +
            "for LocationService");
         System.out.println("Caught exception: " + e);
         System.exit(1);
      }
      boolean done=false;
      java.io.BufferedReader in = 
         new java.io.BufferedReader(
            new java.io.InputStreamReader(System.in));
      while (!done){
         System.out.print("-> ");
         System.out.flush();
         String line = in.readLine();
         if(line.startsWith("agents")){
            java.lang.String[ ] agentList = 
                                agent.all_agent_locations();
            System.out.println(
                 "Located " + agentList.length  + "agents");
            for (int i=0; i < agentList.length; i++){
               System.out.println("\t" + "Agent #" +
                  (i+1) + ": " + agentList[i]);
            }
         }else if(line.startsWith("rep")){
           java.lang.String[ ] repIds = 
                                 agent.all_repository_ids();
           System.out.println("Located " + repIds.length  +
                              "repository Ids");
           for (int i=0; i <repIds.length; i++){
              System.out.println("\t" + "Repository Id #"
                                 + (i+1) + ": " + repIds[i]);
           }
         } else if(line.startsWith("objects ")){
            String names = line.substring(
                         "objects ".length(), line.length());
            PrintObjects(names,agent,orb);
         } else if(line.startsWith("quit")){
            done = true;
         } else {
            System.out.println("Commands: agents\n" + 
               "          repository_ids\n" + 
               "          objects <rep Id>\n" + 
               "          objects <rep Id><obj name>\n" +
               "          quit\n");
         }
      }
   } catch (com.inprise.vbroker.ObjLocation.Fail err){
      System.out.println(
          "Location call failed with reason " + err.reason);
   } catch (java.lang.Exception err){
      System.out.println("Caught error " + err);
      err.printStackTrace();
   }
}
public static void PrintObjects(String names,
               com.inprise.vbroker.ObjLocation.Agent agent,
               org.omg.CORBA.ORB orb)
      throws com.inprise.vbroker.ObjLocation.Fail {
   int space_pos = names.indexOf(' ');
   String repository_id;
   String object_name;
   if (space_pos == -1){
      repository_id = names;
      object_name = null;
   }else {
      repository_id = names.substring(0,names.indexOf(' '));
      object_name = names.substring(names.indexOf(' ') + 1);
   }
   org.omg.CORBA.Object[ ] objects;
   com.inprise.vbroker.ObjLocation.Desc[ ] descriptors;
   if (object_name == null){
      objects = agent.all_instances(repository_id);
      descriptors =
         agent.all_instances_descs(repository_id);
   }else {
      objects =
               agent.all_replica(repository_id,object_name);
               descriptors = agent.all_replica_descs(
                                 repository_id,object_name);
   }
   System.out.println(
                 "Returned " + objects.length  + " objects");
   for (int i=0; i<objects.length; i++){
      System.out.println("\n\nObject #" + (i+1) + ":");
      System.out.println("==================");
      System.out.println("\tRep ID: " + 
                         ((com.inprise.vbroker.CORBA.Object)
                              objects[i])._repository_id());
      System.out.println("\tInstance:" + 
                         ((com.inprise.vbroker.CORBA.Object)
                                objects[i])._object_name());
      System.out.println(
               "\tIOR: " + orb.object_to_string(objects[i]));
      System.out.println();
      System.out.println("Descriptor #" + (i+1));
      System.out.println(
                   "=====================================");
      System.out.println(
                "Host:" + descriptors[i].iiop_locator.host);
      System.out.println(
                "Port:" + descriptors[i].iiop_locator.port);
      System.out.println(
             "Agent Host:" + descriptors[i].agent_hostname);
      System.out.println(
           "Repository Id:" + descriptors[i].repository_id);
      System.out.println(
                "Instance:" + descriptors[i].instance_name);
      System.out.println(
                   "Activable:" + descriptors[i].activable);
   }
}
}