From f83677056891e436bf5ba99e79240df2a44528cd Mon Sep 17 00:00:00 2001 From: "Stephen D. Huston" Date: Fri, 21 Oct 2011 14:42:12 +0000 Subject: Merged out from trunk git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/QPID-2519@1187375 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/examples/old_api/fanout/CMakeLists.txt | 21 ++ cpp/examples/old_api/fanout/Makefile.am | 42 +++ .../old_api/fanout/fanout_fanout_producer.vcproj | 394 +++++++++++++++++++++ cpp/examples/old_api/fanout/fanout_listener.vcproj | 394 +++++++++++++++++++++ cpp/examples/old_api/fanout/fanout_producer.cpp | 105 ++++++ cpp/examples/old_api/fanout/listener.cpp | 125 +++++++ cpp/examples/old_api/fanout/verify | 25 ++ cpp/examples/old_api/fanout/verify.in | 43 +++ 8 files changed, 1149 insertions(+) create mode 100644 cpp/examples/old_api/fanout/CMakeLists.txt create mode 100644 cpp/examples/old_api/fanout/Makefile.am create mode 100644 cpp/examples/old_api/fanout/fanout_fanout_producer.vcproj create mode 100644 cpp/examples/old_api/fanout/fanout_listener.vcproj create mode 100644 cpp/examples/old_api/fanout/fanout_producer.cpp create mode 100644 cpp/examples/old_api/fanout/listener.cpp create mode 100644 cpp/examples/old_api/fanout/verify create mode 100644 cpp/examples/old_api/fanout/verify.in (limited to 'cpp/examples/old_api/fanout') diff --git a/cpp/examples/old_api/fanout/CMakeLists.txt b/cpp/examples/old_api/fanout/CMakeLists.txt new file mode 100644 index 0000000000..3f89d67650 --- /dev/null +++ b/cpp/examples/old_api/fanout/CMakeLists.txt @@ -0,0 +1,21 @@ +# +# 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. +# + +add_example(fanout fanout_producer) +add_example(fanout listener) diff --git a/cpp/examples/old_api/fanout/Makefile.am b/cpp/examples/old_api/fanout/Makefile.am new file mode 100644 index 0000000000..3ab43b0279 --- /dev/null +++ b/cpp/examples/old_api/fanout/Makefile.am @@ -0,0 +1,42 @@ +# +# 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. +# +examplesdir=$(pkgdatadir)/examples/old_api/fanout + +MAKELDFLAGS=$(CLIENTFLAGS) +include $(top_srcdir)/examples/makedist.mk + +noinst_PROGRAMS=fanout_producer listener +fanout_producer_SOURCES=fanout_producer.cpp +fanout_producer_LDADD=$(CLIENT_LIB) + +listener_SOURCES=listener.cpp +listener_LDADD=$(CLIENT_LIB) + +examples_DATA= \ + fanout_producer.cpp \ + listener.cpp \ + $(MAKEDIST) + +EXTRA_DIST= \ + $(examples_DATA) \ + CMakeLists.txt \ + verify \ + verify.in \ + fanout_fanout_producer.vcproj \ + fanout_listener.vcproj diff --git a/cpp/examples/old_api/fanout/fanout_fanout_producer.vcproj b/cpp/examples/old_api/fanout/fanout_fanout_producer.vcproj new file mode 100644 index 0000000000..daff5f3cf0 --- /dev/null +++ b/cpp/examples/old_api/fanout/fanout_fanout_producer.vcproj @@ -0,0 +1,394 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cpp/examples/old_api/fanout/fanout_listener.vcproj b/cpp/examples/old_api/fanout/fanout_listener.vcproj new file mode 100644 index 0000000000..f0e91b7dc6 --- /dev/null +++ b/cpp/examples/old_api/fanout/fanout_listener.vcproj @@ -0,0 +1,394 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cpp/examples/old_api/fanout/fanout_producer.cpp b/cpp/examples/old_api/fanout/fanout_producer.cpp new file mode 100644 index 0000000000..decd4d314d --- /dev/null +++ b/cpp/examples/old_api/fanout/fanout_producer.cpp @@ -0,0 +1,105 @@ +/* + * + * 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. + * + */ + + +/** + * fanout_producer.cpp: + * + * This program is one of two programs designed to be used + * together. + * + * fanout_producer.cpp (this program): + * + * Publishes messages to the "amq.fanout" exchange. + * + * listener.cpp + * + * Creates a private queue, binds it to the "amq.fanout" + * exchange, and reads messages from its queue as they + * arrive. Messages sent before the listener binds the queue are + * not received. + * + * Multiple listeners can run at the same time. + * + */ + + +#include +#include +#include +#include + + +#include +#include + +#include + +using namespace qpid::client; +using namespace qpid::framing; + +using std::stringstream; +using std::string; + +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 -------------------------------------------- + + // 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()); + // Asynchronous transfer sends messages as quickly as + // possible without waiting for confirmation. + async(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/old_api/fanout/listener.cpp b/cpp/examples/old_api/fanout/listener.cpp new file mode 100644 index 0000000000..cd3071c29a --- /dev/null +++ b/cpp/examples/old_api/fanout/listener.cpp @@ -0,0 +1,125 @@ +/* + * + * 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. + * + */ + + +/** + * listener.cpp + * + * This program is one of two programs designed to be used + * together. + * + * fanout_producer.cpp + * + * Publishes messages to the "amq.fanout" exchange. + * + * listener.cpp (this program) + * + * Creates a private queue, binds it to the "amq.fanout" + * exchange, and reads messages from its queue as they + * arrive. Messages sent before the listener binds the queue are + * not received. + * + * Multiple listeners can run at the same time. + * + */ + + +#include +#include +#include +#include +#include + +#include +#include + +using namespace qpid::client; +using namespace qpid::framing; + + +class Listener : public MessageListener{ + private: + SubscriptionManager& subscriptions; + public: + Listener(SubscriptionManager& subscriptions); + virtual void received(Message& message); +}; + +Listener::Listener(SubscriptionManager& subs) : subscriptions(subs) +{} + +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 " << message.getDestination() + << std::endl; + subscriptions.cancel(message.getDestination()); + } +} + +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 -------------------------------------------- + + // Each client creates its own private queue, using the + // session id to guarantee a unique name. It then routes + // all messages from the fanout exchange to its own queue + // by binding to the queue. + // + // The binding specifies a binding key, but for a fanout + // exchange, the binding key is optional and is not used + // for routing decisions. It can be useful for tracking + // messages and routing in logs. + + std::string myQueue=session.getId().getName(); + session.queueDeclare(arg::queue=myQueue, arg::exclusive=true, + arg::autoDelete=true); + + session.exchangeBind(arg::exchange="amq.fanout", arg::queue=myQueue, arg::bindingKey="my-key"); + + // Create a listener and subscribe it to my queue. + SubscriptionManager subscriptions(session); + Listener listener(subscriptions); + subscriptions.subscribe(listener, myQueue); + + // Receive messages until the subscription is cancelled + // by Listener::received() + std::cout << "Listening" << std::endl; + subscriptions.run(); + + //--------------------------------------------------------------------------- + + connection.close(); + return 0; + } catch(const std::exception& error) { + std::cout << error.what() << std::endl; + } + return 1; +} + + diff --git a/cpp/examples/old_api/fanout/verify b/cpp/examples/old_api/fanout/verify new file mode 100644 index 0000000000..2eaadff56b --- /dev/null +++ b/cpp/examples/old_api/fanout/verify @@ -0,0 +1,25 @@ +# +# 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. +# + +# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify +background "Listening" ./listener +background "Listening" ./listener +background "Listening" ./listener +clients ./fanout_producer +outputs ./fanout_producer.out "./listener.out | remove_uuid" "./listenerX.out | remove_uuid" "./listenerXX.out | remove_uuid" diff --git a/cpp/examples/old_api/fanout/verify.in b/cpp/examples/old_api/fanout/verify.in new file mode 100644 index 0000000000..8f8612ce67 --- /dev/null +++ b/cpp/examples/old_api/fanout/verify.in @@ -0,0 +1,43 @@ +==== fanout_producer.out +==== listener.out | remove_uuid +Listening +Message: Message 0 +Message: Message 1 +Message: Message 2 +Message: Message 3 +Message: Message 4 +Message: Message 5 +Message: Message 6 +Message: Message 7 +Message: Message 8 +Message: Message 9 +Message: That's all, folks! +Shutting down listener for +==== listenerX.out | remove_uuid +Listening +Message: Message 0 +Message: Message 1 +Message: Message 2 +Message: Message 3 +Message: Message 4 +Message: Message 5 +Message: Message 6 +Message: Message 7 +Message: Message 8 +Message: Message 9 +Message: That's all, folks! +Shutting down listener for +==== listenerXX.out | remove_uuid +Listening +Message: Message 0 +Message: Message 1 +Message: Message 2 +Message: Message 3 +Message: Message 4 +Message: Message 5 +Message: Message 6 +Message: Message 7 +Message: Message 8 +Message: Message 9 +Message: That's all, folks! +Shutting down listener for -- cgit v1.2.1