diff options
Diffstat (limited to 'Swiften')
-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 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterController.cpp | 5 | ||||
-rw-r--r-- | Swiften/Roster/XMPPRosterController.h | 1 |
6 files changed, 84 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_); diff --git a/Swiften/Roster/XMPPRosterController.cpp b/Swiften/Roster/XMPPRosterController.cpp index 7743ec8..feaa7d4 100644 --- a/Swiften/Roster/XMPPRosterController.cpp +++ b/Swiften/Roster/XMPPRosterController.cpp @@ -25,6 +25,11 @@ namespace Swift { */ XMPPRosterController::XMPPRosterController(IQRouter* iqRouter, XMPPRoster* xmppRoster) : iqRouter_(iqRouter), rosterPushResponder_(iqRouter), xmppRoster_(xmppRoster) { rosterPushResponder_.onRosterReceived.connect(boost::bind(&XMPPRosterController::handleRosterReceived, this, _1)); + rosterPushResponder_.start(); +} + +XMPPRosterController::~XMPPRosterController() { + rosterPushResponder_.stop(); } void XMPPRosterController::requestRoster() { diff --git a/Swiften/Roster/XMPPRosterController.h b/Swiften/Roster/XMPPRosterController.h index 3ef7795..22a63ad 100644 --- a/Swiften/Roster/XMPPRosterController.h +++ b/Swiften/Roster/XMPPRosterController.h @@ -22,6 +22,7 @@ namespace Swift { class XMPPRosterController { public: XMPPRosterController(IQRouter *iqRouter, XMPPRoster* xmppRoster); + ~XMPPRosterController(); void requestRoster(); |