Continues migration with DDS, Common
This commit is contained in:
1 parent
9b15cee841
commit
fa48db111e
668 files changed
+111841
-2
No files matched your search
Executable
+36
@@ -0,0 +1,36 @@
|
||||
#
|
||||
# $Id$
|
||||
# $HeadURL$
|
||||
# $Author$
|
||||
#
|
||||
|
||||
SET(SRCS
|
||||
dds_connector.cpp
|
||||
dds_connector.h
|
||||
publisher.cpp
|
||||
publisher.h
|
||||
pub_sub_base.cpp
|
||||
pub_sub_base.h
|
||||
pub_sub_base_p.h
|
||||
subscriber.cpp
|
||||
subscriber.h
|
||||
)
|
||||
|
||||
NGL_ADD_LIBRARY2(AAR SV WS HLA ddsconnector SHARED ${SRCS})
|
||||
NGL_TARGET_INCLUDE_DIRECTORIES(
|
||||
INTERFACE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../../
|
||||
)
|
||||
NGL_TARGET_LINK_LIBRARIES(ddsconnector
|
||||
PRIVATE
|
||||
NGLLogger
|
||||
PUBLIC
|
||||
RTI::nddsc
|
||||
RTI::nddscore
|
||||
RTI::nddscpp2
|
||||
ddsQoS
|
||||
ddsdtc
|
||||
ddsdts
|
||||
ProjectConfig
|
||||
Qt5::Core
|
||||
)
|
||||
Executable
+283
@@ -0,0 +1,283 @@
|
||||
|
||||
|
||||
|
||||
#include "dds_connector.h"
|
||||
#include "publisher.h"
|
||||
#include "subscriber.h"
|
||||
|
||||
#include <NGLLogger/NGLLoggerBootstrap.h>
|
||||
#include <DDS/QosProfile/DDS_DefaultQos.h>
|
||||
#include <DDS/Generic/wait_set_handler.h>
|
||||
#include <QCoreApplication>
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
|
||||
DdsConnector::DdsConnector(int domainId,
|
||||
const dds::domain::DomainParticipant &domainParticipant,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId,
|
||||
int exerciseId,
|
||||
DDS::QosProfile::ConnectionType connectionType,
|
||||
const QUrl &tcpConnectionUrl)
|
||||
: m_domainId(domainId)
|
||||
, m_defaultPartitionType(defaultPartitionType)
|
||||
, m_partitionId(partitionId)
|
||||
, m_exerciseId(exerciseId)
|
||||
, m_tcpConnectionUrl(tcpConnectionUrl)
|
||||
, m_connectionType(tcpConnectionUrl.isEmpty() ? connectionType : DDS::QosProfile::ConnectionType::Tcp)
|
||||
, m_domainParticipant(domainParticipant)
|
||||
, m_publisher(nullptr)
|
||||
, m_subscriber(nullptr)
|
||||
, m_wsHandler(nullptr)
|
||||
, m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("Common.DDSConnector"))
|
||||
{
|
||||
initInternal();
|
||||
DEBUG_LOG(m_logger) << "DdsConnector ctor: " << m_domainId << "," << m_partitionId << ","<< m_exerciseId << "," << static_cast<int>(m_defaultPartitionType);
|
||||
}
|
||||
|
||||
DdsConnector::DdsConnector(int domainId,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId, int exerciseId,
|
||||
const QUrl &tcpConnectionUrl)
|
||||
: DdsConnector(domainId,
|
||||
nullptr,
|
||||
defaultPartitionType,
|
||||
partitionId,
|
||||
exerciseId,
|
||||
DDS::QosProfile::ConnectionType::Server,
|
||||
tcpConnectionUrl)
|
||||
{}
|
||||
|
||||
DdsConnector::DdsConnector(const dds::domain::DomainParticipant &domainParticipant,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId, int exerciseId,
|
||||
const QUrl &tcpConnectionUrl)
|
||||
: DdsConnector(domainParticipant.domain_id(),
|
||||
domainParticipant,
|
||||
defaultPartitionType,
|
||||
partitionId,
|
||||
exerciseId,
|
||||
DDS::QosProfile::ConnectionType::Server,
|
||||
tcpConnectionUrl)
|
||||
{}
|
||||
|
||||
|
||||
DdsConnector::DdsConnector(const dds::domain::DomainParticipant &domainParticipant,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
DDS::QosProfile::ConnectionType connectionType,
|
||||
int partitionId,
|
||||
int exerciseId
|
||||
)
|
||||
: DdsConnector(domainParticipant.domain_id(),
|
||||
domainParticipant,
|
||||
defaultPartitionType,
|
||||
partitionId,
|
||||
exerciseId,
|
||||
connectionType,
|
||||
QUrl())
|
||||
{}
|
||||
|
||||
DdsConnector::DdsConnector(int domainId,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId, int exerciseId,
|
||||
DDS::QosProfile::ConnectionType connectionType)
|
||||
: DdsConnector(domainId,
|
||||
nullptr,
|
||||
defaultPartitionType,
|
||||
partitionId,
|
||||
exerciseId,
|
||||
connectionType,
|
||||
QUrl())
|
||||
{
|
||||
Q_ASSERT(connectionType != DDS::QosProfile::ConnectionType::Tcp);
|
||||
}
|
||||
|
||||
|
||||
DdsConnector::~DdsConnector()
|
||||
{
|
||||
delete m_subscriber;
|
||||
delete m_publisher;
|
||||
delete m_wsHandler;
|
||||
}
|
||||
|
||||
bool DdsConnector::init(const QStringList &qosFileList)
|
||||
{
|
||||
return DDS::QosProfile::setDefault(qosFileList);
|
||||
}
|
||||
|
||||
QUrl DdsConnector::getTcpConnectionUrl() const
|
||||
{
|
||||
DEBUG_LOG(m_logger) << this << " gettcpConnectionUrl: " << m_tcpConnectionUrl.toString();
|
||||
return m_tcpConnectionUrl;
|
||||
}
|
||||
|
||||
dds::pub::Publisher DdsConnector::getDdsPublisher() const
|
||||
{
|
||||
return m_publisher ? m_publisher->getDdsPublisher() : nullptr;
|
||||
}
|
||||
|
||||
dds::sub::Subscriber DdsConnector::getDdsSubscriber() const
|
||||
{
|
||||
return m_subscriber ? m_subscriber->getDdsSubscriber() : nullptr;
|
||||
}
|
||||
|
||||
DDSGeneric::SubscriberListener *DdsConnector::getSubscriberListener()
|
||||
{
|
||||
return m_subscriber ? m_subscriber->getSubscriberListener() : nullptr;
|
||||
}
|
||||
|
||||
void DdsConnector::initPublisherSubscriber()
|
||||
{
|
||||
initSubscriber();
|
||||
initPublisher();
|
||||
}
|
||||
|
||||
void DdsConnector::initPublisher()
|
||||
{
|
||||
if (m_publisher)
|
||||
return;
|
||||
initDomainParticipant();
|
||||
|
||||
setPublisher(new DDSConnector::Publisher(m_domainParticipant, getDefaultPartition()));
|
||||
}
|
||||
|
||||
void DdsConnector::initSubscriber()
|
||||
{
|
||||
if (m_subscriber)
|
||||
return;
|
||||
initDomainParticipant();
|
||||
|
||||
setSubscriber(new DDSConnector::Subscriber(m_domainParticipant, getDefaultPartition()));
|
||||
}
|
||||
|
||||
void DdsConnector::initDomainParticipant()
|
||||
{
|
||||
if (m_domainParticipant == nullptr)
|
||||
setDomainParticipant(dds::domain::DomainParticipant(m_domainId,
|
||||
DDS::QosProfile::domainParticipantDefault(m_connectionType, m_tcpConnectionUrl)));
|
||||
}
|
||||
|
||||
|
||||
std::string DdsConnector::getDefaultPartition() const
|
||||
{
|
||||
return getPartitionString(m_defaultPartitionType, m_partitionId);
|
||||
}
|
||||
|
||||
std::string DdsConnector::getPartitionString(IDL::PartitionType pt, int partitionId)
|
||||
{
|
||||
switch (pt)
|
||||
{
|
||||
case IDL::PartitionType::Any:
|
||||
return "*";
|
||||
case IDL::PartitionType::Global:
|
||||
return IDL::exercisePartition(0);
|
||||
case IDL::PartitionType::Exercise:
|
||||
case IDL::PartitionType::AllExercises:
|
||||
return IDL::exercisePartition(partitionId);
|
||||
case IDL::PartitionType::Replay:
|
||||
return IDL::replayPartition(partitionId);
|
||||
}
|
||||
|
||||
NGL::INGLLogger &m_logger = NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("Common.DDSConnector");
|
||||
ERROR_LOG(m_logger) << "Invalid partition type: " << static_cast<int>(pt);
|
||||
return std::string();
|
||||
}
|
||||
|
||||
void DdsConnector::setDomainParticipant(const dds::domain::DomainParticipant &participant)
|
||||
{
|
||||
m_domainParticipant = participant;
|
||||
if (m_domainParticipant == nullptr)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
dds::domain::qos::DomainParticipantQos dpQos(m_domainParticipant.qos());
|
||||
DDS::QosProfile::print(dpQos, m_logger);
|
||||
rti::core::policy::EntityName entityName;
|
||||
entityName.name(qAppName().toStdString() + (" - DP ") + std::to_string(m_domainId));
|
||||
dpQos << entityName;
|
||||
m_domainParticipant.qos(dpQos);
|
||||
m_domainParticipant->default_datareader_qos(DDS::QosProfile::dataReaderDefault(m_connectionType));
|
||||
m_domainParticipant.enable();
|
||||
}
|
||||
catch (const dds::core::Exception &ex)
|
||||
{
|
||||
WARNING_LOG(m_logger) << "Could not enable DomainParticipant: " << ex.what();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void DdsConnector::setPublisher(Publisher *publisher)
|
||||
{
|
||||
if (m_publisher && publisher != m_publisher)
|
||||
{
|
||||
WARNING_LOG(m_logger) << "Trying to set publisher but there is already a publisher set - overwriting!";
|
||||
delete m_publisher;
|
||||
}
|
||||
m_publisher = publisher;
|
||||
if (m_publisher)
|
||||
DDS::QosProfile::print(m_publisher->getDdsPublisher().default_datawriter_qos(), m_logger);
|
||||
}
|
||||
|
||||
void DdsConnector::enableBatchedWriter(bool bEnable)
|
||||
{
|
||||
if (m_publisher)
|
||||
m_publisher->enableBatchedWriter(bEnable);
|
||||
}
|
||||
|
||||
|
||||
void DdsConnector::setSubscriber(Subscriber *subscriber)
|
||||
{
|
||||
if (m_subscriber && subscriber != m_subscriber)
|
||||
{
|
||||
WARNING_LOG(m_logger) << "Trying to set subscriber but there is already a subscriber set - overwriting!";
|
||||
delete m_subscriber;
|
||||
}
|
||||
m_subscriber = subscriber;
|
||||
if (m_subscriber)
|
||||
DDS::QosProfile::print(m_subscriber->getDdsSubscriber().default_datareader_qos(), m_logger);
|
||||
}
|
||||
|
||||
void DdsConnector::initInternal()
|
||||
{
|
||||
if (m_tcpConnectionUrl.isValid())
|
||||
{
|
||||
m_connectionType = DDS::QosProfile::ConnectionType::Tcp;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tcpConnectionUrl.clear();
|
||||
if (m_connectionType == DDS::QosProfile::ConnectionType::Tcp)
|
||||
m_connectionType = DDS::QosProfile::ConnectionType::ShmUdp;
|
||||
}
|
||||
|
||||
if (m_defaultPartitionType == IDL::PartitionType::Any ||
|
||||
m_defaultPartitionType == IDL::PartitionType::AllExercises)
|
||||
m_partitionId = -1;
|
||||
if (m_defaultPartitionType == IDL::PartitionType::Exercise && m_partitionId < 0)
|
||||
WARNING_LOG(m_logger) << "Want to subscribe to an exercise, but no exercise Id is given!";
|
||||
if (m_defaultPartitionType == IDL::PartitionType::Global)
|
||||
{
|
||||
m_partitionId = 0;
|
||||
m_exerciseId = 0;
|
||||
}
|
||||
INFO_LOG(m_logger) << "DDSConnector(" << m_domainId << ") for partition: " << m_partitionId
|
||||
<< " with partition type " << static_cast<int>(m_defaultPartitionType);
|
||||
|
||||
|
||||
dds::domain::qos::DomainParticipantFactoryQos domainParticipantFactoryQos = dds::domain::DomainParticipant::participant_factory_qos();
|
||||
|
||||
domainParticipantFactoryQos << dds::core::policy::EntityFactory::ManuallyEnable();
|
||||
dds::domain::DomainParticipant::participant_factory_qos(domainParticipantFactoryQos);
|
||||
}
|
||||
|
||||
|
||||
void DdsConnector::createWaitSetHandler()
|
||||
{
|
||||
if (!m_wsHandler)
|
||||
m_wsHandler = new DDSGeneric::WaitSetHandler;
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+214
@@ -0,0 +1,214 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <dds/pub/CoherentSet.hpp>
|
||||
#include <dds/pub/Publisher.hpp>
|
||||
#include <dds/sub/Subscriber.hpp>
|
||||
|
||||
#include <QtCore/QStringList>
|
||||
#include <Common/ProjectConfig/project_defines.h>
|
||||
#include <DDS/DDSConnector/ddsconnector_export.h>
|
||||
#include <DDS/Generic/generic_base.h>
|
||||
#include <QUrl>
|
||||
|
||||
class QObject;
|
||||
namespace DDS
|
||||
{
|
||||
namespace QosProfile
|
||||
{
|
||||
enum class ConnectionType;
|
||||
}
|
||||
}
|
||||
namespace NGL
|
||||
{
|
||||
class INGLLogger;
|
||||
}
|
||||
namespace DDSGeneric
|
||||
{
|
||||
class WaitSetHandler;
|
||||
class SubscriberListener;
|
||||
}
|
||||
namespace DDSConnector
|
||||
{
|
||||
class Publisher;
|
||||
class Subscriber;
|
||||
|
||||
class DDSCONNECTOR_EXPORT DdsConnector
|
||||
{
|
||||
DdsConnector(int domainId,
|
||||
const dds::domain::DomainParticipant &domainParticipant,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId,
|
||||
int exerciseId,
|
||||
DDS::QosProfile::ConnectionType connectionType,
|
||||
const QUrl &tcpConnectionUrl);
|
||||
public:
|
||||
DdsConnector(const DdsConnector &) = delete;
|
||||
DdsConnector &operator=(const DdsConnector &) = delete;
|
||||
|
||||
|
||||
|
||||
DdsConnector(int domainId,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId = 0,
|
||||
int exerciseId = 0,
|
||||
const QUrl &tcpConnectionUrl = QUrl());
|
||||
|
||||
|
||||
DdsConnector(const dds::domain::DomainParticipant &domainParticipant,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId = 0,
|
||||
int exerciseId = 0,
|
||||
const QUrl &tcpConnectionUrl = QUrl());
|
||||
|
||||
DdsConnector(const dds::domain::DomainParticipant &domainParticipant,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
DDS::QosProfile::ConnectionType connectionType,
|
||||
int partitionId = 0,
|
||||
int exerciseId = 0);
|
||||
|
||||
DdsConnector(int domainId,
|
||||
IDL::PartitionType defaultPartitionType,
|
||||
int partitionId,
|
||||
int exerciseId,
|
||||
DDS::QosProfile::ConnectionType connectionType);
|
||||
|
||||
virtual ~DdsConnector();
|
||||
|
||||
|
||||
|
||||
virtual bool init(const QStringList &qosFileList = QStringList());
|
||||
|
||||
dds::domain::DomainParticipant getDomainParticipant() const { return m_domainParticipant; }
|
||||
void setDomainParticipant(const dds::domain::DomainParticipant &participant);
|
||||
|
||||
Publisher *getPublisher() { return m_publisher; }
|
||||
dds::pub::Publisher getDdsPublisher() const;
|
||||
void setPublisher(Publisher *publisher);
|
||||
void enableBatchedWriter(bool bEnable);
|
||||
|
||||
Subscriber *getSubscriber() { return m_subscriber; }
|
||||
dds::sub::Subscriber getDdsSubscriber() const;
|
||||
void setSubscriber(Subscriber *subscriber);
|
||||
DDSGeneric::SubscriberListener *getSubscriberListener();
|
||||
|
||||
const DDSGeneric::WaitSetHandler *getWaitSetHandler() const { return m_wsHandler; }
|
||||
|
||||
int getDomainId() const { return m_domainId; }
|
||||
IDL::PartitionType getPartitionType() const { return m_defaultPartitionType; }
|
||||
int getPartitionId() const { return m_partitionId; }
|
||||
int getExerciseId() const { return m_exerciseId; }
|
||||
std::string getDefaultPartition() const;
|
||||
QUrl getTcpConnectionUrl() const;
|
||||
static std::string getPartitionString(IDL::PartitionType pt, int partitionId = -1);
|
||||
|
||||
|
||||
|
||||
void initPublisherSubscriber();
|
||||
void initPublisher();
|
||||
void initSubscriber();
|
||||
void initDomainParticipant();
|
||||
|
||||
template<typename T>
|
||||
T *createDTS(const std::string &topicName = DDS::Helper::getShortTopicName(DDS::topic_type_name<typename T::TopicType>::value()))
|
||||
{
|
||||
if (getDomainParticipant() == nullptr ||
|
||||
getDdsPublisher() == nullptr)
|
||||
return nullptr;
|
||||
T *dts = new T(getDomainParticipant(),
|
||||
getDdsPublisher(),
|
||||
topicName);
|
||||
return dts;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *createDTC(const std::string &topicName = DDS::Helper::getShortTopicName(DDS::topic_type_name<typename T::TopicType>::value()))
|
||||
{
|
||||
if (getDomainParticipant() == nullptr ||
|
||||
getDdsSubscriber() == nullptr)
|
||||
return nullptr;
|
||||
T *dtc = new T(getDomainParticipant(),
|
||||
getDdsSubscriber(),
|
||||
getSubscriberListener(),
|
||||
topicName);
|
||||
return dtc;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *createDTC(const dds::topic::Filter &filter,
|
||||
const std::string &topicName = DDS::Helper::getShortTopicName(DDS::topic_type_name<typename T::TopicType>::value()))
|
||||
{
|
||||
if (getDomainParticipant() == nullptr ||
|
||||
getDdsSubscriber() == nullptr)
|
||||
return nullptr;
|
||||
T *dtc = new T(getDomainParticipant(),
|
||||
getDdsSubscriber(),
|
||||
getSubscriberListener(),
|
||||
filter,
|
||||
topicName);
|
||||
return dtc;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *createFTS()
|
||||
{
|
||||
if (getDomainParticipant() == nullptr ||
|
||||
getDdsPublisher() == nullptr ||
|
||||
getDdsSubscriber() == nullptr)
|
||||
return nullptr;
|
||||
createWaitSetHandler();
|
||||
T *fts = new T(getDomainParticipant(),
|
||||
getDdsPublisher(),
|
||||
getDdsSubscriber(),
|
||||
m_wsHandler);
|
||||
return fts;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T *createFTC(QObject *objToInform)
|
||||
{
|
||||
if (getDomainParticipant() == nullptr ||
|
||||
getDdsPublisher() == nullptr ||
|
||||
getDdsSubscriber() == nullptr)
|
||||
return nullptr;
|
||||
T *ftc = new T(getDomainParticipant(),
|
||||
getDdsPublisher(),
|
||||
getDdsSubscriber(),
|
||||
objToInform,
|
||||
IDL::toStdString(m_defaultPartitionType),
|
||||
m_partitionId);
|
||||
return ftc;
|
||||
}
|
||||
|
||||
private:
|
||||
void initInternal();
|
||||
void createWaitSetHandler();
|
||||
private:
|
||||
int m_domainId;
|
||||
IDL::PartitionType m_defaultPartitionType;
|
||||
int m_partitionId;
|
||||
int m_exerciseId;
|
||||
|
||||
QUrl m_tcpConnectionUrl;
|
||||
DDS::QosProfile::ConnectionType m_connectionType;
|
||||
dds::domain::DomainParticipant m_domainParticipant;
|
||||
DDSConnector::Publisher *m_publisher;
|
||||
DDSConnector::Subscriber *m_subscriber;
|
||||
DDSGeneric::WaitSetHandler *m_wsHandler;
|
||||
NGL::INGLLogger &m_logger;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class CoherentSet
|
||||
{
|
||||
public:
|
||||
explicit CoherentSet(const std::shared_ptr<DdsConnector> &connector)
|
||||
: m_coherentSet(connector->getDdsPublisher())
|
||||
{}
|
||||
private:
|
||||
dds::pub::CoherentSet m_coherentSet;
|
||||
};
|
||||
|
||||
}
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
|
||||
|
||||
|
||||
#include "pub_sub_base.h"
|
||||
|
||||
#include <NGLLogger/NGLLoggerBootstrap.h>
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
PubSubBase::PubSubBase(const dds::domain::DomainParticipant &domainParticipant, const std::string &partition, const char *loggerName)
|
||||
: m_domainParticipant(domainParticipant)
|
||||
, m_partitions({partition})
|
||||
, m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger(loggerName))
|
||||
{}
|
||||
}
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <DDS/DDSConnector/ddsconnector_export.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <dds/domain/DomainParticipant.hpp>
|
||||
|
||||
namespace NGL
|
||||
{
|
||||
class INGLLogger;
|
||||
}
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
class DDSCONNECTOR_EXPORT PubSubBase
|
||||
{
|
||||
protected:
|
||||
PubSubBase(const dds::domain::DomainParticipant &domainParticipant,
|
||||
const std::string &partition,
|
||||
const char *loggerName);
|
||||
public:
|
||||
virtual ~PubSubBase() = default;
|
||||
|
||||
void setDomainParticipant(const dds::domain::DomainParticipant &domainParticipant) { m_domainParticipant = domainParticipant; }
|
||||
const dds::domain::DomainParticipant &getDomainParticipant() const { return m_domainParticipant; }
|
||||
|
||||
protected:
|
||||
template<typename P>
|
||||
void updatePartitionQoS(P &pubSub);
|
||||
|
||||
protected:
|
||||
dds::domain::DomainParticipant m_domainParticipant;
|
||||
std::vector<std::string> m_partitions;
|
||||
NGL::INGLLogger &m_logger;
|
||||
};
|
||||
}
|
||||
Executable
+22
@@ -0,0 +1,22 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
template<typename P>
|
||||
void PubSubBase::updatePartitionQoS(P &pubSub)
|
||||
{
|
||||
if (pubSub == nullptr)
|
||||
return;
|
||||
|
||||
auto publisherQos = pubSub.qos();
|
||||
|
||||
|
||||
dds::core::policy::Partition partitionQos;
|
||||
partitionQos.name( m_partitions );
|
||||
publisherQos << partitionQos;
|
||||
|
||||
pubSub.qos(publisherQos);
|
||||
}
|
||||
}
|
||||
Executable
+106
@@ -0,0 +1,106 @@
|
||||
|
||||
|
||||
#include "publisher.h"
|
||||
#include "pub_sub_base_p.h"
|
||||
|
||||
#include <dds/pub/AnyDataWriter.hpp>
|
||||
#include <DDS/QosProfile/DDS_DefaultQos.h>
|
||||
#include <NGLLogger/INGLLogger.h>
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
|
||||
Publisher::Publisher(const dds::domain::DomainParticipant &domainParticipant, const std::string &partition)
|
||||
: PubSubBase(domainParticipant, partition, "DDSConnector.Publisher")
|
||||
, m_ddsPublisher(nullptr)
|
||||
{
|
||||
Q_ASSERT(domainParticipant != nullptr);
|
||||
initDDS();
|
||||
}
|
||||
|
||||
|
||||
Publisher::~Publisher()
|
||||
{
|
||||
try
|
||||
{
|
||||
deleteDDS();
|
||||
} catch (...)
|
||||
{}
|
||||
}
|
||||
|
||||
void Publisher::initDDS()
|
||||
{
|
||||
if (m_partitions.empty())
|
||||
return;
|
||||
|
||||
|
||||
if (m_logger.isDebugEnabled()) {
|
||||
DEBUG_LOG(m_logger) << "Publisher (DataWriter) Qos: ";
|
||||
dds::core::QosProvider qosProvider = dds::core::QosProvider::Default();
|
||||
DDS::QosProfile::print(qosProvider.datawriter_qos(), m_logger);
|
||||
}
|
||||
|
||||
dds::pub::qos::PublisherQos publisherQos = m_domainParticipant.default_publisher_qos();
|
||||
|
||||
|
||||
rti::core::policy::EntityName entityName;
|
||||
entityName.name(std::string("Pub ") + m_partitions.front());
|
||||
publisherQos << entityName;
|
||||
|
||||
DEBUG_LOG(m_logger) << "Publish data on partition " << m_partitions.front();
|
||||
|
||||
m_ddsPublisher = dds::pub::Publisher(m_domainParticipant, publisherQos);
|
||||
|
||||
enableBatchedWriter(false);
|
||||
|
||||
updatePartitionQoS(m_ddsPublisher);
|
||||
}
|
||||
|
||||
void Publisher::enableBatchedWriter(bool bEnable)
|
||||
{
|
||||
dds::pub::qos::DataWriterQos dwQos = m_ddsPublisher.default_datawriter_qos();
|
||||
auto batch = (bEnable ? rti::core::policy::Batch::Enabled() : rti::core::policy::Batch::Disabled());
|
||||
if (bEnable)
|
||||
{
|
||||
batch.max_data_bytes(64 * 1024);
|
||||
batch.max_flush_delay(dds::core::Duration::from_millisecs(10));
|
||||
}
|
||||
dwQos << batch;
|
||||
m_ddsPublisher.default_datawriter_qos(dwQos);
|
||||
}
|
||||
|
||||
void Publisher::deleteDDS()
|
||||
{
|
||||
qDeleteAll(m_dtsMap);
|
||||
qDeleteAll(m_dynamicTypeMap);
|
||||
m_ddsPublisher = nullptr;
|
||||
}
|
||||
|
||||
bool Publisher::write(const dds::core::xtypes::DynamicData &data, const std::string &topicName)
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "Publish dynamic data for topic (" << topicName << ")";
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
dds::pub::AnyDataWriter* anyWriter = m_dynamicTypeMap.value(topicName);
|
||||
if (anyWriter == nullptr)
|
||||
{
|
||||
dds::topic::Topic<dds::core::xtypes::DynamicData> topic(m_domainParticipant, topicName, data.type());
|
||||
auto writer = dds::pub::DataWriter<dds::core::xtypes::DynamicData>(m_ddsPublisher, topic);
|
||||
anyWriter = new dds::pub::AnyDataWriter(writer);
|
||||
m_dynamicTypeMap.insert(topicName, anyWriter);
|
||||
}
|
||||
anyWriter->get<dds::core::xtypes::DynamicData>().write(data);
|
||||
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
ERROR_LOG(m_logger) << "Failed to publish data for topic (" << topicName << ")";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Executable
+67
@@ -0,0 +1,67 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QMap>
|
||||
#include <NGLLogger/INGLLogger.h>
|
||||
#include <DDS/DTS/generic_dts_impl.h>
|
||||
#include <DDS/Generic/generic_data_writer.h>
|
||||
|
||||
#include "pub_sub_base.h"
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
class DDSCONNECTOR_EXPORT Publisher : public PubSubBase
|
||||
{
|
||||
public:
|
||||
Publisher(const dds::domain::DomainParticipant &domainParticipant, const std::string &partition);
|
||||
~Publisher();
|
||||
|
||||
void enableBatchedWriter(bool bEnable);
|
||||
dds::pub::Publisher getDdsPublisher() const { return m_ddsPublisher; }
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool write(const T &data)
|
||||
{
|
||||
using myDWO = DDSGeneric::GenericDataWriter<T>;
|
||||
using myDTS = DTS::GenericDtsImpl< myDWO >;
|
||||
|
||||
std::string dwoType = DDS::topic_type_name<T>::value();
|
||||
DEBUG_LOG(m_logger) << "dwoType = " << dwoType;
|
||||
|
||||
myDTS *dts = static_cast<myDTS *>(m_dtsMap.value(dwoType));
|
||||
if (dts == nullptr)
|
||||
{
|
||||
dts = new myDTS(m_domainParticipant, m_ddsPublisher);
|
||||
m_dtsMap.insert(dwoType, dts);
|
||||
}
|
||||
|
||||
return dts->write(data);
|
||||
}
|
||||
|
||||
|
||||
bool write(const dds::core::xtypes::DynamicData &data)
|
||||
{
|
||||
return write(data, data.type().name());
|
||||
}
|
||||
|
||||
bool write(const dds::core::xtypes::DynamicData &data, const std::string &topicName);
|
||||
|
||||
|
||||
protected:
|
||||
void initDDS();
|
||||
void deleteDDS();
|
||||
|
||||
protected:
|
||||
dds::pub::Publisher m_ddsPublisher;
|
||||
|
||||
|
||||
QMap<std::string, DTS::Base*> m_dtsMap;
|
||||
|
||||
|
||||
|
||||
QMap<std::string, dds::pub::AnyDataWriter*> m_dynamicTypeMap;
|
||||
};
|
||||
|
||||
}
|
||||
Executable
+74
@@ -0,0 +1,74 @@
|
||||
|
||||
|
||||
|
||||
#include "subscriber.h"
|
||||
#include "pub_sub_base_p.h"
|
||||
|
||||
#include <DDS/QosProfile/DDS_DefaultQos.h>
|
||||
#include <NGLLogger/INGLLogger.h>
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
Subscriber::Subscriber(const dds::domain::DomainParticipant &domainParticipant, const std::string &partition, bool bUseSubscriberListener)
|
||||
: PubSubBase(domainParticipant, partition, "DDSConnector.Subscriber")
|
||||
, m_ddsSubscriber(nullptr)
|
||||
, m_subscriberListener(bUseSubscriberListener ? new DDSGeneric::SubscriberListener : nullptr)
|
||||
, m_subscriberListenerBinder(nullptr)
|
||||
{
|
||||
Q_ASSERT(domainParticipant != nullptr);
|
||||
initDDS();
|
||||
}
|
||||
|
||||
Subscriber::~Subscriber()
|
||||
{
|
||||
try
|
||||
{
|
||||
deleteDDS();
|
||||
} catch (...)
|
||||
{}
|
||||
}
|
||||
|
||||
void Subscriber::initDDS()
|
||||
{
|
||||
if (m_partitions.empty())
|
||||
return;
|
||||
|
||||
|
||||
if (m_logger.isDebugEnabled()) {
|
||||
DEBUG_LOG(m_logger) << "Subscriber (DataReader) Qos: ";
|
||||
dds::core::QosProvider qosProvider = dds::core::QosProvider::Default();
|
||||
DDS::QosProfile::print(qosProvider.datareader_qos(), m_logger);
|
||||
}
|
||||
|
||||
dds::sub::qos::SubscriberQos subscriberQos = m_domainParticipant.default_subscriber_qos();
|
||||
|
||||
|
||||
rti::core::policy::EntityName entityName;
|
||||
entityName.name(std::string("Sub ") + m_partitions.front());
|
||||
subscriberQos << entityName;
|
||||
|
||||
DEBUG_LOG(m_logger) << "Subscribe on partition " << m_partitions.front();
|
||||
|
||||
m_ddsSubscriber = dds::sub::Subscriber(m_domainParticipant,
|
||||
subscriberQos);
|
||||
|
||||
if (m_subscriberListener)
|
||||
m_subscriberListenerBinder = rti::core::bind_and_manage_listener(m_ddsSubscriber,
|
||||
m_subscriberListener,
|
||||
DDSGeneric::SubscriberListener::mask());
|
||||
|
||||
updatePartitionQoS(m_ddsSubscriber);
|
||||
}
|
||||
|
||||
void Subscriber::deleteDDS()
|
||||
{
|
||||
if (m_subscriberListener)
|
||||
{
|
||||
m_subscriberListener->prepareShutdown();
|
||||
m_subscriberListener = nullptr;
|
||||
}
|
||||
m_subscriberListenerBinder = nullptr;
|
||||
m_ddsSubscriber = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+71
@@ -0,0 +1,71 @@
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "pub_sub_base.h"
|
||||
|
||||
#include <DDS/Generic/dynamic_data_reader.h>
|
||||
#include <DDS/DTC/generic_dtc_impl.h>
|
||||
#include <DDS/Generic/topic_description.h>
|
||||
#include <rti/core/ListenerBinder.hpp>
|
||||
|
||||
namespace NGL
|
||||
{
|
||||
class INGLLogger;
|
||||
}
|
||||
|
||||
namespace DDSConnector
|
||||
{
|
||||
class DDSCONNECTOR_EXPORT Subscriber : public PubSubBase
|
||||
{
|
||||
|
||||
public:
|
||||
Subscriber(const dds::domain::DomainParticipant &domainParticipant, const std::string &partition, bool bUseSubscriberListener = true);
|
||||
virtual ~Subscriber();
|
||||
|
||||
dds::sub::Subscriber getDdsSubscriber() const { return m_ddsSubscriber; }
|
||||
DDSGeneric::SubscriberListener *getSubscriberListener() { return m_subscriberListener; }
|
||||
|
||||
template <typename DTC_TYPE>
|
||||
DTC_TYPE *create(std::function<void(const typename DTC_TYPE::TopicTypeRead &, DataModification modType)> cb)
|
||||
{
|
||||
DTC_TYPE* dtc = new DTC_TYPE;
|
||||
if (!dtc->create(m_domainParticipant, m_ddsSubscriber, m_subscriberListener))
|
||||
{
|
||||
delete dtc;
|
||||
return nullptr;
|
||||
}
|
||||
dtc->setSubscriberCallback(cb);
|
||||
return dtc;
|
||||
}
|
||||
|
||||
|
||||
DTC::Ddtc<DDSGeneric::DynamicDataReader> *create(const std::function<void(const DDSGeneric::DynamicData &, const std::string &, DataModification modType)> &cb,
|
||||
const dds::core::xtypes::StructType &structType,
|
||||
DDSGeneric::SubscriberListener *listener = nullptr,
|
||||
const std::string &specialTopicName = std::string())
|
||||
{
|
||||
if (!listener)
|
||||
listener = getSubscriberListener();
|
||||
|
||||
DTC::DdtcImpl<DDSGeneric::DynamicDataReader> *ddtc =
|
||||
new DTC::DdtcImpl<DDSGeneric::DynamicDataReader>(getDomainParticipant(),
|
||||
getDdsSubscriber(),
|
||||
listener,
|
||||
structType,
|
||||
specialTopicName);
|
||||
ddtc->setSubscriberCallback(cb);
|
||||
return ddtc;
|
||||
}
|
||||
|
||||
protected:
|
||||
void initDDS();
|
||||
void deleteDDS();
|
||||
|
||||
protected:
|
||||
dds::sub::Subscriber m_ddsSubscriber;
|
||||
DDSGeneric::SubscriberListener *m_subscriberListener;
|
||||
rti::core::ListenerBinder< dds::sub::Subscriber, dds::sub::SubscriberListener> m_subscriberListenerBinder;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user