Skip to content

Commit

Permalink
DDS create_datareader (eProsima#1154)
Browse files Browse the repository at this point in the history
* create_datareader unit test on PIM

Signed-off-by: Iker Luengo <[email protected]>

* adapt implementation to PIM

Signed-off-by: Iker Luengo <[email protected]>

* adapt tests and examples

Signed-off-by: Iker Luengo <[email protected]>

* create_datareader unit test on PSM

Signed-off-by: Iker Luengo <[email protected]>

* create_datareader implementation on PSM

Signed-off-by: Iker Luengo <[email protected]>

* Remove attributes from DataReader

Signed-off-by: Iker Luengo <[email protected]>

* reference count on Topic

Signed-off-by: Iker Luengo <[email protected]>

* correct linker error on Windows

Signed-off-by: Iker Luengo <[email protected]>

* Apply suggested changes

Signed-off-by: Iker Luengo <[email protected]>
  • Loading branch information
IkerLuengo authored Apr 17, 2020
1 parent 13dd968 commit f54bb8c
Show file tree
Hide file tree
Showing 28 changed files with 1,160 additions and 385 deletions.
35 changes: 28 additions & 7 deletions examples/C++/DDS/DynamicHelloWorldExample/HelloWorldSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,26 @@ bool HelloWorldSubscriber::init()
}

// CREATE THE COMMON READER ATTRIBUTES
qos_ = DATAREADER_QOS_DEFAULT;
qos_.reliability().kind = RELIABLE_RELIABILITY_QOS;
topic_.topicKind = eprosima::fastrtps::rtps::NO_KEY;
topic_.topicDataType = "HelloWorld";
topic_.topicName = "DDSDynHelloWorldTopic";

return true;
}

HelloWorldSubscriber::~HelloWorldSubscriber()
{
for (const auto& it : topics_)
{
mp_subscriber->delete_datareader(it.first);
mp_participant->delete_topic(it.second);
}
if (mp_subscriber != nullptr)
{
mp_participant->delete_subscriber(mp_subscriber);
}

DomainParticipantFactory::get_instance()->delete_participant(mp_participant);
topics_.clear();
readers_.clear();
datas_.clear();
}
Expand Down Expand Up @@ -117,15 +126,15 @@ void HelloWorldSubscriber::SubListener::on_data_available(
void HelloWorldSubscriber::SubListener::on_type_discovery(
DomainParticipant*,
const eprosima::fastrtps::rtps::SampleIdentity&,
const eprosima::fastrtps::string_255& topic,
const eprosima::fastrtps::string_255& topic_name,
const eprosima::fastrtps::types::TypeIdentifier*,
const eprosima::fastrtps::types::TypeObject*,
eprosima::fastrtps::types::DynamicType_ptr dyn_type)
{
TypeSupport m_type(new eprosima::fastrtps::types::DynamicPubSubType(dyn_type));
subscriber_->participant()->register_type(m_type);

std::cout << "Discovered type: " << m_type->getName() << " from topic " << topic << std::endl;
std::cout << "Discovered type: " << m_type->getName() << " from topic " << topic_name << std::endl;

if (subscriber_->mp_subscriber == nullptr)
{
Expand All @@ -142,12 +151,24 @@ void HelloWorldSubscriber::SubListener::on_type_discovery(
return;
}
}
subscriber_->topic_.topicDataType = m_type->getName();

//CREATE THE TOPIC
eprosima::fastdds::dds::Topic* topic = subscriber_->mp_participant->create_topic(
"HelloWorldTopic",
m_type->getName(),
TOPIC_QOS_DEFAULT);

if (topic == nullptr)
{
return;
}

DataReader* reader = subscriber_->mp_subscriber->create_datareader(
subscriber_->topic_,
topic,
subscriber_->qos_,
&subscriber_->m_listener);

subscriber_->topics_[reader] = topic;
subscriber_->readers_[reader] = dyn_type;
eprosima::fastrtps::types::DynamicData_ptr data(
eprosima::fastrtps::types::DynamicDataFactory::get_instance()->create_data(dyn_type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class HelloWorldSubscriber

eprosima::fastdds::dds::Subscriber* mp_subscriber;

std::map<eprosima::fastdds::dds::DataReader*, eprosima::fastdds::dds::Topic*> topics_;

std::map<eprosima::fastdds::dds::DataReader*, eprosima::fastrtps::types::DynamicType_ptr> readers_;

std::map<eprosima::fastdds::dds::DataReader*, eprosima::fastrtps::types::DynamicData_ptr> datas_;
Expand All @@ -69,8 +71,6 @@ class HelloWorldSubscriber

eprosima::fastdds::dds::DataReaderQos qos_;

eprosima::fastrtps::TopicAttributes topic_;

std::mutex mutex_;

public:
Expand Down
32 changes: 27 additions & 5 deletions examples/C++/DDS/HelloWorldExample/HelloWorldSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ using namespace eprosima::fastdds::dds;
HelloWorldSubscriber::HelloWorldSubscriber()
: participant_(nullptr)
, subscriber_(nullptr)
, topic_(nullptr)
, reader_(nullptr)
, type_(new HelloWorldPubSubType())
{
}
Expand All @@ -56,13 +58,21 @@ bool HelloWorldSubscriber::init()
return false;
}

//CREATE THE TOPIC
topic_ = participant_->create_topic(
"HelloWorldTopic",
"HelloWorld",
TOPIC_QOS_DEFAULT);

if (topic_ == nullptr)
{
return false;
}

// CREATE THE READER
DataReaderQos rqos;
DataReaderQos rqos = DATAREADER_QOS_DEFAULT;
rqos.reliability().kind = RELIABLE_RELIABILITY_QOS;
eprosima::fastrtps::TopicAttributes topic_att;
topic_att.topicDataType = "HelloWorld";
topic_att.topicName = "HelloWorldTopic";
reader_ = subscriber_->create_datareader(topic_att, rqos, &listener_);
reader_ = subscriber_->create_datareader(topic_, rqos, &listener_);

if (reader_ == nullptr)
{
Expand All @@ -74,6 +84,18 @@ bool HelloWorldSubscriber::init()

HelloWorldSubscriber::~HelloWorldSubscriber()
{
if (reader_ != nullptr)
{
subscriber_->delete_datareader(reader_);
}
if (topic_ != nullptr)
{
participant_->delete_topic(topic_);
}
if (subscriber_ != nullptr)
{
participant_->delete_subscriber(subscriber_);
}
DomainParticipantFactory::get_instance()->delete_participant(participant_);
}

Expand Down
2 changes: 2 additions & 0 deletions examples/C++/DDS/HelloWorldExample/HelloWorldSubscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class HelloWorldSubscriber

eprosima::fastdds::dds::Subscriber* subscriber_;

eprosima::fastdds::dds::Topic* topic_;

eprosima::fastdds::dds::DataReader* reader_;

eprosima::fastdds::dds::TypeSupport type_;
Expand Down
39 changes: 30 additions & 9 deletions examples/C++/DDS/TypeLookupService/TypeLookupSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,31 @@ bool TypeLookupSubscriber::init()
}

// CREATE THE COMMON READER ATTRIBUTES
qos_ = DATAREADER_QOS_DEFAULT;
qos_.durability().kind = eprosima::fastdds::dds::TRANSIENT_LOCAL_DURABILITY_QOS;
qos_.reliability().kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS;
topic_.topicKind = NO_KEY;
topic_.topicDataType = "TypeLookup";
topic_.topicName = "TypeLookupTopic";
topic_.historyQos.kind = eprosima::fastdds::dds::KEEP_LAST_HISTORY_QOS;
topic_.historyQos.depth = 30;
topic_.resourceLimitsQos.max_samples = 50;
topic_.resourceLimitsQos.allocated_samples = 20;
qos_.history().kind = eprosima::fastdds::dds::KEEP_LAST_HISTORY_QOS;
qos_.history().depth = 30;
qos_.resource_limits().max_samples = 50;
qos_.resource_limits().allocated_samples = 20;

return true;
}

TypeLookupSubscriber::~TypeLookupSubscriber()
{
for (const auto& it : topics_)
{
mp_subscriber->delete_datareader(it.first);
mp_participant->delete_topic(it.second);
}
if (mp_subscriber != nullptr)
{
mp_participant->delete_subscriber(mp_subscriber);
}

DomainParticipantFactory::get_instance()->delete_participant(mp_participant);
topics_.clear();
readers_.clear();
datas_.clear();
}
Expand Down Expand Up @@ -169,9 +178,20 @@ void TypeLookupSubscriber::SubListener::on_type_information_received(
return;
}
}
subscriber_->topic_.topicDataType = name;

//CREATE THE TOPIC
eprosima::fastdds::dds::Topic* topic = subscriber_->mp_participant->create_topic(
"TypeLookupTopic",
name,
TOPIC_QOS_DEFAULT);

if (topic == nullptr)
{
return;
}

DataReader* reader = subscriber_->mp_subscriber->create_datareader(
subscriber_->topic_,
topic,
subscriber_->qos_,
&subscriber_->m_listener);

Expand Down Expand Up @@ -207,6 +227,7 @@ void TypeLookupSubscriber::SubListener::on_type_information_received(
}
else
{
subscriber_->topics_[reader] = topic;
subscriber_->readers_[reader] = type;
types::DynamicData_ptr data(types::DynamicDataFactory::get_instance()->create_data(type));
subscriber_->datas_[reader] = data;
Expand Down
4 changes: 2 additions & 2 deletions examples/C++/DDS/TypeLookupService/TypeLookupSubscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class TypeLookupSubscriber

eprosima::fastdds::dds::Subscriber* mp_subscriber;

std::map<eprosima::fastdds::dds::DataReader*, eprosima::fastdds::dds::Topic*> topics_;

std::map<eprosima::fastdds::dds::DataReader*, eprosima::fastrtps::types::DynamicType_ptr> readers_;

std::map<eprosima::fastdds::dds::DataReader*, eprosima::fastrtps::types::DynamicData_ptr> datas_;
Expand All @@ -69,8 +71,6 @@ class TypeLookupSubscriber

eprosima::fastdds::dds::DataReaderQos qos_;

eprosima::fastrtps::TopicAttributes topic_;

std::mutex mutex_;

public:
Expand Down
Loading

0 comments on commit f54bb8c

Please sign in to comment.