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

|
5.3.8.6 AdapterActivator使用例 (リクエスト受信時)
(1)IDL定義
module ODsample{
interface intf1{
long add( in long a, in long b );
};
interface intf2{
long sub( in long a, in long b );
};
};
(2)ネーミングサービスの登録
OD_or_adm -c IDL:ODsample/intf1:1.0 -a Imple_POAsample10 -n ODsample::POAsample10-1
OD_or_adm -c IDL:ODsample/intf2:1.0 -a Imple_POAsample10 -n ODsample::POAsample10-2
(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;
static CosNaming::Name name2;
int main( int argc, char* argv[] )
{
CORBA::Long val1 = 0; // inパラメタ用変数
CORBA::Long val2 = 0; // inパラメタ用変数
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::POAsample10-1";
name[0]->kind = ( const CORBA::Char* )"";
obj = cos_naming->resolve( name, env );
// サーバアプリケーションのオブジェクトリファレンス獲得
ODsample::intf1* target1 = ODsample::intf1::_narrow( obj );
printf( "val1=>" );
scanf( "%d", &val1 );
printf( "val2=>" );
scanf( "%d", &val2 );
// サーバアプリケーションのメソッド呼出し
CORBA::Long ret1 = target1->add( val1, val2, env );
// メソッドの結果表示
printf( "val1 + val2 = %d\n", ret1 );
CORBA::release( target1 );
// ネーミングサービスのresolveメソッドを発行し、
// サーバアプリケーションのオブジェクトリファレンスの獲得
name2.length( 1 );
name2[0]->id = ( const CORBA::Char* )"ODsample::POAsample10-2";
name2[0]->kind = ( const CORBA::Char* )"";
obj = cos_naming->resolve( name2, env );
// サーバアプリケーションのオブジェクトリファレンス獲得
ODsample::intf2* target2 = ODsample::intf2::_narrow( obj );
printf( "val1=>" );
scanf( "%d", &val1 );
printf( "val2=>" );
scanf( "%d", &val2 );
// サーバアプリケーションのメソッド呼出し
CORBA::Long ret2 = target2->sub( val1, val2, env );
// メソッドの結果表示
printf( "val1 - val2 = %d\n", ret2 );
CORBA::release( target2 );
} 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 <iostream.h>
#include <stdlib.h>
#include "orb_cplus.h"
#include "poa.h"
#include "CosNaming_cplus.h"
#include "simple.h"
static CORBA::ORB_var orb;
static CORBA::Object_var obj;
static CORBA::Environment env;
static PortableServer::POA_var rootPOA;
static CORBA::PolicyList policies( 4 );
static PortableServer::POAManager_var poa_manager;
// ユーザアプリケーション:メイン処理クラス
int main( int argc, char* argv[] )
{
int current_argc = argc;
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 );
// AdapterActivatorの生成
PortableServer::AdapterActivator_var adp = new UserAdapterActivator();
// インプリ用POAのAdapterActivatorに設定
rootPOA->the_activator( adp, 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;
}
// AdapterActivator:POA生成クラス(AdapterActivatorを継承)
class UserAdapterActivator : public PortableServer::AdapterActivator
{
public:
UserAdapterActivator() {};
~UserAdapterActivator() {};
CORBA::Boolean unknown_adapter(
PortableServer::POA_ptr parent,
const CORBA::Char* name,
CORBA::Environment& env )
throw( CORBA::Exception )
{
// ポリシリスト作成
CORBA::PolicyList policies( 4 );
policies.length( 4 );
policies[0] = rootPOA->create_servant_retention_policy(
PortableServer::NON_RETAIN, env );
policies[1] = rootPOA->create_request_processing_policy(
PortableServer::USE_DEFAULT_SERVANT, env );
policies[2] = rootPOA->create_id_assignment_policy(
PortableServer::SYSTEM_ID, env );
policies[3] = rootPOA->create_id_uniqueness_policy(
PortableServer::MULTIPLE_ID, env );
try {
if( !strcmp( name, "IDL:ODsample/intf1:1.0" ) ) {
// intf1用のPOA作成
PortableServer::POA_ptr intf_POA
= parent->create_POA( name, NULL, policies, env );
// Servantの生成
ODsample_intf1_impl* svt = new ODsample_intf1_impl();
// Default Servantに設定
intf_POA->set_servant( svt, env );
return( CORBA_TRUE );
}
else if( !strcmp( name, "IDL:ODsample/intf2:1.0" ) ) {
// intf2用のPOA作成
PortableServer::POA_ptr intf_POA
= parent->create_POA( name, NULL, policies, env );
// Servantの生成
ODsample_intf2_impl* svt = new ODsample_intf2_impl();
// Default Servantに設定
intf_POA->set_servant( svt, env );
return( CORBA_TRUE );
}
} catch( CORBA::SystemException& se ) {
cout << "exception-id: " << se.id() << endl;
return( CORBA_FALSE );
} catch( CORBA::UserException& ue ) {
cout << "exception-id: " << ue.id() << endl;
return( CORBA_FALSE );
} catch( CORBA::Exception& e ) {
cout << "exception-id: " << e.id() << endl;
return( CORBA_FALSE );
}
return CORBA_TRUE;
};
};
// メソッド実装
CORBA::Long ODsample_intf1_impl::add(
CORBA::Long a,
CORBA::Long b,
CORBA::Environment& env )
throw( CORBA::Exception )
{
return a + b;
};
CORBA::Long ODsample_intf2_impl::sub(
CORBA::Long a,
CORBA::Long b,
CORBA::Environment& env )
throw( CORBA::Exception )
{
return a - b;
};
(6)例外情報の獲得
サーバアプリケーションで例外を獲得する方法の詳細については、“サーバアプリケーションの例外処理”を参照してください。
Copyright 2006 FUJITSU LIMITED