BuiltInTopicUtils.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #ifndef BUILTINTOPICUTILS_H
00009 #define BUILTINTOPICUTILS_H
00010 
00011 #include "dcps_export.h"
00012 #include "dds/DdsDcpsInfrastructureC.h"
00013 #include "dds/DdsDcpsInfoUtilsC.h"
00014 #include "dds/DdsDcpsSubscriptionC.h"
00015 #include "dds/DdsDcpsCoreC.h"
00016 #include "Service_Participant.h"
00017 #include "DomainParticipantImpl.h"
00018 
00019 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00020 
00021 namespace OpenDDS {
00022 namespace DCPS {
00023 
00024 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PARTICIPANT_TOPIC;
00025 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PARTICIPANT_TOPIC_TYPE;
00026 
00027 OpenDDS_Dcps_Export extern const char* const BUILT_IN_TOPIC_TOPIC;
00028 OpenDDS_Dcps_Export extern const char* const BUILT_IN_TOPIC_TOPIC_TYPE;
00029 
00030 OpenDDS_Dcps_Export extern const char* const BUILT_IN_SUBSCRIPTION_TOPIC;
00031 OpenDDS_Dcps_Export extern const char* const BUILT_IN_SUBSCRIPTION_TOPIC_TYPE;
00032 
00033 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PUBLICATION_TOPIC;
00034 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PUBLICATION_TOPIC_TYPE;
00035 
00036 /**
00037  * Returns true if the topic name and type pair matches one of the built-in
00038  * topic name and type pairs.
00039  */
00040 inline bool topicIsBIT(const char* name, const char* type)
00041 {
00042     return (
00043       !ACE_OS::strcmp(name, OpenDDS::DCPS::BUILT_IN_PARTICIPANT_TOPIC) &&
00044       !ACE_OS::strcmp(type, OpenDDS::DCPS::BUILT_IN_PARTICIPANT_TOPIC_TYPE)
00045     ) || (
00046       !ACE_OS::strcmp(name, OpenDDS::DCPS::BUILT_IN_TOPIC_TOPIC) &&
00047       !ACE_OS::strcmp(type, OpenDDS::DCPS::BUILT_IN_TOPIC_TOPIC_TYPE)
00048     ) || (
00049       !ACE_OS::strcmp(name, OpenDDS::DCPS::BUILT_IN_SUBSCRIPTION_TOPIC) &&
00050       !ACE_OS::strcmp(type, OpenDDS::DCPS::BUILT_IN_SUBSCRIPTION_TOPIC_TYPE)
00051     ) || (
00052       !ACE_OS::strcmp(name, OpenDDS::DCPS::BUILT_IN_PUBLICATION_TOPIC) &&
00053       !ACE_OS::strcmp(type, OpenDDS::DCPS::BUILT_IN_PUBLICATION_TOPIC_TYPE)
00054     );
00055 }
00056 
00057 class DomainParticipantImpl;
00058 
00059 /**
00060  * Functor for ordering BuiltinKey_t.
00061  *
00062  * Use this like this:
00063  *   std::map<DDS::BuiltinTopicKey_t, int, OpenDDS::DCPS::BuiltinTopicKeyLess> MapType;
00064  */
00065 class BuiltinTopicKeyLess {
00066 public:
00067   bool operator()(
00068     const DDS::BuiltinTopicKey_t& lhs,
00069     const DDS::BuiltinTopicKey_t& rhs) const;
00070 };
00071 
00072 template<typename TopicType>
00073 DDS::BuiltinTopicKey_t keyFromSample(TopicType* sample);
00074 
00075 #if !defined (DDS_HAS_MINIMUM_BIT)
00076 
00077 template<class BIT_Reader_var, class BIT_DataSeq>
00078 DDS::ReturnCode_t instance_handle_to_bit_data(
00079   DomainParticipantImpl* dp,
00080   const char* bit_name,
00081   const DDS::InstanceHandle_t& handle,
00082   BIT_DataSeq& data)
00083 {
00084   DDS::Subscriber_var bit_subscriber = dp->get_builtin_subscriber();
00085 
00086   DDS::DataReader_var reader = bit_subscriber->lookup_datareader(bit_name);
00087 
00088   typedef typename BIT_Reader_var::_obj_type BIT_Reader;
00089   BIT_Reader_var bit_reader = BIT_Reader::_narrow(reader.in());
00090 
00091   const ACE_Time_Value due = ACE_OS::gettimeofday() +
00092     ACE_Time_Value(TheServiceParticipant->bit_lookup_duration_msec() / 1000,
00093                    (TheServiceParticipant->bit_lookup_duration_msec() % 1000)
00094                    * 1000);
00095 
00096     // Look for the data from builtin topic datareader until we get results or
00097     // timeout.
00098     // This is to resolve the problem of lookup return nothing. This could happen
00099     // when the add_association is called before the builtin topic datareader got
00100     // the published data.
00101   while (true) {
00102     DDS::SampleInfoSeq the_info;
00103     BIT_DataSeq the_data;
00104     const DDS::ReturnCode_t ret =
00105       bit_reader->read_instance(the_data,
00106                                 the_info,
00107                                 DDS::LENGTH_UNLIMITED,
00108                                 handle,
00109                                 DDS::ANY_SAMPLE_STATE,
00110                                 DDS::ANY_VIEW_STATE,
00111                                 DDS::ANY_INSTANCE_STATE);
00112 
00113     if (ret == DDS::RETCODE_OK) {
00114       data.length(1);
00115       data[0] = the_data[0];
00116       return ret;
00117     }
00118 
00119     if (ret != DDS::RETCODE_BAD_PARAMETER && ret != DDS::RETCODE_NO_DATA) {
00120       ACE_ERROR_RETURN((LM_ERROR,
00121                         ACE_TEXT("(%P|%t) ERROR: instance_handle_to_repo_id, ")
00122                         ACE_TEXT("read instance 0x%x returned error %d.\n"),
00123                         handle, ret),
00124                        ret);
00125     }
00126 
00127     const ACE_Time_Value now = ACE_OS::gettimeofday();
00128 
00129     if (now < due) {
00130       if (DCPS_debug_level >= 10) {
00131         ACE_DEBUG((LM_DEBUG,
00132                    ACE_TEXT("(%P|%t) instance_handle_to_repo_id, ")
00133                    ACE_TEXT("BIT reader read_instance failed - trying again.\n")));
00134       }
00135 
00136       ACE_Time_Value tv = due - now;
00137 
00138       if (tv > ACE_Time_Value(0, 100000)) {
00139         tv = ACE_Time_Value(0, 100000);
00140       }
00141 
00142       ACE_OS::sleep(tv);
00143 
00144     } else {
00145       ACE_ERROR_RETURN((LM_ERROR,
00146                         ACE_TEXT("(%P|%t) ERROR: instance_handle_to_repo_id,")
00147                         ACE_TEXT(" timeout. \n")),
00148                        DDS::RETCODE_ERROR);
00149       return DDS::RETCODE_TIMEOUT;
00150     }
00151   }
00152 }
00153 #endif
00154 
00155 inline
00156 bool
00157 BuiltinTopicKeyLess::operator()(const DDS::BuiltinTopicKey_t& lhs,
00158                                 const DDS::BuiltinTopicKey_t& rhs) const
00159 {
00160   // N.B.  This assumes that the MS index is 2 and the LS index is 0.
00161   return (lhs.value[2] < rhs.value[2])? true:
00162          (lhs.value[2] > rhs.value[2])? false:
00163          (lhs.value[1] < rhs.value[1])? true:
00164          (lhs.value[1] > rhs.value[1])? false:
00165          (lhs.value[0] < rhs.value[0])? true:
00166          false;
00167 
00168 }
00169 
00170 #if !defined (DDS_HAS_MINIMUM_BIT)
00171 
00172 template<>
00173 inline
00174 DDS::BuiltinTopicKey_t
00175 keyFromSample<DDS::ParticipantBuiltinTopicData>(
00176   DDS::ParticipantBuiltinTopicData* sample)
00177 {
00178   return sample->key;
00179 }
00180 
00181 template<>
00182 inline
00183 DDS::BuiltinTopicKey_t
00184 keyFromSample<DDS::TopicBuiltinTopicData>(
00185   DDS::TopicBuiltinTopicData* sample)
00186 {
00187   return sample->key;
00188 }
00189 
00190 template<>
00191 inline
00192 DDS::BuiltinTopicKey_t
00193 keyFromSample<DDS::SubscriptionBuiltinTopicData>(
00194   DDS::SubscriptionBuiltinTopicData* sample)
00195 {
00196   return sample->key;
00197 }
00198 
00199 template<>
00200 inline
00201 DDS::BuiltinTopicKey_t
00202 keyFromSample<DDS::PublicationBuiltinTopicData>(
00203   DDS::PublicationBuiltinTopicData* sample)
00204 {
00205   return sample->key;
00206 }
00207 
00208 #endif
00209 
00210 template<typename TopicType>
00211 inline
00212 DDS::BuiltinTopicKey_t keyFromSample(TopicType*)
00213 {
00214   DDS::BuiltinTopicKey_t value;
00215   value.value[0] = value.value[1] = value.value[2] = 0;
00216   return value;
00217 }
00218 
00219 } // namespace DCPS
00220 } // namespace OpenDDS
00221 
00222 OPENDDS_END_VERSIONED_NAMESPACE_DECL
00223 
00224 #endif /* BUILTINTOPICUTILS_H  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 10 Aug 2018 for OpenDDS by  doxygen 1.6.1