CETDDS.cpp
Go to the documentation of this file.
1 // This file (CETDDS.cxx) was created by Ron Rechenmacher <ron@fnal.gov> on
2 // Jan 21, 2010. "TERMS AND CONDITIONS" governing this file are in the README
3 // or COPYING file. If you do not have such a file, one can be obtained by
4 // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 // $RCSfile: CETDDS.cpp,v $
6 // rev="$Revision: 1.10.14.1 $$Date: 2019/09/27 00:07:32 $";
7 
8 #include "rms/provider/CETDDS.h"
9 using namespace DDS;
10 #include "rms/provider/CheckStatus.h"
11 #include "boost/random.hpp"
12 #include "boost/lexical_cast.hpp"
13 
14 namespace gov {
15 
16 namespace fnal {
17 
18 namespace cd {
19 
20 namespace rms {
21 
22 namespace provider {
23 
25  std::string const& topic_name )
26 {
27  std::string extendedTopicName(topic_name);
28  extendedTopicName.append( "_" );
29  extendedTopicName.append( type_name );
30  boost::replace_all( extendedTopicName, "::", "_" );
31  return extendedTopicName;
32 }
34  std::string const& topic_name )
35 {
36  return create_extended_topic_name_for(type_name,topic_name.substr(topic_name.find("_")+1));
37 }
38 
40  std::string const& topic_name )
41 {
42  filter.first="( header.target = %0 )";
43  filter.second.clear();
44  std::string topic_name_tmp(topic_name.substr(0,topic_name.rfind("_")));
45 
46  //restore dashes replaced in DDSConnection::registerSender
47  boost::replace_all(topic_name_tmp,"_dash_","-");
48  filter.second.push_back(topic_name_tmp);
49 
50  return filter;
51 }
52 
53 std::string unique_topic_name_for(std::string const& extended_topic_name )
54 {
55  static boost::mt19937 rand(static_cast<unsigned int>(std::time(0)));
56  std::string unique_extended_topic_name(extended_topic_name);
57  unique_extended_topic_name.append(boost::lexical_cast<std::string>(rand()));
58  return unique_extended_topic_name;
59 }
60 
61 bool string2bool (std::string const& bool_string)
62 {
63  return !bool_string.empty() && (
64  strcasecmp(bool_string.c_str(),"true")==0
65  || atoi(bool_string.c_str()) !=0);
66 }
67 
68 void dumpReceivedSamples( dds::SampleInfoSeq const& infoSeq, const unsigned int length, const int status )
69 {
70  std::cout << "++++++++++++++++++++++++++++++++++++++++" << std::endl;
71  std::cout << "status=" << status << " topic_seq.length()=" << length << std::endl;
72 
73  for ( uint32_t idx = 0; idx < length; ++idx ) {
74  std::cout << "sample state = " << infoSeq[idx].sample_state << std::endl;
75  std::cout << "view state = " << infoSeq[idx].view_state << std::endl;
76  std::cout << "instance state = " << infoSeq[idx].instance_state << std::endl;
77  std::cout << "valid data = " << (( int ) infoSeq[idx].valid_data ) << std::endl;
78  //std::cout << "source timestamp = " << infoSeq[idx].source_timestamp << std::endl;
79  std::cout << "disposed generation count = " << infoSeq[idx].disposed_generation_count << std::endl;
80  std::cout << "no writers generation count = " << infoSeq[idx].no_writers_generation_count << std::endl;
81  std::cout << "sample rank = " << infoSeq[idx].sample_rank << std::endl;
82  std::cout << "generation rank = " << infoSeq[idx].generation_rank << std::endl;
83  std::cout << "absolute generation rank = " << infoSeq[idx].absolute_generation_rank << std::endl;
84  }
85 
86  std::cout << "----------------------------------------" << std::endl;
87 }
88 
89 void DPSingleton::reconnect_participant()
90 {
91  disconnect_participant();
92  connect_participant();
93 }
94 
95 void DPSingleton::disconnect_participant()
96 {
97  dp.reset();
98 
99 #if CETDDS_DEBUG1
100  std::cout << "Disconnected DDS domain participant" << std::endl;
101 #endif
102 }
103 
104 dds::DomainParticipant& DPSingleton::get_participant()
105 {
106  try
107  {
108  ::DDS::Time_t current_time;
109 
110  if(dp.get()==NULL)
111  GENERATE_RMS_NOTCONNECTED_EXCEPTION("DDS domain participant is not connected.");
112 
113  dp->get()->get_current_time(current_time);
114 
115  return *dp;
116  }
117  catch (...)
118  {
119  disconnect_participant();
120  #if CETDDS_DEBUG1
121  std::cout << "DDS domain participant is not connected." << std::endl;
122  #endif
123  GENERATE_RMS_NOTCONNECTED_EXCEPTION("DDS domain participant is not connected.");
124  }
125 }
126 
127 void DPSingleton::connect_participant()
128 {
129  ReturnCode_t status;
130 
131  dp.reset(new dds::DomainParticipant);
132 
133  /* Tailor a qos policy and make it the new default. */
134  dds::TopicQos reliable_topic_qos(*dp);
135 
136  status = dp->get()->get_default_topic_qos( reliable_topic_qos );
137  checkStatus( status, "dds::DomainParticipant::get_default_topic_qos" );
138 
139  reliable_topic_qos.set_reliable();
140  reliable_topic_qos.set_volatile();
141 
142  reliable_topic_qos.set_keep_last(novadaq::EnvVarCache::getInstance().getEnvVarAsLong("RMS_SAMPLE_HISTORY_SIZE",10));
143 
144  status = dp->get()->set_default_topic_qos( reliable_topic_qos );
145 
146  checkStatus( status, "dds::DomainParticipant::set_default_topic_qos" );
147 
148 #if CETDDS_DEBUG1
149  std::cout << "Connected DDS domain participant" << std::endl;
150 #endif
151 }
152 
153 DPSingleton::DPSingleton():
154  dp(),signalHandler(new ProcessSignalHandler)
155 
156 {
157  try
158  {
160  }catch(...)
161  {
163  std::cerr << "DDS domain participant is not connected." << std::endl;
164  }
165 
166  if (!signalHandler->setupSignalHandler(SIGINT))
167  std::cerr << "Unable to setup signalHandler for SIGINT in DPSingleton class." << std::endl;
168 
169  if (!signalHandler->setupSignalHandler(SIGTERM))
170  std::cerr << "Unable to setup signalHandler for SIGTERM in DPSingleton class." << std::endl;
171 
172  if (!signalHandler->setupSignalHandler(SIGUSR1))
173  std::cerr << "Unable to setup signalHandler for SIGUSR1 in DPSingleton class." << std::endl;
174 
175  if (!signalHandler->setupSignalHandler(SIGSEGV))
176  std::cerr << "Unable to setup signalHandler for SIGSEGV in DPSingleton class." << std::endl;
177 
178 }
179 void DPSingleton::addToCleanupList(base::Notifiable* notifiable)
180 {
181  assert( notifiable != NULL );
182 
183  signalHandler->addToNotificationList (notifiable);
184 }
185 
186 void DPSingleton::removeFromCleanupList(base::Notifiable* notifiable)
187 {
188  assert( notifiable != NULL );
189 
190  signalHandler->removeFromNotificationList(notifiable);
191 }
192 
193 
194 } // end of namespace provider
195 
196 } // end of namespace rms
197 
198 } // end of namespace cd
199 
200 } // end of namespace fnal
201 
202 } // end of namespace gov
int status
Definition: fabricate.py:1613
static EnvVarCache & getInstance()
Definition: EnvVarCache.cpp:12
OStream cerr
Definition: OStream.cxx:7
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
dds_topic_filter_t & create_topic_filter_for(dds_topic_filter_t &filter, std::string const &type_name, std::string const &topic_name)
Definition: CETDDS.cpp:39
std::pair< std::string, std::vector< std::string > > dds_topic_filter_t
Definition: CETDDS.h:63
Module that kips a configurable number of events between each that it allows through. Note that this module really skips (N-1) events, it uses a simple modular division as its critera. This module will cut down the data sample to 1/N of its original size.
bool replace_all(std::string &in, std::string const &from, std::string const &to)
Replace all occurrences of from in string with to.
std::string unique_topic_name_for(std::string const &extended_topic_name)
Definition: CETDDS.cpp:53
void addToCleanupList(base::Notifiable *notifiable)
Definition: CETDDS.cpp:179
length
Definition: demo0.py:21
std::string create_extended_topic_name_for(std::string const &type_name, std::string const &topic_name)
Definition: CETDDS.cpp:24
Definition: fnal.py:1
OStream cout
Definition: OStream.cxx:6
void removeFromCleanupList(base::Notifiable *notifiable)
Definition: CETDDS.cpp:186
bool string2bool(std::string const &bool_string)
Definition: CETDDS.cpp:61
void dumpReceivedSamples(dds::SampleInfoSeq const &infoSeq, const unsigned int length, const int status)
Definition: CETDDS.cpp:68
std::string create_content_filtered_topic_name_for(std::string const &type_name, std::string const &topic_name)
Definition: CETDDS.cpp:33
#define GENERATE_RMS_NOTCONNECTED_EXCEPTION(msg)
assert(nhit_max >=nhit_nbins)
void checkStatus(DDS::ReturnCode_t status, const char *info)
Definition: CheckStatus.cpp:64
ProcessSignalHandler_t signalHandler
Definition: CETDDS.h:176
c cd(1)
enum BeamMode string