39 #include <sys/socket.h>
45 #include "boost/scoped_array.hpp"
46 #include "boost/property_tree/ptree.hpp"
47 #include "boost/property_tree/json_parser.hpp"
48 #include "boost/foreach.hpp"
58 #include "activemq/core/ActiveMQConnectionFactory.h"
60 namespace pexExceptions = lsst::pex::exceptions;
61 namespace dafBase = lsst::daf::base;
65 using std::numeric_limits;
72 const std::string Event::TYPE =
"TYPE";
73 const std::string Event::EVENTTIME =
"EVENTTIME";
74 const std::string Event::RUNID =
"RUNID";
75 const std::string Event::STATUS =
"STATUS";
76 const std::string Event::TOPIC =
"TOPIC";
77 const std::string Event::PUBTIME =
"PUBTIME";
79 const std::string Event::UNINITIALIZED =
"uninitialized";
86 _keywords.insert(TYPE);
87 _keywords.insert(EVENTTIME);
88 _keywords.insert(STATUS);
89 _keywords.insert(TOPIC);
90 _keywords.insert(PUBTIME);
93 Event::Event(cms::TextMessage *msg) {
95 vector<std::string>names = msg->getPropertyNames();
97 _psp = processTextMessage(msg);
99 for (vector<std::string>::iterator
name = names.begin();
name != names.end(); ++
name) {
100 _keywords.insert(*
name);
103 _psp->set(EVENTTIME, msg->getCMSTimestamp());
105 for (vector<std::string>::iterator n = names.begin(); n != names.end(); ++n) {
106 std::string
const&
name = *n;
107 cms::Message::ValueType vType = msg->getPropertyValueType(name);
109 case cms::Message::NULL_TYPE:
110 _psp->set(name, NULL);
112 case cms::Message::BOOLEAN_TYPE:
113 _psp->set(name, msg->getBooleanProperty(name));
115 case cms::Message::BYTE_TYPE:
116 case cms::Message::CHAR_TYPE:
117 _psp->set(name, msg->getByteProperty(name));
119 case cms::Message::SHORT_TYPE:
120 _psp->set(name, msg->getShortProperty(name));
122 case cms::Message::INTEGER_TYPE:
123 _psp->set(name, msg->getIntProperty(name));
125 case cms::Message::LONG_TYPE:
126 _psp->set(name, msg->getLongProperty(name));
128 case cms::Message::DOUBLE_TYPE:
129 _psp->set(name, msg->getDoubleProperty(name));
131 case cms::Message::FLOAT_TYPE:
132 _psp->set(name, msg->getFloatProperty(name));
134 case cms::Message::STRING_TYPE:
135 _psp->set(name, msg->getStringProperty(name));
137 case cms::Message::BYTE_ARRAY_TYPE:
138 case cms::Message::UNKNOWN_TYPE:
140 std::string msg(
"Data type represented by "+name+
" is not permitted supported");
141 throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
148 vector<std::string> Event::getFilterablePropertyNames() {
149 vector<std::string> _names;
150 set<std::string>::iterator keyIterator;
151 for (keyIterator = _keywords.begin(); keyIterator != _keywords.end(); keyIterator++) {
152 _names.push_back(*keyIterator);
157 vector<std::string> Event::getCustomPropertyNames() {
158 vector<std::string> names = _psp->names();
160 vector<std::string>::iterator nameIterator;
161 set<std::string>::iterator keyIterator;
163 for (nameIterator = names.begin(); nameIterator != names.end();) {
164 keyIterator = _keywords.find(*nameIterator);
165 if (keyIterator == _keywords.end())
168 names.erase(nameIterator);
174 const std::string empty;
176 _constructor(empty, ps, p);
180 const std::string empty;
181 _constructor(empty, ps, filterable);
186 _constructor(runId, *psp, p);
191 _constructor(runId, ps, p);
195 _constructor(runId, ps, filterable);
207 if (!_psp->exists(STATUS)) {
208 _psp->set(STATUS,
"unknown");
211 if (!_psp->exists(EVENTTIME)) {
216 if (!runId.empty()) {
217 _keywords.insert(RUNID);
218 _psp->set(RUNID, runId);
222 _psp->set(TYPE, EventTypes::EVENT);
225 _psp->set(TOPIC, Event::UNINITIALIZED);
228 _psp->set(PUBTIME, (
long long)0);
231 vector<std::string> names = filterable.
names();
233 for (vector<std::string>::iterator
name = names.begin();
name != names.end(); ++
name) {
234 _keywords.insert(*
name);
237 _psp->combine(filterable.PropertySet::deepCopy());
241 void Event::populateHeader(cms::TextMessage* msg)
const {
242 set<std::string>::iterator keyIterator;
243 for (keyIterator = _keywords.begin(); keyIterator != _keywords.end(); keyIterator++) {
244 std::string
const&
name = *keyIterator;
245 std::type_info
const& t = _psp->typeOf(name);
246 if (t ==
typeid(
bool)) {
247 msg->setBooleanProperty(name, _psp->get<
bool>(name));
248 }
else if (t ==
typeid(
short)) {
249 msg->setShortProperty(name, _psp->get<
short>(name));
250 }
else if (t ==
typeid(
int)) {
251 msg->setIntProperty(name, _psp->get<
int>(name));
252 }
else if (t ==
typeid(
long)) {
253 msg->setLongProperty(name, _psp->get<
long>(name));
254 }
else if (t ==
typeid(
long long)) {
255 msg->setLongProperty(name, _psp->get<
long long>(name));
256 }
else if (t ==
typeid(
double)) {
257 msg->setDoubleProperty(name, _psp->get<
double>(name));
258 }
else if (t ==
typeid(
float)) {
259 msg->setFloatProperty(name, _psp->get<
float>(name));
260 }
else if (t ==
typeid(std::string)) {
261 msg->setStringProperty(name, _psp->get<std::string>(name));
263 std::string msg(
"Data type represented in "+ name +
" is not permitted in event header");
264 throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
270 long long Event::getEventTime() {
271 return _psp->get<
long long>(EVENTTIME);
274 void Event::setEventTime(
long long nsecs) {
275 _psp->set(EVENTTIME, nsecs);
278 void Event::updateEventTime() {
283 std::string Event::getEventDate() {
284 long long eventTime = _psp->get<
long long>(EVENTTIME);
287 struct tm gmTime = dateTime.gmtime();
288 return asctime(&gmTime);
295 set<std::string>::iterator keyIterator;
296 for (keyIterator = _keywords.begin(); keyIterator != _keywords.end(); keyIterator++)
297 psp->remove(*keyIterator);
310 void Event::setPubTime(
long long t) {
311 _psp->set(PUBTIME, t);
314 long long Event::getPubTime() {
315 return _psp->get<
long long>(PUBTIME);
318 std::string Event::getPubDate() {
319 long long _pubTime = _psp->get<
long long>(PUBTIME);
321 return std::string();
325 struct tm pubTime = dateTime.
gmtime();
326 return asctime(&pubTime);
329 std::string Event::getRunId() {
330 if (_psp->exists(RUNID))
331 return _psp->get<std::string>(RUNID);
332 throw LSST_EXCEPT(pexExceptions::RuntimeError, std::string(
"property RUNID not found"));
335 std::string Event::getType() {
336 return _psp->get<std::string>(TYPE);
339 std::string Event::getStatus() {
340 return _psp->get<std::string>(STATUS);
343 void Event::setStatus(std::string
status) {
344 return _psp->set(STATUS, status);
347 void Event::setTopic(std::string topic) {
348 _psp->set(TOPIC, topic);
351 std::string Event::getTopic() {
352 return _psp->get<std::string>(TOPIC);
355 void Event::marshall(cms::TextMessage *msg) {
359 psp = getCustomPropertySet();
360 std::string payload = marshall(*psp);
361 msg->setText(payload);
364 template<
typename T>
void Event::add(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child) {
368 typename std::vector<T>::iterator
iter;
369 for (iter = vec.begin(); iter != vec.end(); iter++) {
370 boost::property_tree::ptree pt;
372 child.put_child(name, pt);
377 std::vector<std::string> names = ps.
paramNames(
false);
379 boost::property_tree::ptree child;
381 for (vector<std::string>::iterator n = names.begin(); n != names.end(); ++n) {
382 std::string
name = *n;
384 if (ps.
typeOf(name) ==
typeid(bool)) {
385 add<bool>(
name,
"bool", ps, child);
386 }
else if (ps.
typeOf(name) ==
typeid(long)) {
387 add<long>(
name,
"long", ps, child);
388 }
else if (ps.
typeOf(name) ==
typeid(
long long)) {
389 add<long long>(
name,
"long long", ps, child);
390 }
else if (ps.
typeOf(name) ==
typeid(int)) {
391 add<int>(
name,
"int", ps, child);
392 }
else if (ps.
typeOf(name) ==
typeid(float)) {
393 add<float>(
name,
"float", ps, child);
394 }
else if (ps.
typeOf(name) ==
typeid(double)) {
395 add<double>(
name,
"double", ps, child);
396 }
else if (ps.
typeOf(name) ==
typeid(std::string)) {
397 add<std::string>(
name,
"string", ps, child);
400 std::vector<lsst::daf::base::DateTime>::iterator
iter;
401 for (iter = vec.begin(); iter != vec.end(); iter++) {
402 boost::property_tree::ptree pt;
403 pt.put(
"datetime", (*iter).nsecs());
404 child.put_child(name, pt);
407 std::string msg(
"Couldn't marshall "+name);
408 throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
411 std::ostringstream payload;
412 write_json(payload, child,
false);
414 return payload.str();
420 if (textMessage == NULL)
423 std::string text = textMessage->getText();
425 return unmarshall(text);
435 bool Event::addDataItem(std::string
const& typeInfo, boost::property_tree::ptree& item, std::string
const& key,
PropertySet& ps) {
436 if (typeInfo ==
"string") {
437 std::string value = item.get_value<std::string>();
439 }
else if (typeInfo ==
"bool") {
440 bool value = item.get_value<
bool>();
442 }
else if (typeInfo ==
"long") {
443 long value = item.get_value<
long>();
445 }
else if (typeInfo ==
"long long") {
446 long long value = item.get_value<
long long>();
448 }
else if (typeInfo ==
"int") {
449 int value = item.get_value<
int>();
451 }
else if (typeInfo ==
"float") {
452 float value = item.get_value<
float>();
454 }
else if (typeInfo ==
"double") {
455 double value = item.get_value<
double>();
457 }
else if (typeInfo ==
"datetime") {
458 long long value = item.get_value<
long long>();
474 BOOST_FOREACH(boost::property_tree::ptree::value_type
const &v, child.get_child(
"")) {
475 std::string label = v.first;
476 BOOST_FOREACH(boost::property_tree::ptree::value_type &v2, child.get_child(label)) {
477 const bool b = addDataItem(v2.first, v2.second, label, *psp);
494 boost::property_tree::ptree pt;
495 std::istringstream is (text);
500 BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt) {
501 std::string key = v.first;
503 boost::property_tree::ptree child = v.second;
504 BOOST_FOREACH(boost::property_tree::ptree::value_type &v2, child) {
505 std::string key2 = v2.first;
507 const bool b = addDataItem(key2, v2.second, key, *psp);
509 std::string value = v2.second.get_value<std::string>();
523 template void Event::add<bool>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
525 template void Event::add<int>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
527 template void Event::add<float>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
529 template void Event::add<double>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
531 template void Event::add<long>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
533 template void Event::add<long long>(std::string
const&
name, std::string
const& tag,
PropertySet const& ps, boost::property_tree::ptree& child);
535 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
Include files required for standard LSST Exception handling.
boost::shared_ptr< PropertySet > Ptr
std::vector< std::string > names(bool topLevelOnly=true) const
defines the EventLibrary class
Interface for DateTime class.
#define LSST_EXCEPT(type,...)
std::type_info const & typeOf(std::string const &name) const
Class for storing generic metadata.
virtual Ptr deepCopy(void) const
struct tm gmtime(void) const
afw::table::Key< double > b
Interface for PropertySet class.
void add(std::string const &name, T const &value)
std::vector< T > getArray(std::string const &name) const