LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 std::string &hostName, const std::string &topicName, int hostPort=EventBroker::DEFAULTHOSTPORT)
 Transmits events to the specified host and topic. More...
 
 ~EventTransmitter ()
 destructor More...
 
std::string getTopicName ()
 get the topic name of this EventTransmitter More...
 
void publishEvent (Event &event)
 Publish an Event to this object's topic. More...
 

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
 

Detailed Description

Transmit events to the event bus.

Definition at line 60 of file EventTransmitter.h.

Constructor & Destructor Documentation

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
RuntimeErrorif local socket can't be created
RuntimeErrorif connect to local socket fails
RuntimeErrorif connect to remote ActiveMQ host fails

Definition at line 64 of file EventTransmitter.cc.

64  {
65  EventLibrary().initializeLibrary();
66 
67  init(hostName, topicName, hostPort);
68 }
void init(const std::string &hostName, const std::string &topicName, int port)
lsst::ctrl::events::EventTransmitter::~EventTransmitter ( )

destructor

Definition at line 147 of file EventTransmitter.cc.

147  {
148 
149  if (_topic != NULL)
150  delete _topic;
151  _topic = NULL;
152 
153  try {
154  if( _producer != NULL )
155  delete _producer;
156  } catch ( cms::CMSException& e ) {
157  e.printStackTrace();
158  }
159  _producer = NULL;
160 
161  // Close open resources.
162  try {
163  if( _session != NULL )
164  _session->close();
165  if( _connection != NULL )
166  _connection->close();
167  } catch ( cms::CMSException& e ) {
168  e.printStackTrace();
169  }
170 
171  try {
172  if( _session != NULL )
173  delete _session;
174  } catch ( cms::CMSException& e ) {
175  e.printStackTrace();
176  }
177  _session = NULL;
178 
179  try {
180  if( _connection != NULL )
181  delete _connection;
182  } catch ( cms::CMSException& e ) {
183  e.printStackTrace();
184  }
185  _connection = NULL;
186 }
activemq::commands::ActiveMQTopic * _topic

Member Function Documentation

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

get the topic name of this EventTransmitter

Returns
a std::string containing the topic name

Definition at line 143 of file EventTransmitter.cc.

143  {
144  return _topicName;
145 }
void lsst::ctrl::events::EventTransmitter::init ( const std::string &  hostName,
const std::string &  topicName,
int  port 
)
private

Definition at line 73 of file EventTransmitter.cc.

73  {
74  _connection = NULL;
75  _session = NULL;
76 
77  _producer = NULL;
78  _topicName = topicName;
79  _topic = NULL;
80 
81  // set up a connection to the ActiveMQ server for message transmission
82  try {
83  std::stringstream ss;
84 
85  ss << hostPort;
86 
87  /*
88  * Create a ConnectionFactory to connect to hostName, and
89  * create a topic for this.
90  */
91  string brokerUri = "tcp://"+hostName+":"+ss.str()+"?wireFormat=openwire&transport.useAsyncSend=true";
92 
93  activemq::core::ActiveMQConnectionFactory* connectionFactory =
94  new activemq::core::ActiveMQConnectionFactory( brokerUri );
95 
96  _connection = 0;
97  try {
98  _connection = connectionFactory->createConnection();
99  _connection->start();
100  delete connectionFactory;
101  }
102  catch (cms::CMSException& e) {
103  delete connectionFactory;
104  std::string msg("Failed to connect to broker: ");
105  msg += e.getMessage();
106  msg += " (is broker running?)";
107  throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
108  }
109 
110  _session = _connection->createSession( cms::Session::AUTO_ACKNOWLEDGE );
111 
112  // Create the destination topic
113  //_destination = _session->createTopic( topicName );
114 
115  // Create Topic
116  _topic = new activemq::commands::ActiveMQTopic(_topicName);
117 
118 
119  // Create a MessageProducer from the Session to the Topic or Queue
120  _producer = _session->createProducer(NULL);
121  _producer->setDeliveryMode( cms::DeliveryMode::NON_PERSISTENT );
122  } catch ( cms::CMSException& e ) {
123  throw LSST_EXCEPT(pexExceptions::RuntimeError, std::string("Trouble creating EventTransmitter: ") + e.getMessage());
124  }
125 }
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
activemq::commands::ActiveMQTopic * _topic
void lsst::ctrl::events::EventTransmitter::publishEvent ( Event event)

Publish an Event to this object's topic.

Parameters
eventan Event to publish

Definition at line 127 of file EventTransmitter.cc.

127  {
128  long long pubtime;
129  cms::TextMessage* message = _session->createTextMessage();
130 
131  event.marshall(message);
132 
133  message->setStringProperty("TOPIC", _topicName);
134 
135  // wait until the last moment to timestamp publication time
136  pubtime = dafBase::DateTime::now().nsecs();
137  message->setLongProperty("PUBTIME", pubtime);
138 
139  _producer->send(_topic, message);
140  delete message;
141 }
static DateTime now(void)
Definition: DateTime.cc:554
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:441
activemq::commands::ActiveMQTopic * _topic

Member Data Documentation

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

Definition at line 108 of file EventTransmitter.h.

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

Definition at line 96 of file EventTransmitter.h.

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

Definition at line 102 of file EventTransmitter.h.

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

Definition at line 105 of file EventTransmitter.h.

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

Definition at line 99 of file EventTransmitter.h.

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

Definition at line 110 of file EventTransmitter.h.

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

Definition at line 111 of file EventTransmitter.h.


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