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

[目次][索引][前へ][次へ]

24.6 サーバアプリケーションのサンプル

コードサンプル24-6に,Borland Enterprise Server VisiBrokerをインストールしたディレクトリのexamples/vbe/dynanyに入っているサーバアプリケーションを示します。サーバアプリケーションは次の動作を行います。

  1. VisiBroker ORBを初期化します。
  2. POAのポリシーを生成します。
  3. PrintManagerオブジェクトを生成します。
  4. PrintManagerオブジェクトをエクスポートします。
  5. メッセージを出力し,オペレーション要求が入力されるのを待ちます。

コードサンプル24-6 サーバアプリケーションのサンプル(C++)
 
. . .
int main(int argc, char* const* argv) {
 
   try {
      // Initialize the ORB.
      CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
      int Verbose = 0;
      // get a reference to the rootPOA
      PortableServer::POA_var rootPOA =
      PortableServer::POA::_narrow(
            orb->resolve_initial_references("RootPOA"));
      CORBA::PolicyList policies;
      policies.length(1);
      policies[(CORBA::ULong)0] = 
            rootPOA->create_lifespan_policy(
            PortableServer::PERSISTENT);
      // Create serverPOA with the right policies
      PortableServer::POA_var serverPOA =
            rootPOA->create_POA("serverPoa",
            rootPOA->the_POAManager(),
            policies );
      // Resolve Dynamic Any Factory
      DynamicAny::DynAnyFactory_var factory = 
            orb->resolve_initial_references("DynAnyFactory");
      PortableServer::ObjectId_var managerId =
            PortableServer::string_to_ObjectId(
            "PrinterManager");
      // Create the printer manager object.
      PrinterManagerImpl manager( 
            orb, factory, serverPOA, managerId);
      // Export the newly create object.
      serverPOA->activate_object_with_id(managerId,&manager);
      // Activate the POA Manager
      rootPOA->the_POAManager()->activate();
      cout << serverPOA->servant_to_reference(&manager) 
            << "is ready" << endl;
      // Wait for incoming requests
      orb->run();
   }
   catch(const CORBA::Exception& e) {
      cerr << e << endl;
   }
}
 

コードサンプル24-7 サーバアプリケーションのサンプル(Java)
 
// Server.java
 
import java.util.*;
import org.omg.DynamicAny.*;
import org.omg.PortableServer.*;
import com.inprise.vbroker.PortableServerExt.*;
 
public class Server {
 
   public static void main(String[ ] args) {
      try {
         // Initialize the ORB.
         org.omg.CORBA.ORB orb =
                          org.omg.CORBA.ORB.init(args,null);
 
         // Resolve RootPOA
         POA rootPoa = POAHelper.narrow(
                  orb.resolve_initial_references("RootPOA"));
         rootPoa.the_POAManager().activate();
 
         // Create a BindSupport Policy that makes POA
         // register each servant with osagent
         org.omg.CORBA.Any any = orb.create_any();
         BindSupportPolicyValueHelper.insert(any,
                        BindSupportPolicyValue.BY_INSTANCE);
 
         org.omg.CORBA.Policy bsPolicy = orb.create_policy(
                        BIND_SUPPORT_POLICY_TYPE.value, any);
 
         // Create policies for our testPOA
         org.omg.CORBA.Policy[ ] policies = {
            rootPoa.create_lifespan_policy(
               LifespanPolicyValue.PERSISTENT),
            bsPolicy
         };
 
         // Create managerPOA with the right policies
         POA serverPoa = rootPoa.create_POA(
           "serverPoa", rootPoa.the_POAManager(), policies);
 
         // Resolve Dynamic Any Factory
         DynAnyFactory factory =
            DynAnyFactoryHelper.narrow(
            orb.resolve_initial_references("DynAnyFactory"));
 
         byte[ ] oid = "PrinterManager".getBytes();
 
         // Create the printer manager object.
         PrinterManagerImpl manager = new PrinterManagerImpl(
            (com.inprise.vbroker.CORBA.ORB)orb,
            factory, serverPoa, oid);
 
         // Export the newly create object.
 
         serverPoa.activate_object_with_id(oid, manager);
 
         System.out.println(manager + "is ready.");
   // Wait for incoming requests
         orb.run();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
}
 

コードサンプル24-8は,PrinterManagerインプリメンテーションが,次の手順で,コンパイル時にAnyが含む型を知らないで,DynAnyを使ってAnyオブジェクトを処理する様子を示します。

  1. DynAnyオブジェクトを生成し,受け取ったAnyで初期化します。
  2. DynAnyオブジェクトタイプに対してswitchを実行します。
  3. DynAnyが基本データ型を含む場合,値を出力します。
  4. DynAnyがAny 型を含む場合,Any用にDynAnyを生成し,内容を確定し,値を出力します。
  5. DynAnyがenumを含む場合,enum用にDynEnumを生成し,文字列値を出力します。
  6. DynAnyがunionを含む場合,union用にDynUnionを生成し,unionの識別子とメンバを出力します。
  7. DynAnyがstruct,array,またはsequenceを含む場合,含まれるコンポーネント間を移動し,各値を出力します。

コードサンプル24-8 PrinterManagerのインプリメンテーション(C++)
 
// PrinterManager Implementation
class PrinterManagerImpl : public POA_Printer::PrinterManager
{
   CORBA::ORB_var           _orb;
   DynamicAny::DynAnyFactory_var   _factory;
   PortableServer::POA_var         _poa;
   PortableServer::ObjectId_var    _oid;
 
public:
   PrinterManagerImpl(CORBA::ORB_ptr orb,
      DynamicAny::DynAnyFactory_ptr DynAnyFactory,
      PortableServer::POA_ptr poa,
      PortableServer::ObjectId_ptr oid
      ) : _orb(orb), _factory(DynAnyFactory),
          _poa(poa), _oid(oid) {}
 
   void printAny(const CORBA::Any& info) {
   try {
      // Create a DynAny object
      DynamicAny::DynAny_var dynAny = 
          _factory->create_dyn_any(info); display(dynAny);
   }
   catch (CORBA::Exception& e) {
      cout << "Unable to create Dynamic Any from factory" 
            << endl;
   }
}
   void shutdown() {
   try {
      _poa->deactivate_object(_oid);
       cout << "Server shutting down..." << endl;
      _orb->shutdown(0UL);
   }
catch (const CORBA::Exception& e){
   cout << e << endl;
   }
}
 
void display(DynamicAny::DynAny_var value) {
   switch(value->type()->kind()) {
   case CORBA::tk_null:
    case CORBA::tk_void: {
     break;
    }
    case CORBA::tk_short: {
      cout << value->get_short() << endl;
      break;
   }
   case CORBA::tk_ushort: {
     cout << value->get_ushort() << endl;
     break;
    }
   case CORBA::tk_long: {
   cout << value->get_long() << endl;
   break;
   }
   case CORBA::tk_ulong: {
    cout << value->get_ulong() << endl;
    break;
   }
   case CORBA::tk_float: {
     cout << value->get_float() << endl;
     break;
   }
   case CORBA::tk_double: {
     cout << value->get_double() << endl;
     break;
   }
   case CORBA::tk_boolean: {
     cout << value->get_boolean() << endl;
     break;
   }
   case CORBA::tk_char: {
     cout << value->get_char() << endl;
     break;
   }
   case CORBA::tk_octet: {
     cout << value->get_octet() << endl;
     break;
   }
   case CORBA::tk_string: {
     cout << value->get_string() << endl;
     break;
   }
   case CORBA::tk_any: {
     DynamicAny::DynAny_var dynAny =
          _factory->create_dyn_any(*(value->get_any()));
     display(dynAny);
     break;
   }
   case CORBA::tk_TypeCode: {
     cout << value->get_typecode() << endl;
     break;
   }
   case CORBA::tk_objref: {
     cout << value->get_reference() << endl;
     break;
   }
   case CORBA::tk_enum: {
     DynamicAny::DynEnum_var dynEnum =
           DynamicAny::DynEnum::_narrow(value);
     cout << dynEnum->get_as_string() << endl;
     break;
   }
   case CORBA::tk_union: {
     DynamicAny::DynUnion_var dynUnion =
           DynamicAny::DynUnion::_narrow(value);
      display(dynUnion->get_discriminator());
      display(dynUnion->member());
      break;
      }
      case CORBA::tk_struct:
      case CORBA::tk_array:
      case CORBA::tk_sequence: {
        value->rewind();
      CORBA::Boolean next = 1UL;
        while(next) {
        DynamicAny::DynAny_var d = 
              value->current_component();
          display(d);
          next =value->next();
        }
        break;
      }
      case CORBA::tk_longlong: {
        cout << value->get_longlong() << endl;
        break;
      }
      case CORBA::tk_ulonglong: {
        cout << value->get_ulonglong() << endl;
        break;
      }
      case CORBA::tk_wstring: {
        cout << value->get_wstring() << endl;
        break;
      }
      case CORBA::tk_wchar: {
        cout << value->get_wchar() << endl;
        break;
      }
      default:
        cout << "Invalid Type" << endl;
      }
   }
};
 

コードサンプル24-9 PrinterManagerのインプリメンテーション(Java)
 
// PrinterManagerImpl.java
 
import java.util.*;
import org.omg.DynamicAny.*;
import org.omg.PortableServer.*;
 
public class PrinterManagerImpl extends
                              Printer.PrinterManagerPOA {
   private com.inprise.vbroker.CORBA.ORB _orb;
   private DynAnyFactory _factory;
   private POA _poa;
   private byte[ ] _oid;
 
   public PrinterManagerImpl(
         com.inprise.vbroker.CORBA.ORB orb,
         DynAnyFactory factory, POA poa, byte[ ] oid) {
      _orb = orb;
      _factory = factory;
      _poa = poa;
      _oid = oid;
   }
 
   public synchronized void printAny(org.omg.CORBA.Any info) {
      // Display info with the assumption that we don't have
      // any info statically about the type inside the any
 
   try {
      // Create a DynAny object
      DynAny dynAny = _factory.create_dyn_any(info);
      display(dynAny);
   }
   catch (Exception e) {
      e.printStackTrace();
   }
 
}
 
public void shutdown() {
   try {
      _poa.deactivate_object(_oid);
      System.out.println("Server shutting down");
      _orb.shutdown(false);
   }
   catch (Exception e) {
      System.out.println(e);
   }
}
 
private void display(DynAny value) throws Exception {
   switch(value.type().kind().value()) {
      case org.omg.CORBA.TCKind._tk_null:
      case org.omg.CORBA.TCKind._tk_void: {
         break;
      }
      case org.omg.CORBA.TCKind._tk_short: {
         System.out.println(value.get_short());
         break;
      }
      case org.omg.CORBA.TCKind._tk_ushort: {
         System.out.println(value.get_ushort());
         break;
      }
      case org.omg.CORBA.TCKind._tk_long: {
         System.out.println(value.get_long());
         break;
      }
      case org.omg.CORBA.TCKind._tk_ulong: {
         System.out.println(value.get_ulong());
         break;
      }
      case org.omg.CORBA.TCKind._tk_float: {
         System.out.println(value.get_float());
         break;
      }
      case org.omg.CORBA.TCKind._tk_double: {
         System.out.println(value.get_double());
         break;
      }
      case org.omg.CORBA.TCKind._tk_boolean: {
         System.out.println(value.get_boolean());
         break;
      }
      case org.omg.CORBA.TCKind._tk_char: {
         System.out.println(value.get_char());
         break;
      }
      case org.omg.CORBA.TCKind._tk_octet: {
         System.out.println(value.get_octet());
         break;
      }
      case org.omg.CORBA.TCKind._tk_string: {
         System.out.println(value.get_string());
         break;
      }
      case org.omg.CORBA.TCKind._tk_any: {
         DynAny dynAny =
               _factory.create_dyn_any(value.get_any());
         display(dynAny);
         break;
      }
      case org.omg.CORBA.TCKind._tk_TypeCode: {
         System.out.println(value.get_typecode());
         break;
      }
      case org.omg.CORBA.TCKind._tk_objref: {
         System.out.println(value.get_reference());
         break;
      }
      case org.omg.CORBA.TCKind._tk_enum: {
         DynEnum dynEnum = DynEnumHelper.narrow(value);
         System.out.println(dynEnum.get_as_string());
         break;
      }
      case org.omg.CORBA.TCKind._tk_union: {
         DynUnion dynUnion = DynUnionHelper.narrow(value);
         display(dynUnion.get_discriminator());
         display(dynUnion.member());
         break;
      }
      case org.omg.CORBA.TCKind._tk_struct:
      case org.omg.CORBA.TCKind._tk_array:
      case org.omg.CORBA.TCKind._tk_sequence: {
         value.rewind();
         boolean next = true;
         while(next) {
            DynAny d = value.current_component();
            display(d);
            next = value.next();
         }
         break;
      }
      case org.omg.CORBA.TCKind._tk_longlong: {
         System.out.println(value.get_longlong());
         break;
      }
      case org.omg.CORBA.TCKind._tk_ulonglong: {
         System.out.println(value.get_ulonglong());
         break;
      }
      case org.omg.CORBA.TCKind._tk_wstring: {
         System.out.println(value.get_wstring());
         break;
      }
      case org.omg.CORBA.TCKind._tk_wchar: {
         System.out.println(value.get_wchar());
         break;
      }
      default:
         System.out.println("Invalid type");
 
 
      }
   }
}