summaryrefslogtreecommitdiff
path: root/cpp/examples/pub-sub
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2008-06-20 21:23:23 +0000
committerAlan Conway <aconway@apache.org>2008-06-20 21:23:23 +0000
commit11ccae93c7550828f7083a74f90a691769271b66 (patch)
tree3d4ef5541f1261daca012ab926ba3fdffcee7315 /cpp/examples/pub-sub
parent7566d73d9c3e1226728ac3699d85ae0ad9715cee (diff)
downloadqpid-python-11ccae93c7550828f7083a74f90a691769271b66.tar.gz
Patch from Manuel Teira: https://issues.apache.org/jira/secure/CommentAssignIssue!default.jspa?action=5&id=12398038
- Use standard automake makefiles to build cpp/examples. - Rationalize examples directory structure. Additions to patch: - Fix for VPATH builds. - Fix make distcheck (finally!) git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@670066 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/examples/pub-sub')
-rw-r--r--cpp/examples/pub-sub/Makefile.am30
-rw-r--r--cpp/examples/pub-sub/topic_listener.cpp171
-rw-r--r--cpp/examples/pub-sub/topic_publisher.cpp128
-rw-r--r--cpp/examples/pub-sub/verify4
-rw-r--r--cpp/examples/pub-sub/verify.in59
-rw-r--r--cpp/examples/pub-sub/verify_cpp_python6
-rw-r--r--cpp/examples/pub-sub/verify_cpp_python.in55
-rw-r--r--cpp/examples/pub-sub/verify_python_cpp6
-rw-r--r--cpp/examples/pub-sub/verify_python_cpp.in59
9 files changed, 518 insertions, 0 deletions
diff --git a/cpp/examples/pub-sub/Makefile.am b/cpp/examples/pub-sub/Makefile.am
new file mode 100644
index 0000000000..8446a1c40c
--- /dev/null
+++ b/cpp/examples/pub-sub/Makefile.am
@@ -0,0 +1,30 @@
+examplesdir=$(pkgdatadir)/examples/pub-sub
+
+include $(top_srcdir)/examples/makedist.mk
+
+noinst_PROGRAMS=topic_listener topic_publisher
+
+topic_listener_SOURCES=topic_listener.cpp
+topic_listener_LDADD=$(CLIENT_LIB)
+
+topic_publisher_SOURCES=topic_publisher.cpp
+topic_publisher_LDADD=$(CLIENT_LIB)
+
+examples_DATA= \
+ topic_listener.cpp \
+ topic_publisher.cpp \
+ $(MAKEDIST)
+
+EXTRA_DIST= \
+ $(examples_DATA) \
+ verify \
+ verify.in \
+ verify_cpp_python \
+ verify_cpp_python.in \
+ verify_python_cpp \
+ verify_python_cpp.in
+
+
+
+
+
diff --git a/cpp/examples/pub-sub/topic_listener.cpp b/cpp/examples/pub-sub/topic_listener.cpp
new file mode 100644
index 0000000000..9996abab19
--- /dev/null
+++ b/cpp/examples/pub-sub/topic_listener.cpp
@@ -0,0 +1,171 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * 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/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/MessageListener.h>
+#include <qpid/client/SubscriptionManager.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+#include <set>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+
+class Listener : public MessageListener {
+ private:
+ Session& session;
+ SubscriptionManager subscriptions;
+ public:
+ Listener(Session& 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& session) :
+ session(session),
+ subscriptions(session)
+{
+}
+
+
+void Listener::prepareQueue(std::string queue, std::string routing_key) {
+
+ /* Create a unique queue name for this consumer by concatenating
+ * the queue name parameter with the Session ID.
+ */
+
+ queue += session.getId().getName();
+ std::cout << "Declaring queue: " << queue << std::endl;
+
+ /* Declare an exclusive queue on the broker
+ */
+
+ session.queueDeclare(arg::queue=queue, arg::exclusive=true, arg::autoDelete=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.exchangeBind(arg::exchange="amq.topic", arg::queue=queue, arg::bindingKey=routing_key);
+ session.exchangeBind(arg::exchange="amq.topic", arg::queue=queue, arg::bindingKey="control");
+
+ /*
+ * subscribe to the queue using the subscription manager.
+ */
+
+ std::cout << "Subscribing to queue " << queue << std::endl;
+ subscriptions.subscribe(*this, queue);
+}
+
+void Listener::received(Message& message) {
+ 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() {
+ // Receive messages
+ subscriptions.run();
+}
+
+int main(int argc, char** argv) {
+ const char* host = argc>1 ? argv[1] : "127.0.0.1";
+ int port = argc>2 ? atoi(argv[2]) : 5672;
+ Connection connection;
+ try {
+ connection.open(host, port);
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ // Create a listener for the session
+
+ Listener listener(session);
+
+ // Subscribe to messages on the queues we are interested in
+
+ listener.prepareQueue("usa", "usa.#");
+ listener.prepareQueue("europe", "europe.#");
+ listener.prepareQueue("news", "#.news");
+ listener.prepareQueue("weather", "#.weather");
+
+ std::cout << "Listening for messages ..." << std::endl;
+
+ // Give up control and receive messages
+ listener.listen();
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/pub-sub/topic_publisher.cpp b/cpp/examples/pub-sub/topic_publisher.cpp
new file mode 100644
index 0000000000..ab485fec8f
--- /dev/null
+++ b/cpp/examples/pub-sub/topic_publisher.cpp
@@ -0,0 +1,128 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+
+/**
+ * topic_publisher.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 (this program):
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * topic_listener.cpp
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ */
+
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/AsyncSession.h>
+#include <qpid/client/Message.h>
+
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+#include <sstream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::stringstream;
+using std::string;
+
+void publish_messages(Session& session, string routing_key)
+{
+ Message message;
+
+ // Set the routing key once, we'll use the same routing key for all
+ // messages.
+
+ message.getDeliveryProperties().setRoutingKey(routing_key);
+ for (int i=0; i<5; i++) {
+ stringstream message_data;
+ message_data << "Message " << i;
+
+ message.setData(message_data.str());
+ // Asynchronous transfer sends messages as quickly as
+ // possible without waiting for confirmation.
+ async(session).messageTransfer(arg::content=message, arg::destination="amq.topic");
+ }
+
+}
+
+/*
+ * no_more_messages()
+ *
+ * Send a message to indicate that no more messages are coming.
+ * Use the 'control' routing key (see comments in topic_config_queues.cpp).
+ *
+ */
+
+void no_more_messages(Session& session)
+{
+ Message message;
+
+ message.getDeliveryProperties().setRoutingKey("control");
+ message.setData("That's all, folks!");
+ session.messageTransfer(arg::content=message, arg::destination="amq.topic");
+}
+
+int main(int argc, char** argv) {
+ const char* host = argc>1 ? argv[1] : "127.0.0.1";
+ int port = argc>2 ? atoi(argv[2]) : 5672;
+ Connection connection;
+ Message message;
+ try {
+ connection.open(host, port);
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ publish_messages(session, "usa.news");
+ publish_messages(session, "usa.weather");
+ publish_messages(session, "europe.news");
+ publish_messages(session, "europe.weather");
+
+ no_more_messages(session);
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/pub-sub/verify b/cpp/examples/pub-sub/verify
new file mode 100644
index 0000000000..3589a4c9da
--- /dev/null
+++ b/cpp/examples/pub-sub/verify
@@ -0,0 +1,4 @@
+# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify
+background "Listening" ./topic_listener
+clients ./topic_publisher
+outputs ./topic_publisher.out "topic_listener.out | remove_uuid | sort"
diff --git a/cpp/examples/pub-sub/verify.in b/cpp/examples/pub-sub/verify.in
new file mode 100644
index 0000000000..6413c5c788
--- /dev/null
+++ b/cpp/examples/pub-sub/verify.in
@@ -0,0 +1,59 @@
+==== topic_publisher.out
+==== topic_listener.out | remove_uuid | sort
+Declaring queue: europe
+Declaring queue: news
+Declaring queue: usa
+Declaring queue: weather
+Listening for messages ...
+Message: Message 0 from europe
+Message: Message 0 from europe
+Message: Message 0 from news
+Message: Message 0 from news
+Message: Message 0 from usa
+Message: Message 0 from usa
+Message: Message 0 from weather
+Message: Message 0 from weather
+Message: Message 1 from europe
+Message: Message 1 from europe
+Message: Message 1 from news
+Message: Message 1 from news
+Message: Message 1 from usa
+Message: Message 1 from usa
+Message: Message 1 from weather
+Message: Message 1 from weather
+Message: Message 2 from europe
+Message: Message 2 from europe
+Message: Message 2 from news
+Message: Message 2 from news
+Message: Message 2 from usa
+Message: Message 2 from usa
+Message: Message 2 from weather
+Message: Message 2 from weather
+Message: Message 3 from europe
+Message: Message 3 from europe
+Message: Message 3 from news
+Message: Message 3 from news
+Message: Message 3 from usa
+Message: Message 3 from usa
+Message: Message 3 from weather
+Message: Message 3 from weather
+Message: Message 4 from europe
+Message: Message 4 from europe
+Message: Message 4 from news
+Message: Message 4 from news
+Message: Message 4 from usa
+Message: Message 4 from usa
+Message: Message 4 from weather
+Message: Message 4 from weather
+Message: That's all, folks! from europe
+Message: That's all, folks! from news
+Message: That's all, folks! from usa
+Message: That's all, folks! from weather
+Shutting down listener for europe
+Shutting down listener for news
+Shutting down listener for usa
+Shutting down listener for weather
+Subscribing to queue europe
+Subscribing to queue news
+Subscribing to queue usa
+Subscribing to queue weather
diff --git a/cpp/examples/pub-sub/verify_cpp_python b/cpp/examples/pub-sub/verify_cpp_python
new file mode 100644
index 0000000000..f6c6850981
--- /dev/null
+++ b/cpp/examples/pub-sub/verify_cpp_python
@@ -0,0 +1,6 @@
+# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify
+py=$PYTHON_EXAMPLES/pubsub
+background "Queues created" $py/topic_subscriber.py
+clients ./topic_publisher
+outputs ./topic_publisher.out "$py/topic_subscriber.py.out | remove_uuid | sort"
+
diff --git a/cpp/examples/pub-sub/verify_cpp_python.in b/cpp/examples/pub-sub/verify_cpp_python.in
new file mode 100644
index 0000000000..951d9ad9dd
--- /dev/null
+++ b/cpp/examples/pub-sub/verify_cpp_python.in
@@ -0,0 +1,55 @@
+==== topic_publisher.out
+==== topic_subscriber.py.out | remove_uuid | sort
+Message 0
+Message 0
+Message 0
+Message 0
+Message 0
+Message 0
+Message 0
+Message 0
+Message 1
+Message 1
+Message 1
+Message 1
+Message 1
+Message 1
+Message 1
+Message 1
+Message 2
+Message 2
+Message 2
+Message 2
+Message 2
+Message 2
+Message 2
+Message 2
+Message 3
+Message 3
+Message 3
+Message 3
+Message 3
+Message 3
+Message 3
+Message 3
+Message 4
+Message 4
+Message 4
+Message 4
+Message 4
+Message 4
+Message 4
+Message 4
+Messages on 'europe' queue:
+Messages on 'news' queue:
+Messages on 'usa' queue:
+Messages on 'weather' queue:
+Queues created - please start the topic producer
+Subscribing local queue 'local_europe' to europe-'
+Subscribing local queue 'local_news' to news-'
+Subscribing local queue 'local_usa' to usa-'
+Subscribing local queue 'local_weather' to weather-'
+That's all, folks!
+That's all, folks!
+That's all, folks!
+That's all, folks!
diff --git a/cpp/examples/pub-sub/verify_python_cpp b/cpp/examples/pub-sub/verify_python_cpp
new file mode 100644
index 0000000000..2ddaad58c2
--- /dev/null
+++ b/cpp/examples/pub-sub/verify_python_cpp
@@ -0,0 +1,6 @@
+# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify
+py=$PYTHON_EXAMPLES/pubsub
+background "Listening" ./topic_listener
+clients $py/topic_publisher.py
+outputs $py/topic_publisher.py.out "topic_listener.out | remove_uuid | sort"
+
diff --git a/cpp/examples/pub-sub/verify_python_cpp.in b/cpp/examples/pub-sub/verify_python_cpp.in
new file mode 100644
index 0000000000..52e8db9d72
--- /dev/null
+++ b/cpp/examples/pub-sub/verify_python_cpp.in
@@ -0,0 +1,59 @@
+==== topic_publisher.py.out
+==== topic_listener.out | remove_uuid | sort
+Declaring queue: europe
+Declaring queue: news
+Declaring queue: usa
+Declaring queue: weather
+Listening for messages ...
+Message: europe.news 0 from europe
+Message: europe.news 0 from news
+Message: europe.news 1 from europe
+Message: europe.news 1 from news
+Message: europe.news 2 from europe
+Message: europe.news 2 from news
+Message: europe.news 3 from europe
+Message: europe.news 3 from news
+Message: europe.news 4 from europe
+Message: europe.news 4 from news
+Message: europe.weather 0 from europe
+Message: europe.weather 0 from weather
+Message: europe.weather 1 from europe
+Message: europe.weather 1 from weather
+Message: europe.weather 2 from europe
+Message: europe.weather 2 from weather
+Message: europe.weather 3 from europe
+Message: europe.weather 3 from weather
+Message: europe.weather 4 from europe
+Message: europe.weather 4 from weather
+Message: That's all, folks! from europe
+Message: That's all, folks! from news
+Message: That's all, folks! from usa
+Message: That's all, folks! from weather
+Message: usa.news 0 from news
+Message: usa.news 0 from usa
+Message: usa.news 1 from news
+Message: usa.news 1 from usa
+Message: usa.news 2 from news
+Message: usa.news 2 from usa
+Message: usa.news 3 from news
+Message: usa.news 3 from usa
+Message: usa.news 4 from news
+Message: usa.news 4 from usa
+Message: usa.weather 0 from usa
+Message: usa.weather 0 from weather
+Message: usa.weather 1 from usa
+Message: usa.weather 1 from weather
+Message: usa.weather 2 from usa
+Message: usa.weather 2 from weather
+Message: usa.weather 3 from usa
+Message: usa.weather 3 from weather
+Message: usa.weather 4 from usa
+Message: usa.weather 4 from weather
+Shutting down listener for europe
+Shutting down listener for news
+Shutting down listener for usa
+Shutting down listener for weather
+Subscribing to queue europe
+Subscribing to queue news
+Subscribing to queue usa
+Subscribing to queue weather