summaryrefslogtreecommitdiff
path: root/cpp/examples/examples
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2007-11-09 19:59:54 +0000
committerAlan Conway <aconway@apache.org>2007-11-09 19:59:54 +0000
commitb78af32f2bb9c725bacec590dbdeecaec9b2906c (patch)
treeb779b2ff59cc9a2e8362b4e34e0574f6828d9c75 /cpp/examples/examples
parent23bc7f447a2b2551dc8ae20280ede45b524e06cd (diff)
downloadqpid-python-b78af32f2bb9c725bacec590dbdeecaec9b2906c.tar.gz
Package examples in rpm %doc directory.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@593638 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/examples/examples')
-rw-r--r--cpp/examples/examples/Makefile6
-rw-r--r--cpp/examples/examples/direct/Makefile20
-rw-r--r--cpp/examples/examples/direct/direct_config_queues.cpp86
-rw-r--r--cpp/examples/examples/direct/direct_persistent_config_queues.cpp101
-rw-r--r--cpp/examples/examples/direct/direct_persistent_publisher.cpp111
-rw-r--r--cpp/examples/examples/direct/direct_publisher.cpp104
-rw-r--r--cpp/examples/examples/direct/listener.cpp134
-rw-r--r--cpp/examples/examples/fanout/Makefile17
-rw-r--r--cpp/examples/examples/fanout/fanout_config_queues.cpp86
-rw-r--r--cpp/examples/examples/fanout/fanout_consumer.cpp83
-rw-r--r--cpp/examples/examples/fanout/fanout_publisher.cpp102
-rw-r--r--cpp/examples/examples/fanout/listener.cpp134
-rw-r--r--cpp/examples/examples/pub-sub/Makefile17
-rw-r--r--cpp/examples/examples/pub-sub/topic_config_queues.cpp128
-rw-r--r--cpp/examples/examples/pub-sub/topic_listener.cpp169
-rw-r--r--cpp/examples/examples/pub-sub/topic_publisher.cpp123
-rw-r--r--cpp/examples/examples/request-response/Makefile15
-rw-r--r--cpp/examples/examples/request-response/client.cpp178
-rw-r--r--cpp/examples/examples/request-response/server.cpp153
19 files changed, 1767 insertions, 0 deletions
diff --git a/cpp/examples/examples/Makefile b/cpp/examples/examples/Makefile
new file mode 100644
index 0000000000..8591bd3361
--- /dev/null
+++ b/cpp/examples/examples/Makefile
@@ -0,0 +1,6 @@
+SUBDIRS=direct fanout pub-sub request-response
+all:
+ for d in $(SUBDIRS); do ( cd $$d; $(MAKE) $@; ) ; done
+clean:
+ for d in $(SUBDIRS); do ( cd $$d; $(MAKE) $@; ) ; done
+
diff --git a/cpp/examples/examples/direct/Makefile b/cpp/examples/examples/direct/Makefile
new file mode 100644
index 0000000000..c94e900a75
--- /dev/null
+++ b/cpp/examples/examples/direct/Makefile
@@ -0,0 +1,20 @@
+CXX=g++
+CXXFLAGS=
+
+PROGRAMS=direct_config_queues listener direct_publisher direct_persistent_publisher
+all: $(PROGRAMS)
+
+direct_config_queues: direct_config_queues.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+listener: listener.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+direct_publisher: direct_publisher.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+direct_persistent_publisher: direct_persistent_publisher.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+clean:
+ rm -f $(PROGRAMS)
diff --git a/cpp/examples/examples/direct/direct_config_queues.cpp b/cpp/examples/examples/direct/direct_config_queues.cpp
new file mode 100644
index 0000000000..3a52d4f62f
--- /dev/null
+++ b/cpp/examples/examples/direct/direct_config_queues.cpp
@@ -0,0 +1,86 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * direct_config_queues.cpp
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp (this program):
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_listener.cpp
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ */
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::string;
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+
+ //--------- Main body of program --------------------------------------------
+
+ // Create a queue named "message_queue", and route all messages whose
+ // routing key is "routing_key to this newly created queue.
+
+ session.queueDeclare(arg::queue="message_queue");
+ session.queueBind(arg::queue="message_queue", arg::routingKey="routing_key");
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+
+}
+
+
+
diff --git a/cpp/examples/examples/direct/direct_persistent_config_queues.cpp b/cpp/examples/examples/direct/direct_persistent_config_queues.cpp
new file mode 100644
index 0000000000..afe076278b
--- /dev/null
+++ b/cpp/examples/examples/direct/direct_persistent_config_queues.cpp
@@ -0,0 +1,101 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * direct_config_durable_queues.cpp
+ *
+ * This program is one of a set of programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ *
+ * direct_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_listener.cpp
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ * direct_persistent_publisher.cpp:
+ *
+ * Publishes a combination of persistent and transient messages
+ * to a broker, specifying a routing key. The persistent messages
+ * survive server restart, the transient ones do not (unless the
+ * queues are configured as durable queues).
+ *
+ * direct_config_durable_queues.cpp (this program):
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue. Uses persistent queues, so all
+ * messages on the queue survive server restart.
+ *
+ *
+ */
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::string;
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+
+ //--------- Main body of program --------------------------------------------
+
+ // Create a queue named "message_queue", and route all messages whose
+ // routing key is "routing_key to this newly created queue.
+
+ session.queueDeclare(arg::queue="message_queue");
+ session.queueBind(arg::queue="message_queue", arg::routingKey="routing_key");
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+
+}
+
+
+
diff --git a/cpp/examples/examples/direct/direct_persistent_publisher.cpp b/cpp/examples/examples/direct/direct_persistent_publisher.cpp
new file mode 100644
index 0000000000..75637c7eb9
--- /dev/null
+++ b/cpp/examples/examples/direct/direct_persistent_publisher.cpp
@@ -0,0 +1,111 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+
+/**
+ * direct_publisher.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp (this program):
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_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/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;
+
+int main() {
+ Connection connection;
+ Message message;
+ try {
+ connection.open("127.0.0.1", 5672 );
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ // The routing key is a message property. We will use the same
+ // routing key for each message, so we'll set this property
+ // just once. (In most simple cases, there is no need to set
+ // other message properties.)
+
+ message.getDeliveryProperties().setRoutingKey("routing_key");
+
+ // Now send some messages ...
+
+ for (int i=0; i<10; i++) {
+ stringstream message_data;
+ message_data << "Message " << i;
+ message.setData(message_data.str());
+
+ // Make odd-numbered messages persistent
+
+ if (i % 1)
+ message.getDeliveryProperties().setDeliveryMode(PERSISTENT);
+ else
+ message.getDeliveryProperties().setDeliveryMode(TRANSIENT);
+
+ session.messageTransfer(arg::content=message);
+ }
+
+ // And send a final message to indicate termination.
+
+ message.setData("That's all, folks!");
+ session.messageTransfer(arg::content=message);
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/direct/direct_publisher.cpp b/cpp/examples/examples/direct/direct_publisher.cpp
new file mode 100644
index 0000000000..5135f926c3
--- /dev/null
+++ b/cpp/examples/examples/direct/direct_publisher.cpp
@@ -0,0 +1,104 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+
+/**
+ * direct_publisher.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp (this program):
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_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/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;
+
+int main() {
+ Connection connection;
+ Message message;
+ try {
+ connection.open("127.0.0.1", 5672 );
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ // The routing key is a message property. We will use the same
+ // routing key for each message, so we'll set this property
+ // just once. (In most simple cases, there is no need to set
+ // other message properties.)
+
+ message.getDeliveryProperties().setRoutingKey("routing_key");
+
+ // Now send some messages ...
+
+ for (int i=0; i<10; i++) {
+ stringstream message_data;
+ message_data << "Message " << i;
+
+ message.setData(message_data.str());
+ session.messageTransfer(arg::content=message);
+ }
+
+ // And send a final message to indicate termination.
+
+ message.setData("That's all, folks!");
+ session.messageTransfer(arg::content=message);
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/direct/listener.cpp b/cpp/examples/examples/direct/listener.cpp
new file mode 100644
index 0000000000..52840efa03
--- /dev/null
+++ b/cpp/examples/examples/direct/listener.cpp
@@ -0,0 +1,134 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * direct_listener.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_listener.cpp (this program):
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ */
+
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/MessageListener.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+
+class Listener : public MessageListener{
+private:
+ std::string destination_name;
+ Dispatcher dispatcher;
+public:
+ Listener(Session& session, string destination_name):
+ destination_name(destination_name),
+ dispatcher(session)
+ {};
+
+ virtual void listen();
+ virtual void received(Message& message);
+ ~Listener() { };
+};
+
+
+void Listener::listen() {
+ std::cout << "Activating listener for: " <<destination_name << std::endl;
+ dispatcher.listen(destination_name, this);
+
+ // ### The following line gives up control - it should be possible
+ // ### to listen without giving up control!
+
+ dispatcher.run();
+}
+
+
+void Listener::received(Message& message) {
+ std::cout << "Message: " << message.getData() << std::endl;
+
+ if (message.getData() == "That's all, folks!") {
+ std::cout << "Shutting down listener for " <<destination_name << std::endl;
+ dispatcher.stop();
+ }
+}
+
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+
+ // 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).
+
+ session.messageSubscribe(arg::queue="message_queue", arg::destination="listener_destination");
+
+ // Allocate some credit ### This really should not be needed for simple examples ! ####
+
+ session.messageFlow(arg::destination="listener_destination", arg::unit=0, arg::value=1);//messages ### Define a constant?
+ session.messageFlow(arg::destination="listener_destination", arg::unit=1, arg::value=0xFFFFFFFF);//bytes ###### Define a constant?
+
+ // Tell the listener to listen to the destination we just
+ // created above.
+
+ Listener listener(session, "listener_destination");
+ listener.listen();
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/fanout/Makefile b/cpp/examples/examples/fanout/Makefile
new file mode 100644
index 0000000000..719a7e7416
--- /dev/null
+++ b/cpp/examples/examples/fanout/Makefile
@@ -0,0 +1,17 @@
+CXX=g++
+CXXFLAGS=
+
+PROGRAMS=fanout_config_queues listener fanout_publisher
+all: $(PROGRAMS)
+
+fanout_config_queues: fanout_config_queues.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+listener: listener.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+fanout_publisher: fanout_publisher.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+clean:
+ rm -f $(PROGRAMS)
diff --git a/cpp/examples/examples/fanout/fanout_config_queues.cpp b/cpp/examples/examples/fanout/fanout_config_queues.cpp
new file mode 100644
index 0000000000..b09b5191d1
--- /dev/null
+++ b/cpp/examples/examples/fanout/fanout_config_queues.cpp
@@ -0,0 +1,86 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * direct_config_queues.cpp
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp (this program):
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_listener.cpp
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ */
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::string;
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+
+ //--------- Main body of program --------------------------------------------
+
+ // Create a queue named "message_queue", and route all messages whose
+ // routing key is "routing_key to this newly created queue.
+
+ session.queueDeclare(arg::queue="message_queue");
+ session.queueBind(arg::queue="message_queue", arg::exchange="amq.fanout");
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+
+}
+
+
+
diff --git a/cpp/examples/examples/fanout/fanout_consumer.cpp b/cpp/examples/examples/fanout/fanout_consumer.cpp
new file mode 100644
index 0000000000..663c765159
--- /dev/null
+++ b/cpp/examples/examples/fanout/fanout_consumer.cpp
@@ -0,0 +1,83 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * direct_listener.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_consumer.cpp (this program):
+ *
+ * Reads from a queue on the broker using session.get().
+ *
+ * This is less efficient that direct_listener.cpp, but simpler,
+ * and can be a better approach when synchronizing messages from
+ * multiple queues.
+ *
+ */
+
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/ClientMessage.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ Listener listener(session, "destination");
+ ### session.get();
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/fanout/fanout_publisher.cpp b/cpp/examples/examples/fanout/fanout_publisher.cpp
new file mode 100644
index 0000000000..976c53aae4
--- /dev/null
+++ b/cpp/examples/examples/fanout/fanout_publisher.cpp
@@ -0,0 +1,102 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+
+/**
+ * direct_publisher.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp (this program):
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_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/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;
+
+int main() {
+ Connection connection;
+ Message message;
+ try {
+ connection.open("127.0.0.1", 5672 );
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ // Unlike topic exchanges and direct exchanges, a fanout
+ // exchange need not set a routing key.
+
+ Message message;
+
+ // Now send some messages ...
+
+ for (int i=0; i<10; i++) {
+ stringstream message_data;
+ message_data << "Message " << i;
+
+ message.setData(message_data.str());
+ session.messageTransfer(arg::content=message, arg::destination="amq.fanout");
+ }
+
+ // And send a final message to indicate termination.
+
+ message.setData("That's all, folks!");
+ session.messageTransfer(arg::content=message, arg::destination="amq.fanout");
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/fanout/listener.cpp b/cpp/examples/examples/fanout/listener.cpp
new file mode 100644
index 0000000000..52840efa03
--- /dev/null
+++ b/cpp/examples/examples/fanout/listener.cpp
@@ -0,0 +1,134 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+/**
+ * direct_listener.cpp:
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs do not specify the exchange type - the
+ * default exchange type is the direct exchange.
+ *
+ * direct_config_queues.cpp:
+ *
+ * Creates a queue on a broker, binding a routing key to route
+ * messages to that queue.
+ *
+ * direct_publisher.cpp:
+ *
+ * Publishes to a broker, specifying a routing key.
+ *
+ * direct_listener.cpp (this program):
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ */
+
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/MessageListener.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+
+class Listener : public MessageListener{
+private:
+ std::string destination_name;
+ Dispatcher dispatcher;
+public:
+ Listener(Session& session, string destination_name):
+ destination_name(destination_name),
+ dispatcher(session)
+ {};
+
+ virtual void listen();
+ virtual void received(Message& message);
+ ~Listener() { };
+};
+
+
+void Listener::listen() {
+ std::cout << "Activating listener for: " <<destination_name << std::endl;
+ dispatcher.listen(destination_name, this);
+
+ // ### The following line gives up control - it should be possible
+ // ### to listen without giving up control!
+
+ dispatcher.run();
+}
+
+
+void Listener::received(Message& message) {
+ std::cout << "Message: " << message.getData() << std::endl;
+
+ if (message.getData() == "That's all, folks!") {
+ std::cout << "Shutting down listener for " <<destination_name << std::endl;
+ dispatcher.stop();
+ }
+}
+
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+
+ // 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).
+
+ session.messageSubscribe(arg::queue="message_queue", arg::destination="listener_destination");
+
+ // Allocate some credit ### This really should not be needed for simple examples ! ####
+
+ session.messageFlow(arg::destination="listener_destination", arg::unit=0, arg::value=1);//messages ### Define a constant?
+ session.messageFlow(arg::destination="listener_destination", arg::unit=1, arg::value=0xFFFFFFFF);//bytes ###### Define a constant?
+
+ // Tell the listener to listen to the destination we just
+ // created above.
+
+ Listener listener(session, "listener_destination");
+ listener.listen();
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/pub-sub/Makefile b/cpp/examples/examples/pub-sub/Makefile
new file mode 100644
index 0000000000..ea08031da5
--- /dev/null
+++ b/cpp/examples/examples/pub-sub/Makefile
@@ -0,0 +1,17 @@
+CXX=g++
+CXXFLAGS=
+
+PROGRAMS=topic_config_queues topic_listener topic_publisher
+all: $(PROGRAMS)
+
+topic_config_queues: topic_config_queues.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+topic_listener: topic_listener.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+topic_publisher: topic_publisher.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+clean:
+ rm -f $(PROGRAMS)
diff --git a/cpp/examples/examples/pub-sub/topic_config_queues.cpp b/cpp/examples/examples/pub-sub/topic_config_queues.cpp
new file mode 100644
index 0000000000..8c05241f3c
--- /dev/null
+++ b/cpp/examples/examples/pub-sub/topic_config_queues.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_config_queues.cpp
+ *
+ * This program is one of three programs designed to be used
+ * together. These programs use the topic exchange.
+ *
+ * topic_config_queues.cpp (this program):
+ *
+ * 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
+ *
+ * Reads from a queue on the broker using a message listener.
+ *
+ */
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Session.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+using std::string;
+
+
+int main() {
+ Connection connection;
+ Message msg;
+ try {
+ connection.open("127.0.0.1", 5672);
+ Session session = connection.newSession();
+
+
+ //--------- Main body of program --------------------------------------------
+
+
+ /* A consumer application reads from the queue, and needs no
+ * knowledge of the exchanges used to route messages to the
+ * queue, or of the routing keys.
+ *
+ * A publisher application writes to the exchange, providing a
+ * routing key, It needs no knowledge of the queues or bindings
+ * used to route messages to consumers.
+ */
+
+
+ /* Create queues on the broker. */
+
+ session.queueDeclare(arg::queue="news_queue");
+ session.queueDeclare(arg::queue="weather_queue");
+ session.queueDeclare(arg::queue="usa_queue");
+ session.queueDeclare(arg::queue="europe_queue");
+
+ /* Bind these queues using routing keys, so messages will be
+ delivered to the right queues. */
+
+ session.queueBind(arg::exchange="amq.topic", arg::queue="news_queue", arg::routingKey="#.news");
+ session.queueBind(arg::exchange="amq.topic", arg::queue="weather_queue", arg::routingKey="#.weather");
+ session.queueBind(arg::exchange="amq.topic", arg::queue="usa_queue", arg::routingKey="usa.#");
+ session.queueBind(arg::exchange="amq.topic", arg::queue="europe_queue", arg::routingKey="europe.#");
+
+
+ /*
+ * We use a separate 'control' routing key for control
+ * messages. All such messages are routed to each queue. In
+ * this demo, we use a message with the content "That's all,
+ * Folks!" to signal that no more messages will be sent, and
+ * users of the queue can stop listening for messages.
+ *
+ * Because wildcard matching can result in more than one match for
+ * a given message, it can place more messages on the queues than
+ * were originally received.
+ *
+ * We do not use wildcard matching for control messages. We
+ * want to make sure that each such message is received once
+ * and only once.
+ */
+
+
+ session.queueBind(arg::exchange="amq.topic", arg::queue="news_queue", arg::routingKey="control");
+ session.queueBind(arg::exchange="amq.topic", arg::queue="weather_queue", arg::routingKey="control");
+ session.queueBind(arg::exchange="amq.topic", arg::queue="usa_queue", arg::routingKey="control");
+ session.queueBind(arg::exchange="amq.topic", arg::queue="europe_queue", arg::routingKey="control");
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+
+}
+
+
+
diff --git a/cpp/examples/examples/pub-sub/topic_listener.cpp b/cpp/examples/examples/pub-sub/topic_listener.cpp
new file mode 100644
index 0000000000..323c93dd0b
--- /dev/null
+++ b/cpp/examples/examples/pub-sub/topic_listener.cpp
@@ -0,0 +1,169 @@
+/*
+ *
+ * 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/Queue.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().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.
+ */
+
+ 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() {
+ subscriptions.run();
+}
+
+int main() {
+ Connection connection;
+ try {
+ connection.open("127.0.0.1", 5672);
+ 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/examples/pub-sub/topic_publisher.cpp b/cpp/examples/examples/pub-sub/topic_publisher.cpp
new file mode 100644
index 0000000000..52c2827e58
--- /dev/null
+++ b/cpp/examples/examples/pub-sub/topic_publisher.cpp
@@ -0,0 +1,123 @@
+/*
+ *
+ * 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/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());
+ 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() {
+ Connection connection;
+ Message message;
+ try {
+ connection.open("127.0.0.1", 5672 );
+ 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/examples/request-response/Makefile b/cpp/examples/examples/request-response/Makefile
new file mode 100644
index 0000000000..32612843eb
--- /dev/null
+++ b/cpp/examples/examples/request-response/Makefile
@@ -0,0 +1,15 @@
+CXX=g++
+CXXFLAGS=
+
+PROGRAMS=client server
+all: $(PROGRAMS)
+
+client: client.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+server: server.cpp
+ $(CXX) $(CXXFLAGS) -lqpidclient -o $@ $^
+
+
+clean:
+ rm -f $(PROGRAMS)
diff --git a/cpp/examples/examples/request-response/client.cpp b/cpp/examples/examples/request-response/client.cpp
new file mode 100644
index 0000000000..59024a1cb6
--- /dev/null
+++ b/cpp/examples/examples/request-response/client.cpp
@@ -0,0 +1,178 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+
+/**
+ * client.cpp
+ *
+ * This program is one of two programs that illustrate the
+ * request/response pattern.
+ *
+ * client.cpp (this program)
+ *
+ * Make requests of a service, print the response.
+ *
+ * service.cpp
+ *
+ * Accept requests, reverse the letters in each message, and
+ * return it as a response.
+ *
+ */
+
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/MessageListener.h>
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+
+#include <sstream>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+
+class Listener : public MessageListener{
+private:
+ Session session;
+ std::string destination_name;
+ Dispatcher dispatcher;
+ int counter;
+public:
+ Listener(Session& session, string destination_name):
+ destination_name(destination_name),
+ dispatcher(session),
+ session(session),
+ counter(0)
+ {};
+
+ virtual void listen();
+ virtual void wait();
+ virtual void received(Message& message);
+ ~Listener() { };
+};
+
+
+void Listener::listen() {
+ std::cout << "Activating response queue listener for: " <<destination_name << std::endl;
+
+ session.messageSubscribe(arg::queue=destination_name, arg::destination=destination_name);
+
+ // Allocate credit. Sigh. This really should not be required #####
+ session.messageFlow(arg::destination=destination_name, arg::unit=0, arg::value=1);//messages ### Define a constant?
+ session.messageFlow(arg::destination=destination_name, arg::unit=1, arg::value=0xFFFFFFFF);//bytes ###### Define a constant?
+
+
+ dispatcher.listen(destination_name, this);
+}
+
+
+void Listener::wait() {
+ std::cout << "Waiting for all responses to arrive ..." << std::endl;
+ dispatcher.run();
+}
+
+
+void Listener::received(Message& message) {
+ std::cout << "Response: " << message.getData() << std::endl;
+
+ ++ counter;
+ if (counter > 3) {
+ std::cout << "Shutting down listener for " << destination_name << std::endl;
+ dispatcher.stop();
+ }
+}
+
+
+using std::stringstream;
+using std::string;
+
+int main() {
+ Connection connection;
+ Message request;
+ try {
+ connection.open("127.0.0.1", 5672 );
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ // Create a response queue so the server can send us responses
+ // to our requests. Use the client's session ID as the name
+ // of the response queue.
+
+ stringstream response_queue;
+ response_queue << "client " << session.getId();
+
+ // Use the name of the response queue as the routing key
+
+ session.queueDeclare(arg::queue=response_queue.str()); // ### Nice if I could just use strstream for this
+ session.queueBind(arg::queue=response_queue.str(), arg::routingKey=response_queue.str());
+
+ // Create a listener for the response queue and start listening.
+
+ Listener listener(session, response_queue.str());
+ listener.listen();
+
+
+ // The routing key for the request queue is simply
+ // "request_queue", and all clients use the same routing key.
+ //
+ // Each client sends the name of their own response queue so
+ // the service knows where to route messages.
+
+ request.getDeliveryProperties().setRoutingKey("request_queue");
+ request.getHeaders().setString("reply-to", response_queue.str());
+
+ // Now send some requests ...
+
+ string s[] = {
+ "Twas brillig, and the slithy toves",
+ "Did gire and gymble in the wabe.",
+ "All mimsy were the borogroves,",
+ "And the mome raths outgrabe."
+ };
+
+
+ for (int i=0; i<4; i++) {
+ request.setData(s[i]);
+ session.messageTransfer(arg::content=request);
+ std::cout << "Request: " << s[i] << std::endl;
+ }
+
+ // And wait for any outstanding responses to arrive
+
+ listener.wait();
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+
diff --git a/cpp/examples/examples/request-response/server.cpp b/cpp/examples/examples/request-response/server.cpp
new file mode 100644
index 0000000000..6c82090794
--- /dev/null
+++ b/cpp/examples/examples/request-response/server.cpp
@@ -0,0 +1,153 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+
+/**
+ * server.cpp
+ *
+ * This program is one of two programs that illustrate the
+ * request/response pattern.
+ *
+ * client.cpp
+ *
+ * Make requests of a service, print the response.
+ *
+ * server.cpp (this program)
+ *
+ * Accept requests, reverse the letters in each message, and
+ * return it as a response.
+ *
+ */
+
+
+#include <qpid/client/Connection.h>
+#include <qpid/client/Dispatcher.h>
+#include <qpid/client/Session.h>
+#include <qpid/client/Message.h>
+#include <qpid/client/MessageListener.h>
+
+
+#include <unistd.h>
+#include <cstdlib>
+#include <iostream>
+#include <algorithm>
+
+#include <sstream>
+#include <string>
+
+using namespace qpid::client;
+using namespace qpid::framing;
+using std::stringstream;
+using std::string;
+
+class Listener : public MessageListener{
+private:
+ std::string destination_name;
+ Dispatcher dispatcher;
+ Session session;
+public:
+ Listener(Session& session, string destination_name):
+ destination_name(destination_name),
+ dispatcher(session),
+ session(session)
+ {};
+
+ virtual void listen();
+ virtual void received(Message& message);
+ virtual void wait();
+ ~Listener() { };
+};
+
+
+void Listener::listen() {
+ std::cout << "Activating request queue listener for: " <<destination_name << std::endl;
+
+ session.messageSubscribe(arg::queue=destination_name, arg::destination=destination_name);
+
+ // ##### Should not be needed. Sigh.
+ session.messageFlow(arg::destination=destination_name, arg::unit=0, arg::value=1);//messages ### Define a constant?
+ session.messageFlow(arg::destination=destination_name, arg::unit=1, arg::value=0xFFFFFFFF);//bytes ###### Define a constant?
+
+ dispatcher.listen(destination_name, this);
+}
+
+
+void Listener::wait() {
+ std::cout << "Waiting for requests" << std::endl;
+ dispatcher.run();
+}
+
+
+void Listener::received(Message& request) {
+
+ Message response;
+ string routingKey = request.getHeaders().getString("reply-to");
+
+ std::cout << "Request:: " << request.getData() << " (" <<routingKey << ")" << std::endl;
+
+ // Transform message content to upper case
+ std::string s = request.getData();
+ std::transform (s.begin(), s.end(), s.begin(), toupper);
+ response.setData(s);
+
+ // Send it back to the user
+ response.getDeliveryProperties().setRoutingKey(routingKey);
+ session.messageTransfer(arg::content=response);
+}
+
+
+int main() {
+ Connection connection;
+ Message message;
+ try {
+ connection.open("127.0.0.1", 5672 );
+ Session session = connection.newSession();
+
+ //--------- Main body of program --------------------------------------------
+
+ // Create a request queue for clients to use when making
+ // requests.
+
+ string request_queue = "request_queue";
+
+ // Use the name of the request queue as the routing key
+
+ session.queueDeclare(arg::queue=request_queue);
+ session.queueBind(arg::queue=request_queue, arg::routingKey=request_queue);
+
+ // Create a listener for the request queue and start listening.
+
+ Listener listener(session, request_queue);
+ listener.listen();
+ listener.wait();
+
+
+ //-----------------------------------------------------------------------------
+
+ connection.close();
+ return 0;
+ } catch(const std::exception& error) {
+ std::cout << error.what() << std::endl;
+ }
+ return 1;
+}
+
+