diff --git a/include/Swiften/Elements/StatsPayload.cpp b/include/Swiften/Elements/StatsPayload.cpp new file mode 100644 index 00000000..e3718721 --- /dev/null +++ b/include/Swiften/Elements/StatsPayload.cpp @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +namespace Swift { + +StatsPayload::StatsPayload() { +} + +} diff --git a/include/Swiften/Elements/StatsPayload.h b/include/Swiften/Elements/StatsPayload.h new file mode 100644 index 00000000..aa1619fe --- /dev/null +++ b/include/Swiften/Elements/StatsPayload.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +#include +#include + +namespace Swift { + class StatsPayload : public Payload { + public: + class Item { + public: + Item(const std::string &name = "", const std::string &units = "", const std::string &value = "") : + name(name), units(units), value(value) { } + + void setName(const std::string &name) { + this->name = name; + } + + const std::string &getName() const { + return name; + } + + void setUnits(const std::string &units) { + this->units = units; + } + + const std::string &getUnits() const { + return units; + } + + void setValue(const std::string &value) { + this->value = value; + } + + const std::string &getValue() const { + return value; + } + + private: + std::string name; + std::string units; + std::string value; + }; + + typedef std::vector StatsPayloadItems; + + StatsPayload(); + + void addItem(const StatsPayload::Item &item) { + items.push_back(item); + } + + const StatsPayloadItems &getItems() const { + return items; + } + + private: + StatsPayloadItems items; + }; +} diff --git a/include/Swiften/Parser/PayloadParsers/StatsParser.cpp b/include/Swiften/Parser/PayloadParsers/StatsParser.cpp new file mode 100644 index 00000000..2c4e421b --- /dev/null +++ b/include/Swiften/Parser/PayloadParsers/StatsParser.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include +#include + +namespace Swift { + +StatsParser::StatsParser() : level_(TopLevel), inItem_(false) { +} + +void StatsParser::handleStartElement(const std::string& element, const std::string& /*ns*/, const AttributeMap& attributes) { + if (level_ == PayloadLevel) { + if (element == "item") { + inItem_ = true; + + currentItem_ = StatsPayload::Item(); + + currentItem_.setName(attributes.getAttribute("name")); + currentItem_.setValue(attributes.getAttribute("value")); + currentItem_.setUnits(attributes.getAttribute("units")); + } + } + ++level_; +} + +void StatsParser::handleEndElement(const std::string& element, const std::string& /*ns*/) { + --level_; + if (level_ == PayloadLevel) { + if (inItem_) { + getPayloadInternal()->addItem(currentItem_); + inItem_ = false; + } + } +} + +void StatsParser::handleCharacterData(const std::string& data) { +} + +} diff --git a/include/Swiften/Parser/PayloadParsers/StatsParser.h b/include/Swiften/Parser/PayloadParsers/StatsParser.h new file mode 100644 index 00000000..53b0c97c --- /dev/null +++ b/include/Swiften/Parser/PayloadParsers/StatsParser.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include + +namespace Swift { + class SerializingParser; + + class StatsParser : public GenericPayloadParser { + public: + StatsParser(); + + virtual void handleStartElement(const std::string& element, const std::string&, const AttributeMap& attributes); + virtual void handleEndElement(const std::string& element, const std::string&); + virtual void handleCharacterData(const std::string& data); + + private: + enum Level { + TopLevel = 0, + PayloadLevel = 1, + ItemLevel = 2 + }; + int level_; + bool inItem_; + StatsPayload::Item currentItem_; + }; +} diff --git a/include/Swiften/Serializer/PayloadSerializers/StatsSerializer.cpp b/include/Swiften/Serializer/PayloadSerializers/StatsSerializer.cpp new file mode 100644 index 00000000..e670a6ee --- /dev/null +++ b/include/Swiften/Serializer/PayloadSerializers/StatsSerializer.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +#include + +#include +#include +#include +#include + +namespace Swift { + +StatsSerializer::StatsSerializer() : GenericPayloadSerializer() { +} + +std::string StatsSerializer::serializePayload(boost::shared_ptr stats) const { + XMLElement queryElement("query", "http://jabber.org/protocol/stats"); + foreach(const StatsPayload::Item& item, stats->getItems()) { + boost::shared_ptr statElement(new XMLElement("stat")); + statElement->setAttribute("name", item.getName()); + statElement->setAttribute("units", item.getUnits()); + statElement->setAttribute("value", item.getValue()); + + queryElement.addNode(statElement); + } + + return queryElement.serialize(); +} + +} diff --git a/include/Swiften/Serializer/PayloadSerializers/StatsSerializer.h b/include/Swiften/Serializer/PayloadSerializers/StatsSerializer.h new file mode 100644 index 00000000..cf45941d --- /dev/null +++ b/include/Swiften/Serializer/PayloadSerializers/StatsSerializer.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2011 Jan Kaluza + * Licensed under the Simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include + +namespace Swift { + class StatsSerializer : public GenericPayloadSerializer { + public: + StatsSerializer(); + + virtual std::string serializePayload(boost::shared_ptr) const; + }; +} diff --git a/src/transport.cpp b/src/transport.cpp index e9d87cb5..7d3677f1 100644 --- a/src/transport.cpp +++ b/src/transport.cpp @@ -34,6 +34,8 @@ #include "Swiften/Serializer/PayloadSerializers/AttentionSerializer.h" #include "Swiften/Parser/PayloadParsers/XHTMLIMParser.h" #include "Swiften/Serializer/PayloadSerializers/XHTMLIMSerializer.h" +#include "Swiften/Parser/PayloadParsers/StatsParser.h" +#include "Swiften/Serializer/PayloadSerializers/StatsSerializer.h" #include "transport/BlockParser.h" #include "transport/BlockSerializer.h" #include "Swiften/Parser/PayloadParsers/InvisibleParser.h" @@ -90,11 +92,13 @@ Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, m_server->addPayloadParserFactory(new GenericPayloadParserFactory("html", "http://jabber.org/protocol/xhtml-im")); m_server->addPayloadParserFactory(new GenericPayloadParserFactory("block", "urn:xmpp:block:0")); m_server->addPayloadParserFactory(new GenericPayloadParserFactory("invisible", "urn:xmpp:invisible:0")); + m_server->addPayloadParserFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/stats")); m_server->addPayloadSerializer(new Swift::AttentionSerializer()); m_server->addPayloadSerializer(new Swift::XHTMLIMSerializer()); m_server->addPayloadSerializer(new Transport::BlockSerializer()); m_server->addPayloadSerializer(new Swift::InvisibleSerializer()); + m_server->addPayloadSerializer(new Swift::StatsSerializer()); m_server->onDataRead.connect(boost::bind(&Component::handleDataRead, this, _1)); m_server->onDataWritten.connect(boost::bind(&Component::handleDataWritten, this, _1)); @@ -113,11 +117,13 @@ Component::Component(Swift::EventLoop *loop, Swift::NetworkFactories *factories, m_component->addPayloadParserFactory(new GenericPayloadParserFactory("html", "http://jabber.org/protocol/xhtml-im")); m_component->addPayloadParserFactory(new GenericPayloadParserFactory("block", "urn:xmpp:block:0")); m_component->addPayloadParserFactory(new GenericPayloadParserFactory("invisible", "urn:xmpp:invisible:0")); + m_component->addPayloadParserFactory(new GenericPayloadParserFactory("query", "http://jabber.org/protocol/stats")); m_component->addPayloadSerializer(new Swift::AttentionSerializer()); m_component->addPayloadSerializer(new Swift::XHTMLIMSerializer()); m_component->addPayloadSerializer(new Transport::BlockSerializer()); m_component->addPayloadSerializer(new Swift::InvisibleSerializer()); + m_component->addPayloadSerializer(new Swift::StatsSerializer()); m_stanzaChannel = m_component->getStanzaChannel(); m_iqRouter = m_component->getIQRouter();