You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2007/11/02 17:25:03 UTC
svn commit: r591388 - in /incubator/qpid/trunk/qpid/cpp:
examples/topic_listener.cpp src/Makefile.am
src/qpid/client/SubscriptionManager.cpp src/qpid/client/SubscriptionManager.h
Author: aconway
Date: Fri Nov 2 09:25:02 2007
New Revision: 591388
URL: http://svn.apache.org/viewvc?rev=591388&view=rev
Log:
Improvements to gsim's SubscriptionManager:
- auto-stop mode: run returns when all subscriptions cancelled.
- flow control.
examples/topic_listener.cpp updated to use SubscriptionManager.
Added:
incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp (with props)
incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h (with props)
Modified:
incubator/qpid/trunk/qpid/cpp/examples/topic_listener.cpp
incubator/qpid/trunk/qpid/cpp/src/Makefile.am
Modified: incubator/qpid/trunk/qpid/cpp/examples/topic_listener.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/examples/topic_listener.cpp?rev=591388&r1=591387&r2=591388&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/examples/topic_listener.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/examples/topic_listener.cpp Fri Nov 2 09:25:02 2007
@@ -20,81 +20,152 @@
*/
/**
- * This file provides one half of a test and example of a pub-sub
- * style of interaction. See topic_listener.cpp for the other half, in
- * which the logic for subscribers is defined.
- *
- * This file contains the publisher logic. The publisher will send a
- * number of messages to the exchange with the appropriate routing key
- * for the logical 'topic'. Once it has done this it will then send a
- * request that each subscriber report back with the number of message
- * it has received and the time that elapsed between receiving the
- * first one and receiving the report request. Once the expected
- * number of reports are received, it sends out a request that each
- * subscriber shutdown.
+ * topic_listener.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs use the topic exchange.
+ *
+ * topic_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * topic_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * topic_listener.cpp (this program):
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
*/
-#include "qpid/Exception.h"
-#include "qpid/client/Channel.h"
-#include "qpid/client/Connection.h"
-#include "qpid/client/Exchange.h"
-#include "qpid/client/MessageListener.h"
-#include "qpid/client/Queue.h"
-#include "qpid/sys/Monitor.h"
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session_0_10.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/MessageListener.h>
+#include <qpid/client/Queue.h>
+#include <qpid/client/SubscriptionManager.h>
+
#include <unistd.h>
-#include "qpid/sys/Time.h"
#include <cstdlib>
#include <iostream>
-#include <time.h>
+#include <set>
using namespace qpid::client;
-using namespace qpid::sys;
-using namespace std;
-
+using namespace qpid::framing;
-bool done = 0;
-
-class Listener : public MessageListener{
-public:
- virtual void received(Message& msg);
+class Listener : public MessageListener {
+ private:
+ Session_0_10& session;
+ SubscriptionManager subscriptions;
+ public:
+ Listener(Session_0_10& session);
+ virtual void prepareQueue(std::string queue, std::string routing_key);
+ virtual void received(Message& message);
+ virtual void listen();
~Listener() { };
};
+/**
+ * Listener::Listener
+ *
+ * Subscribe to the queue, route it to a client destination for the
+ * listener. (The destination name merely identifies the destination
+ * in the listener, you can use any name as long as you use the same
+ * name for the listener).
+ */
+Listener::Listener(Session_0_10& session) :
+ session(session),
+ subscriptions(session)
+{}
+
+
+void Listener::prepareQueue(std::string queue, std::string routing_key) {
+
+ /* Create a unique queue name for this queue by concatenating
+ * the Session ID.
+ */
+ queue += session.getId().str();
+
+ std::cout << "Declaring queue: " << queue << std::endl;
+
+ /* Declare an exclusive queue on the broker
+ */
+
+ session.queueDeclare(arg::queue=queue, arg::exclusive=true);
+
+ /* Route messages to the new queue if they match the routing key.
+ *
+ * Also route any messages to with the "control" routing key to
+ * this queue so we know when it's time to stop. A publisher sends
+ * a message with the content "That's all, Folks!", using the
+ * "control" routing key, when it is finished.
+ */
+
+ session.queueBind(arg::exchange="amq.topic", arg::queue=queue,
+ arg::routingKey=routing_key);
+ session.queueBind(arg::exchange="amq.topic", arg::queue=queue,
+ arg::routingKey="control");
+
+
+ // Subscribe to the queue using the subscription manager.
+ // The name of the subscription defaults to the name of the queue.
+ //
+ std::cout << "Subscribing to queue " << queue << std::endl;
+ subscriptions.subscribe(*this, queue);
+}
+
+void Listener::received(Message& message) {
+ //
+ // message.getDestination() returns the name of the subscription
+ // to which this message was sent, which by default is the name
+ // of the queue subscribed to.
+ //
+ std::cout << "Message: " << message.getData()
+ << " from " << message.getDestination() << std::endl;
+
+ if (message.getData() == "That's all, folks!") {
+ std::cout << "Shutting down listener for "
+ << message.getDestination() << std::endl;
+ subscriptions.cancel(message.getDestination());
+ }
+}
+
+void Listener::listen() {
+ // run() will return when all the subscriptions are cancelled.
+ subscriptions.run();
+}
int main() {
Connection connection;
- Channel channel;
- Message msg;
- cout << "Hello" << endl;
try {
- connection.open("127.0.0.1", 5672, "guest", "guest", "/test");
- connection.openChannel(channel);
+ connection.open("127.0.0.1", 5672);
+ Session_0_10 session = connection.newSession();
+
+ // Create a listener for the session
+
+ Listener listener(session);
- //--------- Main body of program --------------------------------------------
+ // Subscribe to messages on the queues we are interested in
- Queue response("listener");
- Listener listener;
- string routingKey="listener";
- channel.consume(response, routingKey, &listener);
+ listener.prepareQueue("usa", "usa.#");
+ listener.prepareQueue("europe", "europe.#");
+ listener.prepareQueue("news", "#.news");
+ listener.prepareQueue("weather", "#.weather");
- channel.start();
+ std::cout << "Listening for messages ..." << std::endl;
+
+ // Give up control and receive messages
+ listener.listen();
- while (!done)
- sleep(1000);
- //-----------------------------------------------------------------------------
- channel.close();
connection.close();
return 0;
} catch(const std::exception& error) {
- cout << "Unexpected exception: " << error.what() << endl;
+ std::cout << error.what() << std::endl;
}
- connection.close();
- return 1;
+ return 1;
}
-void Listener::received(Message& msg) {
- cout << "Message: " << msg.getData() << endl;
- if (msg.getData() == "That's all, folks!")
- done = 1;
-}
+
Modified: incubator/qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/Makefile.am?rev=591388&r1=591387&r2=591388&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/Makefile.am Fri Nov 2 09:25:02 2007
@@ -197,12 +197,12 @@
libqpidclient_la_LIBADD = libqpidcommon.la
libqpidclient_la_SOURCES = \
- $(rgen_client_cpp) \
- qpid/client/Connection.cpp \
+ $(rgen_client_cpp) \
+ qpid/client/Connection.cpp \
qpid/client/Channel.cpp \
- qpid/client/Exchange.cpp \
- qpid/broker/PersistableMessage.cpp \
- qpid/client/Queue.cpp \
+ qpid/client/Exchange.cpp \
+ qpid/broker/PersistableMessage.cpp \
+ qpid/client/Queue.cpp \
qpid/client/ConnectionImpl.cpp \
qpid/client/Connector.cpp \
qpid/client/Demux.cpp \
@@ -216,7 +216,8 @@
qpid/client/FutureResponse.cpp \
qpid/client/FutureResult.cpp \
qpid/client/SessionCore.cpp \
- qpid/client/StateManager.cpp
+ qpid/client/StateManager.cpp \
+ qpid/client/SubscriptionManager.cpp
nobase_include_HEADERS = \
$(platform_hdr) \
@@ -325,6 +326,7 @@
qpid/client/Response.h \
qpid/client/SessionCore.h \
qpid/client/StateManager.h \
+ qpid/client/SubscriptionManager.h \
qpid/client/TypedResult.h \
qpid/framing/AMQBody.h \
qpid/framing/AMQContentBody.h \
Added: incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp?rev=591388&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp Fri Nov 2 09:25:02 2007
@@ -0,0 +1,97 @@
+/*
+ *
+ * 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 _Subscription_
+#define _Subscription_
+
+#include "SubscriptionManager.h"
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/MessageListener.h>
+#include <set>
+#include <sstream>
+
+
+namespace qpid {
+namespace client {
+
+SubscriptionManager::SubscriptionManager(Session_0_10& s)
+ : dispatcher(s), session(s), messages(1), bytes(UNLIMITED), autoStop(true)
+{}
+
+std::string SubscriptionManager::uniqueTag(const std::string& tag) {
+ // Make unique tag.
+ int count=1;
+ std::string unique=tag;
+ while (subscriptions.find(tag) != subscriptions.end()) {
+ std::ostringstream s;
+ s << tag << "-" << count++;
+ unique=s.str();
+ }
+ subscriptions.insert(unique);
+ return tag;
+}
+
+std::string SubscriptionManager::subscribe(
+ MessageListener& listener, const std::string& q, const std::string& t)
+{
+ std::string tag=uniqueTag(t);
+ using namespace arg;
+ session.messageSubscribe(arg::queue=q, arg::destination=tag);
+ flowLimits(tag, messages, bytes);
+ dispatcher.listen(tag, &listener);
+ return tag;
+}
+
+void SubscriptionManager::flowLimits(
+ const std::string& tag, uint32_t messages, uint32_t bytes) {
+ session.messageFlow(tag, 0, messages);
+ session.messageFlow(tag, 1, bytes);
+}
+
+void SubscriptionManager::flowLimits(uint32_t m, uint32_t b) {
+ messages=m;
+ bytes=b;
+}
+
+void SubscriptionManager::cancel(const std::string tag)
+{
+ if (subscriptions.erase(tag)) {
+ dispatcher.cancel(tag);
+ session.messageCancel(tag);
+ if (autoStop && subscriptions.empty()) stop();
+ }
+}
+
+void SubscriptionManager::run(bool autoStop_)
+{
+ autoStop=autoStop_;
+ if (autoStop && subscriptions.empty()) return;
+ dispatcher.run();
+}
+
+void SubscriptionManager::stop()
+{
+ dispatcher.stop();
+}
+
+}} // namespace qpid::client
+
+#endif
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.cpp
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h?rev=591388&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h Fri Nov 2 09:25:02 2007
@@ -0,0 +1,93 @@
+#ifndef QPID_CLIENT_SUBSCRIPTIONMANAGER_H
+#define QPID_CLIENT_SUBSCRIPTIONMANAGER_H
+
+/*
+ *
+ * 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 <qpid/client/Dispatcher.h>
+#include <qpid/client/Session_0_10.h>
+#include <qpid/client/MessageListener.h>
+#include <set>
+#include <sstream>
+
+namespace qpid {
+namespace client {
+
+struct TagNotUniqueException : public qpid::Exception {
+ TagNotUniqueException() {}
+};
+
+class SubscriptionManager
+{
+ std::set<std::string> subscriptions;
+ qpid::client::Dispatcher dispatcher;
+ qpid::client::Session_0_10& session;
+ std::string uniqueTag(const std::string&);
+ uint32_t messages;
+ uint32_t bytes;
+ bool autoStop;
+
+public:
+ SubscriptionManager(Session_0_10& session);
+
+ /**
+ * Subscribe listener to receive messages from queue.
+ *@param listener Listener object to receive messages.
+ *@param queue Name of the queue to subscribe to.
+ *@param tag Unique destination tag for the listener.
+ * If not specified a unique tag will be generted based on the queue name.
+ *@return Destination tag.
+ *@exception TagNotUniqueException if there is already a subscription
+ * with the same tag.
+ */
+ std::string subscribe(MessageListener& listener,
+ const std::string& queue,
+ const std::string& tag=std::string());
+
+ /** Cancel a subscription. */
+ void cancel(const std::string tag);
+
+ qpid::client::Dispatcher& getDispatcher() { return dispatcher; }
+ size_t size() { return subscriptions.size(); }
+
+ /** Deliver messages until stop() is called.
+ *@param autoStop If true, return when all subscriptions are cancelled.
+ */
+ void run(bool autoStop=true);
+
+ /** Cause run() to return */
+ void stop();
+
+ static const uint32_t UNLIMITED=0xFFFFFFFF;
+
+ /** Set the flow control limits for subscriber with tag.
+ * UNLIMITED means no limit.
+ */
+ void flowLimits(const std::string& tag, uint32_t messages, uint32_t bytes);
+
+ /** Set the initial flow control limits for new subscribers */
+ void flowLimits(uint32_t messages, uint32_t bytes);
+};
+
+
+}} // namespace qpid::client
+
+#endif /*!QPID_CLIENT_SUBSCRIPTIONMANAGER_H*/
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/client/SubscriptionManager.h
------------------------------------------------------------------------------
svn:keywords = Rev Date