summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-11-01 14:50:17 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-11-03 12:17:41 (GMT)
commit210accbcb135a9551cb56e5197a3a5acf8d15f49 (patch)
treed330a827ff3f531ab01ab2bb3b8f047a477702c0 /Swiften/Disco
parent9d9fb66aefef85a1c5ad432391014d15011747d1 (diff)
downloadswift-210accbcb135a9551cb56e5197a3a5acf8d15f49.zip
swift-210accbcb135a9551cb56e5197a3a5acf8d15f49.tar.bz2
Added JIDDiscoInfoResponder + Added "to" parameter to responder callback.
Diffstat (limited to 'Swiften/Disco')
-rw-r--r--Swiften/Disco/DiscoInfoResponder.cpp2
-rw-r--r--Swiften/Disco/DiscoInfoResponder.h2
-rw-r--r--Swiften/Disco/JIDDiscoInfoResponder.cpp54
-rw-r--r--Swiften/Disco/JIDDiscoInfoResponder.h37
-rw-r--r--Swiften/Disco/SConscript1
-rw-r--r--Swiften/Disco/UnitTest/DiscoInfoResponderTest.cpp4
-rw-r--r--Swiften/Disco/UnitTest/JIDDiscoInfoResponderTest.cpp118
7 files changed, 215 insertions, 3 deletions
diff --git a/Swiften/Disco/DiscoInfoResponder.cpp b/Swiften/Disco/DiscoInfoResponder.cpp
index 2e686c7..0254ad9 100644
--- a/Swiften/Disco/DiscoInfoResponder.cpp
+++ b/Swiften/Disco/DiscoInfoResponder.cpp
@@ -28,7 +28,7 @@ void DiscoInfoResponder::setDiscoInfo(const String& node, const DiscoInfo& info)
nodeInfo_[node] = newInfo;
}
-bool DiscoInfoResponder::handleGetRequest(const JID& from, const String& id, boost::shared_ptr<DiscoInfo> info) {
+bool DiscoInfoResponder::handleGetRequest(const JID& from, const JID&, const String& id, boost::shared_ptr<DiscoInfo> info) {
if (info->getNode().isEmpty()) {
sendResponse(from, id, boost::shared_ptr<DiscoInfo>(new DiscoInfo(info_)));
}
diff --git a/Swiften/Disco/DiscoInfoResponder.h b/Swiften/Disco/DiscoInfoResponder.h
index 0dc1172..3861ffd 100644
--- a/Swiften/Disco/DiscoInfoResponder.h
+++ b/Swiften/Disco/DiscoInfoResponder.h
@@ -23,7 +23,7 @@ namespace Swift {
void setDiscoInfo(const String& node, const DiscoInfo& info);
private:
- virtual bool handleGetRequest(const JID& from, const String& id, boost::shared_ptr<DiscoInfo> payload);
+ virtual bool handleGetRequest(const JID& from, const JID& to, const String& id, boost::shared_ptr<DiscoInfo> payload);
private:
DiscoInfo info_;
diff --git a/Swiften/Disco/JIDDiscoInfoResponder.cpp b/Swiften/Disco/JIDDiscoInfoResponder.cpp
new file mode 100644
index 0000000..311447f
--- /dev/null
+++ b/Swiften/Disco/JIDDiscoInfoResponder.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "Swiften/Disco/JIDDiscoInfoResponder.h"
+#include "Swiften/Queries/IQRouter.h"
+#include "Swiften/Elements/DiscoInfo.h"
+
+namespace Swift {
+
+JIDDiscoInfoResponder::JIDDiscoInfoResponder(IQRouter* router) : GetResponder<DiscoInfo>(router) {
+}
+
+void JIDDiscoInfoResponder::clearDiscoInfo(const JID& jid) {
+ info.erase(jid);
+}
+
+void JIDDiscoInfoResponder::setDiscoInfo(const JID& jid, const DiscoInfo& discoInfo) {
+ JIDDiscoInfoMap::iterator i = info.insert(std::make_pair(jid, JIDDiscoInfo())).first;
+ i->second.discoInfo = discoInfo;
+}
+
+void JIDDiscoInfoResponder::setDiscoInfo(const JID& jid, const String& node, const DiscoInfo& discoInfo) {
+ JIDDiscoInfoMap::iterator i = info.insert(std::make_pair(jid, JIDDiscoInfo())).first;
+ DiscoInfo newInfo(discoInfo);
+ newInfo.setNode(node);
+ i->second.nodeDiscoInfo[node] = newInfo;
+}
+
+bool JIDDiscoInfoResponder::handleGetRequest(const JID& from, const JID& to, const String& id, boost::shared_ptr<DiscoInfo> discoInfo) {
+ JIDDiscoInfoMap::const_iterator i = info.find(to);
+ if (i != info.end()) {
+ if (discoInfo->getNode().isEmpty()) {
+ sendResponse(from, to, id, boost::shared_ptr<DiscoInfo>(new DiscoInfo(i->second.discoInfo)));
+ }
+ else {
+ std::map<String,DiscoInfo>::const_iterator j = i->second.nodeDiscoInfo.find(discoInfo->getNode());
+ if (j != i->second.nodeDiscoInfo.end()) {
+ sendResponse(from, to, id, boost::shared_ptr<DiscoInfo>(new DiscoInfo(j->second)));
+ }
+ else {
+ sendError(from, to, id, ErrorPayload::ItemNotFound, ErrorPayload::Cancel);
+ }
+ }
+ }
+ else {
+ sendError(from, to, id, ErrorPayload::ItemNotFound, ErrorPayload::Cancel);
+ }
+ return true;
+}
+
+}
diff --git a/Swiften/Disco/JIDDiscoInfoResponder.h b/Swiften/Disco/JIDDiscoInfoResponder.h
new file mode 100644
index 0000000..aac43de
--- /dev/null
+++ b/Swiften/Disco/JIDDiscoInfoResponder.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <map>
+
+#include "Swiften/Queries/GetResponder.h"
+#include "Swiften/Elements/DiscoInfo.h"
+#include "Swiften/JID/JID.h"
+
+namespace Swift {
+ class IQRouter;
+
+ class JIDDiscoInfoResponder : public GetResponder<DiscoInfo> {
+ public:
+ JIDDiscoInfoResponder(IQRouter* router);
+
+ void clearDiscoInfo(const JID& jid);
+ void setDiscoInfo(const JID& jid, const DiscoInfo& info);
+ void setDiscoInfo(const JID& jid, const String& node, const DiscoInfo& info);
+
+ private:
+ virtual bool handleGetRequest(const JID& from, const JID& to, const String& id, boost::shared_ptr<DiscoInfo> payload);
+
+ private:
+ struct JIDDiscoInfo {
+ DiscoInfo discoInfo;
+ std::map<String, DiscoInfo> nodeDiscoInfo;
+ };
+ typedef std::map<JID, JIDDiscoInfo> JIDDiscoInfoMap;
+ JIDDiscoInfoMap info;
+ };
+}
diff --git a/Swiften/Disco/SConscript b/Swiften/Disco/SConscript
index a791946..ef2bd1b 100644
--- a/Swiften/Disco/SConscript
+++ b/Swiften/Disco/SConscript
@@ -9,5 +9,6 @@ objects = swiften_env.StaticObject([
"CapsFileStorage.cpp",
"ClientDiscoManager.cpp",
"DiscoInfoResponder.cpp",
+ "JIDDiscoInfoResponder.cpp",
])
swiften_env.Append(SWIFTEN_OBJECTS = [objects])
diff --git a/Swiften/Disco/UnitTest/DiscoInfoResponderTest.cpp b/Swiften/Disco/UnitTest/DiscoInfoResponderTest.cpp
index a9e85c0..1b2e54b 100644
--- a/Swiften/Disco/UnitTest/DiscoInfoResponderTest.cpp
+++ b/Swiften/Disco/UnitTest/DiscoInfoResponderTest.cpp
@@ -78,14 +78,16 @@ class DiscoInfoResponderTest : public CppUnit::TestFixture {
void testHandleRequest_GetInvalidNodeInfo() {
DiscoInfoResponder testling(router_);
-
boost::shared_ptr<DiscoInfo> query(new DiscoInfo());
query->setNode("bar-node");
channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com"), "id-1", query));
+ testling.start();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
boost::shared_ptr<ErrorPayload> payload(channel_->iqs_[0]->getPayload<ErrorPayload>());
CPPUNIT_ASSERT(payload);
+
+ testling.stop();
}
private:
diff --git a/Swiften/Disco/UnitTest/JIDDiscoInfoResponderTest.cpp b/Swiften/Disco/UnitTest/JIDDiscoInfoResponderTest.cpp
new file mode 100644
index 0000000..03a3ee8
--- /dev/null
+++ b/Swiften/Disco/UnitTest/JIDDiscoInfoResponderTest.cpp
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <typeinfo>
+
+#include "Swiften/Disco/JIDDiscoInfoResponder.h"
+#include "Swiften/Queries/IQRouter.h"
+#include "Swiften/Queries/DummyIQChannel.h"
+
+using namespace Swift;
+
+class JIDDiscoInfoResponderTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(JIDDiscoInfoResponderTest);
+ CPPUNIT_TEST(testHandleRequest_GetToplevelInfo);
+ CPPUNIT_TEST(testHandleRequest_GetNodeInfo);
+ CPPUNIT_TEST(testHandleRequest_GetInvalidNodeInfo);
+ CPPUNIT_TEST(testHandleRequest_GetUnknownJID);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ channel_ = new DummyIQChannel();
+ router_ = new IQRouter(channel_);
+ }
+
+ void tearDown() {
+ delete router_;
+ delete channel_;
+ }
+
+ void testHandleRequest_GetToplevelInfo() {
+ JIDDiscoInfoResponder testling(router_);
+ testling.start();
+ DiscoInfo discoInfo;
+ discoInfo.addFeature("foo");
+ testling.setDiscoInfo(JID("foo@bar.com/baz"), discoInfo);
+
+ boost::shared_ptr<DiscoInfo> query(new DiscoInfo());
+ channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com/baz"), "id-1", query));
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
+ boost::shared_ptr<DiscoInfo> payload(channel_->iqs_[0]->getPayload<DiscoInfo>());
+ CPPUNIT_ASSERT(payload);
+ CPPUNIT_ASSERT_EQUAL(String(""), payload->getNode());
+ CPPUNIT_ASSERT(payload->hasFeature("foo"));
+
+ testling.stop();
+ }
+
+ void testHandleRequest_GetNodeInfo() {
+ JIDDiscoInfoResponder testling(router_);
+ testling.start();
+ DiscoInfo discoInfo;
+ discoInfo.addFeature("foo");
+ testling.setDiscoInfo(JID("foo@bar.com/baz"), discoInfo);
+ DiscoInfo discoInfoBar;
+ discoInfoBar.addFeature("bar");
+ testling.setDiscoInfo(JID("foo@bar.com/baz"), "bar-node", discoInfoBar);
+
+ boost::shared_ptr<DiscoInfo> query(new DiscoInfo());
+ query->setNode("bar-node");
+ channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com/baz"), "id-1", query));
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
+ boost::shared_ptr<DiscoInfo> payload(channel_->iqs_[0]->getPayload<DiscoInfo>());
+ CPPUNIT_ASSERT(payload);
+ CPPUNIT_ASSERT_EQUAL(String("bar-node"), payload->getNode());
+ CPPUNIT_ASSERT(payload->hasFeature("bar"));
+
+ testling.stop();
+ }
+
+ void testHandleRequest_GetInvalidNodeInfo() {
+ JIDDiscoInfoResponder testling(router_);
+ DiscoInfo discoInfo;
+ discoInfo.addFeature("foo");
+ testling.setDiscoInfo(JID("foo@bar.com/baz"), discoInfo);
+ testling.start();
+
+ boost::shared_ptr<DiscoInfo> query(new DiscoInfo());
+ query->setNode("bar-node");
+ channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com/baz"), "id-1", query));
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
+ boost::shared_ptr<ErrorPayload> payload(channel_->iqs_[0]->getPayload<ErrorPayload>());
+ CPPUNIT_ASSERT(payload);
+
+ testling.stop();
+ }
+
+ void testHandleRequest_GetUnknownJID() {
+ JIDDiscoInfoResponder testling(router_);
+ DiscoInfo discoInfo;
+ discoInfo.addFeature("foo");
+ testling.setDiscoInfo(JID("foo@bar.com/baz"), discoInfo);
+ testling.start();
+
+ boost::shared_ptr<DiscoInfo> query(new DiscoInfo());
+ channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com/fum"), "id-1", query));
+
+ CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
+ boost::shared_ptr<ErrorPayload> payload(channel_->iqs_[0]->getPayload<ErrorPayload>());
+ CPPUNIT_ASSERT(payload);
+
+ testling.stop();
+ }
+
+ private:
+ IQRouter* router_;
+ DummyIQChannel* channel_;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(JIDDiscoInfoResponderTest);