OpenDDS - 从单个IDL结构创建多个主题

时间:2017-09-11 07:32:32

标签: c++ data-distribution-service opendds

在我使用OpenDDS的练习中,我想从单个IDL结构创建多个主题,这可能吗?否则请告诉我怎么做。

我这样做,如果不是正确的方法,请纠正我。 我使用的示例位于OpenDDS-3.12/examples/DCPS/IntroductionToOpenDDS

IDL如下,

StockQuoter.idl
---------------
module StockQuoter
{
#pragma DCPS_DATA_TYPE "StockQuoter::Quote"
#pragma DCPS_DATA_KEY "StockQuoter::Quote ticker"

  struct Quote {
    string ticker;
    string exchange;
    string full_name;
    double value;
    string data;
    TimeBase::TimeT timestamp;
  };
}

publisher.cpp

  // Create TOPICS and TYPES Vector
  std::stringstream ss;
  for(unsigned int idx = 0; idx < 100; ++idx)
  {
      ss << (idx+1);
      TOPICS.push_back("TOPIC" + std::string(ss.str()));
      TYPES.push_back("TYPE" + std::string(ss.str()));
      ss.clear();
      ss.str(std::string());
  }

     // Register
    for( unsigned int idx = 0; idx < 100; ++idx )
    {
        vec_quote_servent.push_back(new StockQuoter::QuoteTypeSupportImpl());
        if (DDS::RETCODE_OK != vec_quote_servent[idx]->register_type(participant.in (), TYPES[idx].c_str()))
        {
          cerr << "register_type for " << TYPES[idx] << " failed." << endl;
          ACE_OS::exit(1);
        }
    }

    // Create a topics
    for( unsigned int idx = 0; idx < 100; ++idx )
    {
         vec_quote_topic.push_back(   participant->create_topic (TOPICS[idx].c_str(),
                                      TYPES[idx].c_str(),
                                      default_topic_qos,
                                      DDS::TopicListener::_nil(),
                                      ::OpenDDS::DCPS::DEFAULT_STATUS_MASK));

         if (CORBA::is_nil (vec_quote_topic[idx].in ())) {
             cerr << "create_topic for " << TOPICS[idx] << " failed." << endl;
             ACE_OS::exit(1);
        }
     }

    // Create DataWriters
    for( unsigned int idx = 0; idx < 100; ++idx )
    {
         vec_quote_base_dw.push_back( pub->create_datawriter(vec_quote_topic[idx].in (),
                                      dw_default_qos,
                                      DDS::DataWriterListener::_nil(),
                                      ::OpenDDS::DCPS::DEFAULT_STATUS_MASK) );

        if (CORBA::is_nil (vec_quote_base_dw[idx].in ())) {
           cerr << "create_datawriter for " << TOPICS[idx] << " failed." << endl;
           ACE_OS::exit(1);
        }

        vec_quote_dw.push_back( StockQuoter::QuoteDataWriter::_narrow(vec_quote_base_dw[idx].in()) );
        if (CORBA::is_nil (vec_quote_dw[idx].in ())) {
          cerr << TOPICS[idx] << " could not be narrowed"<< endl;
          ACE_OS::exit(1);
        }
    }

    // Create handle
    for( unsigned int idx = 0; idx < 100 ; ++idx )
    {
        {
            StockQuoter::Quote topic2;
            topic2.ticker = CORBA::string_dup(TOPICS[idx].c_str());
            vec_topic_handle.push_back(vec_quote_dw[idx]->register_instance(topic2));
        }
    }

      // Publish data
      StockQuoter::Quote vec_quote;
      vec_quote.exchange = STOCK_EXCHANGE_NAME;
      vec_quote.ticker = CORBA::string_dup("VEC_TOPIC");
      vec_quote.full_name = CORBA::string_dup("TOPIC Receipts");
      vec_quote.value = 1600.0 + 10.0*i;
      vec_quote.timestamp = get_timestamp();

      for(unsigned int idx = 0; idx < 100; ++idx )
      {
          vec_quote.value += idx + 10;
          cout << "Publishing " << TOPICS[idx] << " : " << vec_quote.value <<endl;
          ret = vec_quote_dw[idx]->write(vec_quote, vec_topic_handle[idx]);
          if (ret != DDS::RETCODE_OK) {
             ACE_ERROR ((LM_ERROR, ACE_TEXT("(%P|%t) ERROR: TOPIC2 write returned %d.\n"), ret));
          }
      }

2 个答案:

答案 0 :(得分:1)

a,现在我明白了你想问的问题。您可以在每个主题的一个文件中定义不同的主题类型,或者在一个文件中定义所有主题如果在IDL文件中定义多个主题类型,则会为每个文件生成类型支持。让我用你使用的相同例子更精确地描述这个。 IntroductionToOpenDDS示例的IDL文件如下所示:

#include "orbsvcs/TimeBase.idl"

module StockQuoter
{
#pragma DCPS_DATA_TYPE "StockQuoter::Quote"
#pragma DCPS_DATA_KEY "StockQuoter::Quote ticker"

struct Quote {
    string ticker;
    string exchange;
    string full_name;
    double value;
    TimeBase::TimeT timestamp;
};

#pragma DCPS_DATA_TYPE "StockQuoter::ExchangeEvent"
#pragma DCPS_DATA_KEY "StockQuoter::ExchangeEvent exchange"

enum ExchangeEventType { TRADING_OPENED,
                       TRADING_CLOSED,
                       TRADING_SUSPENDED,
                       TRADING_RESUMED };
struct ExchangeEvent {
    string exchange;
    ExchangeEventType event;
    TimeBase::TimeT timestamp;
};
};

如您所见,定义了两种类型:QuoteExchangeEvent。编译此IDL文件时,会生成QuoteExchangeEvent的类型支持。 您已使用类型支持来使用此行(QuoteTypeSupportImpl):

    vec_quote_servent.push_back(new StockQuoter::QuoteTypeSupportImpl());

ExchangeEvent生成了相同类型的支持,您会找到一个名为StockQuoter::ExchangeEvent的类型支持,其中包含StockQuoter::ExchangeEventTypeSupportImpl()方法。只需使用此选项即可创建ExchangeEvent类型的主题。

我希望这会有所帮助。如果需要更多细节,请随时询问。

答案 1 :(得分:0)

您可以从单个IDL文件创建任意数量的主题。你已经用这条线做了:

participant->create_topic (TOPICS[idx].c_str(),
                           TYPES[idx].c_str(),
                           default_topic_qos,
                           DDS::TopicListener::_nil(),
                           ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

但是,您创建的每个主题都具有相同的类型。如果必须,您还可以为主题创建不同的类型。