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 --- .../old_api/request-response/CMakeLists.txt | 21 ++ cpp/examples/old_api/request-response/Makefile.am | 43 +++ cpp/examples/old_api/request-response/client.cpp | 163 +++++++++ .../request-response_client.vcproj | 394 +++++++++++++++++++++ .../request-response_server.vcproj | 394 +++++++++++++++++++++ cpp/examples/old_api/request-response/server.cpp | 161 +++++++++ cpp/examples/old_api/request-response/verify | 24 ++ cpp/examples/old_api/request-response/verify.in | 19 + 8 files changed, 1219 insertions(+) create mode 100644 cpp/examples/old_api/request-response/CMakeLists.txt create mode 100644 cpp/examples/old_api/request-response/Makefile.am create mode 100644 cpp/examples/old_api/request-response/client.cpp create mode 100644 cpp/examples/old_api/request-response/request-response_client.vcproj create mode 100644 cpp/examples/old_api/request-response/request-response_server.vcproj create mode 100644 cpp/examples/old_api/request-response/server.cpp create mode 100644 cpp/examples/old_api/request-response/verify create mode 100644 cpp/examples/old_api/request-response/verify.in (limited to 'cpp/examples/old_api/request-response') diff --git a/cpp/examples/old_api/request-response/CMakeLists.txt b/cpp/examples/old_api/request-response/CMakeLists.txt new file mode 100644 index 0000000000..873a0cfa86 --- /dev/null +++ b/cpp/examples/old_api/request-response/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(request-response client) +add_example(request-response server) diff --git a/cpp/examples/old_api/request-response/Makefile.am b/cpp/examples/old_api/request-response/Makefile.am new file mode 100644 index 0000000000..f48762da51 --- /dev/null +++ b/cpp/examples/old_api/request-response/Makefile.am @@ -0,0 +1,43 @@ +# +# 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/request-response + +MAKELDFLAGS=$(CLIENTFLAGS) +include $(top_srcdir)/examples/makedist.mk + +noinst_PROGRAMS=client server + +client_SOURCES=client.cpp +client_LDADD=$(CLIENT_LIB) + +server_SOURCES=server.cpp +server_LDADD=$(CLIENT_LIB) + +examples_DATA= \ + server.cpp \ + client.cpp \ + $(MAKEDIST) + +EXTRA_DIST= \ + $(examples_DATA) \ + CMakeLists.txt \ + verify \ + verify.in \ + request-response_client.vcproj \ + request-response_server.vcproj diff --git a/cpp/examples/old_api/request-response/client.cpp b/cpp/examples/old_api/request-response/client.cpp new file mode 100644 index 0000000000..679d1c5fc2 --- /dev/null +++ b/cpp/examples/old_api/request-response/client.cpp @@ -0,0 +1,163 @@ +/* + * + * 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) + * + * A client application that sends messages to the "amq.direct" + * exchange, using the routing key "request" to route messages to + * the server. + * + * Each instance of the client creates its own private response + * queue, binding it to the "amq.direct" exchange using it's + * session identifier as the routing key, and places its session + * identifier in the "reply-to" property of each message it sends. + * + * + * server.cpp + * + * A service that accepts messages from a request queue, converts + * their content to upper case, and sends the result to the + * original sender. + * + * This program creates a request queue, binds it to "amq.direct" + * using the routing key "request", then receives messages from + * the request queue. Each incoming message is converted to upper + * case, then sent to the "amq.direct" exchange using the + * request's reply-to property as the routing key for the + * response. + * + * + */ + +#include +#include +#include +#include +#include + +#include +#include + +#include + +using namespace qpid::client; +using namespace qpid::framing; + +using std::stringstream; +using std::string; + +class Listener : public MessageListener{ + private: + SubscriptionManager& subscriptions; + int counter; + public: + Listener(SubscriptionManager& subscriptions); + virtual void received(Message& message); +}; + +Listener::Listener(SubscriptionManager& subs) : subscriptions(subs), counter(0) +{} + +void Listener::received(Message& message) { + std::cout << "Response: " << message.getData() << std::endl; + + ++ counter; + if (counter > 3) { + 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 -------------------------------------------- + + // 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().getName(); + + // Use the name of the response queue as the routing key + + session.queueDeclare(arg::queue=response_queue.str()); + session.exchangeBind(arg::exchange="amq.direct", arg::queue=response_queue.str(), arg::bindingKey=response_queue.str()); + + // Each client sends the name of their own response queue so + // the service knows where to route messages. + + Message request; + request.getDeliveryProperties().setRoutingKey("request"); + request.getMessageProperties().setReplyTo(ReplyTo("amq.direct", response_queue.str())); + + // Create a listener for the response queue and listen for response messages. + std::cout << "Activating response queue listener for: " << response_queue.str() << std::endl; + SubscriptionManager subscriptions(session); + Listener listener(subscriptions); + subscriptions.subscribe(listener, 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, arg::destination="amq.direct"); + std::cout << "Request: " << s[i] << std::endl; + } + + std::cout << "Waiting for all responses to arrive ..." << 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/request-response/request-response_client.vcproj b/cpp/examples/old_api/request-response/request-response_client.vcproj new file mode 100644 index 0000000000..5f9eadde36 --- /dev/null +++ b/cpp/examples/old_api/request-response/request-response_client.vcproj @@ -0,0 +1,394 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cpp/examples/old_api/request-response/request-response_server.vcproj b/cpp/examples/old_api/request-response/request-response_server.vcproj new file mode 100644 index 0000000000..54352b9f46 --- /dev/null +++ b/cpp/examples/old_api/request-response/request-response_server.vcproj @@ -0,0 +1,394 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cpp/examples/old_api/request-response/server.cpp b/cpp/examples/old_api/request-response/server.cpp new file mode 100644 index 0000000000..65a4717b35 --- /dev/null +++ b/cpp/examples/old_api/request-response/server.cpp @@ -0,0 +1,161 @@ +/* + * + * 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 + * + * A client application that sends messages to the "amq.direct" + * exchange, using the routing key "request" to route messages to + * the server. + * + * Each instance of the client creates its own private response + * queue, binding it to the "amq.direct" exchange using it's + * session identifier as the routing key, and places its session + * identifier in the "reply-to" property of each message it sends. + * + * + * server.cpp (this program) + * + * A service that accepts messages from a request queue, converts + * their content to upper case, and sends the result to the + * original sender. + * + * This program creates a request queue, binds it to "amq.direct" + * using the routing key "request", then receives messages from + * the request queue. Each incoming message is converted to upper + * case, then sent to the "amq.direct" exchange using the + * request's reply-to property as the routing key for the + * response. + * + * + */ + + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include + +using namespace qpid::client; +using namespace qpid::framing; +using std::stringstream; +using std::string; + +class Listener : public MessageListener{ + private: + SubscriptionManager& subscriptions; + AsyncSession asyncSession; + public: + Listener(SubscriptionManager& subscriptions, Session& session); + virtual void received(Message& message); +}; + +Listener::Listener(SubscriptionManager& subs, Session& session) + : subscriptions(subs), asyncSession(session) +{} + +void Listener::received(Message& request) { + Message response; + + // Get routing key for response from the request's replyTo property + string routingKey; + + if (request.getMessageProperties().hasReplyTo()) { + routingKey = request.getMessageProperties().getReplyTo().getRoutingKey(); + } else { + std::cout << "Error: " << "No routing key for request (" << request.getData() << ")" << std::endl; + return; + } + + std::cout << "Request: " << request.getData() << " (" <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 request queue for clients to use when making + // requests. + string request_queue = "request"; + + // Use the name of the request queue as the routing key + session.queueDeclare(arg::queue=request_queue); + session.exchangeBind(arg::exchange="amq.direct", arg::queue=request_queue, arg::bindingKey=request_queue); + + // Create a listener and subscribe it to the request_queue + std::cout << "Activating request queue listener for: " << request_queue << std::endl; + SubscriptionManager subscriptions(session); + Listener listener(subscriptions, session); + subscriptions.subscribe(listener, request_queue); + // Deliver messages until the subscription is cancelled + // by Listener::received() + + std::cout << "Waiting for requests" << 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/request-response/verify b/cpp/examples/old_api/request-response/verify new file mode 100644 index 0000000000..dee82413e7 --- /dev/null +++ b/cpp/examples/old_api/request-response/verify @@ -0,0 +1,24 @@ +# +# 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 "Waiting" ./server +clients ./client +kill %% # Must kill the server. +outputs "./client.out | remove_uuid" "server.out | remove_uuid" diff --git a/cpp/examples/old_api/request-response/verify.in b/cpp/examples/old_api/request-response/verify.in new file mode 100644 index 0000000000..7925dc5671 --- /dev/null +++ b/cpp/examples/old_api/request-response/verify.in @@ -0,0 +1,19 @@ +==== client.out | remove_uuid +Activating response queue listener for: client +Request: Twas brillig, and the slithy toves +Request: Did gire and gymble in the wabe. +Request: All mimsy were the borogroves, +Request: And the mome raths outgrabe. +Waiting for all responses to arrive ... +Response: TWAS BRILLIG, AND THE SLITHY TOVES +Response: DID GIRE AND GYMBLE IN THE WABE. +Response: ALL MIMSY WERE THE BOROGROVES, +Response: AND THE MOME RATHS OUTGRABE. +Shutting down listener for client +==== server.out | remove_uuid +Activating request queue listener for: request +Waiting for requests +Request: Twas brillig, and the slithy toves (client) +Request: Did gire and gymble in the wabe. (client) +Request: All mimsy were the borogroves, (client) +Request: And the mome raths outgrabe. (client) -- cgit v1.2.1