/** * Initialisation DDS * $Id$ * $HeadURL$ * $Author$ */ #pragma once #include #include #include #include #include #include #include #include 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; /** * partitionId is ignored for Any, Global and AllExercises */ DdsConnector(int domainId, IDL::PartitionType defaultPartitionType, int partitionId = 0, int exerciseId = 0, const QUrl &tcpConnectionUrl = QUrl()); /** * @brief Create a new DdsConnector with the given domainparticipant e.g. to access other partition with same domainparticipant) */ 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(); /** * @brief initialize the DDSConnector internals, e.g. load QoS profiles * @param qosFileList a custom Qos xml file (if needed) * @return true on success */ 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); // takes ownership void enableBatchedWriter(bool bEnable); Subscriber *getSubscriber() { return m_subscriber; } dds::sub::Subscriber getDdsSubscriber() const; void setSubscriber(Subscriber *subscriber); // takes ownership 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); /** * @brief default initialize publisher / Subscriber / DomainParticipant * Using m_domainId and m_exerciseId as parameter for Publisher/Subscriber */ void initPublisherSubscriber(); void initPublisher(); void initSubscriber(); void initDomainParticipant(); template T *createDTS(const std::string &topicName = DDS::Helper::getShortTopicName(DDS::topic_type_name::value())) { if (getDomainParticipant() == nullptr || getDdsPublisher() == nullptr) return nullptr; T *dts = new T(getDomainParticipant(), getDdsPublisher(), topicName); return dts; } template T *createDTC(const std::string &topicName = DDS::Helper::getShortTopicName(DDS::topic_type_name::value())) { if (getDomainParticipant() == nullptr || getDdsSubscriber() == nullptr) return nullptr; T *dtc = new T(getDomainParticipant(), getDdsSubscriber(), getSubscriberListener(), topicName); return dtc; } template T *createDTC(const dds::topic::Filter &filter, const std::string &topicName = DDS::Helper::getShortTopicName(DDS::topic_type_name::value())) { if (getDomainParticipant() == nullptr || getDdsSubscriber() == nullptr) return nullptr; T *dtc = new T(getDomainParticipant(), getDdsSubscriber(), getSubscriberListener(), filter, topicName); return dtc; } template T *createFTS() { if (getDomainParticipant() == nullptr || getDdsPublisher() == nullptr || getDdsSubscriber() == nullptr) return nullptr; createWaitSetHandler(); T *fts = new T(getDomainParticipant(), getDdsPublisher(), getDdsSubscriber(), m_wsHandler); return fts; } template 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; }; /** * @brief Convenience class for dds::pub::CoherentSet which takes a DdsConnector */ class CoherentSet { public: explicit CoherentSet(const std::shared_ptr &connector) : m_coherentSet(connector->getDdsPublisher()) {} private: dds::pub::CoherentSet m_coherentSet; }; }