From cc873b3f00db4cd0a778bc2ec04f8748d70a92f9 Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Wed, 22 Mar 2017 14:48:20 +0530 Subject: Add Client State Indication Element, its Parser and Serializer License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Adds tests for Client State Parser and Serializer, which passes. Tests performed on Ubuntu 16.04 LTS. Change-Id: I60c63f63e1c0fdd55600ef42faa95989ca5ab75b diff --git a/Swiften/Elements/ClientState.h b/Swiften/Elements/ClientState.h new file mode 100644 index 0000000..868d352 --- /dev/null +++ b/Swiften/Elements/ClientState.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include + +#include +#include + +namespace Swift { + class SWIFTEN_API ClientState : public Payload { + public: + typedef std::shared_ptr ref; + + public: + enum class ClientStateType {Active, Inactive}; + ClientState(ClientStateType state = ClientStateType::Active) : state_(state) { + + } + + ClientStateType getClientState() const { return state_; } + void setClientState(ClientStateType state) {state_ = state;} + + private: + ClientStateType state_; + }; +} diff --git a/Swiften/Elements/DiscoInfo.cpp b/Swiften/Elements/DiscoInfo.cpp index 51a4450..11f0623 100644 --- a/Swiften/Elements/DiscoInfo.cpp +++ b/Swiften/Elements/DiscoInfo.cpp @@ -11,6 +11,7 @@ namespace Swift { const std::string DiscoInfo::ChatStatesFeature = std::string("http://jabber.org/protocol/chatstates"); +const std::string DiscoInfo::ClientStatesFeature = std::string("urn:xmpp:csi:0"); const std::string DiscoInfo::SecurityLabelsFeature = std::string("urn:xmpp:sec-label:0"); const std::string DiscoInfo::SecurityLabelsCatalogFeature = std::string("urn:xmpp:sec-label:catalog:2"); const std::string DiscoInfo::JabberSearchFeature = std::string("jabber:iq:search"); diff --git a/Swiften/Elements/DiscoInfo.h b/Swiften/Elements/DiscoInfo.h index ebc598c..c8009ee 100644 --- a/Swiften/Elements/DiscoInfo.h +++ b/Swiften/Elements/DiscoInfo.h @@ -22,6 +22,7 @@ namespace Swift { typedef std::shared_ptr ref; static const std::string ChatStatesFeature; + static const std::string ClientStatesFeature; static const std::string SecurityLabelsFeature; static const std::string SecurityLabelsCatalogFeature; static const std::string JabberSearchFeature; diff --git a/Swiften/Parser/PayloadParsers/ClientStateParser.cpp b/Swiften/Parser/PayloadParsers/ClientStateParser.cpp new file mode 100644 index 0000000..8a6cc9c --- /dev/null +++ b/Swiften/Parser/PayloadParsers/ClientStateParser.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2017 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +namespace Swift { + +ClientStateParser::ClientStateParser() { +} + +void ClientStateParser::handleStartElement(const std::string& element, const std::string&, const AttributeMap&) { + if (level_ == 0) { + auto state = ClientState::ClientStateType::Active; + if (element == "active") { + state = ClientState::ClientStateType::Active; + } else if (element == "inactive") { + state = ClientState::ClientStateType::Inactive; + } + getPayloadInternal()->setClientState(state); + } + ++level_; +} + +void ClientStateParser::handleEndElement(const std::string&, const std::string&) { + --level_; +} + +void ClientStateParser::handleCharacterData(const std::string&) { + +} + +} diff --git a/Swiften/Parser/PayloadParsers/ClientStateParser.h b/Swiften/Parser/PayloadParsers/ClientStateParser.h new file mode 100644 index 0000000..039ae37 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/ClientStateParser.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2017 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include +#include + +namespace Swift { + class SWIFTEN_API ClientStateParser : public GenericPayloadParser { + public: + ClientStateParser(); + + 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: + int level_ = 0; + }; +} diff --git a/Swiften/Parser/PayloadParsers/ClientStateParserFactory.h b/Swiften/Parser/PayloadParsers/ClientStateParserFactory.h new file mode 100644 index 0000000..95617a1 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/ClientStateParserFactory.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include +#include + +namespace Swift { + class PayloadParserFactoryCollection; + + class SWIFTEN_API ClientStateParserFactory : public PayloadParserFactory { + public: + ClientStateParserFactory() { + } + + virtual bool canParse(const std::string& element, const std::string& ns, const AttributeMap&) const { + return ns == "urn:xmpp:csi:0" && + (element == "active" || element == "inactive"); + } + + virtual PayloadParser* createPayloadParser() { + return new ClientStateParser(); + } + + }; +} diff --git a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp index b67556e..4ad943a 100644 --- a/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp +++ b/Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -127,6 +128,7 @@ FullPayloadParserFactoryCollection::FullPayloadParserFactoryCollection() { factories_.push_back(std::make_shared >("vCard", "vcard-temp")); factories_.push_back(std::make_shared(this)); factories_.push_back(std::make_shared()); + factories_.push_back(std::make_shared()); factories_.push_back(std::make_shared(this)); factories_.push_back(std::make_shared(this)); factories_.push_back(std::make_shared >("x", "jabber:x:conference")); diff --git a/Swiften/Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp b/Swiften/Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp new file mode 100644 index 0000000..ca89040 --- /dev/null +++ b/Swiften/Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +#include +#include + +using namespace Swift; + +TEST(ClientStateParserTest, testParse_Active) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "" + )); + + ClientState::ref payload = parser.getPayload(); + ASSERT_EQ(ClientState::ClientStateType::Active, payload->getClientState()); +} + +TEST(ClientStateParserTest, testParse_Inactive) { + PayloadsParserTester parser; + ASSERT_TRUE(parser.parse( + "" + )); + + ClientState::ref payload = parser.getPayload(); + ASSERT_EQ(ClientState::ClientStateType::Inactive, payload->getClientState()); +} diff --git a/Swiften/Parser/SConscript b/Swiften/Parser/SConscript index 840cbda..1797de1 100644 --- a/Swiften/Parser/SConscript +++ b/Swiften/Parser/SConscript @@ -30,6 +30,7 @@ sources = [ "PayloadParsers/CarbonsReceivedParser.cpp", "PayloadParsers/CarbonsSentParser.cpp", "PayloadParsers/ChatStateParser.cpp", + "PayloadParsers/ClientStateParser.cpp", "PayloadParsers/CapsInfoParser.cpp", "PayloadParsers/DiscoInfoParser.cpp", "PayloadParsers/DiscoItemsParser.cpp", diff --git a/Swiften/SConscript b/Swiften/SConscript index 1103362..2182132 100644 --- a/Swiften/SConscript +++ b/Swiften/SConscript @@ -202,6 +202,7 @@ if env["SCONS_STAGE"] == "build" : "Serializer/PayloadSerializers/CarbonsSentSerializer.cpp", "Serializer/PayloadSerializers/CarbonsReceivedSerializer.cpp", "Serializer/PayloadSerializers/ChatStateSerializer.cpp", + "Serializer/PayloadSerializers/ClientStateSerializer.cpp", "Serializer/PayloadSerializers/DiscoInfoSerializer.cpp", "Serializer/PayloadSerializers/DiscoItemsSerializer.cpp", "Serializer/PayloadSerializers/ErrorSerializer.cpp", @@ -414,6 +415,7 @@ if env["SCONS_STAGE"] == "build" : File("Network/UnitTest/BOSHConnectionPoolTest.cpp"), File("Parser/PayloadParsers/UnitTest/BlockParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/BodyParserTest.cpp"), + File("Parser/PayloadParsers/UnitTest/ClientStateParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/DiscoInfoParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/DiscoItemsParserTest.cpp"), File("Parser/PayloadParsers/UnitTest/ErrorParserTest.cpp"), @@ -482,6 +484,7 @@ if env["SCONS_STAGE"] == "build" : File("Serializer/PayloadSerializers/UnitTest/CarbonsSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/CapsInfoSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/ChatStateSerializerTest.cpp"), + File("Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/FormSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/DiscoInfoSerializerTest.cpp"), File("Serializer/PayloadSerializers/UnitTest/ErrorSerializerTest.cpp"), diff --git a/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.cpp b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.cpp new file mode 100644 index 0000000..1485dcb --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2017 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +namespace Swift { + +ClientStateSerializer::ClientStateSerializer() : GenericPayloadSerializer() { +} + +std::string ClientStateSerializer::serializePayload(std::shared_ptr clientState) const { + std::string result("<"); + switch (clientState->getClientState()) { + case ClientState::ClientStateType::Active: result += "active"; break; + case ClientState::ClientStateType::Inactive: result += "inactive"; break; + } + result += " xmlns=\"urn:xmpp:csi:0\"/>"; + return result; +} + +} diff --git a/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h new file mode 100644 index 0000000..1c4f977 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/ClientStateSerializer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2017 Tarun Gupta. + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#pragma once + +#include +#include +#include + +namespace Swift { + class SWIFTEN_API ClientStateSerializer : public GenericPayloadSerializer { + public: + ClientStateSerializer(); + + virtual std::string serializePayload(std::shared_ptr error) const; + }; +} diff --git a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp index cf0b54c..bb3632b 100644 --- a/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp +++ b/Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -90,6 +91,7 @@ FullPayloadSerializerCollection::FullPayloadSerializerCollection() { serializers_.push_back(new SubjectSerializer()); serializers_.push_back(new ThreadSerializer()); serializers_.push_back(new ChatStateSerializer()); + serializers_.push_back(new ClientStateSerializer()); serializers_.push_back(new PrioritySerializer()); serializers_.push_back(new ErrorSerializer(this)); serializers_.push_back(new RosterSerializer()); diff --git a/Swiften/Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp b/Swiften/Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp new file mode 100644 index 0000000..1622354 --- /dev/null +++ b/Swiften/Serializer/PayloadSerializers/UnitTest/ClientStateSerializerTest.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2017 Tarun Gupta + * Licensed under the simplified BSD license. + * See Documentation/Licenses/BSD-simplified.txt for more information. + */ + +#include + +#include + +using namespace Swift; + +TEST(ClientStateSerializerTest, testSerialize_ActiveState) { + ClientStateSerializer testling; + std::shared_ptr priority(new ClientState(ClientState::ClientStateType::Active)); + + ASSERT_EQ(std::string(""), testling.serialize(priority)); +} + +TEST(ClientStateSerializerTest, testSerialize_InacativeState) { + ClientStateSerializer testling; + std::shared_ptr priority(new ClientState(ClientState::ClientStateType::Inactive)); + + ASSERT_EQ(std::string(""), testling.serialize(priority)); +} -- cgit v0.10.2-6-g49f6