3

在我的练习中,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;
  };
}

发布者.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));
          }
      }
4

2 回答 2

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

我希望这有帮助。如果需要更多详细信息,请随时询问。

于 2017-09-12T09:36:49.540 回答
0

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

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

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

于 2017-09-12T08:03:59.247 回答