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
EventSystem.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
33 #include <iomanip>
34 #include <sstream>
35 #include <stdexcept>
36 
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <netdb.h>
43 
45 #include "lsst/pex/exceptions.h"
48 #include "lsst/ctrl/events/Event.h"
49 #include "lsst/ctrl/events/Host.h"
53 
54 namespace pexExceptions =lsst::pex::exceptions;
55 
56 using namespace std;
57 
58 namespace lsst {
59 namespace ctrl {
60 namespace events {
61 
62 EventSystem::EventSystem() {
64 }
65 
66 EventSystem::~EventSystem() {
67 }
68 
69 EventSystem& EventSystem::getDefaultEventSystem() {
70  static EventSystem eventSystem;
71  return eventSystem;
72 
73 }
74 
75 EventSystem *EventSystem::defaultEventSystem = 0;
76 list<boost::shared_ptr<EventTransmitter> >EventSystem::_transmitters;
77 list<boost::shared_ptr<EventReceiver> >EventSystem::_receivers;
78 
79 void EventSystem::createTransmitter(std::string const& hostName, std::string const& topicName, int hostPort) {
80  boost::shared_ptr<EventTransmitter> transmitter;
81  if ((transmitter = getTransmitter(topicName)) == 0) {
82  boost::shared_ptr<EventTransmitter> transmitter(new EventTransmitter(hostName, topicName, hostPort));
83  _transmitters.push_back(transmitter);
84  return;
85  }
86  throw LSST_EXCEPT(pexExceptions::RuntimeError, "topic "+ topicName + " is already registered with EventSystem");
87 }
88 
89 void EventSystem::createReceiver(std::string const& hostName, std::string const& topicName, int hostPort) {
90  boost::shared_ptr<EventReceiver> receiver;
91  if ((receiver = getReceiver(topicName)) == 0) {
92  boost::shared_ptr<EventReceiver> receiver(new EventReceiver(hostName, topicName, hostPort));
93  _receivers.push_back(receiver);
94  return;
95  }
96  throw LSST_EXCEPT(pexExceptions::RuntimeError, "topic "+ topicName + " is already registered with EventSystem");
97 }
98 
99 void EventSystem::createReceiver(std::string const& hostName, std::string const& topicName, std::string const& selector, int hostPort) {
100  boost::shared_ptr<EventReceiver> receiver(new EventReceiver(hostName, topicName, selector, hostPort));
101  _receivers.push_back(receiver);
102 }
103 
104 
105 void EventSystem::publishEvent(std::string const& topicName, Event& event) {
106  boost::shared_ptr<EventTransmitter> transmitter;
107  if ((transmitter = getTransmitter(topicName)) == 0) {
108  throw LSST_EXCEPT(pexExceptions::RuntimeError, "topic "+ topicName + " is not registered with EventSystem");
109  }
110  transmitter->publishEvent(event);
111 }
112 
115 boost::shared_ptr<EventTransmitter> EventSystem::getTransmitter(std::string const& name) {
116  list<boost::shared_ptr<EventTransmitter> >::iterator i;
117  for (i = _transmitters.begin(); i != _transmitters.end(); i++) {
118  if ((*i)->getTopicName() == name) {
119  return *i;
120  }
121  }
122  return boost::shared_ptr<EventTransmitter>();
123 }
124 
125 
126 Event* EventSystem::receiveEvent(std::string const& topicName) {
127  return receiveEvent(topicName, EventReceiver::infiniteTimeout);
128 }
129 
130 Event* EventSystem::receiveEvent(std::string const& topicName, const long timeout) {
131  boost::shared_ptr<EventReceiver> receiver;
132  if ((receiver = getReceiver(topicName)) == 0) {
133  throw LSST_EXCEPT(pexExceptions::RuntimeError, "Topic "+ topicName +" is not registered with EventSystem");
134  }
135 
136  return receiver->receiveEvent(timeout);
137 }
138 
139 LocationId::Ptr EventSystem::createOriginatorId() const {
140  return LocationId::Ptr(new LocationId());
141 }
142 
145 boost::shared_ptr<EventReceiver> EventSystem::getReceiver(std::string const& name) {
146  list<boost::shared_ptr<EventReceiver> >::iterator i;
147  for (i = _receivers.begin(); i != _receivers.end(); i++) {
148  if ((*i)->getTopicName() == name)
149  return *i;
150  }
151  return boost::shared_ptr<EventReceiver>();
152 }
153 
154 
155 StatusEvent* EventSystem::castToStatusEvent(Event* event) {
156  return (StatusEvent *)event;
157 }
158 
159 CommandEvent* EventSystem::castToCommandEvent(Event* event) {
160  return (CommandEvent *)event;
161 }
162 
163 }}}
Singleton use to make sure the events library is initialized.
Definition: EventLibrary.h:55
Receive events from the event bus.
Definition: EventReceiver.h:59
table::Key< std::string > name
Definition: ApCorrMap.cc:71
defines the Host class
defines the LocationId class
Include files required for standard LSST Exception handling.
boost::shared_ptr< LocationId > Ptr
Definition: LocationId.h:52
Representation of an LSST Event.
Definition: StatusEvent.h:62
Representation of an LSST CommandEvent.
Definition: CommandEvent.h:63
defines the EventLibrary class
Transmit events to the event bus.
defines the CommandEvent class
defines the Event class
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Representation of an LSST Event.
Definition: Event.h:62
Represent process that created an event.
Definition: LocationId.h:49
defines the StatusEvent class
static void initializeLibrary()
initialize the ActiveMQ library, but only do it once.
Definition: EventLibrary.cc:50
Interface for PropertySet class.
This object allows creation of the system&#39;s event transmitters and receivers, which can be specified ...
Definition: EventSystem.h:65
defines the EventSystem class