diff options
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/cluster.mk | 4 | ||||
-rw-r--r-- | cpp/src/qpid/cluster/ClassifierHandler.cpp | 73 | ||||
-rw-r--r-- | cpp/src/qpid/cluster/ClassifierHandler.h | 48 | ||||
-rw-r--r-- | cpp/src/tests/Cluster.cpp | 30 | ||||
-rw-r--r-- | cpp/src/tests/FramingTest.cpp | 1 | ||||
-rw-r--r-- | cpp/src/tests/Makefile.am | 6 |
6 files changed, 156 insertions, 6 deletions
diff --git a/cpp/src/cluster.mk b/cpp/src/cluster.mk index 07b4f045cc..173592dc38 100644 --- a/cpp/src/cluster.mk +++ b/cpp/src/cluster.mk @@ -11,7 +11,9 @@ libqpidcluster_la_SOURCES = \ qpid/cluster/Cpg.cpp \ qpid/cluster/Cpg.h \ qpid/cluster/Dispatchable.h \ - qpid/cluster/ClusterPluginProvider.cpp + qpid/cluster/ClusterPluginProvider.cpp \ + qpid/cluster/ClassifierHandler.h \ + qpid/cluster/ClassifierHandler.cpp libqpidcluster_la_LIBADD= -lcpg libqpidbroker.la diff --git a/cpp/src/qpid/cluster/ClassifierHandler.cpp b/cpp/src/qpid/cluster/ClassifierHandler.cpp new file mode 100644 index 0000000000..0d0465c89e --- /dev/null +++ b/cpp/src/qpid/cluster/ClassifierHandler.cpp @@ -0,0 +1,73 @@ +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "ClassifierHandler.h" + +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/ExchangeDeclareBody.h" +#include "qpid/framing/ExchangeDeleteBody.h" +#include "qpid/framing/QueueBindBody.h" +#include "qpid/framing/QueueDeclareBody.h" +#include "qpid/framing/QueueDeleteBody.h" +#include "qpid/framing/QueueUnbindBody.h" + + +namespace qpid { +namespace cluster { + +using namespace framing; + +typedef uint32_t FullMethodId; // Combind class & method ID. + +FullMethodId fullId(ClassId c, MethodId m) { return c<<16+m; } + +FullMethodId fullId(const shared_ptr<AMQMethodBody>& body) { + return fullId(body->amqpClassId(), body->amqpMethodId()); +} + +template <class M> +FullMethodId fullId() { return fullId(M::CLASS_ID, M::METHOD_ID); } + + +ClassifierHandler::ClassifierHandler(Chain wiring, Chain other) + : FrameHandler(other) +{ + map[fullId<ExchangeDeclareBody>()] = wiring; + map[fullId<ExchangeDeleteBody>()] = wiring; + map[fullId<QueueBindBody>()] = wiring; + map[fullId<QueueDeclareBody>()] = wiring; + map[fullId<QueueDeleteBody>()] = wiring; + map[fullId<QueueUnbindBody>()] = wiring; +} + +void ClassifierHandler::handle(AMQFrame& frame) { + // TODO aconway 2007-07-03: Flatten the frame hierarchy so we + // can do a single lookup to dispatch a frame. + Chain chosen; + shared_ptr<AMQMethodBody> method = + dynamic_pointer_cast<AMQMethodBody>(frame.getBody()); + if (method) + chosen=map[fullId(method)]; + if (chosen) + chosen->handle(frame); + else + next->handle(frame); +} + + +}} // namespace qpid::cluster diff --git a/cpp/src/qpid/cluster/ClassifierHandler.h b/cpp/src/qpid/cluster/ClassifierHandler.h new file mode 100644 index 0000000000..8c2f4ef85f --- /dev/null +++ b/cpp/src/qpid/cluster/ClassifierHandler.h @@ -0,0 +1,48 @@ +#ifndef QPID_CLUSTER_CLASSIFIERHANDLER_H +#define QPID_CLUSTER_CLASSIFIERHANDLER_H + +/* + * + * Copyright (c) 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "qpid/framing/FrameHandler.h" +#include "qpid/framing/amqp_types.h" + +#include <map> + +namespace qpid { +namespace cluster { + +/** + * Classify frames and forward to the appropriate handler. + */ +class ClassifierHandler : public framing::FrameHandler +{ + public: + ClassifierHandler(Chain wiring, Chain other); + + void handle(framing::AMQFrame& frame); + + private: + std::map<uint32_t, framing::FrameHandler::Chain> map; +}; + +}} // namespace qpid::cluster + + + +#endif /*!QPID_CLUSTER_CLASSIFIERHANDLER_H*/ diff --git a/cpp/src/tests/Cluster.cpp b/cpp/src/tests/Cluster.cpp index 2ec140b924..95cd607979 100644 --- a/cpp/src/tests/Cluster.cpp +++ b/cpp/src/tests/Cluster.cpp @@ -22,13 +22,14 @@ #include "Cluster.h" #include "qpid/framing/ChannelPingBody.h" #include "qpid/framing/ChannelOkBody.h" +#include "qpid/cluster/ClassifierHandler.h" static const ProtocolVersion VER; using namespace qpid::log; /** Verify membership in a cluster with one member. */ -BOOST_AUTO_TEST_CASE(clusterOne) { +BOOST_AUTO_TEST_CASE(testClusterOne) { TestCluster cluster("clusterOne", "amqp:one:1"); AMQFrame frame(VER, 1, new ChannelPingBody(VER)); cluster.getToChains().in->handle(frame); @@ -43,7 +44,7 @@ BOOST_AUTO_TEST_CASE(clusterOne) { } /** Fork a process to test a cluster with two members */ -BOOST_AUTO_TEST_CASE(clusterTwo) { +BOOST_AUTO_TEST_CASE(testClusterTwo) { pid_t pid=fork(); BOOST_REQUIRE(pid >= 0); if (pid) { // Parent, see Cluster_child.cpp for child. @@ -69,3 +70,28 @@ BOOST_AUTO_TEST_CASE(clusterTwo) { BOOST_REQUIRE(execl("./Cluster_child", "./Cluster_child", NULL)); } } + +struct CountHandler : public FrameHandler { + CountHandler() : count(0) {} + void handle(AMQFrame&) { count++; } + size_t count; +}; + +/** Test the ClassifierHandler */ +BOOST_AUTO_TEST_CASE(testClassifierHandlerWiring) { + AMQFrame queueDecl(VER, 0, new QueueDeclareBody(VER)); + AMQFrame messageTrans(VER, 0, new MessageTransferBody(VER)); + shared_ptr<CountHandler> wiring(new CountHandler()); + shared_ptr<CountHandler> other(new CountHandler()); + + ClassifierHandler classify(wiring, other); + + classify.handle(queueDecl); + BOOST_CHECK_EQUAL(1u, wiring->count); + BOOST_CHECK_EQUAL(0u, other->count); + + classify.handle(messageTrans); + BOOST_CHECK_EQUAL(1u, wiring->count); + BOOST_CHECK_EQUAL(1u, other->count); +} + diff --git a/cpp/src/tests/FramingTest.cpp b/cpp/src/tests/FramingTest.cpp index 9c60af7866..cac2ce986a 100644 --- a/cpp/src/tests/FramingTest.cpp +++ b/cpp/src/tests/FramingTest.cpp @@ -352,6 +352,7 @@ class FramingTest : public CppUnit::TestCase void print(std::ostream&) const {} MethodId amqpMethodId() const { return 0; } ClassId amqpClassId() const { return 0; } + FullMethodId getFullMethodId() const { return 0; } void encodeContent(Buffer& ) const {} void decodeContent(Buffer& ) {} }; diff --git a/cpp/src/tests/Makefile.am b/cpp/src/tests/Makefile.am index 5bb2c80b2c..3adf8818db 100644 --- a/cpp/src/tests/Makefile.am +++ b/cpp/src/tests/Makefile.am @@ -87,9 +87,9 @@ testprogs= \ client_test \ echo_service \ topic_listener \ - topic_publisher \ - interop_runner -check_PROGRAMS += $(testprogs) + topic_publisher + +check_PROGRAMS += $(testprogs) interop_runner TESTS_ENVIRONMENT = VALGRIND=$(VALGRIND) srcdir=$(srcdir) $(srcdir)/run_test |