[openrtm-users 00266] Re: サービスポートのRTC以外からの利用に関して

Ando Noriaki n-ando @ aist.go.jp
2007年 11月 16日 (金) 09:17:50 JST


金広様

安藤です

> サービスポートに定義されているIDLのインタフェースをRTC以外の
> 通常のCORBAクライアントから呼び出す方法はありますでしょうか?
> どのようにするとサービスの参照を取得できるでしょうか?

RTCのPortにconnectをする際に、そのポート自身のオブジェクトリファレンスのみ
をConnectorProfileにセットしてconnectしてください。
そうすると、戻ってきたConnectorProfile::propertiesの中に、
port.[Service Type].[Service Name] というキーでCORBA::Any型で
オブジェクトリファレンスが入っています。
それを、使用したいサービスの型にnarrowして使用してください。

ちょっとごちゃごちゃしてますが、おおよそ以下のようになります。

このファイルとMakefileを添付します。
添付のtar-ballをexamples/SimpleServiceの下で展開しmakeしてください。
make -f Makefile.clientでmakeできます。

このプログラムを試す手順は以下の通りです。
MyServiceProviderのサービスポートがProvideしてるMyServiceインターフェースを
取得して、MyServiceインターフェースのオペレーションであるecho()を呼んでいます。

1. ネームサーバをローカルに立ち上げる
2. MyServiceProviderをterminal1で立ち上げる
3. terminal2で下のプログラムをコンパイルしたものを起動
4. terminal1で以下のように表示される
MyService::echo() was called.
Message: hogehoge

プログラム中にはクラスリファレンスおよびIDLリファレンスへのURLも
書いてありますので適宜参照しながらごらんください。


------------------
#include <rtm/CorbaNaming.h>
#include <iostream>
#include "MyService.hh"
#include <rtm/NVUtil.h>

int main(int argc, char** argv)
{
  CORBA::ORB_var orb;
  orb = CORBA::ORB_init(argc, argv);

  // RTC::CorbaNaming を使用してネームサーバにアクセス
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/ClassReference/classRTC_1_1CorbaNaming.html
  RTC::CorbaNaming ns(orb, "localhost");

  // RTObjectを取得
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/IDLReference/interfaceRTC_1_1RTObject.html
  CORBA::Object_var obj = ns.resolve("MyServiceProvider0.rtc");
  RTC::RTObject_var rtc = RTC::RTObject::_narrow(obj);

  // ComponentProfileを取得
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/IDLReference/structRTC_1_1ComponentProfile.html
  RTC::ComponentProfile* prof;
  prof = rtc->get_component_profile();
  std::cout << "RTC name: " << prof->instance_name << std::endl;

  // PortProfileを取得
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/IDLReference/structRTC_1_1PortProfile.html
  RTC::PortProfileList port_prof;
  port_prof = prof->port_profiles;
  for (CORBA::ULong i(0), len(port_prof.length()); i < len; ++i)
    {
      std::cout << "name: " << port_prof[i].name << std::endl;

      // インターフェースを表示してみる
      //  http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/IDLReference/structRTC_1_1PortInterfaceProfile.html
      RTC::PortInterfaceProfileList  ifs(port_prof[i].interfaces);
      for (CORBA::ULong j(0), jlen(ifs.length()); j < jlen; ++j)
	{
	  std::cout << "IF name: " << ifs[j].instance_name << std::endl;
	  std::cout << "IF type: " << ifs[j].type_name << std::endl;
	  const char* pol;
	  pol = ifs[j].polarity == RTC::PROVIDED ? "Provided" : "Required";
	  std::cout << "IF polarity: " << pol << std::endl;
	}
    }

  RTC::Port_var port;
  port = port_prof[0].port_ref;



  //
  // ConnectorProfile のportメンバに自分自身のリファレンスのみ入れて
  // connect する。戻ってきたConnectorProfileのpropertiesの中には
  // サービスのObjectReferenceが入っているので取得する。
  //
  // サービスの接続に関する情報はクラスリファレンスのCorbaPortを参照
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/ClassReference/classRTC_1_1CorbaPort.html
  //
  // ConnectorProfile
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/IDLReference/structRTC_1_1ConnectorProfile.html
  RTC::ConnectorProfile con_prof;
  con_prof.name = CORBA::string_dup("tekitouna_namae0");
  con_prof.connector_id = "";
  con_prof.ports.length(1);
  con_prof.ports[0] = port;
  con_prof.properties.length(0);
  if (CORBA::is_nil(port))
    {
      std::cout << "nil reference" << std::endl;
      return 0;
    }

  if (port->connect(con_prof) != RTC::RTC_ERROR)
    {
      // エラーは無視
      std::cout << "ignore error" << std::endl;
    }
  std::cout << "connect OK" << std::endl;

  CORBA::Object_ptr aobj;
  MyService_var mysvc;

  // NVUtilでpropertiesの中からサービスのオブジェクトリファレンスを取得
  // http://www.is.aist.go.jp/rt/OpenRTM-aist/doxygen/ClassReference/namespaceNVUtil.html
  if (NVUtil::find(con_prof.properties, "port.MyService.myservice0")
>>= CORBA::Any::to_object(aobj))
    {
      mysvc = MyService::_narrow(aobj);
    }

  // サービスを呼ぶ
  mysvc->echo("hogehoge");
  // MyServiceProviderを実行した画面で以下のように表示されるはず
  //
  // MyService::echo() was called.
  // Message: hogehoge



}



-- 
安藤慶昭@独立行政法人産業技術総合研究所 研究員
                   知能システム研究部門 タスクインテリジェンス研究グループ
                   〒305-8568 茨城県つくば市梅園1-1-1 中央第2
                   TEL: 029-861-5981 FAX: 029-861-5971
                   n-ando @ aist.go.jp, n-ando @ ieee.org
-------------- next part --------------
テキスト形式以外の添付ファイルを保管しました...
ファイル名: CorbaClient.tar.gz
型:         application/x-gzip
サイズ:     1630 バイト
説明:       無し
URL:        <http://www.openrtm.org/pipermail/openrtm-users/attachments/20071116/822efff6/attachment-0001.bin>


openrtm-users メーリングリストの案内