Skip to content

Commit

Permalink
DDS DataWriter: create, delete and write (eProsima#1156)
Browse files Browse the repository at this point in the history
* Refs #8020 Replace WriterQos by DataWriterQos on create_publisher method

Signed-off-by: Laura Martin <[email protected]>

* Refs #8020 create_datawriter standard implementation

Signed-off-by: Laura Martin <[email protected]>

* Refs #8020 Fix examples and tests

Signed-off-by: Laura Martin <[email protected]>

* Refs #8020 Add unittest for create and delete datawriter functions

Signed-off-by: Laura Martin <[email protected]>

* Refs #8088 Added topic reference and dereference calls in create and delete datawriter

Signed-off-by: Laura Martin <[email protected]>

* Fix examples and tests

Signed-off-by: Laura Martin <[email protected]>

* Fix compilation error

Signed-off-by: Laura Martin <[email protected]>

* Apply suggested changes and fix warnings

Signed-off-by: Laura Martin <[email protected]>

* Fix warnings and add datawriter to DeleteTopicInUse unittest

Signed-off-by: Laura Martin <[email protected]>

* Refs #8020 Move listener methods to DataXXXListener

This should resolve the warnings on MAC and make the class hierarchy
compliant with the standard

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

* resolve warnings on Windows

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

* Refs #8020 Change PIM write to follow the standard

Signed-off-by: Laura Martin <[email protected]>

* Refs #8020 Add write preconditions unittest

Signed-off-by: Laura Martin <[email protected]>

* fix warnings

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

Co-authored-by: Iker Luengo <[email protected]>
  • Loading branch information
lauramg15 and IkerLuengo authored Apr 20, 2020
1 parent f54bb8c commit bb60dc3
Show file tree
Hide file tree
Showing 36 changed files with 789 additions and 543 deletions.
33 changes: 23 additions & 10 deletions examples/C++/DDS/DynamicHelloWorldExample/HelloWorldPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,28 @@ bool HelloWorldPublisher::init()
}

//REGISTER THE TYPE
m_type.get()->auto_fill_type_information(false);
m_type.get()->auto_fill_type_object(true);

mp_participant->register_type(m_type);

//CREATE THE PUBLISHER
//PublisherQos qos;
eprosima::fastrtps::TopicAttributes topic_attr;
topic_attr.topicKind = eprosima::fastrtps::rtps::NO_KEY;
topic_attr.topicDataType = "HelloWorld";
topic_attr.topicName = "DDSDynHelloWorldTopic";
topic_attr.auto_fill_type_object = true; // Share the type with readers.
topic_attr.auto_fill_type_information = false;
DataWriterQos qos;
qos.reliability().kind = RELIABLE_RELIABILITY_QOS;
mp_publisher = mp_participant->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);

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

topic_ = mp_participant->create_topic("DDSDynHelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);

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

// CREATE THE WRITER
writer_ = mp_publisher->create_datawriter(topic_attr, qos, &m_listener);
writer_ = mp_publisher->create_datawriter(topic_, DATAWRITER_QOS_DEFAULT, &m_listener);

if (writer_ == nullptr)
{
Expand All @@ -113,6 +114,18 @@ bool HelloWorldPublisher::init()

HelloWorldPublisher::~HelloWorldPublisher()
{
if (writer_ != nullptr)
{
mp_publisher->delete_datawriter(writer_);
}
if (mp_publisher != nullptr)
{
mp_participant->delete_publisher(mp_publisher);
}
if (topic_ != nullptr)
{
mp_participant->delete_topic(topic_);
}
DomainParticipantFactory::get_instance()->delete_participant(mp_participant);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class HelloWorldPublisher

eprosima::fastdds::dds::Publisher* mp_publisher;

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

eprosima::fastdds::dds::DataWriter* writer_;

bool stop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastrtps/subscriber/SampleInfo.h>
#include <fastrtps/rtps/common/Types.h>

Expand Down Expand Up @@ -76,8 +75,7 @@ class HelloWorldSubscriber
public:

class SubListener
: public eprosima::fastdds::dds::DataReaderListener
, public eprosima::fastdds::dds::DomainParticipantListener
: public eprosima::fastdds::dds::DomainParticipantListener
{
public:

Expand Down
29 changes: 22 additions & 7 deletions examples/C++/DDS/HelloWorldExample/HelloWorldPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using namespace eprosima::fastdds::dds;
HelloWorldPublisher::HelloWorldPublisher()
: participant_(nullptr)
, publisher_(nullptr)
, topic_(nullptr)
, writer_(nullptr)
, type_(new HelloWorldPubSubType())
{
}
Expand All @@ -54,31 +56,44 @@ bool HelloWorldPublisher::init()
type_.register_type(participant_, type_->getName());

//CREATE THE PUBLISHER
eprosima::fastrtps::TopicAttributes topic_attr;
topic_attr.topicDataType = "HelloWorld";
topic_attr.topicName = "HelloWorldTopic";
DataWriterQos qos;
qos.reliability().kind = RELIABLE_RELIABILITY_QOS;
publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);

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

topic_ = participant_->create_topic("HelloWorldTopic", "HelloWorld", TOPIC_QOS_DEFAULT);

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

// CREATE THE WRITER
writer_ = publisher_->create_datawriter(topic_attr, qos, &listener_);
writer_ = publisher_->create_datawriter(topic_, DATAWRITER_QOS_DEFAULT, &listener_);

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

return true;
}

HelloWorldPublisher::~HelloWorldPublisher()
{
if (writer_ != nullptr)
{
publisher_->delete_datawriter(writer_);
}
if (publisher_ != nullptr)
{
participant_->delete_publisher(publisher_);
}
if (topic_ != nullptr)
{
participant_->delete_topic(topic_);
}
DomainParticipantFactory::get_instance()->delete_participant(participant_);
}

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

eprosima::fastdds::dds::Publisher* publisher_;

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

eprosima::fastdds::dds::DataWriter* writer_;

bool stop_;
Expand Down
44 changes: 29 additions & 15 deletions examples/C++/DDS/TypeLookupService/TypeLookupPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,34 @@ bool TypeLookupPublisher::init()
}

//REGISTER THE TYPE
m_type.get()->auto_fill_type_information(true);
m_type.get()->auto_fill_type_object(false);
mp_participant->register_type(m_type);

//CREATE THE PUBLISHER
TopicAttributes topic_attr;
topic_attr.topicKind = NO_KEY;
topic_attr.topicDataType = "TypeLookup";
topic_attr.topicName = "TypeLookupTopic";
topic_attr.historyQos.kind = eprosima::fastdds::dds::KEEP_LAST_HISTORY_QOS;
topic_attr.historyQos.depth = 30;
topic_attr.resourceLimitsQos.max_samples = 50;
topic_attr.resourceLimitsQos.allocated_samples = 20;
topic_attr.auto_fill_type_object = false;
topic_attr.auto_fill_type_information = true; // Share the type with readers.
DataWriterQos qos;
qos.reliable_writer_data().times.heartbeatPeriod.seconds = 2;
qos.reliable_writer_data().times.heartbeatPeriod.nanosec = 200 * 1000 * 1000;
qos.reliability().kind = eprosima::fastdds::dds::RELIABLE_RELIABILITY_QOS;
mp_publisher = mp_participant->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);

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

topic_ = mp_participant->create_topic("TypeLookupTopic", "TypeLookup", TOPIC_QOS_DEFAULT);

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

// CREATE THE WRITER
writer_ = mp_publisher->create_datawriter(topic_attr, qos, &m_listener);
DataWriterQos wqos;
wqos.history().kind = eprosima::fastdds::dds::KEEP_LAST_HISTORY_QOS;
wqos.history().depth = 30;
wqos.resource_limits().max_samples = 50;
wqos.resource_limits().allocated_samples = 20;
wqos.reliable_writer_data().times.heartbeatPeriod.seconds = 2;
wqos.reliable_writer_data().times.heartbeatPeriod.nanosec = 200 * 1000 * 1000;
writer_ = mp_publisher->create_datawriter(topic_, wqos, &m_listener);

if (writer_ == nullptr)
{
Expand All @@ -111,6 +113,18 @@ bool TypeLookupPublisher::init()

TypeLookupPublisher::~TypeLookupPublisher()
{
if (writer_ != nullptr)
{
mp_publisher->delete_datawriter(writer_);
}
if (mp_publisher != nullptr)
{
mp_participant->delete_publisher(mp_publisher);
}
if (topic_ != nullptr)
{
mp_participant->delete_topic(topic_);
}
DomainParticipantFactory::get_instance()->delete_participant(mp_participant);
}

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

eprosima::fastdds::dds::Publisher* mp_publisher;

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

eprosima::fastdds::dds::DataWriter* writer_;

bool stop;
Expand Down
4 changes: 1 addition & 3 deletions examples/C++/DDS/TypeLookupService/TypeLookupSubscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <fastdds/dds/domain/DomainParticipantListener.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastrtps/subscriber/SampleInfo.h>
#include <fastrtps/rtps/common/Types.h>

Expand Down Expand Up @@ -76,8 +75,7 @@ class TypeLookupSubscriber
public:

class SubListener
: public eprosima::fastdds::dds::DataReaderListener
, public eprosima::fastdds::dds::DomainParticipantListener
: public eprosima::fastdds::dds::DomainParticipantListener
{
public:

Expand Down
6 changes: 3 additions & 3 deletions include/fastdds/dds/core/policy/QosPolicies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public:
* @param data data to copy in the newly created object \
*/ \
RTPS_DllAPI TClassName( \
const TClassName& data) = default; \
const TClassName &data) = default; \
\
/** \
* Construct from underlying collection type. \
Expand All @@ -759,7 +759,7 @@ public:
* @param data data to copy in the newly created object \
*/ \
RTPS_DllAPI TClassName( \
const collection_type& data) \
const collection_type &data) \
: GenericDataQosPolicy(TPid, data) \
{ \
} \
Expand All @@ -776,7 +776,7 @@ public:
* @return reference to the current object. \
*/ \
TClassName& operator =( \
const TClassName& b) = default; \
const TClassName &b) = default; \
\
};

Expand Down
17 changes: 14 additions & 3 deletions include/fastdds/dds/domain/DomainParticipantListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <fastdds/rtps/participant/ParticipantDiscoveryInfo.h>
#include <fastdds/rtps/reader/ReaderDiscoveryInfo.h>
#include <fastdds/rtps/writer/WriterDiscoveryInfo.h>
#include <fastdds/dds/publisher/PublisherListener.hpp>
#include <fastdds/dds/subscriber/SubscriberListener.hpp>
#include <fastdds/dds/topic/TopicListener.hpp>

#include <fastrtps/types/TypeIdentifier.h>
#include <fastrtps/types/TypeObject.h>
Expand All @@ -38,13 +41,20 @@ class DomainParticipant;
* Class DomainParticipantListener, overrides behaviour towards certain events.
* @ingroup FASTDDS_MODULE
*/
class DomainParticipantListener
class DomainParticipantListener :
public PublisherListener,
public SubscriberListener,
public TopicListener
{
public:

DomainParticipantListener() {}
DomainParticipantListener()
{
}

virtual ~DomainParticipantListener() {}
virtual ~DomainParticipantListener()
{
}

/*!
* This method is called when a new Participant is discovered, or a previously discovered participant changes
Expand All @@ -66,6 +76,7 @@ class DomainParticipantListener
{
(void)participant, (void)info;
}

#endif

/*!
Expand Down
Loading

0 comments on commit bb60dc3

Please sign in to comment.