summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-10-10 13:31:39 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-10-10 13:31:39 (GMT)
commit29e4e51e21b8902c82456cc85af46fa5f5889f04 (patch)
tree5ad1d62b82effb59d46d92a272b3ebe998ce6e08 /Swiften
parentfb7fe3dd7cf48b0e4a7337212129520dff5a236d (diff)
downloadswift-29e4e51e21b8902c82456cc85af46fa5f5889f04.zip
swift-29e4e51e21b8902c82456cc85af46fa5f5889f04.tar.bz2
Refactor responders to only start on start().
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/Queries/Responder.h55
-rw-r--r--Swiften/Queries/Responders/RosterPushResponder.h2
-rw-r--r--Swiften/Queries/Responders/UnitTest/DiscoInfoResponderTest.cpp8
-rw-r--r--Swiften/Queries/UnitTest/ResponderTest.cpp21
-rw-r--r--Swiften/Roster/XMPPRosterController.cpp5
-rw-r--r--Swiften/Roster/XMPPRosterController.h1
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();