#ifndef __DDS_EASY_MODEL_HPP__ #define __DDS_EASY_MODEL_HPP__ #include #include #include #include #define UT_DDS_PARAMETER_CONFIG_FILENAME "dds_parameter.json" namespace unitree { namespace common { class DdsEasyModel { public: explicit DdsEasyModel(); ~DdsEasyModel(); void Init(uint32_t domainId); void Init(const std::string& ddsParameterFileName = ""); void Init(const JsonMap& param); template void SetTopic(const std::string& topic) { DdsTopicChannelPtr channel = GetChannel(topic); if (!channel) { channel = DdsTopicChannelPtr(new DdsTopicChannel()); mChannelMap[topic] = std::static_pointer_cast(channel); DdsTopicQos topicQos; GetTopicQos(topic, topicQos); channel->SetTopic(mParticipant, topic, topicQos); } DdsWriterPtr writer = channel->GetWriter(); if (!writer) { DdsWriterQos writerQos; GetWriterQos(topic, writerQos); channel->SetWriter(GetPublisher(topic), writerQos); } else { UT_THROW(CommonException, std::string("topic reader is already exist. topic:") + topic); } } template void SetTopic(const std::string& topic, const DdsMessageHandler& handler, int32_t queuelen = 0) { DdsReaderCallback cb(handler); SetTopic(topic, cb, queuelen); } template void SetTopic(const std::string& topic, const DdsReaderCallback& rcb, int32_t queuelen = 0) { DdsTopicChannelPtr channel = GetChannel(topic); if (!channel) { channel = DdsTopicChannelPtr(new DdsTopicChannel()); mChannelMap[topic] = std::static_pointer_cast(channel); DdsTopicQos topicQos; GetTopicQos(topic, topicQos); channel->SetTopic(mParticipant, topic, topicQos); } DdsReaderPtr reader = channel->GetReader(); if (!reader) { DdsReaderQos readerQos; GetReaderQos(topic, readerQos); channel->SetReader(GetSubscriber(topic), readerQos, rcb, queuelen); } else { UT_THROW(CommonException, std::string("topic reader is already exist. topic:") + topic); } } template bool WriteMessage(const std::string topic, const MSG& message, int64_t waitMicrosec = 0) { DdsTopicChannelPtr channel = GetChannel(topic); if (channel == NULL) { return false; } return channel->Write(message, waitMicrosec); } bool WriteMessage(const std::string topic, const void* message, int64_t waitMicrosec = 0); int64_t GetLastDataAvailableTime(const std::string topic); private: void GetTopicQos(const std::string& topic, DdsTopicQos& qos); void GetWriterQos(const std::string& topic, DdsWriterQos& qos); void GetReaderQos(const std::string& topic, DdsReaderQos& qos); DdsTopicChannelAbstractPtr GetChannel(const std::string& topic); template DdsTopicChannelPtr GetChannel(const std::string& topic) { DdsTopicChannelPtr channel; DdsTopicChannelAbstractPtr channelAbstract = GetChannel(topic); if (channelAbstract) { channel = std::static_pointer_cast>(channelAbstract); } return channel; } DdsSubscriberPtr GetSubscriber(const std::string& topic); DdsSubscriberPtr GetSubscriberDefault(); DdsPublisherPtr GetPublisher(const std::string& topic); DdsPublisherPtr GetPublisherDefault(); private: DdsParameter mDdsParameter; DdsParticipantPtr mParticipant; std::vector mPublisherList; std::vector mSubscriberList; DdsPublisherPtr mPublisherDefault; DdsSubscriberPtr mSubscriberDefault; std::map mChannelMap; Logger *mLogger; }; using DdsEasyModelPtr = std::shared_ptr; } } #endif//__DDS_EASY_MODEL_HPP__