LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lsst.ctrl.events::EventTransmitter Class Reference

Transmit events to the event bus. More...

#include <EventTransmitter.h>

Public Member Functions

 EventTransmitter (const pexPolicy::Policy &policy)
 Configures an EventTransmitter based on Policy contents. More...
 
 EventTransmitter (const std::string &hostName, const std::string &topicName, int hostPort=EventBroker::DEFAULTHOSTPORT)
 Transmits events to the specified host and topic. More...
 
 ~EventTransmitter ()
 Destructor for EventTransmitter. More...
 
std::string getTopicName ()
 get the topic name of this EventTransmitter More...
 
void publishEvent (Event &event)
 

Private Member Functions

void init (const std::string &hostName, const std::string &topicName, int port)
 

Private Attributes

cms::Connection * _connection
 
cms::Session * _session
 
cms::Destination * _destination
 
cms::MessageProducer * _producer
 
std::string _brokerUri
 
activemq::commands::ActiveMQTopic * _topic
 
std::string _topicName
 
bool _turnEventsOff
 

Detailed Description

Transmit events to the event bus.

Definition at line 67 of file EventTransmitter.h.

Constructor & Destructor Documentation

lsst.ctrl.events::EventTransmitter::EventTransmitter ( const pexPolicy::Policy policy)

Configures an EventTransmitter based on Policy contents.

The Policy object is checked for four keywords:

topicName - the name of the topic to send to hostName - the name of the host hosting the event broker hostPort - the port the event broker is listening on turnEventsOff - turn off event transmission

Defaults are:

turnEventsOff = false hostPort = EventSystem::DEFAULTHOSTPORT

The dependencies for these keywords are as follows:

1) If turnEventsOff is specified as true, no further checking is done, and no events will be transmitted.

2) If no topicName is specified, a NotFound exception is thrown

Parameters
policythe policy object to use when building the receiver
Exceptions
throwsNotFoundError if expected keywords are missing in Policy object
throwsRuntimeError if connection to transport mechanism fails

Definition at line 94 of file EventTransmitter.cc.

94  {
95  int hostPort;
96 
97  EventLibrary().initializeLibrary();
98 
99  try {
100  _turnEventsOff = policy.getBool("turnEventsoff");
101  } catch (pexPolicy::NameNotFound& e) {
102  _turnEventsOff = false;
103  }
104  if (_turnEventsOff == true)
105  return;
106 
107  if (!policy.exists("topicName")) {
108  throw LSST_EXCEPT(pexExceptions::NotFoundError, "topicName not found in policy");
109  }
110  _topicName = policy.getString("topicName");
111 
112  std::string hostName;
113  try {
114  hostName = policy.getString("hostName");
115  } catch (pexPolicy::NameNotFound& e) {
116  throw LSST_EXCEPT(pexExceptions::NotFoundError, "hostName not found in policy");
117  }
118 
119  try {
120  hostPort = policy.getInt("hostPort");
121  } catch (pexPolicy::NameNotFound& e) {
122  hostPort = EventBroker::DEFAULTHOSTPORT;
123  }
124  init(hostName, _topicName, hostPort);
125 }
int getInt(const std::string &name) const
Definition: Policy.h:603
bool exists(const std::string &name) const
Definition: Policy.h:944
bool getBool(const std::string &name) const
Definition: Policy.h:589
const std::string getString(const std::string &name) const
Definition: Policy.h:631
static const int DEFAULTHOSTPORT
Definition: EventBroker.h:43
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
void init(const std::string &hostName, const std::string &topicName, int port)
lsst.ctrl.events::EventTransmitter::EventTransmitter ( const std::string &  hostName,
const std::string &  topicName,
int  hostPort = EventBroker::DEFAULTHOSTPORT 
)

Transmits events to the specified host and topic.

Parameters
hostNamethe machine hosting the message broker
topicNamethe topic to transmit events to
hostPortthe port number which the message broker is listening to
Exceptions
throwsRuntimeError if local socket can't be created
throwsRuntimeError if connect to local socket fails
throwsRuntimeError if connect to remote ActiveMQ host fails

Definition at line 136 of file EventTransmitter.cc.

136  {
137  EventLibrary().initializeLibrary();
138 
139  _turnEventsOff = false;
140  init(hostName, topicName, hostPort);
141 }
void init(const std::string &hostName, const std::string &topicName, int port)
lsst.ctrl.events::EventTransmitter::~EventTransmitter ( )

Destructor for EventTransmitter.

Definition at line 226 of file EventTransmitter.cc.

226  {
227 
228 /*
229  // Destroy resources.
230  try {
231  if( _destination != NULL )
232  delete _destination;
233  } catch ( cms::CMSException& e ) {
234  e.printStackTrace();
235  }
236  _destination = NULL;
237 */
238  if (_topic != NULL)
239  delete _topic;
240 
241  try {
242  if( _producer != NULL )
243  delete _producer;
244  } catch ( cms::CMSException& e ) {
245  e.printStackTrace();
246  }
247  _producer = NULL;
248 
249  // Close open resources.
250  try {
251  if( _session != NULL )
252  _session->close();
253  if( _connection != NULL )
254  _connection->close();
255  } catch ( cms::CMSException& e ) {
256  e.printStackTrace();
257  }
258 
259  try {
260  if( _session != NULL )
261  delete _session;
262  } catch ( cms::CMSException& e ) {
263  e.printStackTrace();
264  }
265  _session = NULL;
266 
267  try {
268  if( _connection != NULL )
269  delete _connection;
270  } catch ( cms::CMSException& e ) {
271  e.printStackTrace();
272  }
273  _connection = NULL;
274 }
activemq::commands::ActiveMQTopic * _topic
cms::MessageProducer * _producer

Member Function Documentation

std::string lsst.ctrl.events::EventTransmitter::getTopicName ( )

get the topic name of this EventTransmitter

Definition at line 220 of file EventTransmitter.cc.

220  {
221  return _topicName;
222 }
void lsst.ctrl.events::EventTransmitter::init ( const std::string &  hostName,
const std::string &  topicName,
int  hostPort 
)
private

private initialization method for configuring EventTransmitter

Definition at line 145 of file EventTransmitter.cc.

145  {
146  _connection = NULL;
147  _session = NULL;
148  // _destination = NULL;
149  _producer = NULL;
150  _topicName = topicName;
151  _topic = NULL;
152 
153  if (_turnEventsOff == true)
154  return;
155 
156  // set up a connection to the ActiveMQ server for message transmission
157  try {
158  std::stringstream ss;
159 
160  ss << hostPort;
161 
162  /*
163  * Create a ConnectionFactory to connect to hostName, and
164  * create a topic for this.
165  */
166  string brokerUri = "tcp://"+hostName+":"+ss.str()+"?wireFormat=openwire&transport.useAsyncSend=true";
167 
168  activemq::core::ActiveMQConnectionFactory* connectionFactory =
169  new activemq::core::ActiveMQConnectionFactory( brokerUri );
170 
171  _connection = 0;
172  try {
173  _connection = connectionFactory->createConnection();
174  _connection->start();
175  delete connectionFactory;
176  }
177  catch (cms::CMSException& e) {
178  delete connectionFactory;
179  std::string msg("Failed to connect to broker: ");
180  msg += e.getMessage();
181  msg += " (is broker running?)";
182  throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
183  }
184 
185  _session = _connection->createSession( cms::Session::AUTO_ACKNOWLEDGE );
186 
187  // Create the destination topic
188  //_destination = _session->createTopic( topicName );
189 
190  // Create Topic
191  _topic = new activemq::commands::ActiveMQTopic(_topicName);
192 
193 
194  // Create a MessageProducer from the Session to the Topic or Queue
195  _producer = _session->createProducer(NULL);
196  _producer->setDeliveryMode( cms::DeliveryMode::NON_PERSISTENT );
197  } catch ( cms::CMSException& e ) {
198  throw LSST_EXCEPT(pexExceptions::RuntimeError, std::string("Trouble creating EventTransmitter: ") + e.getMessage());
199  }
200 }
activemq::commands::ActiveMQTopic * _topic
cms::MessageProducer * _producer
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
void lsst.ctrl.events::EventTransmitter::publishEvent ( Event event)

Definition at line 202 of file EventTransmitter.cc.

202  {
203  long long pubtime;
204  cms::TextMessage* message = _session->createTextMessage();
205 
206  event.marshall(message);
207 
208  message->setStringProperty("TOPIC", _topicName);
209 
210  // wait until the last moment to timestamp publication time
211  pubtime = dafBase::DateTime::now().nsecs();
212  message->setLongProperty("PUBTIME", pubtime);
213 
214  _producer->send(_topic, message);
215  delete message;
216 }
static DateTime now(void)
Definition: DateTime.cc:553
activemq::commands::ActiveMQTopic * _topic
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
cms::MessageProducer * _producer

Member Data Documentation

std::string lsst.ctrl.events::EventTransmitter::_brokerUri
private

Definition at line 95 of file EventTransmitter.h.

cms::Connection* lsst.ctrl.events::EventTransmitter::_connection
private

Definition at line 83 of file EventTransmitter.h.

cms::Destination* lsst.ctrl.events::EventTransmitter::_destination
private

Definition at line 89 of file EventTransmitter.h.

cms::MessageProducer* lsst.ctrl.events::EventTransmitter::_producer
private

Definition at line 92 of file EventTransmitter.h.

cms::Session* lsst.ctrl.events::EventTransmitter::_session
private

Definition at line 86 of file EventTransmitter.h.

activemq::commands::ActiveMQTopic* lsst.ctrl.events::EventTransmitter::_topic
private

Definition at line 97 of file EventTransmitter.h.

std::string lsst.ctrl.events::EventTransmitter::_topicName
private

Definition at line 98 of file EventTransmitter.h.

bool lsst.ctrl.events::EventTransmitter::_turnEventsOff
private

Definition at line 101 of file EventTransmitter.h.


The documentation for this class was generated from the following files: