Interstage Application Server アプリケーション作成ガイド (CORBAサービス編)
|
目次
索引

|
5.3.8.2 Active Object Map(AOM)使用例 (Factory-1方式)
(1)IDL定義
module ODsample{
interface intf{
readonly attribute long value;
void add( in long a );
void sub( in long b );
};
interface Factory {
intf create();
void destroy( in intf obj );
};
};
(2)ネーミングサービスの登録
OD_or_adm -c IDL:ODsample/Factory:1.0 -a Imple_POAsample3 -n ODsample::POAsample3
(3)アプリケーション構成概要図

(4)クライアントアプリケーション
#include <stdlib.h>
#include <iostream.h>
#include "orb_cplus.h"
#include "CosNaming_cplus.h"
#include "simple.h"
static CORBA::Object_var obj;
static CORBA::Environment env;
static CORBA::ORB_var orb;
static CosNaming::NamingContext_var cos_naming;
static CosNaming::Name name;
int main( int argc, char *argv[] )
{
try {
// ORBの生成と初期化
orb = CORBA::ORB_init( argc, argv, FJ_OM_ORBid, env );
// ネーミングサービスのオブジェクトリファレンスの取得
obj = orb->resolve_initial_references(
CORBA_ORB_ObjectId_NameService, env );
cos_naming = CosNaming::NamingContext::_narrow( obj );
// ネーミングサービスのresolveメソッドを発行し、
// サーバアプリケーションのオブジェクトリファレンスの獲得
name.length( 1 );
name[0]->id = ( const CORBA::Char* )"ODsample::POAsample3";
name[0]->kind = ( const CORBA::Char* )"";
obj = cos_naming->resolve( name, env );
// Factoryのオブジェクトリファレンス獲得
ODsample::Factory* target = ODsample::Factory::_narrow( obj );
// Factoryでインタフェース用オブジェクトリファレンス作成
ODsample::intf* obj2 = target->create( env );
CORBA::Long a = 10; // inパラメタ用変数
CORBA::Long b = 5;
// サーバアプリケーションのメソッド呼出し
CORBA::Long ret = obj2->value( env );
printf( "value = %d\n", ret );
obj2->add( a, env );
printf( "add( %d )\n", a );
ret = obj2->value( env );
printf( "value = %d\n", ret );
obj2->sub( b, env );
printf( "sub( %d )\n", b );
ret = obj2->value( env );
printf( "value = %d\n", ret );
// インスタンスの解放
target->destroy( obj2, env );
CORBA::release( target );
CORBA::release( obj2 );
} catch( CORBA::SystemException& se ) {
cout << "exception-id: " << se.id() << endl;
exit(1);
} catch( CORBA::Exception& e ) {
cout << "exception-id: " << e.id() << endl;
exit(1);
}
return 0;
}
(5)サーバアプリケーション
#include <stdlib.h>
#include "orb_cplus.h"
#include "simple.h"
#include "iostream.h"
#include "poa.h"
#include "CosNaming_cplus.h"
static CORBA::ORB_var orb;
static CORBA::Object_var obj;
static CORBA::Environment env;
static PortableServer::POA_var rootPOA;
static PortableServer::POA_var factoryPOA;
static CORBA::PolicyList factory_policies( 4 );
static PortableServer::POAManager_var poa_manager;
static long val = 10;
// ユーザアプリケーション:メイン処理クラス
int main( int argc, char*argv[] )
{
try {
// ORBの生成と初期化
orb = CORBA::ORB_init( current_argc, argv, FJ_OM_ORBid, env );
// RootPOAのオブジェクトリファレンスの取得
obj = orb->resolve_initial_references( "RootPOA", env );
// RootPOAのPOAオブジェクト獲得
rootPOA = PortableServer::POA::_narrow( obj );
// Factoryインタフェース用のPOA作成
// ポリシリスト作成
factory_policies.length( 4 );
factory_policies[0] = rootPOA->create_servant_retention_policy(
PortableServer::NON_RETAIN, env );
factory_policies[1] = rootPOA->create_request_processing_policy(
PortableServer::USE_DEFAULT_SERVANT, env );
factory_policies[2] = rootPOA->create_id_assignment_policy(
PortableServer::SYSTEM_ID, env );
factory_policies[3] = rootPOA->create_id_uniqueness_policy(
PortableServer::MULTIPLE_ID, env );
factoryPOA = rootPOA->create_POA( "IDL:ODsample/Factory:1.0",
PortableServer::POAManager::_nil(), factory_policies ,env);
// FactoryServantの生成
ODsample_Factory_impl* svt = new ODsample_Factory_impl();
// Factoryインタフェース用POAのDefault Servantに設定
factoryPOA->set_servant( svt, env );
// POAマネージャの獲得
poa_manager = rootPOA->the_POAManager( env );
// POAマネージャのactivate
poa_manager->activate( env );
orb->run( env );
} catch( CORBA::SystemException& se ) {
cout << "exception-id: " << se.id() << endl;
exit(1);
} catch( CORBA::Exception& e ) {
cout << "exception-id: " << e.id() << endl;
exit(1);
}
return 0;
}
// Factory_impl::create:Factoryメソッドの実装
ODsample::intf_ptr ODsample_Factory_impl::create(
CORBA::Environment& env )
throw( CORBA::Exception )
try{
// Servantの生成
ODsample_intf_impl* svt = new ODsample_intf_impl();
// Servantからオブジェクトリファレンスの生成
// IMPLICIT_ACTIVATIONポリシなので、AOMへ自動登録される
CORBA::Object_var _tmpObj = rootPOA->servant_to_reference( svt, env );
ODsample::intf_ptr ior = ODsample::intf::_narrow( _tmpObj );
return ior;
} catch( CORBA::Exception& e ) {
cout << "exception-id: " << e.id() << endl;
throw e;
}
};
// Factory_impl::destroy:Factoryメソッドの実装
void ODsample_Factory_impl::destroy(
ODsample::intf_ptr obj,
CORBA::Environment& env )
throw( CORBA::Exception )
{
try{
// オブジェクトリファレンスからオブジェクトIDを求める
PortableServer::ObjectId_var oid = rootPOA->reference_to_id( obj, env );
// Servantオブジェクトの取得
PortableServer::Servant svt = id_to_servant( *oid, env );
// Servantをdeactiveにする
rootPOA->deactivate_object( *oid, env );
// Servantの削除
svt->servant_delete();
} catch( CORBA::Exception& e ) {
cout << "exception-id: " << e.id() << endl;
throw e;
}
};
// ODsample::intf:メソッド実装クラス
void ODsample_intf_impl::add(
CORBA::Long a,
CORBA::Environment& env)
throw( CORBA::Exception ){
cout << "ODsample::intf::add()" << endl;
cout << " val : " << val << endl;
cout << " a : " << a << endl;
val = val + a;
cout << " result: " << val << endl;
};
void ODsample_intf_impl::sub(
CORBA::Long b,
CORBA::Environment& env)
throw( CORBA::Exception ){
cout << "ODsample::intf::sub()" << endl;
cout << " val : " << val << endl;
cout << " b : " << b << endl;
val = val - b;
cout << " result: " << val << endl;
};
CORBA::Long ODsample_intf_impl::value(
CORBA::Environment& env)
throw( CORBA::Exception ){
return val;
};
(6)例外情報の獲得
サーバアプリケーションで例外を獲得する方法の詳細については、“サーバアプリケーションの例外処理”を参照してください。
Copyright 2006 FUJITSU LIMITED