Added StatsPayload, StatsParser, StatsSerializer
This commit is contained in:
parent
bfe84737ee
commit
6ff8b24992
7 changed files with 217 additions and 0 deletions
14
include/Swiften/Elements/StatsPayload.cpp
Normal file
14
include/Swiften/Elements/StatsPayload.cpp
Normal file
|
@ -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 <Swiften/Elements/StatsPayload.h>
|
||||
|
||||
namespace Swift {
|
||||
|
||||
StatsPayload::StatsPayload() {
|
||||
}
|
||||
|
||||
}
|
67
include/Swiften/Elements/StatsPayload.h
Normal file
67
include/Swiften/Elements/StatsPayload.h
Normal file
|
@ -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 <vector>
|
||||
|
||||
#include <string>
|
||||
#include <Swiften/Elements/Payload.h>
|
||||
|
||||
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<StatsPayload::Item> StatsPayloadItems;
|
||||
|
||||
StatsPayload();
|
||||
|
||||
void addItem(const StatsPayload::Item &item) {
|
||||
items.push_back(item);
|
||||
}
|
||||
|
||||
const StatsPayloadItems &getItems() const {
|
||||
return items;
|
||||
}
|
||||
|
||||
private:
|
||||
StatsPayloadItems items;
|
||||
};
|
||||
}
|
43
include/Swiften/Parser/PayloadParsers/StatsParser.cpp
Normal file
43
include/Swiften/Parser/PayloadParsers/StatsParser.cpp
Normal file
|
@ -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 <Swiften/Parser/PayloadParsers/StatsParser.h>
|
||||
#include <Swiften/Parser/SerializingParser.h>
|
||||
|
||||
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) {
|
||||
}
|
||||
|
||||
}
|
33
include/Swiften/Parser/PayloadParsers/StatsParser.h
Normal file
33
include/Swiften/Parser/PayloadParsers/StatsParser.h
Normal file
|
@ -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 <Swiften/Elements/StatsPayload.h>
|
||||
#include <Swiften/Parser/GenericPayloadParser.h>
|
||||
|
||||
namespace Swift {
|
||||
class SerializingParser;
|
||||
|
||||
class StatsParser : public GenericPayloadParser<StatsPayload> {
|
||||
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_;
|
||||
};
|
||||
}
|
|
@ -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 <Swiften/Serializer/PayloadSerializers/StatsSerializer.h>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include <Swiften/Base/foreach.h>
|
||||
#include <Swiften/Serializer/XML/XMLTextNode.h>
|
||||
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
|
||||
#include <Swiften/Serializer/XML/XMLElement.h>
|
||||
|
||||
namespace Swift {
|
||||
|
||||
StatsSerializer::StatsSerializer() : GenericPayloadSerializer<StatsPayload>() {
|
||||
}
|
||||
|
||||
std::string StatsSerializer::serializePayload(boost::shared_ptr<StatsPayload> stats) const {
|
||||
XMLElement queryElement("query", "http://jabber.org/protocol/stats");
|
||||
foreach(const StatsPayload::Item& item, stats->getItems()) {
|
||||
boost::shared_ptr<XMLElement> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <Swiften/Serializer/GenericPayloadSerializer.h>
|
||||
#include <Swiften/Elements/StatsPayload.h>
|
||||
|
||||
namespace Swift {
|
||||
class StatsSerializer : public GenericPayloadSerializer<StatsPayload> {
|
||||
public:
|
||||
StatsSerializer();
|
||||
|
||||
virtual std::string serializePayload(boost::shared_ptr<StatsPayload>) const;
|
||||
};
|
||||
}
|
|
@ -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<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
|
||||
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
|
||||
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
|
||||
m_server->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("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<Swift::XHTMLIMParser>("html", "http://jabber.org/protocol/xhtml-im"));
|
||||
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Transport::BlockParser>("block", "urn:xmpp:block:0"));
|
||||
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::InvisibleParser>("invisible", "urn:xmpp:invisible:0"));
|
||||
m_component->addPayloadParserFactory(new GenericPayloadParserFactory<Swift::StatsParser>("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();
|
||||
|
|
Loading…
Add table
Reference in a new issue