Debian Codegraph @

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