| Interstage Application Server アプリケーション作成ガイド (CORBAサービス編) |
目次
索引
![]()
|
| 付録F 動的スケルトンインタフェースのプログラミング | > F.2 C++言語の開発 | > F.2.2 Portable Object Adpter : POA |
ゲートウェイでは、以下に示す処理を行います。
ゲートウェイの実装例を以下に示します。
【ゲートウェイ実装例】
class DSIServant : public virtual PortableServer::DynamicImplementation
{
public:
DSIServant(){ _rep_id = "IDL:ODdemo/calculator:1.0"; };
~DSIServant(){};
CORBA::Boolean invoke(
CORBA::ServerRequest_ptr sr,
CORBA::Environment &env )
{
try{
// (1)メソッド名の取得
char *_name = sr->op_name();
//メソッド名の解析
if( !strcmp( _name, "calculate" )){
// (2)パラメタリストの設定
//パラメタリストの作成
CORBA::NVList_ptr _list = NULL;
orb->create_list( 2, _list );
//パラメタ情報の登録
CORBA::Any _p1, _p2;
_p1 <<= (CORBA::Long)0;
_p2 <<= (CORBA::Long)0;
_list->add_value( "a", _p1, CORBA::ARG_IN );
_list->add_value( "b", _p2, CORBA::ARG_IN );
// (3)パラメタの解析
sr->params(_list);
CORBA::Long _param1, _param2;
*(_list->item(0)->value()) >>= _param1;
*(_list->item(1)->value()) >>= _param2;
//処理部の起動
UserServant *_sv = new UserServant();
ODdemo::calculator::result *_result = _sv->calculate( _param1, _param2, env );
// (4)復帰情報の設定
CORBA::Any *_res = new CORBA::Any();
_res->replace( _tc_ODdemo_calculator_result, (void*)_result, CORBA_TRUE );
sr->result(_res);
_sv->servant_delete();
}
CORBA::string_free(_name);
} catch( ODdemo::calculator::ZEROPARAM *_zeroparam ) {
// (5) 例外情報の設定
env.exception(_zeroparam);
CORBA::Any *_any = new CORBA::Any( _tc_ODdemo_calculator_ZEROPARAM, _zeroparam );
sr->exception( _any, env );
return true;
} catch( CORBA::Exception e ) {
// 例外発生時の処理を記述してください。
// 復帰情報あるいは例外情報を設定してください
}
return true;
};
// (6)実装が必要なメソッド
CORBA::RepositoryId _primary_interface(
const PortableServer::ObjectId &oid,
PortableServer::POA_ptr poa,
CORBA::Environment& env )
{
return _rep_id;
};
private:
CORBA::RepositoryId _rep_id;
};
//処理部
class UserServant : public virtual PortableServer::ServantBase
{
public:
UserServant(){};
~UserServant(){};
ODdemo::calculator::result *calculate(
CORBA::Long a,
CORBA::Long b,
CORBA::Environment &env )
throw( CORBA::Exception )
{
if( b == 0 ){
throw new ODdemo::calculator::ZEROPARAM();
}
ODdemo::calculator::result *_result =
new ODdemo::calculator::result();
_result->add_result = a + b;
_result->subtract_result = a - b;
_result->multiple_result = a * b;
_result->divide_result = (CORBA::Float)(a / b);
return _result;
}
};
ゲートウェイを実装するServantオブジェクトは、PortableServer::DynamicImplementationを継承するものとして作成します。また、クライアントからのリクエストに対して起動されるメソッドとしてinvoke()メソッドを実装します。起動の際、invoke()の引数としてServerRequestクラスのオブジェクト(上記例ではreq)が渡されます。
処理部では、ゲートウェイを実装するServantオブジェクト内で、そのServantに対してservant_delete()を発行するために、PortableServer::ServantBaseクラスを継承します。
【ゲートウェイServantの必須メソッド(1)】
CORBA::boolean invoke( CORBA::ServerRequest_ptr req )
渡されたServerRequestクラスに登録されているオペレーション名をCORBA::ServerRequest::op_name()メソッドにより取得します。
生成したNVListオブジェクトを引数として、CORBA::ServerRequest::params()メソッドを呼び出します。この結果、アプリケーションに渡すパラメタ値が取得されます。
CORBA::ServerRequest::result()メソッドを発行して復帰情報の設定を行います。
CORBA::ServerRequest::exception()メソッドを発行し例外情報の設定を行います。
ゲートウェイを実装するServantは、OMG規約上_primary_interface()メソッドを実装しておく必要があります。上記の例のように、PortableServer::POAクラスのオブジェクト、PortableServer::ObjectIdオブジェクトを引数とし、インタフェースリポジトリID相当の文字列("IDL:xxx:1.0")を戻り値とする形式で実装します。引数の値をメソッド内部で扱う必要は特にありません。
【ゲートウェイServantの必須メソッド(2)】
virtual CORBA::RepositoryId _primary_interface(
const PortableServer::ObjectId& oid, );
PortableServer::POA_ptr poa,
CORBA::Environment& = CORBA::Environment())
throw( CORBA::Exception ) = 0;
目次
索引
![]()
|