トランザクショナル分散オブジェクト基盤 TPBroker Object Transaction Monitor プログラマーズガイド

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

2.7.2 ユーザ例外通知を利用するサーバアプリケーションの例(C++)

ユーザ例外通知を利用するサーバアプリケーションの処理の流れとコードの例を示します。斜体で示しているコードは,雛形クラスとして自動生成される部分です。太字で示しているコードは,同期型呼び出しのコードと異なる部分です。

サーバアプリケーションの作成時には,ユーザは,自動生成された雛形クラスEEE_TSCimplにTSCユーザオブジェクトのコードを記述します。また,雛形クラスEEE_TSCfactimplにTSCユーザオブジェクトファクトリのコードを記述します。

<この項の構成>
(1) TSCユーザオブジェクト(EEE_TSCimpl)とTSCユーザオブジェクトファクトリ(EEE_TSCfactimpl)のヘッダのコード
(2) TSCユーザオブジェクト(EEE_TSCimpl)とTSCユーザオブジェクトファクトリ(EEE_TSCfactimpl)のコード
(3) サービス登録処理の流れ
(4) サービス登録処理のコード

(1) TSCユーザオブジェクト(EEE_TSCimpl)とTSCユーザオブジェクトファクトリ(EEE_TSCfactimpl)のヘッダのコード

 
//
// "UserExcept_TSC_t.hh"
//
#ifndef _EEEfile_TSC_T_HDR
#define _EEEfile_TSC_T_HDR
 
#include <tscobject.h>
 
#include "EEEfile_TSC_s.hh"
 
class EEE_TSCfactimpl : public TSCObjectFactory
{
public:
  // コンストラクタの引数の数および型を変更することもできます。
  EEE_TSCfactimpl();
  virtual ~EEE_TSCfactimpl();
 
  virtual TSCObject_ptr create();
  virtual void destroy(TSCObject_ptr tsc_object);
 
};
 
class EEE_TSCimpl : public EEE_TSCsk
{
private:
 
public:
  // コンストラクタの引数の数および型を変更することもできます。
  EEE_TSCimpl();
  EEE_TSCimpl(const char* _tpbroker_object_name);
 
  virtual ~EEE_TSCimpl();
 
  void call();  
 
};
 
#endif  // _EEEfile_TSC_T_HDR

(2) TSCユーザオブジェクト(EEE_TSCimpl)とTSCユーザオブジェクトファクトリ(EEE_TSCfactimpl)のコード

 
//
// "UserExcept_TSC_t.cpp"
//
#include "EEEfile_TSC_t.hh"
 
EEE_TSCfactimpl::EEE_TSCfactimpl()
{
  // Constructor of factory implementation.
  // Write user own code.
  // TSCユーザオブジェクトファクトリのコンストラクタのコードを記述して
  // ください。
  // コンストラクタの引数の数および型を変更することもできます。
}
 
EEE_TSCfactimpl::~EEE_TSCfactimpl()
{
  // Destructor of factory implementation.
  // Write user own code.
  // TSCユーザオブジェクトファクトリのデストラクタのコードを記述して
  // ください。
}
 
TSCObject_ptr
EEE_TSCfactimpl::create()
{
  // Method to create user object.
  // Write user own code.
  // サーバオブジェクトを生成するコードを記述します。
  // 必要に応じて変更してください。
  return new EEE_TSCimpl();
}
 
void
EEE_TSCfactimpl::destroy(TSCObject_ptr tsc_obj)
{
  // Method to destroy user object.
  // Write user own code.
  // 後処理のコードを記述します。
  // 必要に応じて変更してください。
  delete tsc_obj;
}
 
EEE_TSCimpl::EEE_TSCimpl()
{
  // Constructor of implementation.
  // Write user own code.
  // TSCユーザオブジェクトファクトリのコンストラクタのコードを記述して
  // ください。
  // コンストラクタの引数の数および型を変更することもできます。
}
 
EEE_TSCimpl::EEE_TSCimpl(const char* _tpbroker_object_name)
      : EEE_TSCsk(_tpbroker_object_name)
{
  // Constructor of implementation.
  // Write user own code.
  // TSCユーザオブジェクトファクトリのコンストラクタのコードを記述して
  // ください。
  // コンストラクタの引数の数および型を変更することもできます。
}
 
EEE_TSCimpl::~EEE_TSCimpl()
{
  // Destructor of implementation.
  // Write user own code.
  // TSCユーザオブジェクトファクトリのデストラクタのコードを記述して
  // ください。
}
 
void
EEE_TSCimpl::call()
{
  // Operation "::EEE::call".
  // Write user own code.
 
  throw UserExcept(123456);
}

(3) サービス登録処理の流れ

  1. TPBrokerの初期化処理
  2. TPBroker OTMの初期化処理
  3. TSCデーモンへの接続
  4. TSCユーザオブジェクトファクトリ,TSCユーザアクセプタの生成(new),および各種設定
  5. TSCルートアクセプタの生成および各種設定
  6. TSCルートアクセプタの活性化
  7. 実行制御の受け渡し
  8. TSCルートアクセプタの非活性化
  9. TSCルートアクセプタの削除
  10. TSCユーザオブジェクトファクトリおよびTSCユーザアクセプタの削除(delete)
  11. TSCデーモンへの接続解放
  12. TPBroker OTMの終了処理

(4) サービス登録処理のコード

 
//
// "Server.cpp"
//
#include <stdio.h>
#include <iostream.h>
 
#include <tscadm.h>
#include <tscobject.h>
#include <tscexcept.h>
 
#include "EEEfile_TSC_t.hh"
 
#define ERR_FORMAT
    "EC=%d,DC=%d,PC=%d,CS=%d,MC1=%d,MC2=%d,MC3=%d,MC4=%d\n"
 
int main(int argc, char** argv)
{
 
  ////////
  // 1, TPBrokerの初期化処理
  ////////
 
  CORBA::ORB_ptr orb = 0;
  try
  {
    // ORBの初期化
    orb = CORBA::ORB_init(argc, argv);
  }
  catch(CORBA::SystemException& se)
  {
    // 例外処理
    cerr << se << endl;
    exit(1);
  }
 
  ////////
  // 2, TPBroker OTMの初期化処理
  ////////
 
  try
  {
    // TSCの初期化
    TSCAdm::initServer(argc, argv, orb);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    exit(1);
  }
 
  ////////
  // 3, TSCデーモンへの接続
  ////////
 
  TSCDomain_ptr tsc_domain = 0;
 
  try
  {
    tsc_domain = new TSCDomain(0, 0);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      TSCAdm::endServer();
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  TSCServer_ptr tsc_server = 0;
 
  try
  {
    // TSCデーモンの参照オブジェクトを取得
    tsc_server = TSCAdm::getTSCServer(tsc_domain);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      TSCAdm::endServer();
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  ////////
  // 4, TSCユーザオブジェクトファクトリ,TSCユーザアクセプタ(new),
  //    および各種設定
  ////////
 
  // EEE_TSCfactimplの生成
  TSCObjectFactory_ptr my_obj_fact = new EEE_TSCfactimpl();
 
  // TSCAcceptorの生成
  TSCAcceptor_ptr my_acpt = 0;
 
  try
  {
    my_acpt = new EEE_TSCacpt(my_obj_fact);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      delete my_obj_fact;
      TSCAdm::releaseTSCServer(tsc_server);
      TSCAdm::endServer();
      exit(1);
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  ////////
  // 5, TSCルートアクセプタの生成および各種設定
  ////////
 
  // TSCRootAcceptorの生成
  TSCRootAcceptor_ptr my_rt_acpt = 0;
 
  try
  {
    my_rt_acpt = TSCRootAcceptor::create(tsc_server);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      delete my_acpt;
      delete my_obj_fact;
      TSCAdm::releaseTSCServer(tsc_server);
      TSCAdm::endServer();
      exit(1);
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  try
  {
    // TSCRootAcceptorへの登録
    my_rt_acpt->registerAcceptor(my_acpt);
 
    // TSCRootAcceptorのパラレルカウント指定もできます。
    // デフォルト値は1
    // オプション引数でデフォルト値を変更することもできます。
    // my_rt_acpt->setParallelCount(5);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      TSCRootAcceptor::destroy(my_rt_acpt);
      delete my_acpt;
      delete my_obj_fact;
      TSCAdm::releaseTSCServer(tsc_server);
      TSCAdm::endServer();
      exit(1);
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  ////////
  // 6, TSCルートアクセプタの活性化
  ////////
 
  try
  {
    // オブジェクトの活性化
    my_rt_acpt->activate("serviceX");
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      TSCRootAcceptor::destroy(my_rt_acpt);
      delete my_acpt;
      delete my_obj_fact;
      TSCAdm::releaseTSCServer(tsc_server);
      TSCAdm::endServer();
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  ////////
  // 7, 実行制御の受け渡し
  ////////
 
  try
  {
    TSCAdm::serverMainloop();
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      my_rt_acpt->deactivate();
      TSCRootAcceptor::destroy(my_rt_acpt);
      delete my_acpt;
      delete my_obj_fact;
      TSCAdm::releaseTSCServer(tsc_server);
      TSCAdm::endServer();
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  ////////
  // 8, TSCルートアクセプタの非活性化
  ////////
 
  try
  {
    // オブジェクトの非活性化
    my_rt_acpt->deactivate();
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      TSCRootAcceptor::destroy(my_rt_acpt);
      delete my_acpt;
      delete my_obj_fact;
      TSCAdm::releaseTSCServer(tsc_server);
      TSCAdm::endServer();
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  ////////
  // 9, TSCルートアクセプタの削除
  ////////
 
  TSCRootAcceptor::destroy(my_rt_acpt);
 
  ////////
  // 10, TSCユーザオブジェクトファクトリおよびTSCユーザアクセプタの
  //     削除(delete)
  ////////
 
  delete my_acpt;
  delete my_obj_fact;
 
  ////////
  // 11, TSCデーモンへの接続解放
  ////////
 
  try
  {
    TSCAdm::releaseTSCServer(tsc_server);
  }
  catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    try
    {
      TSCAdm::endServer();
    }
    catch(TSCSystemException& se)
    {
      exit(1);
    }
    exit(1);
  }
 
  delete tsc_domain;
 
  ////////
  // 12, TPBroker OTMの終了処理
  ////////
 
  try
  {
    TSCAdm::endServer();
  }
    catch(TSCSystemException& se)
  {
    // 例外処理
    fprintf(stderr, ERR_FORMAT,
       se.getErrorCode(), se.getDetailCode(),
       se.getPlaceCode(), se.getCompletionStatus(),
       se.getMaintenanceCode1(), se.getMaintenanceCode2(),
       se.getMaintenanceCode3(), se.getMaintenanceCode4());
    exit(1);
  }
 
  exit(0);
 
};