00001 #ifndef DDSMANAGER_H
00002 #define DDSMANAGER_H
00003
00004 #include <map>
00005
00006 #include "checkStatus.h"
00007 #include "singleton.h"
00008
00009 #include <assert.h>
00010
00011 using namespace DDS;
00012 using namespace Templates;
00013
00021 class DDSManager : public Singleton<DDSManager>
00022 {
00026 friend class Singleton<DDSManager>;
00027 private:
00031 DDSManager() {
00032 std::cout<<std::endl<<"DDSManager singelton constructor called"<<std::endl<<std::endl;
00033 };
00034
00039 DDSManager(const DDSManager &){}
00040
00048 virtual bool createParticipant(std::string domain_name) {
00049 bool bSuccess = false;
00050
00051
00052 DomainId_t m_domain = DDS::DOMAIN_ID_DEFAULT;
00053
00054
00055 m_dpf = DomainParticipantFactory::get_instance();
00056 bSuccess = checkHandle(m_dpf.in(), "DDS::DomainParticipantFactory::get_instance"); assert(bSuccess);
00057 DomainParticipant_var participant = m_dpf->create_participant(m_domain, PARTICIPANT_QOS_DEFAULT, NULL,
00058 DDS::DATA_AVAILABLE_STATUS);
00059 bSuccess = checkHandle(participant.in(),
00060 "DDS::DomainParticipantFactory::create_participant"); assert(bSuccess);
00061 m_participants[domain_name] = participant;
00062 return true;
00063 }
00064
00074 virtual bool createSubscriber(std::string domain_name, std::string partition_name) {
00075 bool bSuccess = false;
00076
00077 SubscriberQos sub_qos;
00078 ReturnCode_t status = m_participants[domain_name]->get_default_subscriber_qos(sub_qos);
00079 bSuccess = checkStatus(status, "DDS::DomainParticipant::get_default_subscriber_qos"); assert(bSuccess);
00080 sub_qos.partition.name.length(1);
00081 sub_qos.partition.name[0] = partition_name.c_str();
00082 Subscriber_var sub = m_participants[domain_name]->create_subscriber(sub_qos, NULL, STATUS_MASK_NONE);
00083 bSuccess = checkHandle(sub.in(), "DDS::DomainParticipant::create_subscriber"); assert(bSuccess);
00084 m_subscribers[domain_name][partition_name] = sub;
00085 return true;
00086 }
00087
00097 virtual bool createPublisher(std::string domain_name, std::string partition_name) {
00098 bool bSuccess = false;
00099
00100 PublisherQos pub_qos;
00101 ReturnCode_t status = m_participants[domain_name]->get_default_publisher_qos(pub_qos);
00102 bSuccess = checkStatus(status, "DDS::DomainParticipant::get_default_publisher_qos"); assert(bSuccess);
00103 pub_qos.partition.name.length(1);
00104 pub_qos.partition.name[0] = partition_name.c_str();
00105 Publisher_var pub = m_participants[domain_name]->create_publisher(pub_qos, NULL, STATUS_MASK_NONE);
00106 bSuccess = checkHandle(pub.in(), "DDS::DomainParticipant::create_publisher"); assert(bSuccess);
00107 m_publishers[domain_name][partition_name] = pub;
00108 return true;
00109 }
00110
00111 public:
00116 virtual ~DDSManager(){};
00117
00125 virtual bool lookupOrCreateParticipant(std::string domain_name) {
00126
00127 if (m_participants.find(domain_name) == m_participants.end()) {
00128 return createParticipant(domain_name);
00129 }
00130
00131 return true;
00132 }
00133
00143 bool lookupOrCreateSubscriber(std::string domain_name, std::string partition_name) {
00144 if(lookupOrCreateParticipant(domain_name) == false) {
00145 return false;
00146 }
00147 if (m_subscribers[domain_name].find(partition_name) == m_subscribers[domain_name].end()) {
00148 return createSubscriber(domain_name, partition_name);
00149 }
00150 return true;
00151 }
00152
00162 bool lookupOrCreatePublisher(std::string domain_name, std::string partition_name) {
00163 if(lookupOrCreateParticipant(domain_name) == false) {
00164 return false;
00165 }
00166 if (m_publishers[domain_name].find(partition_name) == m_publishers[domain_name].end()) {
00167 return createPublisher(domain_name, partition_name);
00168 }
00169 return true;
00170 }
00171
00177 bool registerType(std::string domain_name, TypeSupport *ts) {
00178 bool bSuccess = false;
00179
00180 if(lookupOrCreateParticipant(domain_name) == false) {
00181 return false;
00182 }
00183
00184
00185 ReturnCode_t status = ts->register_type(m_participants[domain_name].in(), ts->get_type_name());
00186 bSuccess = checkStatus(status, "register_type"); assert(bSuccess);
00187 return true;
00188 }
00189
00197 Topic_var createTopic(std::string domain_name, const char* topic_name, const char* type_name) {
00198 bool bSuccess = false;
00199
00200 if(lookupOrCreateParticipant(domain_name) == false) {
00201 return 0;
00202 }
00203 Topic_var topic;
00204 TopicQos topic_qos;
00205 ReturnCode_t status = m_participants[domain_name]->get_default_topic_qos(topic_qos);
00206 bSuccess = checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos"); assert(bSuccess);
00207 topic_qos.reliability.kind = RELIABLE_RELIABILITY_QOS;
00208 topic_qos.durability.kind = TRANSIENT_DURABILITY_QOS;
00209 topic_qos.history.kind = KEEP_ALL_HISTORY_QOS;
00210 m_reliable_topic_qos[domain_name] = topic_qos;
00211
00212 status = m_participants[domain_name]->set_default_topic_qos(m_reliable_topic_qos[domain_name]);
00213 bSuccess = checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos"); assert(bSuccess);
00214
00215
00216 topic = m_participants[domain_name]->create_topic(topic_name, type_name,
00217 m_reliable_topic_qos[domain_name], NULL, STATUS_MASK_NONE);
00218 bSuccess = checkHandle(topic.in(), "DDS::DomainParticipant::create_topic ()"); assert(bSuccess);
00219 return topic;
00220 }
00221
00229 DataReader_ptr createReader(std::string domain_name, std::string partition_name, Topic_var topic) {
00230
00231
00232 if(lookupOrCreateSubscriber(domain_name, partition_name) == false) {
00233 return false;
00234 }
00235 return m_subscribers[domain_name][partition_name]->create_datareader(topic.in(),
00236 DATAREADER_QOS_USE_TOPIC_QOS, NULL,
00237 STATUS_MASK_NONE);
00238 }
00239
00247 DataWriter_ptr createWriter(std::string domain_name, std::string partition_name, Topic_var topic) {
00248 bool bSuccess = false;
00249
00250
00251 if(lookupOrCreatePublisher(domain_name, partition_name) == false) {
00252 return 0;
00253 }
00254 DataWriterQos dw_qos;
00255 ReturnCode_t status = m_publishers[domain_name][partition_name]->get_default_datawriter_qos(dw_qos);
00256 bSuccess = checkStatus(status, "DDS::DomainParticipant::get_default_publisher_qos"); assert(bSuccess);
00257 status = m_publishers[domain_name][partition_name]->copy_from_topic_qos(dw_qos, m_reliable_topic_qos[domain_name]);
00258 bSuccess = checkStatus(status, "DDS::Publisher::copy_from_topic_qos"); assert(bSuccess);
00259
00260
00261
00262
00263 bool autodispose_unregistered_instances = false;
00264 dw_qos.writer_data_lifecycle.autodispose_unregistered_instances = autodispose_unregistered_instances;
00265 DataWriter_ptr writer = m_publishers[domain_name][partition_name]->create_datawriter(topic.in(), dw_qos, NULL,
00266 STATUS_MASK_NONE);
00267 bSuccess = checkHandle(writer, "DDS::Publisher::create_datawriter"); assert(bSuccess);
00268 return writer;
00269 }
00270 public:
00272 DomainParticipantFactory_var m_dpf;
00274 std::map<std::string, TopicQos> m_reliable_topic_qos;
00275
00277 std::map<std::string, DomainParticipant_var> m_participants;
00279 std::map< std::string, std::map< std::string, Subscriber_var > > m_subscribers;
00281 std::map< std::string, std::map< std::string, Publisher_var > > m_publishers;
00282 };
00283
00284 #endif // BASECOMPONENTDDS_H