diff --git a/LCS/MessageBus/include/MessageBus/Message.h b/LCS/MessageBus/include/MessageBus/Message.h index 487ef6688547ee4034a6c889b2e10e602573be2e..b6ada16d93f8221187e4e895756a11db348fc873 100644 --- a/LCS/MessageBus/include/MessageBus/Message.h +++ b/LCS/MessageBus/include/MessageBus/Message.h @@ -45,7 +45,7 @@ class MessageContent { public: // Construct a message - MessageContent() {}; + MessageContent(); // With header info MessageContent( @@ -80,20 +80,52 @@ public: virtual ~MessageContent(); + /* + * A 'Property' is getter/setter for a part of the message content. + * + * It provides a string interface. + */ + class Property { + public: + // normal getters and setters + void set(const std::string &value) { itsContent->setXMLvalue(itsKey, value); } + std::string get() const { return itsContent->getXMLvalue(itsKey); } + + // C++ operator overloading + void operator=(const std::string &value) { set(value); } + operator std::string () const { return get(); } + + bool operator==(const Property &other) const { return (std::string)*this == (std::string)other; } + bool operator==(const std::string &other) const { return (std::string)*this == other; } + bool operator==(const char *other) const { return (std::string)*this == std::string(other); } + + private: + Property(): itsContent(0), itsKey("") {} + + void attach(MessageContent *content, const std::string &key) { itsContent = content; itsKey = key; } + + MessageContent *itsContent; + std::string itsKey; + + friend class MessageContent; + }; + // Return properties of the constructed or received message - std::string system() const { return (getXMLvalue("message/header/system")); } - std::string headerVersion() const { return (getXMLvalue("message/header/version")); } - std::string protocol() const { return (getXMLvalue("message/header/protocol/name")); } - std::string protocolVersion() const { return (getXMLvalue("message/header/protocol/version")); } - std::string from() const { return (getXMLvalue("message/header/source/name")); } - std::string forUser() const { return (getXMLvalue("message/header/source/user")); } - std::string uuid() const { return (getXMLvalue("message/header/source/uuid")); } - std::string summary() const { return (getXMLvalue("message/header/source/summary")); } - std::string timestamp() const { return (getXMLvalue("message/header/source/timestamp")); } - std::string momid() const { return (getXMLvalue("message/header/ids/momid")); } - std::string sasid() const { return (getXMLvalue("message/header/ids/sasid")); } - std::string payload() const { return (getXMLvalue("message/payload")); } - std::string header() const { return (getXMLvalue("message/header")); } + Property system; + Property headerVersion; + Property protocol; + Property protocolVersion; + + Property name; + Property user; + Property uuid; + Property summary; + Property timestamp; + Property momid; + Property sasid; + + Property payload; + Property header; // Return a QPID message with our content qpid::messaging::Message qpidMsg() const; @@ -111,15 +143,23 @@ public: void setXMLvalue(const std::string& key, const std::string& data); private: + void addProperties(); + // -- datamembers -- std::string itsContent; }; inline std::ostream &operator<<(std::ostream &os, const MessageContent &msg) -{ +{ return (msg.print(os)); } +inline std::ostream &operator<<(std::ostream &os, const MessageContent::Property &prop) +{ + os << (std::string)prop; + return os; +} + class Message { public: diff --git a/LCS/MessageBus/src/Message.cc b/LCS/MessageBus/src/Message.cc index a504b834e85745a94bffb7fdbbacdcdc832cccee..a0490fd974684bbf463f85e3205c70aa2b14d99c 100644 --- a/LCS/MessageBus/src/Message.cc +++ b/LCS/MessageBus/src/Message.cc @@ -84,6 +84,11 @@ static string _uuid() { return uuid.str(); } +MessageContent::MessageContent() +{ + addProperties(); +} + MessageContent::MessageContent(const std::string &from, const std::string &forUser, const std::string &summary, @@ -94,29 +99,55 @@ MessageContent::MessageContent(const std::string &from, : itsContent(LOFAR_MSG_TEMPLATE) { - setXMLvalue("message/header/system", LOFAR::system); - setXMLvalue("message/header/version", LOFAR::headerVersion); - setXMLvalue("message/header/protocol/name", protocol); - setXMLvalue("message/header/protocol/version", protocolVersion); - setXMLvalue("message/header/source/name", from); - setXMLvalue("message/header/source/user", forUser); - setXMLvalue("message/header/source/uuid", _uuid()); - setXMLvalue("message/header/source/summary", summary); - setXMLvalue("message/header/source/timestamp", _timestamp()); - setXMLvalue("message/header/ids/momid", momid); - setXMLvalue("message/header/ids/sasid", sasid); + addProperties(); + + this->system = LOFAR::system; + this->headerVersion = LOFAR::headerVersion; + + this->protocol = protocol; + this->protocolVersion = protocolVersion; + this->name = from; + this->user = forUser; + this->uuid = _uuid(); + this->summary = summary; + this->timestamp = _timestamp(); + this->momid = momid; + this->sasid = sasid; } MessageContent::MessageContent(const qpid::messaging::Message &qpidMsg) : itsContent(qpidMsg.getContent()) { + addProperties(); } MessageContent::~MessageContent() { } +void MessageContent::addProperties() +{ + system .attach(this, "message/header/system"); + headerVersion .attach(this, "message/header/version"); + + protocol .attach(this, "message/header/protocol/name"); + protocolVersion.attach(this, "message/header/protocol/version"); + + name .attach(this, "message/header/source/name"); + user .attach(this, "message/header/source/user"); + uuid .attach(this, "message/header/source/uuid"); + + summary .attach(this, "message/header/source/summary"); + timestamp .attach(this, "message/header/source/timestamp"); + + momid .attach(this, "message/header/ids/momid"); + sasid .attach(this, "message/header/ids/sasid"); + + payload .attach(this, "message/payload"); + header .attach(this, "message/header"); +} + qpid::messaging::Message MessageContent::qpidMsg() const { qpid::messaging::Message qpidMsg; @@ -139,27 +170,24 @@ void MessageContent::setTXTPayload (const std::string &payload) std::string MessageContent::short_desc() const { - return formatString("[%s] [sasid %s] %s", uuid().c_str(), sasid().c_str(), summary().c_str()); + return formatString("[%s] [sasid %s] %s", uuid.get().c_str(), sasid.get().c_str(), summary.get().c_str()); } std::ostream& MessageContent::print (std::ostream& os) const { - os << "system : " << system() << endl; - os << "systemversion : " << headerVersion() << endl; - os << "protocolName : " << protocol() << endl; - os << "protocolVersion: " << protocolVersion() << endl; - os << "summary : " << summary() << endl; - os << "timestamp : " << timestamp() << endl; - os << "source : " << from() << endl; - os << "user : " << forUser() << endl; - os << "uuid : " << uuid() << endl; - os << "momid : " << momid() << endl; - os << "sasid : " << sasid() << endl; - os << "payload : " << payload() << endl; - os << "BEGIN FULL PACKET" << endl; - os << itsContent << endl; - os << "END FULL PACKET" << endl; - return (os); + os << "system : " << system << endl; + os << "systemversion : " << headerVersion << endl; + os << "protocolName : " << protocol << endl; + os << "protocolVersion: " << protocolVersion << endl; + os << "summary : " << summary << endl; + os << "timestamp : " << timestamp << endl; + os << "source (name) : " << name << endl; + os << "user : " << user << endl; + os << "uuid : " << uuid << endl; + os << "momid : " << momid << endl; + os << "sasid : " << sasid << endl; + os << "payload : " << payload << endl; + return (os); } string MessageContent::getXMLvalue(const string& key) const diff --git a/LCS/MessageBus/test/tMessage.cc b/LCS/MessageBus/test/tMessage.cc index ae57b47c5bc865b6891582670d459b8f053da919..d06b2d36da9bb6841d7d98a5a8d53dc0a6612d75 100644 --- a/LCS/MessageBus/test/tMessage.cc +++ b/LCS/MessageBus/test/tMessage.cc @@ -36,30 +36,30 @@ SUITE(MessageContent) { TEST(newmsg) { // Create a message from scratch - MessageContent msg("FROM", "FORUSER", "SUMMARY", "PROTOCOL", "1.2", "MOMID", "SASID"); + MessageContent msg("NAME", "USER", "SUMMARY", "PROTOCOL", "1.2", "MOMID", "SASID"); - CHECK_EQUAL("FROM", msg.from()); - CHECK_EQUAL("FORUSER", msg.forUser()); - CHECK_EQUAL("SUMMARY", msg.summary()); - CHECK_EQUAL("PROTOCOL", msg.protocol()); - CHECK_EQUAL("1.2", msg.protocolVersion()); - CHECK_EQUAL("MOMID", msg.momid()); - CHECK_EQUAL("SASID", msg.sasid()); + CHECK_EQUAL("NAME", msg.name.get()); + CHECK_EQUAL("USER", msg.user.get()); + CHECK_EQUAL("SUMMARY", msg.summary.get()); + CHECK_EQUAL("PROTOCOL", msg.protocol.get()); + CHECK_EQUAL("1.2", msg.protocolVersion.get()); + CHECK_EQUAL("MOMID", msg.momid.get()); + CHECK_EQUAL("SASID", msg.sasid.get()); } TEST(existingmsg) { - MessageContent orig("FROM", "FORUSER", "SUMMARY", "PROTOCOL", "1.2", "MOMID", "SASID"); + MessageContent orig("NAME", "USER", "SUMMARY", "PROTOCOL", "1.2", "MOMID", "SASID"); // Create a qpid message and parse it again MessageContent copy(orig.qpidMsg()); - CHECK_EQUAL(orig.from(), copy.from()); - CHECK_EQUAL(orig.forUser(), copy.forUser()); - CHECK_EQUAL(orig.summary(), copy.summary()); - CHECK_EQUAL(orig.protocol(), copy.protocol()); - CHECK_EQUAL(orig.protocolVersion(), copy.protocolVersion()); - CHECK_EQUAL(orig.momid(), copy.momid()); - CHECK_EQUAL(orig.sasid(), copy.sasid()); + CHECK_EQUAL(orig.name, copy.name); + CHECK_EQUAL(orig.user, copy.user); + CHECK_EQUAL(orig.summary, copy.summary); + CHECK_EQUAL(orig.protocol, copy.protocol); + CHECK_EQUAL(orig.protocolVersion, copy.protocolVersion); + CHECK_EQUAL(orig.momid, copy.momid); + CHECK_EQUAL(orig.sasid, copy.sasid); } }