34 #include "boost/property_tree/ptree.hpp"
35 #include "boost/property_tree/json_parser.hpp"
36 #include "boost/foreach.hpp"
46 #include "activemq/core/ActiveMQConnectionFactory.h"
48 namespace pexExceptions = lsst::pex::exceptions;
49 namespace dafBase = lsst::daf::base;
53 using std::numeric_limits;
60 const std::string Event::TYPE =
"TYPE";
61 const std::string Event::EVENTTIME =
"EVENTTIME";
62 const std::string Event::RUNID =
"RUNID";
63 const std::string Event::STATUS =
"STATUS";
64 const std::string Event::TOPIC =
"TOPIC";
65 const std::string Event::QUEUE =
"QUEUE";
66 const std::string Event::PUBTIME =
"PUBTIME";
68 const std::string Event::UNINITIALIZED =
"uninitialized";
75 _keywords.insert(TYPE);
76 _keywords.insert(EVENTTIME);
77 _keywords.insert(STATUS);
78 _keywords.insert(TOPIC);
79 _keywords.insert(PUBTIME);
83 Event::Event(cms::TextMessage *msg) {
85 vector<std::string>names = msg->getPropertyNames();
87 _psp = processTextMessage(msg);
89 for (std::string
name : names) {
90 _keywords.insert(
name);
93 _psp->set(EVENTTIME, msg->getCMSTimestamp());
95 for (std::string n : names) {
97 std::string
const&
name = n;
98 cms::Message::ValueType vType = msg->getPropertyValueType(name);
100 case cms::Message::NULL_TYPE:
101 _psp->set(name, NULL);
103 case cms::Message::BOOLEAN_TYPE:
104 _psp->set(name, msg->getBooleanProperty(name));
106 case cms::Message::BYTE_TYPE:
107 case cms::Message::CHAR_TYPE:
108 _psp->set(name, msg->getByteProperty(name));
110 case cms::Message::SHORT_TYPE:
111 _psp->set(name, msg->getShortProperty(name));
113 case cms::Message::INTEGER_TYPE:
114 _psp->set(name, msg->getIntProperty(name));
116 case cms::Message::LONG_TYPE:
117 _psp->set(name, msg->getLongProperty(name));
119 case cms::Message::DOUBLE_TYPE:
120 _psp->set(name, msg->getDoubleProperty(name));
122 case cms::Message::FLOAT_TYPE:
123 _psp->set(name, msg->getFloatProperty(name));
125 case cms::Message::STRING_TYPE:
126 _psp->set(name, msg->getStringProperty(name));
128 case cms::Message::BYTE_ARRAY_TYPE:
129 case cms::Message::UNKNOWN_TYPE:
131 std::string msg(
"Data type represented by "+name+
" is not permitted supported");
132 throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
138 vector<std::string> Event::getFilterablePropertyNames() {
139 vector<std::string> _names;
140 for (std::string keyIterator : _keywords) {
141 _names.push_back(keyIterator);
146 vector<std::string> Event::getCustomPropertyNames() {
147 vector<std::string> names = _psp->names();
149 vector<std::string>::iterator nameIterator;
150 set<std::string>::iterator keyIterator;
152 for (nameIterator = names.begin(); nameIterator != names.end();) {
153 keyIterator = _keywords.find(*nameIterator);
154 if (keyIterator == _keywords.end())
157 names.erase(nameIterator);
163 const std::string empty;
165 _constructor(empty, ps, p);
169 const std::string empty;
170 _constructor(empty, ps, filterable);
175 _constructor(runId, *psp, p);
180 _constructor(runId, ps, p);
184 _constructor(runId, ps, filterable);
196 if (!_psp->exists(STATUS)) {
197 _psp->set(STATUS,
"unknown");
200 if (!_psp->exists(EVENTTIME)) {
205 if (!runId.empty()) {
206 _keywords.insert(RUNID);
207 _psp->set(RUNID, runId);
210 if (!_psp->exists(TYPE))
211 _psp->set(TYPE, EventTypes::EVENT);
214 _psp->set(TOPIC, Event::UNINITIALIZED);
217 _psp->set(PUBTIME, (
long long)0);
220 vector<std::string> names = filterable.
names();
222 for (std::string
name : names) {
223 _keywords.insert(
name);
226 _psp->combine(filterable.PropertySet::deepCopy());
230 void Event::populateHeader(cms::TextMessage* msg)
const {
231 for (std::string
name : _keywords) {
232 std::type_info
const& t = _psp->typeOf(
name);
233 if (t ==
typeid(
bool)) {
234 msg->setBooleanProperty(
name, _psp->get<
bool>(
name));
235 }
else if (t ==
typeid(
short)) {
236 msg->setShortProperty(
name, _psp->get<
short>(
name));
237 }
else if (t ==
typeid(
int)) {
238 msg->setIntProperty(
name, _psp->get<
int>(
name));
239 }
else if (t ==
typeid(
long)) {
240 msg->setLongProperty(
name, _psp->get<
long>(
name));
241 }
else if (t ==
typeid(
long long)) {
242 msg->setLongProperty(
name, _psp->get<
long long>(
name));
243 }
else if (t ==
typeid(
double)) {
244 msg->setDoubleProperty(
name, _psp->get<
double>(
name));
245 }
else if (t ==
typeid(
float)) {
246 msg->setFloatProperty(
name, _psp->get<
float>(
name));
247 }
else if (t ==
typeid(std::string)) {
248 msg->setStringProperty(
name, _psp->get<std::string>(
name));
250 std::string msg(
"Data type represented in "+
name +
" is not permitted in event header");
251 throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
257 long long Event::getEventTime() {
258 return _psp->get<
long long>(EVENTTIME);
261 void Event::setEventTime(
long long nsecs) {
262 _psp->set(EVENTTIME, nsecs);
265 void Event::updateEventTime() {
270 std::string Event::getEventDate() {
271 long long eventTime = _psp->get<
long long>(EVENTTIME);
275 return asctime(&gmTime);
282 for (std::string keyword : _keywords) {
283 psp->remove(keyword);
297 void Event::setPubTime(
long long t) {
298 _psp->set(PUBTIME, t);
301 long long Event::getPubTime() {
302 return _psp->get<
long long>(PUBTIME);
305 std::string Event::getPubDate() {
306 long long _pubTime = _psp->get<
long long>(PUBTIME);
308 return std::string();
313 return asctime(&pubTime);
316 std::string Event::getRunId() {
317 if (_psp->exists(RUNID))
318 return _psp->get<std::string>(RUNID);
319 throw LSST_EXCEPT(pexExceptions::RuntimeError, std::string(
"property RUNID not found"));
322 void Event::setRunId(std::string runid) {
323 _keywords.insert(RUNID);
324 _psp->set(RUNID, runid);
327 std::string Event::getType() {
328 return _psp->get<std::string>(TYPE);
331 std::string Event::getStatus() {
332 return _psp->get<std::string>(STATUS);
335 void Event::setStatus(std::string
status) {
336 _psp->set(STATUS, status);
339 void Event::setTopic(std::string topic) {
340 _psp->set(TOPIC, topic);
343 std::string Event::getTopic() {
344 return _psp->get<std::string>(TOPIC);
347 void Event::marshall(cms::TextMessage *msg) {
351 psp = getCustomPropertySet();
352 std::string payload = marshall(*psp);
353 msg->setText(payload);
356 template<
typename T>
void Event::add(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child) {
360 boost::property_tree::ptree children;
361 for (
auto iter: vec) {
362 boost::property_tree::ptree pt;
364 children.push_back(std::make_pair(tag, pt));
366 child.put_child(name, children);
370 std::vector<std::string> names = ps.
paramNames(
false);
372 boost::property_tree::ptree child;
374 for (std::string
name : names) {
376 add<bool>(
name,
"bool", ps, child);
378 add<long>(
name,
"long", ps, child);
379 }
else if (ps.
typeOf(
name) ==
typeid(
long long)) {
380 add<long long>(
name,
"long long", ps, child);
382 add<int>(
name,
"int", ps, child);
383 }
else if (ps.
typeOf(
name) ==
typeid(float)) {
384 add<float>(
name,
"float", ps, child);
385 }
else if (ps.
typeOf(
name) ==
typeid(double)) {
386 add<double>(
name,
"double", ps, child);
387 }
else if (ps.
typeOf(
name) ==
typeid(std::string)) {
388 add<std::string>(
name,
"string", ps, child);
392 boost::property_tree::ptree pt;
393 pt.put(
"datetime", (dateTime).nsecs());
394 child.put_child(
name, pt);
397 std::string msg(
"Couldn't marshall "+
name);
398 throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
401 std::ostringstream payload;
402 write_json(payload, child,
false);
404 return payload.str();
410 if (textMessage == NULL)
413 std::string text = textMessage->getText();
426 bool Event::addDataItem(std::string
const& typeInfo, boost::property_tree::ptree& item, std::string
const& key,
PropertySet& ps) {
427 if (typeInfo ==
"string") {
428 std::string value = item.get_value<std::string>();
430 }
else if (typeInfo ==
"bool") {
431 bool value = item.get_value<
bool>();
433 }
else if (typeInfo ==
"long") {
434 long value = item.get_value<
long>();
436 }
else if (typeInfo ==
"long long") {
437 long long value = item.get_value<
long long>();
439 }
else if (typeInfo ==
"int") {
440 int value = item.get_value<
int>();
442 }
else if (typeInfo ==
"float") {
443 float value = item.get_value<
float>();
445 }
else if (typeInfo ==
"double") {
446 double value = item.get_value<
double>();
448 }
else if (typeInfo ==
"datetime") {
449 long long value = item.get_value<
long long>();
465 BOOST_FOREACH(boost::property_tree::ptree::value_type
const &v, child.get_child(
"")) {
466 std::string label = v.first;
467 BOOST_FOREACH(boost::property_tree::ptree::value_type &v2, child.get_child(label)) {
468 const bool b = addDataItem(v2.first, v2.second, label, *psp);
485 boost::property_tree::ptree pt;
486 std::istringstream is (text);
491 BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt) {
492 std::string key = v.first;
494 boost::property_tree::ptree child = v.second;
495 BOOST_FOREACH(boost::property_tree::ptree::value_type &v2, child) {
496 std::string key2 = v2.first;
498 const bool b = addDataItem(key2, v2.second, key, *psp);
500 std::string value = v2.second.get_value<std::string>();
514 template void Event::add<bool>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
516 template void Event::add<int>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
518 template void Event::add<float>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
520 template void Event::add<double>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
522 template void Event::add<long>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
524 template void Event::add<long long>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
526 template void Event::add<std::string>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
std::vector< std::string > paramNames(bool topLevelOnly=true) const
static DateTime now(void)
Class for handling dates/times, including MJD, UTC, and TAI.
table::Key< std::string > name
size_t nameCount(bool topLevelOnly=true) const
std::vector< std::string > names(bool topLevelOnly=true) const
lsst::daf::base::PropertySet PropertySet
struct tm gmtime(Timescale time) const
defines the EventLibrary class
Interface for DateTime class.
#define LSST_EXCEPT(type,...)
std::type_info const & typeOf(std::string const &name) const
Representation of an LSST Event.
Class for storing generic metadata.
virtual Ptr deepCopy(void) const
afw::table::Key< double > b
Interface for PropertySet class.
Include files required for standard LSST Exception handling.
void add(std::string const &name, T const &value)
std::vector< T > getArray(std::string const &name) const