Debian Codegraph @

cpp/examples/reconnect_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
24 #include <proton/connection.hpp>
25 #include <proton/connection_options.hpp>
26 #include <proton/container.hpp>
27 #include <proton/delivery.hpp>
28 #include <proton/link.hpp>
29 #include <proton/message.hpp>
30 #include <proton/message_id.hpp>
31 #include <proton/messaging_handler.hpp>
32 #include <proton/reconnect_options.hpp>
33 #include <proton/value.hpp>
34 #include <proton/types.hpp>
35
36 #include <iostream>
37 #include <map>
38 #include <string>
39
40
41 class reconnect_client : public proton::messaging_handler {
42 std::string url;
43 std::string address;
44 std::vector<std::string> failovers;
45 proton::sender sender;
46 int sent;
47 int expected;
48 int received;
49
50 public:
51 reconnect_client(const std::string &u, const std::string& a, int c, const std::vector<std::string>& f) :
52 url(u), address(a), failovers(f), sent(0), expected(c), received(0) {}
53
54 private:
55 void on_container_start(proton::container &c) override {
56 proton::connection_options co;
57
58 co.failover_urls(failovers);
59 c.connect(url, co);
60 }
61
62 void on_connection_open(proton::connection & c) override {
63 c.open_receiver(address);
64 c.open_sender(address);
65 // reconnect we probably lost the last message sent
66 sent = received;
67 std::cout << "simple_recv listening on " << url << std::endl;
68 }
69
70 void on_message(proton::delivery &d, proton::message &msg) override {
71 if (proton::coerce<int>(msg.id()) < received) {
72 return; // Ignore duplicate
73 }
74
75 if (expected == 0 || received < expected) {
76 std::cout << msg.body() << std::endl;
77 received++;
78
79 if (received == expected) {
80 d.receiver().close();
81 sender.close();
82 d.connection().close();
83 } else {
84 // See if we can send any messages now
85 send(sender);
86 }
87 }
88 }
89
90 void send(proton::sender& s) {
91 // Only send with credit and only allow one outstanding message
92 while (s.credit() && sent < received+1) {
93 std::map<std::string, int> m;
94 m["sequence"] = sent + 1;
95
96 proton::message msg;
97 msg.id(sent + 1);
98 msg.body(m);
99
100 std::cout << "Sending: " << sent+1 << std::endl;
101 s.send(msg);
102 sent++;
103 }
104 }
105
106 void on_sender_open(proton::sender & s) override {
107 sender = s;
108 }
109
110 void on_sendable(proton::sender &s) override {
111 send(s);
112 }
113 };
114
115 int main(int argc, const char** argv) {
116 try {
117 if (argc < 4) {
118 std ::cerr <<
119 "Usage: " << argv[0] << " CONNECTION-URL AMQP-ADDRESS MESSAGE-COUNT FAILOVER-URL...\n"
120 "CONNECTION-URL: connection address, e.g.'amqp://127.0.0.1'\n"
121 "AMQP-ADDRESS: AMQP node address, e.g. 'examples'\n"
122 "MESSAGE-COUNT: number of messages to receive\n"
123 "FAILOVER_URL...: zero or more failover urls\n";
124 return 1;
125 }
126 const char *url = argv[1];
127 const char *address = argv[2];
128 int message_count = atoi(argv[3]);
129 std::vector<std::string> failovers(&argv[4], &argv[argc]);
130
131 reconnect_client client(url, address, message_count, failovers);
132 proton::container(client).run();
133
134 return 0;
135 } catch (const std::exception& e) {
136 std::cerr << e.what() << std::endl;
137 }
138
139 return 1;
140 }