LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
lsst::ctrl::events::EventReceiver Class Reference

Receive events from the event bus. More...

#include <EventReceiver.h>

Public Member Functions

 EventReceiver (const std::string &hostName, const std::string &topicName, int hostPort=EventBroker::DEFAULTHOSTPORT)
 Receives events from the specified host and topic. More...
 
 EventReceiver (const std::string &hostName, const std::string &topicName, const std::string &selector, int hostPort=EventBroker::DEFAULTHOSTPORT)
 Receives events from the specified host and topic. More...
 
virtual ~EventReceiver ()
 destructor More...
 
EventreceiveEvent ()
 Wait until an Event is received. More...
 
EventreceiveEvent (long timeout)
 wait for a length of time for an event to be received. More...
 
std::string getTopicName ()
 get topic name More...
 

Static Public Attributes

static const long infiniteTimeout = -1
 

Private Member Functions

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

Private Attributes

cms::Connection * _connection
 
cms::Session * _session
 
cms::Destination * _destination
 
cms::MessageConsumer * _consumer
 
std::string _topic
 
std::string _selector
 

Detailed Description

Receive events from the event bus.

Definition at line 59 of file EventReceiver.h.

Constructor & Destructor Documentation

lsst::ctrl::events::EventReceiver::EventReceiver ( const std::string &  hostName,
const std::string &  topicName,
int  hostPort = EventBroker::DEFAULTHOSTPORT 
)

Receives events from the specified host and topic.

Parameters
hostNamethe machine hosting the message broker
topicNamethe topic to receive events from
hostPortthe port the message broker is listening on
Exceptions
throwslsst::pex::exceptions::RuntimeError if connection fails to initialize

Definition at line 60 of file EventReceiver.cc.

60  {
61  init(hostName, topicName, "", hostPort);
62 }
void init(const std::string &hostName, const std::string &topicName, const std::string &selector, int hostPort)
lsst::ctrl::events::EventReceiver::EventReceiver ( const std::string &  hostName,
const std::string &  topicName,
const std::string &  selector,
int  hostPort = EventBroker::DEFAULTHOSTPORT 
)

Receives events from the specified host and topic.

Parameters
hostNamethe machine hosting the message broker
topicNamethe topic to receive events from
selectorthe message selector expression to use. A selector value of "" is equivalent to no selector.
hostPortthe port the message broker is listening on
Exceptions
throwslsst::pex::exceptions::RuntimeError if connection fails to initialize

Definition at line 64 of file EventReceiver.cc.

64  {
65  init(hostName, topicName, selector, hostPort);
66 }
void init(const std::string &hostName, const std::string &topicName, const std::string &selector, int hostPort)
lsst::ctrl::events::EventReceiver::~EventReceiver ( )
virtual

destructor

Definition at line 147 of file EventReceiver.cc.

147  {
148 
149  // Destroy resources.
150  try {
151  if( _destination != NULL )
152  delete _destination;
153  } catch ( cms::CMSException& e ) {
154  e.printStackTrace();
155  }
156  _destination = NULL;
157 
158  try {
159  if( _consumer != NULL )
160  delete _consumer;
161  } catch ( cms::CMSException& e ) {
162  e.printStackTrace();
163  }
164  _consumer = NULL;
165 
166  // Close open resources.
167  try {
168  if( _session != NULL )
169  _session->close();
170  } catch ( cms::CMSException& e ) {
171  e.printStackTrace();
172  }
173  try {
174  if( _connection != NULL )
175  _connection->close();
176  } catch ( cms::CMSException& e ) {
177  e.printStackTrace();
178  }
179 
180  try {
181  if( _session != NULL )
182  delete _session;
183  } catch ( cms::CMSException& e ) {
184  e.printStackTrace();
185  }
186  _session = NULL;
187 
188  try {
189  if( _connection != NULL )
190  delete _connection;
191  } catch ( cms::CMSException& e ) {
192  e.printStackTrace();
193  }
194  _connection = NULL;
195 }
cms::MessageConsumer * _consumer

Member Function Documentation

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

get topic name

Returns
the topic name used by this EventReceiver

Definition at line 143 of file EventReceiver.cc.

143  {
144  return _topic;
145 }
void lsst::ctrl::events::EventReceiver::init ( const std::string &  hostName,
const std::string &  topicName,
const std::string &  selector,
int  hostPort 
)
private

private method for initialization of EventReceiver.

Definition at line 70 of file EventReceiver.cc.

70  {
71 
72  EventLibrary().initializeLibrary();
73  _connection = NULL;
74  _session = NULL;
75  _destination = NULL;
76  _consumer = NULL;
77  _topic = topicName;
78  _selector = selector;
79 
80  try {
81  std::stringstream ss;
82 
83  ss << hostPort;
84 
85  string jmsURL = "tcp://"+hostName+":"+ss.str()+"?wireFormat=openwire";
86 
87  activemqCore::ActiveMQConnectionFactory* connectionFactory =
88  new activemqCore::ActiveMQConnectionFactory( jmsURL );
89 
90  _connection = 0;
91  try {
92  _connection = connectionFactory->createConnection();
93  _connection->start();
94  delete connectionFactory;
95  }
96  catch (cms::CMSException& e) {
97  delete connectionFactory;
98  std::string msg("Failed to connect to broker: ");
99  msg += e.getMessage();
100  msg += " (is broker running?)";
101  throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
102  }
103 
104  _session = _connection->createSession( cms::Session::AUTO_ACKNOWLEDGE );
105 
106  _destination = _session->createTopic( topicName );
107 
108  if (_selector == "")
109  _consumer = _session->createConsumer( _destination );
110  else
111  _consumer = _session->createConsumer( _destination, selector );
112 
113  } catch ( cms::CMSException& e ) {
114  throw LSST_EXCEPT(pexExceptions::RuntimeError, std::string("Trouble creating EventReceiver: ") + e.getMessage());
115  }
116 }
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
cms::MessageConsumer * _consumer
Event * lsst::ctrl::events::EventReceiver::receiveEvent ( )

Wait until an Event is received.

Note
Caller is responsible for deleting received Event.

Definition at line 118 of file EventReceiver.cc.

118  {
120 }
Event * receiveEvent()
Wait until an Event is received.
Event * lsst::ctrl::events::EventReceiver::receiveEvent ( long  timeout)

wait for a length of time for an event to be received.

Parameters
timeoutthe length of time to wait in milliseconds; value of -1 waits indefinately.
Note
Caller is responsible for deleting received Event.

Definition at line 122 of file EventReceiver.cc.

122  {
123  PropertySet::Ptr psp;
124 
125  cms::TextMessage* textMessage;
126  try {
127  cms::Message* msg = _consumer->receive(timeout);
128  if (msg == NULL) return NULL;
129  textMessage = dynamic_cast<cms::TextMessage* >(msg);
130  if (textMessage == NULL)
131  throw LSST_EXCEPT(pexExceptions::RuntimeError, "Unexpected JMS Message type");
132  } catch (activemq::exceptions::ActiveMQException& e) {
133  throw LSST_EXCEPT(pexExceptions::RuntimeError, e.getMessage());
134  }
135 
136 
137  Event* event = EventFactory().createEvent(textMessage);
138  delete textMessage;
139 
140  return event;
141 }
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
cms::MessageConsumer * _consumer

Member Data Documentation

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

Definition at line 110 of file EventReceiver.h.

cms::MessageConsumer* lsst::ctrl::events::EventReceiver::_consumer
private

Definition at line 119 of file EventReceiver.h.

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

Definition at line 116 of file EventReceiver.h.

std::string lsst::ctrl::events::EventReceiver::_selector
private

Definition at line 125 of file EventReceiver.h.

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

Definition at line 113 of file EventReceiver.h.

std::string lsst::ctrl::events::EventReceiver::_topic
private

Definition at line 122 of file EventReceiver.h.

const long lsst::ctrl::events::EventReceiver::infiniteTimeout = -1
static

Definition at line 104 of file EventReceiver.h.


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