Debian Codegraph @

cpp/examples/client.cpp

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "options.hpp"
23 #include <proton/connection.hpp>
24 #include <proton/container.hpp>
25 #include <proton/delivery.hpp>
26 #include <proton/message.hpp>
27 #include <proton/messaging_handler.hpp>
28 #include <proton/receiver_options.hpp>
29 #include <proton/source_options.hpp>
30 #include <proton/tracker.hpp>
31
32 #include <iostream>
33 #include <vector>
34
35
36 using proton::receiver_options;
37 using proton::source_options;
38
39 class client : public proton::messaging_handler {
40 private:
41 std::string url;
42 std::vector<std::string> requests;
43 proton::sender sender;
44 proton::receiver receiver;
45
46 public:
47 client(const std::string &u, const std::vector<std::string>& r) : url(u), requests(r) {}
48
49 void on_container_start(proton::container &c) override {
50 sender = c.open_sender(url);
51 // Create a receiver requesting a dynamically created queue
52 // for the message source.
53 receiver_options opts = receiver_options().source(source_options().dynamic(true));
54 receiver = sender.connection().open_receiver("", opts);
55 }
56
57 void send_request() {
58 proton::message req;
59 req.body(requests.front());
60 req.reply_to(receiver.source().address());
61 sender.send(req);
62 }
63
64 void on_receiver_open(proton::receiver &) override {
65 send_request();
66 }
67
68 void on_message(proton::delivery &d, proton::message &response) override {
69 if (requests.empty()) return; // Spurious extra message!
70
71 std::cout << requests.front() << " => " << response.body() << std::endl;
72 requests.erase(requests.begin());
73
74 if (!requests.empty()) {
75 send_request();
76 } else {
77 d.connection().close();
78 }
79 }
80 };
81
82 int main(int argc, char **argv) {
83 std::string url("127.0.0.1:5672/examples");
84 example::options opts(argc, argv);
85
86 opts.add_value(url, 'a', "address", "connect and send to URL", "URL");
87
88 try {
89 opts.parse();
90
91 std::vector<std::string> requests;
92 requests.push_back("Twas brillig, and the slithy toves");
93 requests.push_back("Did gire and gymble in the wabe.");
94 requests.push_back("All mimsy were the borogroves,");
95 requests.push_back("And the mome raths outgrabe.");
96
97 client c(url, requests);
98 proton::container(c).run();
99
100 return 0;
101 } catch (const example::bad_option& e) {
102 std::cout << opts << std::endl << e.what() << std::endl;
103 } catch (const std::exception& e) {
104 std::cerr << e.what() << std::endl;
105 }
106
107 return 1;
108 }