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 namespace OpenDDS {
00020 namespace DCPS {
00021 
00022 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PARTICIPANT_TOPIC;
00023 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PARTICIPANT_TOPIC_TYPE;
00024 
00025 OpenDDS_Dcps_Export extern const char* const BUILT_IN_TOPIC_TOPIC;
00026 OpenDDS_Dcps_Export extern const char* const BUILT_IN_TOPIC_TOPIC_TYPE;
00027 
00028 OpenDDS_Dcps_Export extern const char* const BUILT_IN_SUBSCRIPTION_TOPIC;
00029 OpenDDS_Dcps_Export extern const char* const BUILT_IN_SUBSCRIPTION_TOPIC_TYPE;
00030 
00031 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PUBLICATION_TOPIC;
00032 OpenDDS_Dcps_Export extern const char* const BUILT_IN_PUBLICATION_TOPIC_TYPE;
00033 
00034 
00035 class DomainParticipantImpl;
00036 
00037 /**
00038  * Functor for ordering BuiltinKey_t.
00039  *
00040  * Use this like this:
00041  *   std::map<DDS::BuiltinTopicKey_t, int, OpenDDS::DCPS::BuiltinTopicKeyLess> MapType;
00042  */
00043 class BuiltinTopicKeyLess {
00044 public:
00045   bool operator()(
00046     const DDS::BuiltinTopicKey_t& lhs,
00047     const DDS::BuiltinTopicKey_t& rhs) const;
00048 };
00049 
00050 template<typename TopicType>
00051 DDS::BuiltinTopicKey_t keyFromSample(TopicType* sample);
00052 
00053 #if !defined (DDS_HAS_MINIMUM_BIT)
00054 
00055 // changed from member function template to class template
00056 // to avoid VC++ v6 build problem.
00057 /*
00058  * Template method to retrieve the repository id by instance handle
00059  * from builtin topic.
00060  */
00061 template <class BIT_Reader, class BIT_Reader_var, class BIT_DataSeq>
00062 class BIT_Helper_1 {
00063 public:
00064   DDS::ReturnCode_t instance_handle_to_bit_data(
00065     DomainParticipantImpl*       dp,
00066     const char*                  bit_name,
00067     const DDS::InstanceHandle_t& handle,
00068     BIT_DataSeq&                 data)
00069   {
00070     DDS::Subscriber_var bit_subscriber = dp->get_builtin_subscriber();
00071 
00072     DDS::DataReader_var reader =
00073       bit_subscriber->lookup_datareader(bit_name);
00074 
00075     BIT_Reader_var bit_reader = BIT_Reader::_narrow(reader.in());
00076 
00077     ACE_Time_Value due = ACE_OS::gettimeofday() +
00078       ACE_Time_Value(TheServiceParticipant->bit_lookup_duration_msec() / 1000,
00079                      (TheServiceParticipant->bit_lookup_duration_msec() % 1000)*1000);
00080 
00081     DDS::ReturnCode_t ret = DDS::RETCODE_OK;
00082 
00083     // Look for the data from builtin topic datareader until we get results or
00084     // timeout.
00085     // This is to resolve the problem of lookup return nothing. This could happen
00086     // when the add_association is called before the builtin topic datareader got
00087     // the published data.
00088     while (1) {
00089       DDS::SampleInfoSeq the_info;
00090       BIT_DataSeq the_data;
00091       ret = bit_reader->read_instance(the_data,
00092                                       the_info,
00093                                       DDS::LENGTH_UNLIMITED, // zero-copy
00094                                       handle,
00095                                       DDS::ANY_SAMPLE_STATE,
00096                                       DDS::ANY_VIEW_STATE,
00097                                       DDS::ANY_INSTANCE_STATE);
00098 
00099       if (ret != DDS::RETCODE_OK && ret != DDS::RETCODE_NO_DATA) {
00100         ACE_ERROR_RETURN((LM_ERROR,
00101                           ACE_TEXT("(%P|%t) ERROR: BIT_Helper::instance_handle_to_repo_id, ")
00102                           ACE_TEXT(" read instance 0x%x returned error %d. \n"),
00103                           handle, ret),
00104                          ret);
00105       }
00106 
00107       if (ret == DDS::RETCODE_OK) {
00108         data.length(1);
00109         data[0] = the_data[0];
00110         return ret;
00111       }
00112 
00113       ACE_Time_Value now = ACE_OS::gettimeofday();
00114 
00115       if (now < due) {
00116         if (DCPS_debug_level >= 10)
00117           ACE_DEBUG((LM_DEBUG,
00118                      ACE_TEXT("(%P|%t) BIT_Helper::instance_handle_to_repo_id, ")
00119                      ACE_TEXT(" BIT reader read_instance failed - trying again. \n")));
00120 
00121         ACE_Time_Value tv = due - now;
00122 
00123         if (tv > ACE_Time_Value(0, 100000)) {
00124           tv = ACE_Time_Value(0, 100000);
00125         }
00126 
00127         ACE_OS::sleep(tv);
00128 
00129       } else {
00130         ACE_ERROR_RETURN((LM_ERROR,
00131                           ACE_TEXT("(%P|%t) ERROR: BIT_Helper::instance_handle_to_repo_id, ")
00132                           ACE_TEXT(" timeout. \n")),
00133                          DDS::RETCODE_ERROR);
00134         return DDS::RETCODE_TIMEOUT;
00135       }
00136     }
00137   }
00138 };
00139 
00140 #endif
00141 
00142 inline
00143 bool
00144 BuiltinTopicKeyLess::operator()(const DDS::BuiltinTopicKey_t& lhs,
00145                                 const DDS::BuiltinTopicKey_t& rhs) const
00146 {
00147   // N.B.  This assumes that the MS index is 2 and the LS index is 0.
00148   return (lhs.value[2] < rhs.value[2])? true:
00149          (lhs.value[2] > rhs.value[2])? false:
00150          (lhs.value[1] < rhs.value[1])? true:
00151          (lhs.value[1] > rhs.value[1])? false:
00152          (lhs.value[0] < rhs.value[0])? true:
00153          false;
00154 
00155 }
00156 
00157 #if !defined (DDS_HAS_MINIMUM_BIT)
00158 
00159 template<>
00160 inline
00161 DDS::BuiltinTopicKey_t
00162 keyFromSample<DDS::ParticipantBuiltinTopicData>(
00163   DDS::ParticipantBuiltinTopicData* sample)
00164 {
00165   return sample->key;
00166 }
00167 
00168 template<>
00169 inline
00170 DDS::BuiltinTopicKey_t
00171 keyFromSample<DDS::TopicBuiltinTopicData>(
00172   DDS::TopicBuiltinTopicData* sample)
00173 {
00174   return sample->key;
00175 }
00176 
00177 template<>
00178 inline
00179 DDS::BuiltinTopicKey_t
00180 keyFromSample<DDS::SubscriptionBuiltinTopicData>(
00181   DDS::SubscriptionBuiltinTopicData* sample)
00182 {
00183   return sample->key;
00184 }
00185 
00186 template<>
00187 inline
00188 DDS::BuiltinTopicKey_t
00189 keyFromSample<DDS::PublicationBuiltinTopicData>(
00190   DDS::PublicationBuiltinTopicData* sample)
00191 {
00192   return sample->key;
00193 }
00194 
00195 #endif
00196 
00197 template<typename TopicType>
00198 inline
00199 DDS::BuiltinTopicKey_t keyFromSample(TopicType*)
00200 {
00201   DDS::BuiltinTopicKey_t value;
00202   value.value[0] = value.value[1] = value.value[2] = 0;
00203   return value;
00204 }
00205 
00206 } // namespace DCPS
00207 } // namespace OpenDDS
00208 
00209 #endif /* BUILTINTOPICUTILS_H  */

Generated on Fri Feb 12 20:05:19 2016 for OpenDDS by  doxygen 1.4.7