RmsSender.h
Go to the documentation of this file.
1 #ifndef _RMS_SENDER_H
2 #define _RMS_SENDER_H
3 
4 #include <rms/base/RmsCloseable.h>
5 #include <rms/base/RmsDestination.h>
6 #include <rms/base/RmsRuntimeException.h>
7 
8 #include <boost/shared_ptr.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <boost/interprocess/smart_ptr/unique_ptr.hpp>
11 
12 #include <string>
13 #include <iostream>
14 
15 namespace gov {
16 
17 namespace fnal {
18 
19 namespace cd {
20 
21 namespace rms {
22 
23 /**
24  * Populates the correlation fields in the target message using the
25  * appropriate fields in the source message.
26  */
27 template <class S, class T>
28 void fillCorrelationFields(S const& sourceMessage, T& targetMessage) {
29  targetMessage.header.correlationId = sourceMessage.header.id;
30  targetMessage.header.correlationTime = sourceMessage.header.sentTime;
31 }
32 
33 /**
34  * This class contains functionality for sending messages of specified
35  * types to a specified destination.
36  *
37  * @author Kurt Biery
38  * @version $Revision: 1.9.14.1 $ $Date: 2019/09/27 00:07:31 $
39  */
40 
41 template <class C, class M>
42 class RmsSender : public base::RmsCloseable {
43 
44 public:
45  RmsSender(boost::shared_ptr<C> parentConnection, base::RmsDestination dest);
46  ~RmsSender();
47 
48  void sendMessage(M& message);
49 
50  void close() throw();
51  bool hasBeenClosed() throw(){return _hasBeenClosed;};
52 private:
53  /**
54  * Handle to the RMS connection
55  */
56  boost::shared_ptr<C> _myConnection;
57 
58  /**
59  * RmsDestination that we are listening to
60  */
61  base::RmsDestination _myDestination;
62 
63  unsigned int _senderHandle;
64 
66 
67 };
68 
69 /**
70  * Constructs an RmsSender instance.
71  *
72  * @param parentConn Connection object which is used for
73  * sending messages.
74  * @param dest The destination that we are sending messages to.
75  * @throws RmsRuntimeException If the destination is invalid.
76  */
77 template <class C, class M> RmsSender<C, M>::
78 RmsSender(boost::shared_ptr<C> parentConn, base::RmsDestination dest) :
79  _myConnection(parentConn),
80  _myDestination(dest),
81  _hasBeenClosed(false)
82 {
83  _myDestination.validate();
84  if (! _myConnection->supportsDestination(_myDestination)) {
86  msg.append("The current connection does not support ");
87  msg.append("the requested destination: ");
88  msg.append(_myDestination.toString());
90  }
91 
92  M dummyObj;
93  _senderHandle = _myConnection->registerSender(dummyObj, _myDestination);
94 }
95 
96 /**
97  * Destructs an RmsSender instance.
98  */
99 template <class C, class M> RmsSender<C, M>::~RmsSender()
100 {
101  try {
102  this->close();
103  }
104  catch (...) {
105  std::cerr << "Exception in RmsSender destructor." << std::endl;
106  }
107 }
108 
109 /**
110  * Sends the specified message. The message argument is modified by this
111  * method - "standard" message fields are filled in as a convenience.
112  *
113  * @param message The message to be sent.
114  * @throws RmsCommunicationException If an error occurs when trying
115  * to send the message to the destination(s).
116  */
117 template <class C, class M> void RmsSender<C, M>::sendMessage(M& message)
118 {
119  try
120  {
121  if (_hasBeenClosed)
122  GENERATE_RMS_NOTCONNECTED_EXCEPTION("RMS connection was closed.");
123 
124  _myConnection->sendMessage(_senderHandle, message);
125  }
127  LOG_EXCEPTION("Caught RmsNotConnectedException in RmsSender::sendMessage(); message=", ex );
128  }
129  catch ( ... ) {
130  this->close();
131 
132  LOG_EXCEPTION("Caught exception in RmsSender::sendMessage(). Closing RMS connection.","");
133  }
134 }
135 
136 /**
137  * Closes the RmsSender.
138  */
139 template <class C, class M> void RmsSender<C, M>::close() throw()
140 {
141  if (_hasBeenClosed) {
142  return;
143  }
144 
145  _hasBeenClosed = true;
146 
147  try {
148  _myConnection->unRegisterSender(_senderHandle);
149  } catch (...) {}
150 
151  _myConnection.reset();
152 
153 #if CETDDS_DEBUG1
154  std::cout << "Closed RmsSender connection" << std::endl;
155 #endif
156 }
157 
158 } // end of namespace rms
159 
160 } // end of namespace cd
161 
162 } // end of namespace fnal
163 
164 } // end of namespace gov
165 
166 #endif
167 // kate: indent-mode cstyle; space-indent on; indent-width 0;
#define S(x, n)
base::RmsDestination _myDestination
Definition: RmsSender.h:61
#define LOG_EXCEPTION(message, what)
OStream cerr
Definition: OStream.cxx:7
boost::shared_ptr< C > _myConnection
Definition: RmsSender.h:51
void sendMessage(M &message)
Definition: RmsSender.h:117
Definition: fnal.py:1
void fillCorrelationFields(S const &sourceMessage, T &targetMessage)
Definition: RmsSender.h:28
#define GENERATE_RMS_RUNTIME_EXCEPTION(msg)
OStream cout
Definition: OStream.cxx:6
#define GENERATE_RMS_NOTCONNECTED_EXCEPTION(msg)
double T
Definition: Xdiff_gwt.C:5
RmsSender(boost::shared_ptr< C > parentConnection, base::RmsDestination dest)
Definition: RmsSender.h:78
c cd(1)
enum BeamMode string