You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by gs...@apache.org on 2007/09/18 16:45:34 UTC
svn commit: r576935 - in /incubator/qpid/trunk/qpid/cpp/src: Makefile.am
qpid/client/ClientMessage.h qpid/client/Dispatcher.cpp
qpid/client/Dispatcher.h qpid/framing/TransferContent.h
tests/ClientSessionTest.cpp
Author: gsim
Date: Tue Sep 18 07:45:33 2007
New Revision: 576935
URL: http://svn.apache.org/viewvc?rev=576935&view=rev
Log:
Added Dispatcher class (plus test). This converts incoming MessageTransfer framesets to Messages and pumps them to registered listeners.
Added:
incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp (with props)
incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h (with props)
Modified:
incubator/qpid/trunk/qpid/cpp/src/Makefile.am
incubator/qpid/trunk/qpid/cpp/src/qpid/client/ClientMessage.h
incubator/qpid/trunk/qpid/cpp/src/qpid/framing/TransferContent.h
incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp
Modified: incubator/qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/Makefile.am?rev=576935&r1=576934&r2=576935&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/Makefile.am Tue Sep 18 07:45:33 2007
@@ -214,6 +214,7 @@
qpid/client/ConnectionImpl.cpp \
qpid/client/Connector.cpp \
qpid/client/Demux.cpp \
+ qpid/client/Dispatcher.cpp \
qpid/client/MessageListener.cpp \
qpid/client/Correlator.cpp \
qpid/client/CompletionTracker.cpp \
@@ -301,6 +302,7 @@
qpid/client/Connector.h \
qpid/client/Completion.h \
qpid/client/Demux.h \
+ qpid/client/Dispatcher.h \
qpid/client/MessageListener.h \
qpid/client/MessageQueue.h \
qpid/client/BlockingQueue.h \
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/client/ClientMessage.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/ClientMessage.h?rev=576935&r1=576934&r2=576935&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/ClientMessage.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/ClientMessage.h Tue Sep 18 07:45:33 2007
@@ -22,6 +22,8 @@
*
*/
#include <string>
+#include "qpid/client/Session.h"
+#include "qpid/framing/MessageTransferBody.h"
#include "qpid/framing/TransferContent.h"
namespace qpid {
@@ -40,12 +42,7 @@
std::string getDestination() const
{
- return destination;
- }
-
- void setDestination(const std::string& dest)
- {
- destination = dest;
+ return method.getDestination();
}
bool isRedelivered() const
@@ -53,7 +50,8 @@
return hasDeliveryProperties() && getDeliveryProperties().getRedelivered();
}
- void setRedelivered(bool redelivered) {
+ void setRedelivered(bool redelivered)
+ {
getDeliveryProperties().setRedelivered(redelivered);
}
@@ -62,8 +60,25 @@
return getMessageProperties().getApplicationHeaders();
}
+ void acknowledge(Session& session, bool cumulative = true, bool send = true) const
+ {
+ session.execution().completed(id, cumulative, send);
+ }
+
+ Message(const framing::FrameSet& frameset) : method(*frameset.as<framing::MessageTransferBody>()), id(frameset.getId())
+ {
+ populate(frameset);
+ }
+
+ const framing::MessageTransferBody& getMethod() const
+ {
+ return method;
+ }
+
private:
- std::string destination;
+ //method and id are only set for received messages:
+ const framing::MessageTransferBody method;
+ const framing::SequenceNumber id;
};
}}
Added: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp?rev=576935&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp Tue Sep 18 07:45:33 2007
@@ -0,0 +1,150 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 "Dispatcher.h"
+
+#include "qpid/client/Session.h"
+#include "qpid/framing/FrameSet.h"
+#include "qpid/framing/MessageTransferBody.h"
+#include "qpid/log/Statement.h"
+#include "BlockingQueue.h"
+#include "ClientMessage.h"
+
+using qpid::framing::FrameSet;
+using qpid::framing::MessageTransferBody;
+using qpid::sys::Mutex;
+using qpid::sys::ScopedLock;
+using qpid::sys::Thread;
+
+namespace qpid {
+namespace client {
+
+ Subscriber::Subscriber(Session& s, MessageListener* l, bool a, uint f) : session(s), listener(l), autoAck(a), ackFrequency(f), count(0) {}
+
+void Subscriber::received(Message& msg)
+{
+ if (listener) {
+ listener->received(msg);
+ if (autoAck) {
+ bool send = (++count >= ackFrequency);
+ msg.acknowledge(session, true, send);
+ if (send) count = 0;
+ }
+ }
+}
+
+
+ Dispatcher::Dispatcher(Session& s, const std::string& q) : session(s), queue(q), running(false), stopped(false)
+{
+}
+
+void Dispatcher::start()
+{
+ worker = Thread(this);
+}
+
+void Dispatcher::run()
+{
+ BlockingQueue<FrameSet::shared_ptr>& q = queue.empty() ?
+ session.execution().getDemux().getDefault() :
+ session.execution().getDemux().get(queue);
+
+ startRunning();
+ stopped = false;
+ while (!isStopped()) {
+ FrameSet::shared_ptr content = q.pop();
+ if (content->isA<MessageTransferBody>()) {
+ Message msg(*content);
+ Subscriber::shared_ptr listener = find(msg.getDestination());
+ if (!listener) {
+ QPID_LOG(error, "No message listener set: " << content->getMethod());
+ } else {
+ listener->received(msg);
+ }
+ } else {
+ if (handler.get()) {
+ handler->handle(*content);
+ } else {
+ QPID_LOG(error, "Unhandled method: " << content->getMethod());
+ }
+ }
+ }
+ stopRunning();
+}
+
+void Dispatcher::stop()
+{
+ ScopedLock<Mutex> l(lock);
+ stopped = true;
+}
+
+bool Dispatcher::isStopped()
+{
+ ScopedLock<Mutex> l(lock);
+ return stopped;
+}
+
+/**
+ * Prevent concurrent threads invoking run.
+ */
+void Dispatcher::startRunning()
+{
+ ScopedLock<Mutex> l(lock);
+ if (running) {
+ throw Exception("Dispatcher is already running.");
+ }
+ running = true;
+}
+
+void Dispatcher::stopRunning()
+{
+ ScopedLock<Mutex> l(lock);
+ running = false;
+}
+
+Subscriber::shared_ptr Dispatcher::find(const std::string& name)
+{
+ ScopedLock<Mutex> l(lock);
+ Listeners::iterator i = listeners.find(name);
+ if (i == listeners.end()) {
+ return defaultListener;
+ }
+ return i->second;
+}
+
+void Dispatcher::listen(MessageListener* listener, bool autoAck, uint ackFrequency)
+{
+ ScopedLock<Mutex> l(lock);
+ defaultListener = Subscriber::shared_ptr(new Subscriber(session, listener, autoAck, ackFrequency));
+}
+
+void Dispatcher::listen(const std::string& destination, MessageListener* listener, bool autoAck, uint ackFrequency)
+{
+ ScopedLock<Mutex> l(lock);
+ listeners[destination] = Subscriber::shared_ptr(new Subscriber(session, listener, autoAck, ackFrequency));
+}
+
+void Dispatcher::cancel(const std::string& destination)
+{
+ ScopedLock<Mutex> l(lock);
+ listeners.erase(destination);
+}
+
+}}
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.cpp
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h?rev=576935&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h Tue Sep 18 07:45:33 2007
@@ -0,0 +1,87 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ *
+ */
+#ifndef _Dispatcher_
+#define _Dispatcher_
+
+#include <map>
+#include <memory>
+#include <string>
+#include <boost/shared_ptr.hpp>
+#include "qpid/sys/Mutex.h"
+#include "qpid/sys/Runnable.h"
+#include "qpid/sys/Thread.h"
+#include "MessageListener.h"
+
+namespace qpid {
+namespace client {
+
+class Session;
+
+class Subscriber : public MessageListener
+{
+ Session& session;
+ MessageListener* const listener;
+ const bool autoAck;
+ const uint ackFrequency;
+ uint count;
+
+public:
+ typedef boost::shared_ptr<Subscriber> shared_ptr;
+ Subscriber(Session& session, MessageListener* listener, bool autoAck = true, uint ackFrequency = 1);
+ void received(Message& msg);
+
+};
+
+typedef framing::Handler<framing::FrameSet> FrameSetHandler;
+
+class Dispatcher : public sys::Runnable
+{
+ typedef std::map<std::string, Subscriber::shared_ptr> Listeners;
+ sys::Mutex lock;
+ sys::Thread worker;
+ Session& session;
+ const std::string queue;
+ bool running;
+ bool stopped;
+ Listeners listeners;
+ Subscriber::shared_ptr defaultListener;
+ std::auto_ptr<FrameSetHandler> handler;
+
+ Subscriber::shared_ptr find(const std::string& name);
+ void startRunning();
+ void stopRunning();
+ bool isStopped();
+
+public:
+ Dispatcher(Session& session, const std::string& queue = "");
+
+ void start();
+ void run();
+ void stop();
+
+ void listen(MessageListener* listener, bool autoAck = true, uint ackFrequency = 1);
+ void listen(const std::string& destination, MessageListener* listener, bool autoAck = true, uint ackFrequency = 1);
+ void cancel(const std::string& destination);
+};
+
+}}
+
+#endif
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/Dispatcher.h
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/framing/TransferContent.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/framing/TransferContent.h?rev=576935&r1=576934&r2=576935&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/framing/TransferContent.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/framing/TransferContent.h Tue Sep 18 07:45:33 2007
@@ -37,7 +37,7 @@
AMQHeaderBody header;
std::string data;
public:
- TransferContent(const std::string& data);
+ TransferContent(const std::string& data = "");
AMQHeaderBody getHeader() const;
void setData(const std::string&);
void appendData(const std::string&);
Modified: incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp?rev=576935&r1=576934&r2=576935&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/ClientSessionTest.cpp Tue Sep 18 07:45:33 2007
@@ -18,27 +18,55 @@
* under the License.
*
*/
-#include <vector>
+#include <list>
#include "qpid_test_plugin.h"
#include "InProcessBroker.h"
+#include "qpid/client/Dispatcher.h"
#include "qpid/client/Session.h"
#include "qpid/framing/TransferContent.h"
using namespace qpid::client;
using namespace qpid::framing;
+struct DummyListener : public MessageListener
+{
+ std::list<Message> messages;
+ std::string name;
+ uint expected;
+ uint count;
+ Dispatcher dispatcher;
+
+ DummyListener(Session& session, const std::string& _name, uint _expected) : name(_name), expected(_expected), count(0),
+ dispatcher(session) {}
+
+ void listen()
+ {
+ dispatcher.listen(name, this, true, 1);
+ dispatcher.run();
+ }
+
+ void received(Message& msg)
+ {
+ messages.push_back(msg);
+ if (++count == expected) {
+ dispatcher.stop();
+ }
+ }
+};
+
class ClientSessionTest : public CppUnit::TestCase
{
CPPUNIT_TEST_SUITE(ClientSessionTest);
CPPUNIT_TEST(testQueueQuery);
CPPUNIT_TEST(testTransfer);
+ CPPUNIT_TEST(testDispatcher);
CPPUNIT_TEST_SUITE_END();
boost::shared_ptr<Connector> broker;
Connection connection;
Session session;
- public:
+public:
ClientSessionTest() : broker(new qpid::broker::InProcessBroker()), connection(broker)
{
@@ -77,6 +105,37 @@
CPPUNIT_ASSERT_EQUAL(data, msg->getContent());
//confirm receipt:
session.execution().completed(msg->getId(), true, true);
+ }
+
+ void testDispatcher()
+ {
+ session.queueDeclare_(queue="my-queue", exclusive=true, autoDelete=true);
+
+ TransferContent msg1("One");
+ msg1.getDeliveryProperties().setRoutingKey("my-queue");
+ session.messageTransfer_(content=msg1);
+
+ TransferContent msg2("Two");
+ msg2.getDeliveryProperties().setRoutingKey("my-queue");
+ session.messageTransfer_(content=msg2);
+
+ TransferContent msg3("Three");
+ msg3.getDeliveryProperties().setRoutingKey("my-queue");
+ session.messageTransfer_(content=msg3);
+
+ session.messageSubscribe_(queue="my-queue", destination="my-dest", acquireMode=1);
+ session.messageFlow((destination="my-dest", unit=0, value=1));//messages
+ session.messageFlow((destination="my-dest", unit=1, value=0xFFFFFFFF));//bytes
+ DummyListener listener(session, "my-dest", 3);
+ listener.listen();
+ CPPUNIT_ASSERT_EQUAL((size_t) 3, listener.messages.size());
+ CPPUNIT_ASSERT_EQUAL(std::string("One"), listener.messages.front().getData());
+ listener.messages.pop_front();
+ CPPUNIT_ASSERT_EQUAL(std::string("Two"), listener.messages.front().getData());
+ listener.messages.pop_front();
+ CPPUNIT_ASSERT_EQUAL(std::string("Three"), listener.messages.front().getData());
+ listener.messages.pop_front();
+
}
};