RTI DDS. c++11例子 -1

Participant Use Cases

Setting up a DomainParticipant

// When not explicitly provided to the constructor, the 
// created DomainParticipants's QoS is default, has a NULL listener, and a 
// status mask dds::core::status::StatusMask::all()
// Creating a DomainParticipants on domain 0
dds::domain::DomainParticipant participant1(0) ;
// Creating a DomainParticipants with a non-default QoS values
dds::domain::qos::DomainParticipantQos qos;
 // Set the initial peers. 
 // The peers stored in initial_peers are merely potential peers--there is 
 // no requirement that the peer DomainParticipants are actually up and
 // running or even will eventually exist. The Connext discovery process 
 // will try to contact all potential peer participants in the list 
 // periodically using unicast transports. 
 const char * initial_peers_array[] = {
   "host1",
   "10.10.30.192",
   "1@localhost",        
   "2@host2",
   "my://", /* all unicast addrs on transport plugins with alias "my" */
   "2@shmem://", /* shared memory */
   "FF00:ABCD::0",
   "1@FF00:0:1234::0",
   "225.1.2.3",
   "3@225.1.0.55",
   "FAA0::0#0/0/R"};
const dds::core::StringSeq initial_peers_list(initial_peers_array, initial_peers_array+11);
// Add the initial peers list to the DiscoveryQosPolicy while modifying
// the participant's qos inline
qos << rti::core::policy::Discovery().initial_peers(initial_peers_list);
// Creating a DomainParticipants on domain 1 with modified QoS
dds::domain::DomainParticipant participant2(1, qos);
// Creating a DomainParticipants on domain 2, with modified QoS, and a listener
ExampleParticipantListener *listener = new ExampleParticipantListener;
dds::domain::DomainParticipant participant3(2, qos, listener);
// Creating a DomainParticipants on domain 3, with modified QoS, a listener and 
// a data_available() status mask
dds::domain::DomainParticipant participant4(
   3, qos, listener, dds::core::status::StatusMask::data_available());
// Creating an entity with a listener automatically retains the entity, so 
// we must either explicitly close the DomainParticipant...
participant3.close();

// ... or set the listener to null
participant4.listener(NULL, dds::core::status::StatusMask::none());

Looking up DomainParticipants

// Create and retain two DomainParticipants on domain 0 and one on domain 1
    create_and_retain_participant(0);
    create_and_retain_participant(0);
    create_and_retain_participant(1);
    // We don't have any references to the created DomainParticipants but we 
    // can look them up using the domain id they were created on
    // If there is more than one participant on the same domain then this
    // operation will return only one of them. It is unspecified which one it
    // will return
    dds::domain::DomainParticipant participant1 = dds::domain::find(0);
    dds::domain::DomainParticipant participant2 = dds::domain::find(1);

Finalizing the factory

// The DomainParticipantFactory is a singleton that the C++ API uses 
// implicitly to create participants. RTI Connext provides 
// dds::domain::DomainParticipant::finalize_participant_factory() 
// for users who want to release memory used by the participant factory. 
dds::domain::DomainParticipant::finalize_participant_factory();

Topic Use Cases

Setting up a Topic

 // Create the participant
    dds::domain::DomainParticipant participant(0);
    // When not explicitly provided to the constructor, the 
    // created topic's QoS is default, has a NULL listener, and a status mask
    // dds::core::status::StatusMask::all()
    // Creating a topic with user-defined type Foo and topic name MyTopic1
    dds::topic::Topic<Foo> topic1(participant, "MyTopic1");
    // Creating a topic with user-defined type Foo, topic name MyTopic1, 
    // type name MyTypeName
    dds::topic::Topic<Foo> topic2(participant, "MyTopic2", "MyTypeName2");
    // Creating a topic with user-defined type Foo, topic name MyTopic3, and
    // modified QoS
    dds::topic::qos::TopicQos qos;
    qos << dds::core::policy::Durability::TransientLocal();
    dds::topic::Topic<Foo> topic3(participant, "MyTopic3", qos);
    // Creating a topic with user-defined type Foo, topic name MyTopic4, type 
    // name MyTypeName4 and modified QoS
    dds::topic::Topic<Foo> topic4(participant, "MyTopic4", "MyTypeName4", qos);
    // Creating a topic with user-defined type Foo, topic name MyTopic5, 
    // modified QoS, and a non-NULL listener
    ExampleTopicListener *listener = new ExampleTopicListener; 
    dds::topic::Topic<Foo> topic5(participant, "MyTopic5", qos, listener);
    // Creating a topic with user-defined type Foo, topic name MyTopic6, type 
    // name MyTypeName6, modified QoS, and a non-NULL listener
    dds::topic::Topic<Foo> topic6(
       participant, "MyTopic6", "MyTypeName6", qos, listener);
    // Creating a topic with user-defined type Foo, topic name MyTopic7, 
    // modified QoS, a non-NULL listener, and a inconsistent_topic status mask
    dds::topic::Topic<Foo> topic7(
       participant, "MyTopic7", qos, listener, 
       dds::core::status::StatusMask::inconsistent_topic());
    // Creating a topic with user-defined type Foo, topic name MyTopic8, type 
    // name MyTypeName8, modified QoS, and a non-NULL listener, and 
    // a inconsistent_topic status mask
    dds::topic::Topic<Foo> topic8(
       participant, "MyTopic8", "MyTypeName8", qos, listener, 
       dds::core::status::StatusMask::inconsistent_topic());
    // Creating an Topic with a listener automatically retains the Topic, so 
    // we must either explicitly close the Topic...
    topic4.close();
    topic5.close();
    topic6.close();
    topic7.close();
    
    // ... or set the listener to null
    topic8.listener(NULL, dds::core::status::StatusMask::none());

Discovering Topics

Lookup local Topics by their topic name

void howto_lookup_topics()
{
    dds::domain::DomainParticipant participant(0);
    // Create two Topics and retain them
    create_and_retain_topic(participant, "Topic1");
    create_and_retain_topic(participant, "Topic2");
    // We don't have any references to the created Topics but we can look 
    // them up using topic name that they were created with
    dds::topic::Topic<Foo> topic1 = 
        dds::topic::find<dds::topic::Topic<Foo> >(participant, "Topic1");
    dds::topic::Topic<Foo> topic2 = 
        dds::topic::find<dds::topic::Topic<Foo> >(participant, "Topic2");
    // We retained the topics so we must close them now
    topic1.close();
    topic2.close();
}
void create_and_retain_topic(
   dds::domain::DomainParticipant &participant, const std::string& topic_name)
{
    dds::topic::Topic<Foo> topic(participant, topic_name);
    topic.retain();
}

The next two examples assume the following setup

   // Create two DomainParticipants on the same domain
    dds::domain::DomainParticipant participant1(1);
    dds::domain::DomainParticipant participant2(1);
    
    // Create some Topics for both of the Participants
    dds::topic::Topic<Foo> topic1(participant1, "Topic1");
    dds::topic::Topic<Foo> topic2(participant1, "Topic2");
    dds::topic::Topic<Foo> topic3(participant2, "Topic3");

Discover all Topics in a domain

  // Get a list of the InstanceHandles of all of the topics in the 
    // domain. topic_count will = 3
    dds::core::InstanceHandleSeq handles1;
    int topic_count = dds::topic::discover_any_topic(
       participant1, std::back_inserter(handles1));
    // discover_any_topic also takes a forward iterator and a max number of 
    // topics to discover. topic_count will = 2. There is no guarantee
    // as to which two topics' handles will be retrieved
    dds::core::InstanceHandleSeq handles2(10, dds::core::InstanceHandle::nil());
    topic_count = dds::topic::discover_any_topic(participant2, handles2.begin(), 2);

Retrieve the TopicBuiltinTopicData for a Topic

   std::vector<dds::topic::TopicBuiltinTopicData> topic_data1;
    // Get the TopicBuiltinTopicData for all of the participant's local topics
    // topic_count will = 2 because participant1 has 2 associated Topics
    topic_count = dds::topic::discover_topic_data(participant1, std::back_inserter(topic_data1));
    std::vector<dds::topic::TopicBuiltinTopicData> topic_data2(10);
    // discover_any_topic also takes a forward iterator and a max number of 
    // topics to get the data for. Because we're using participant2, 
    // topic_count will = 1
    topic_count = dds::topic::discover_topic_data(participant2, topic_data2.begin());
    // You can also retrieve data about a specific Topic or Topics with their 
    // InstanceHandles. Because discover_topic_data will only retrieve data
    // about local topics, using an InstanceHandle that refers to a remote 
    // Topic will cause an UnsupportedError to be thrown in either of the 
    // following cases
    // Retrieve the TopicBuiltinTopicData for topic3
    dds::topic::TopicBuiltinTopicData topic_data = 
        dds::topic::discover_topic_data(participant2, topic3->instance_handle());
    // Pass in a container with multiple handles and retrieve the 
    // TopicBuiltinTopicData for each of the Topics associated with each of the 
    // InstanceHandles 
    std::vector<dds::topic::TopicBuiltinTopicData> topic_data3(10);
    dds::core::InstanceHandleSeq local_handles;
    local_handles.push_back(topic1->instance_handle());
    local_handles.push_back(topic2->instance_handle());
    // topic_count will equal 2
    topic_count = dds::topic::discover_topic_data(
       participant1, topic_data3.begin(), local_handles);

Publisher Use Cases

Setting up a publisher

 // Create the participant
    dds::domain::DomainParticipant participant(0);
    // When not explicitly provided to the constructor, the 
    // created publisher's QoS is default, has a NULL listener, and a status mask
    // dds::core::status::StatusMask::all()
    // Creating a publisher
    dds::pub::Publisher publisher1(participant);
    // Creating a publisher with modified QoS
    dds::pub::qos::PublisherQos qos;
    qos << rti::core::policy::EntityName("PublisherName");
    dds::pub::Publisher publisher2(participant, qos);
    // Creating a publisher with modified QoS and a listener
    ExamplePublisherListener *listener = new ExamplePublisherListener;
    dds::pub::Publisher publisher3(participant, qos, listener);
    // Creating a publisher with modified QoS, a listener, and a sample_rejected() 
    // status mask
    dds::pub::Publisher publisher4(
       participant, qos, listener, dds::core::status::StatusMask::offered_deadline_missed());
    // Creating an entity with a listener automatically retains the entity, so 
    // we must either explicitly close the Publihser...
    publisher3.close();
    
    // ... or set the listener to null
    publisher4.listener(NULL, dds::core::status::StatusMask::none());

Looking up Publishers

void howto_lookup_publishers()
{
    dds::domain::DomainParticipant participant(0);
    // Create three Publishers and retain them
    create_and_retain_publisher(participant);
    create_and_retain_publisher(participant);
    create_and_retain_publisher(participant);
    // We don't have any references to the created Publishers but we can look 
    // them up using the participant that they were created with
    std::vector<dds::pub::Publisher> publishers1;
    // publisher_count will equal 3 and publishers1 will hold a reference to 
    // each of the three publishers that were created
    int publisher_count = 
        rti::pub::find_publishers(participant, std::back_inserter(publishers1));
    // rti::pub::publishers also takes a forward iterator and a max number of
    // publishers to look up
    std::vector<dds::pub::Publisher> publishers2(2, dds::pub::Publisher(dds::core::null));
    // publisher_count will equal 2 because we're only asking for 2
    publisher_count = 
        rti::pub::find_publishers(participant, publishers2.begin(), 2);
    // We retained the publishers so we must close them now
    for (std::vector<dds::pub::Publisher>::iterator it = publishers1.begin();
          it != publishers1.end(); 
          it++) {
        (*it).close();
    }
}
void create_and_retain_publisher(dds::domain::DomainParticipant &participant)
{
    dds::pub::Publisher publisher(participant);
    publisher.retain();
}

DataWriter Use Cases

Setting up a DataWriter

// Create the Participant
    dds::domain::DomainParticipant participant(0);
    // Create a Topic
    dds::topic::Topic<Foo> topic(participant, "MyTopicName");
    // When not explicitly provided to the constructor, the 
    // created DataWriter's QoS is default, has a NULL listener, and a status mask
    // dds::core::status::StatusMask::all()
    // Creating a DataWriter, creating the subscriber inline
    dds::pub::DataWriter<Foo> writer1(dds::pub::Publisher(participant), topic);
    // Create a Publisher
    dds::pub::Publisher publisher(participant);
    // Create a DataWriter with modified QoS
    dds::pub::qos::DataWriterQos writer_qos = 
            dds::core::QosProvider::Default().datawriter_qos();
    writer_qos << dds::core::policy::Liveliness::ManualByParticipant();
    dds::pub::DataWriter<Foo> writer2(publisher, topic, writer_qos);
    // Create a DataWriter with the Qos of a topic
    writer_qos = topic.qos(); // Copy the policies in TopicQos into a DataWriterQos
    dds::pub::DataWriter<Foo> writer3(publisher, topic, writer_qos);
    // Create a DataWriter with modified QoS and a listener
    ExampleDataWriterListener<Foo> listener;
    dds::pub::DataWriter<Foo> writer4(
       publisher, 
       topic, 
       writer_qos, 
       &listener);
    // Create a DataWriter with modified QoS, a listener, and a liveliness_lost() 
    // status mask
    dds::pub::DataWriter<Foo> writer5(
       publisher, 
       topic, 
       writer_qos, 
       &listener, 
       dds::core::status::StatusMask::liveliness_lost());
    // Create a DataWriter, then set a listener using bind_and_manage_listener
    dds::pub::DataWriter<Foo> writer6(publisher, topic);
    auto listener_binder = rti::core::bind_and_manage_listener(
        writer6,
        new ExampleDataWriterListener<Foo>, 
        dds::core::status::StatusMask::liveliness_lost());
    // Creating an entity with a listener automatically retains the entity, so 
    // we must either explicitly close the DataWriter...
    writer4.close();
    
    // ... or set the listener to null
    writer5.listener(NULL, dds::core::status::StatusMask::none());
    // For writer6 we don't need to do anything, since the listener binder
    // takes care of setting the listener to null

Managing instances

The snippets in this section assume the following setup

dds::domain::DomainParticipant participant(0);
dds::topic::Topic<Foo> topic(participant, "MyTopic");
dds::pub::DataWriter<Foo> writer(dds::pub::Publisher(participant), topic);
Foo sample;
sample.x(100);

Getting an instance “key” value of user data type Foo

// Instead of a sample of type Foo, key_value() could also take a 
// TopicInstance<Foo> object. key_value only populates the key field in the 
// sample
Foo retrieved_sample_key;
writer.key_value(retrieved_sample_key, handle);

Registering an instance of type Foo

dds::core::InstanceHandle handle = writer.register_instance(sample);

Unregistering an instance of type Foo

writer.unregister_instance(handle);

Disposing of an instance of type Foo

writer.dispose_instance(handle);

Sending data

Set up a DataWriter
Register an instance
Write instances of type Foo (see also Working with IDL types)

 Foo data;
    dds::core::InstanceHandle handle = 
        dds::core::InstanceHandle::nil();
    dds::core::Time timestamp(123);
    // Write a sample 
    writer.write(data);
    // You can also write a sample with a timestamp that will be used as the 
    // source timestamp for this sample
    writer.write(data, timestamp);
    // You can also write a sample with an InstanceHandle
    writer.write(data, handle);
    // The above call to write is equivalent to the following
    writer.write(dds::topic::TopicInstance<Foo>(handle, data));
    // Or a combination of handle and timestamp
    writer.write(data, handle, timestamp);
    // The above call to write is equivalent to the following
    writer.write(dds::topic::TopicInstance<Foo>(handle, data), timestamp);
    // Create a vector of three default-constructed samples
    std::vector<Foo> samples(3, Foo());
    // You can also write samples that are stored in a container by 
    // passing in an iterator range to write()
    writer.write(samples.begin(), samples.end());
    // You can add a timestamp to the above call to write
    writer.write(samples.begin(), samples.end(), timestamp);
    // Create a vector of three instance handles
    std::vector<dds::core::InstanceHandle> handles(
       3, dds::core::InstanceHandle::nil());
    // Write 3 samples along with their handles
    writer.write(samples.begin(), samples.end(), 
                 handles.begin(), handles.end());
    // You can add a timestamp to the above call to write
    writer.write(samples.begin(), samples.end(), 
                 handles.begin(), handles.end(), 
                 timestamp);
    // You can also use operator << to write samples
    writer << Foo(1, 2)
           << std::make_pair(Foo(3, 4), timestamp) // add a timestamp
           << std::make_pair(Foo(5, 6), handle); // add an instance handle
## DataWriter Listeners
Implementing a DataWriterListener class

```cpp
// To create a DataWriterListener, inherit from the NoOpDataWriterListener class
// and then override any of the listener callback functions 
template <typename T>
class ExampleDataWriterListener : public dds::pub::NoOpDataWriterListener<T> 
{
public:
    ExampleDataWriterListener() {}
public:
    void on_publication_matched(
       dds::pub::DataWriter<T>&,
       const dds::core::status::PublicationMatchedStatus &status)
    {
        std::cout << "on_publication_matched callback" << std::endl;
        // Access information from the status
        std::cout << "total_count = " << status.total_count() << std::endl;
        std::cout << "total_count_change = " << status.total_count_change() << std::endl;
        std::cout << "current_count = " << status.current_count() << std::endl;
        std::cout << "current_count_change = " << status.current_count_change() << std::endl;
        dds::core::InstanceHandle handle = status.last_subscription_handle();
        // Extension
        std::cout << "current_count_peak = " << status->current_count_peak() << std::endl;
    }
    void on_offered_incompatible_qos(
       dds::pub::DataWriter<T>&,
       const dds::core::status::OfferedIncompatibleQosStatus &status)
    {
        std::cout << "on_offered_incompatible_qos callback" << std::endl;
        // Access information from the status
        std::cout << "total_count = " << status.total_count() << std::endl;
        std::cout << "total_count_change = " << status.total_count_change() << std::endl;
        std::cout << "last_policy_id = " << status.last_policy_id() << std::endl;
        dds::core::policy::QosPolicyCountSeq qos_seq = status.policies();
        if (qos_seq.size() > 0) {
            std::cout << "policy_id of one first incompatible Qos policy = " 
                << qos_seq[0].policy_id() << std::endl;
        }
    }
    void on_offered_deadline_missed(
       dds::pub::DataWriter<T>&,
       const dds::core::status::OfferedDeadlineMissedStatus &status)
    {
        std::cout << "on_offered_deadline_missed callback" << std::endl;
        // Access information from the status
        std::cout << "total_count = " << status.total_count() << std::endl;
        std::cout << "total_count_change = " << status.total_count_change() << std::endl;
        dds::core::InstanceHandle handle = status.last_instance_handle();
    }
    void on_liveliness_lost(
       dds::pub::DataWriter<T>&,
       const dds::core::status::LivelinessLostStatus &status)
    {
        std::cout << "on_liveliness_lost callback" << std::endl;
        // Access information from the status
        std::cout << "total_count = " << status.total_count() << std::endl;
        std::cout << "total_count_change = " << status.total_count_change() << std::endl;
    }
};

Setting a listener on a DataWriter

 dds::domain::DomainParticipant participant(0);
    dds::topic::Topic<Foo> topic(participant, "Topic1");
    ExampleDataWriterListener<Foo> *listener = new ExampleDataWriterListener<Foo>;
    // Create a DataWriter with a listener and default 
    // dds::core::status::StatusMask::all()
    dds::pub::DataWriter<Foo> writer1(
       dds::pub::Publisher(participant), 
       topic,
       dds::pub::qos::DataWriterQos(), 
       listener);
    // Create a DataWriter with a null listener
    dds::pub::DataWriter<Foo> writer2(
       dds::pub::Publisher(participant), topic);
    // Set the listener for the DataWriter after creation
    writer2.listener(
       listener, 
       dds::core::status::StatusMask::offered_incompatible_qos());
    // Setting a listener on an entity automatically retains the entity so 
    // we must explicitly call close() in order for the DataWriter to be
    // destroyed 
    writer1.close();
    // Instead of calling close, you can set the DataWriter's listener to null 
    // and this will implicitly unretain the DataWriter so that it will be 
    // destructed once all references to it go out of scope
    writer2.listener(NULL, dds::core::status::StatusMask::none());
    delete listener;

The ListenerBinder class will take care of setting the listener and mask for an entity during creation and detaching the listener from the entity when the ListenerBinder is destructed. This is helpful because you will not have to explcitly close the entity or detach the listener yourself in order to teardown the entity

  dds::domain::DomainParticipant participant(0);
    dds::topic::Topic<Foo> topic(participant, "Topic1");
    // Create a DataWriter with a null listener
    dds::pub::DataWriter<Foo> writer(dds::pub::Publisher(participant), topic);
    // Attach the listener to the DataWriter after creation using a 
    // ListenerBinder. (Note: rti/core/ListenerBinder.hpp needs to be included)
    auto listener_binder = rti::core::bind_and_manage_listener(
            writer, 
            new ExampleDataWriterListener<Foo>, 
            dds::core::status::StatusMask::liveliness_lost());
    // To get the listener:
    ExampleDataWriterListener<Foo> *listener = listener_binder.listener(); 
    // To get the entity:
    dds::pub::DataWriter<Foo> writer2 = listener_binder.entity();
    assert(writer == writer2);
    // When the reference count reaches zero, the listener_binder destructor 
    // sets the writer's listener to NULL, and deletes the listener (to not
    // delete the listener we would have used bind_listener() instead of 
    // bind_and_manage_listener()).
    //

Looking up DataWriters

Lookup DataWriters by topic name

  dds::domain::DomainParticipant participant(0);
    dds::pub::Publisher publisher(participant);
    dds::topic::Topic<Foo> topic1(participant, "Topic1");
    dds::topic::Topic<Foo> topic2(participant, "Topic2");
    // Create three DataWriters for topic1 and one for topic2 and retain them
    create_and_retain_writer(publisher, topic1);
    create_and_retain_writer(publisher, topic1);
    create_and_retain_writer(publisher, topic2);
    // We don't have any references to the created DataWriters but we can look 
    // them up using the publisher and topic name that they were created with
    std::vector<dds::pub::DataWriter<Foo> > writers;
    // writer_count will equal 1. If more than one DataWriter belongs to a 
    // publisher with the same topic name then find may return any one of them
    int writer_count = 
        dds::pub::find<dds::pub::DataWriter<Foo> >(
           publisher, 
           "Topic1", 
           std::back_inserter(writers));
    writer_count = 
        dds::pub::find<dds::pub::DataWriter<Foo> >(
           publisher, 
           "Topic2", 
           std::back_inserter(writers));

Getting Matched Subscriptions

Lookup a DataWriter’s matched subscriptions

  dds::domain::DomainParticipant participant(0);
    dds::topic::Topic<Foo> topic(participant, "Topic1");
    dds::pub::DataWriter<Foo> writer(dds::pub::Publisher(participant), topic);
    // Create two readers that will match with the above writer
    dds::sub::DataReader<Foo> reader1(dds::sub::Subscriber(participant), topic);
    dds::sub::DataReader<Foo> reader2(dds::sub::Subscriber(participant), topic);
    // Get a list of the InstanceHandles corresponding to the DataWriter's 
    // matched subscriptions.
    dds::core::InstanceHandleSeq subscription_handles1 = 
        dds::pub::matched_subscriptions(writer);
    // matched_subscriptions also takes in an iterator range and will return
    // an InstanceHandle iterator to the end of the InstanceHandleSeq
    dds::core::InstanceHandleSeq subscription_handles2(2, dds::core::null);
    dds::core::InstanceHandleSeq::iterator last = 
        dds::pub::matched_subscriptions(
           writer, 
           subscription_handles2.begin(), 
           subscription_handles2.end());
    // now last == subscription_handles2.end()

Use the InstanceHandles obtained from matched_subscriptions() to get the SubscriptionBuiltinTopicData for the subscriptions

	    dds::topic::SubscriptionBuiltinTopicData subscription_data =
        dds::pub::matched_subscription_data(writer, subscription_handles1[0]);

Subscriber Use Cases

The following #includes are needed for the examples on this page

#include <dds/sub/ddssub.hpp>
#include <dds/domain/ddsdomain.hpp>
#include <dds/core/ddscore.hpp>

Setting up a subscriber

Set up participant
Create a Subscriber with each of the available constructors

// Create the participant
dds::domain::DomainParticipant participant(0);
// When not explicitly provided to the constructor, the 
// created Subscriber's QoS is default, has a NULL listener, and a status mask
// dds::core::status::StatusMask::all()
// Creating a Subscriber
dds::sub::Subscriber subscriber1(participant);    
// Creating a Subscriber with modified QoS
dds::sub::qos::SubscriberQos qos;
qos << dds::core::policy::Presentation::GroupAccessScope(true, false);
dds::sub::Subscriber subscriber2(participant, qos);
// Creating a Subscriber with modified QoS and a listener
ExampleSubscriberListener *listener = new ExampleSubscriberListener;
dds::sub::Subscriber subscriber3(participant, qos, listener);
// Creating a Subscriber with modified QoS, a listener, and a sample_rejected() 
// status mask
dds::sub::Subscriber subscriber4(
   participant, qos, listener, dds::core::status::StatusMask::sample_rejected());
// Creating an entity with a listener automatically retains the entity, so 
// we must either explicitly close the Subscriber...
subscriber3.close();

// ... or set the listener to null
subscriber4.listener(NULL, dds::core::status::StatusMask::none());

Looking up Subscribers

Lookup Subscribers

void howto_lookup_subscribers()
{
    dds::domain::DomainParticipant participant(0);
    // Create three Subscribers and retain them
    create_and_retain_subscriber(participant);
    create_and_retain_subscriber(participant);
    create_and_retain_subscriber(participant);
    // We don't have any references to the created Subscribers but we can look 
    // them up using the participant that they were created with
    std::vector<dds::sub::Subscriber> subscribers1;
    // subscriber_count will equal 3 and subscribers1 will hold a reference to 
    // each of the three subscribers that were created
    int subscriber_count = 
        rti::sub::find_subscribers(participant, std::back_inserter(subscribers1));
    // rti::sub::subscribers also takes a forward iterator and a max number of
    // subscribers to look up
    std::vector<dds::sub::Subscriber> subscribers2(2, dds::sub::Subscriber(dds::core::null));
    // subscriber_count will equal 2 because we're only asking for 2
    subscriber_count = 
        rti::sub::find_subscribers(participant, subscribers2.begin(), 2);
    // We retained the subscribers so we must close them now
    for (std::vector<dds::sub::Subscriber>::iterator it = subscribers1.begin();
          it != subscribers1.end(); 
          it++) {
        (*it).close();
    }
}
void create_and_retain_subscriber(dds::domain::DomainParticipant &participant)
{
    dds::sub::Subscriber subscriber(participant);
    subscriber.retain();
}
  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值