Debian Codegraph @

cpp/examples/simple_send.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
24 #include <proton/connection.hpp>
25 #include <proton/connection_options.hpp>
26 #include <proton/container.hpp>
27 #include <proton/message.hpp>
28 #include <proton/message_id.hpp>
29 #include <proton/messaging_handler.hpp>
30 #include <proton/reconnect_options.hpp>
31 #include <proton/tracker.hpp>
32 #include <proton/types.hpp>
33
34 #include <iostream>
35 #include <map>
36
37
38 class simple_send : public proton::messaging_handler {
39 private:
40 std::string url;
41 std::string user;
42 std::string password;
43 bool reconnect;
44 proton::sender sender;
45 int sent;
46 int confirmed;
47 int total;
48
49 public:
50 simple_send(const std::string &s, const std::string &u, const std::string &p, bool r, int c) :
51 url(s), user(u), password(p), reconnect(r), sent(0), confirmed(0), total(c) {}
52
53 void on_container_start(proton::container &c) override {
54 proton::connection_options co;
55 if (!user.empty()) co.user(user);
56 if (!password.empty()) co.password(password);
57 if (reconnect) co.reconnect(proton::reconnect_options());
58 sender = c.open_sender(url, co);
59 }
60
61 void on_connection_open(proton::connection& c) override {
62 if (c.reconnected()) {
63 sent = confirmed; // Re-send unconfirmed messages after a reconnect
64 }
65 }
66
67 void on_sendable(proton::sender &s) override {
68 while (s.credit() && sent < total) {
69 proton::message msg;
70 std::map<std::string, int> m;
71 m["sequence"] = sent + 1;
72
73 msg.id(sent + 1);
74 msg.body(m);
75
76 s.send(msg);
77 sent++;
78 }
79 }
80
81 void on_tracker_accept(proton::tracker &t) override {
82 confirmed++;
83
84 if (confirmed == total) {
85 std::cout << "all messages confirmed" << std::endl;
86 t.connection().close();
87 }
88 }
89
90 void on_transport_close(proton::transport &) override {
91 sent = confirmed;
92 }
93 };
94
95 int main(int argc, char **argv) {
96 std::string address("127.0.0.1:5672/examples");
97 std::string user;
98 std::string password;
99 bool reconnect = false;
100 int message_count = 100;
101 example::options opts(argc, argv);
102
103 opts.add_value(address, 'a', "address", "connect and send to URL", "URL");
104 opts.add_value(user, 'u', "user", "authenticate as USER", "USER");
105 opts.add_value(password, 'p', "password", "authenticate with PASSWORD", "PASSWORD");
106 opts.add_flag(reconnect, 'r', "reconnect", "reconnect on connection failure");
107 opts.add_value(message_count, 'm', "messages", "send COUNT messages", "COUNT");
108
109 try {
110 opts.parse();
111
112 simple_send send(address, user, password, reconnect, message_count);
113 proton::container(send).run();
114
115 return 0;
116 } catch (const example::bad_option& e) {
117 std::cout << opts << std::endl << e.what() << std::endl;
118 } catch (const std::exception& e) {
119 std::cerr << e.what() << std::endl;
120 }
121
122 return 1;
123 }