diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-10-10 13:31:39 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-10-10 13:31:39 (GMT) |
commit | 29e4e51e21b8902c82456cc85af46fa5f5889f04 (patch) | |
tree | 5ad1d62b82effb59d46d92a272b3ebe998ce6e08 /Swiften/Queries | |
parent | fb7fe3dd7cf48b0e4a7337212129520dff5a236d (diff) | |
download | swift-contrib-29e4e51e21b8902c82456cc85af46fa5f5889f04.zip swift-contrib-29e4e51e21b8902c82456cc85af46fa5f5889f04.tar.bz2 |
Refactor responders to only start on start().
Diffstat (limited to 'Swiften/Queries')
-rw-r--r-- | Swiften/Queries/Responder.h | 55 | ||||
-rw-r--r-- | Swiften/Queries/Responders/RosterPushResponder.h | 2 | ||||
-rw-r--r-- | Swiften/Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp | 8 | ||||
-rw-r--r-- | Swiften/Queries/UnitTest/ResponderTest.cpp | 21 |
4 files changed, 78 insertions, 8 deletions
diff --git a/Swiften/Queries/Responder.h b/Swiften/Queries/Responder.h index b38c3c4..2333b5f 100644 --- a/Swiften/Queries/Responder.h +++ b/Swiften/Queries/Responder.h @@ -4,33 +4,78 @@ * See Documentation/Licenses/GPLv3.txt for more information. */ -#ifndef SWIFTEN_Responder_H -#define SWIFTEN_Responder_H +#pragma once #include "Swiften/Queries/IQHandler.h" #include "Swiften/Queries/IQRouter.h" #include "Swiften/Elements/ErrorPayload.h" namespace Swift { + /** + * A class for handling incoming IQ Get and Set requests of a specific payload type. + * + * Concrete subclasses of this class need to implement handleGetRequest() and handleSetRequest() to + * implement the behavior of the responder. + * + * \tparam PAYLOAD_TYPE The type of payload this Responder handles. Only IQ requests containing this + * payload type will be passed to handleGetRequest() and handleSetRequest() + */ template<typename PAYLOAD_TYPE> class Responder : public IQHandler { public: Responder(IQRouter* router) : router_(router) { - router_->addHandler(this); } ~Responder() { + } + + /** + * Starts the responder. + * + * After the responder has started, it will start receiving and responding to requests. + * + * \see stop() + */ + void start() { + router_->addHandler(this); + } + + /** + * Stops the responder. + * + * When the responder is stopped, it will no longer receive incoming requests. + * + * \see start() + */ + void stop() { router_->removeHandler(this); } protected: + /** + * Handle an incoming IQ-Get request containing a payload of class PAYLOAD_TYPE. + * + * This method is implemented in the concrete subclasses. + */ virtual bool handleGetRequest(const JID& from, const String& id, boost::shared_ptr<PAYLOAD_TYPE> payload) = 0; + + /** + * Handle an incoming IQ-Set request containing a payload of class PAYLOAD_TYPE. + * + * This method is implemented in the concrete subclasses. + */ virtual bool handleSetRequest(const JID& from, const String& id, boost::shared_ptr<PAYLOAD_TYPE> payload) = 0; - void sendResponse(const JID& to, const String& id, boost::shared_ptr<Payload> payload) { + /** + * Convenience function for sending an IQ response. + */ + void sendResponse(const JID& to, const String& id, boost::shared_ptr<PAYLOAD_TYPE> payload) { router_->sendIQ(IQ::createResult(to, id, payload)); } + /** + * Convenience function for responding with an error. + */ void sendError(const JID& to, const String& id, ErrorPayload::Condition condition, ErrorPayload::Type type) { router_->sendIQ(IQ::createError(to, id, condition, type)); } @@ -60,5 +105,3 @@ namespace Swift { IQRouter* router_; }; } - -#endif diff --git a/Swiften/Queries/Responders/RosterPushResponder.h b/Swiften/Queries/Responders/RosterPushResponder.h index 8c57fd3..48111a7 100644 --- a/Swiften/Queries/Responders/RosterPushResponder.h +++ b/Swiften/Queries/Responders/RosterPushResponder.h @@ -22,7 +22,7 @@ namespace Swift { private: virtual bool handleSetRequest(const JID& from, const String& id, boost::shared_ptr<RosterPayload> payload) { onRosterReceived(payload); - sendResponse(from, id, boost::shared_ptr<Payload>()); + sendResponse(from, id, boost::shared_ptr<RosterPayload>()); return true; } }; diff --git a/Swiften/Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp b/Swiften/Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp index 62d9aca..20fa9ff 100644 --- a/Swiften/Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp +++ b/Swiften/Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp @@ -35,7 +35,8 @@ class DiscoInfoResponderTest : public CppUnit::TestFixture { } void testHandleRequest_GetToplevelInfo() { - DiscoInfoResponder testling(router_); + DiscoInfoResponder testling(router_); + testling.start(); DiscoInfo discoInfo; discoInfo.addFeature("foo"); testling.setDiscoInfo(discoInfo); @@ -48,10 +49,13 @@ class DiscoInfoResponderTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(payload); CPPUNIT_ASSERT_EQUAL(String(""), payload->getNode()); CPPUNIT_ASSERT(payload->hasFeature("foo")); + + testling.stop(); } void testHandleRequest_GetNodeInfo() { DiscoInfoResponder testling(router_); + testling.start(); DiscoInfo discoInfo; discoInfo.addFeature("foo"); testling.setDiscoInfo(discoInfo); @@ -68,6 +72,8 @@ class DiscoInfoResponderTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(payload); CPPUNIT_ASSERT_EQUAL(String("bar-node"), payload->getNode()); CPPUNIT_ASSERT(payload->hasFeature("bar")); + + testling.stop(); } void testHandleRequest_GetInvalidNodeInfo() { diff --git a/Swiften/Queries/UnitTest/ResponderTest.cpp b/Swiften/Queries/UnitTest/ResponderTest.cpp index 513c6ac..9824c98 100644 --- a/Swiften/Queries/UnitTest/ResponderTest.cpp +++ b/Swiften/Queries/UnitTest/ResponderTest.cpp @@ -20,6 +20,8 @@ class ResponderTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(ResponderTest); CPPUNIT_TEST(testConstructor); + CPPUNIT_TEST(testStart); + CPPUNIT_TEST(testStop); CPPUNIT_TEST(testHandleIQ_Set); CPPUNIT_TEST(testHandleIQ_Get); CPPUNIT_TEST(testHandleIQ_Error); @@ -46,9 +48,28 @@ class ResponderTest : public CppUnit::TestFixture channel_->onIQReceived(createRequest(IQ::Set)); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(testling.setPayloads_.size())); + } + + void testStart() { + MyResponder testling(router_); + + testling.start(); + channel_->onIQReceived(createRequest(IQ::Set)); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling.setPayloads_.size())); } + void testStop() { + MyResponder testling(router_); + + testling.start(); + testling.stop(); + channel_->onIQReceived(createRequest(IQ::Set)); + + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(testling.setPayloads_.size())); + } + void testHandleIQ_Set() { MyResponder testling(router_); |