Debian Codegraph @

cpp/examples/flow_control.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/listen_handler.hpp>
29 #include <proton/listener.hpp>
30 #include <proton/message.hpp>
31 #include <proton/messaging_handler.hpp>
32 #include <proton/receiver_options.hpp>
33 #include <proton/sender.hpp>
34 #include <proton/tracker.hpp>
35
36 #include <iostream>
37 #include <sstream>
38
39
40 namespace {
41
42 bool verbose = true;
43
44 void verify(bool success, const std::string &msg) {
45 if (!success)
46 throw std::runtime_error("example failure:" + msg);
47 else {
48 std::cout << "success: " << msg << std::endl;
49 if (verbose) std::cout << std::endl;
50 }
51 }
52
53 }
54
55 // flow_sender manages the incoming connection and acts as the message sender.
56 class flow_sender : public proton::messaging_handler {
57 private:
58 int available; // Number of messages the sender may send assuming sufficient credit.
59 int sequence;
60
61 public:
62 flow_sender() : available(0), sequence(0) {}
63
64 void send_available_messages(proton::sender &s) {
65 for (int i = sequence; available && s.credit() > 0; i++) {
66 std::ostringstream mbody;
67 mbody << "flow_sender message " << sequence++;
68 proton::message m(mbody.str());
69 s.send(m);
70 available--;
71 }
72 }
73
74 void on_sendable(proton::sender &s) override {
75 if (verbose)
76 std::cout << "flow_sender in \"on_sendable\" with credit " << s.credit()
77 << " and " << available << " available messages" << std::endl;
78 send_available_messages(s);
79 }
80
81 void on_sender_drain_start(proton::sender &s) override {
82 if (verbose)
83 std::cout << "flow_sender in \"on_drain_start\" with credit " << s.credit()
84 << " and " << available << " available messages" << std::endl;
85 send_available_messages(s);
86 if (s.credit()) {
87 s.return_credit(); // return the rest
88 }
89 }
90
91 void set_available(int n) { available = n; }
92 };
93
94 class flow_receiver : public proton::messaging_handler {
95 public:
96 int stage;
97 int received;
98 flow_sender &sender;
99
100 flow_receiver(flow_sender &s) : stage(0), received(0), sender(s) {}
101
102 void example_setup(int n) {
103 received = 0;
104 sender.set_available(n);
105 }
106
107 void run_stage(proton::receiver &r, const std::string &caller) {
108 // Serialize the progression of the flow control examples.
109 switch (stage) {
110 case 0:
111 if (verbose) std::cout << "Example 1. Simple use of credit." << std::endl;
112 // TODO: add timeout callbacks, show no messages until credit.
113 example_setup(2);
114 r.add_credit(2);
115 break;
116 case 1:
117 if (r.credit() > 0) return;
118 verify(received == 2, "Example 1: simple credit");
119
120 if (verbose) std::cout << "Example 2. Use basic drain, sender has 3 \"immediate\" messages." << std::endl;
121 example_setup(3);
122 r.add_credit(5); // ask for up to 5
123 r.drain(); // but only use what's available
124 break;
125 case 2:
126 if (caller == "on_message") return;
127 if (caller == "on_receiver_drain_finish") {
128 // Note that unused credit of 2 at sender is returned and is now 0.
129 verify(received == 3 && r.credit() == 0, "Example 2: basic drain");
130
131 if (verbose) std::cout << "Example 3. Drain use with no credit." << std::endl;
132 example_setup(0);
133 r.drain();
134 break;
135 }
136 verify(false, "example 2 run_stage");
137 return;
138
139 case 3:
140 verify(caller == "on_receiver_drain_finish" && received == 0, "Example 3: drain without credit");
141
142 if (verbose) std::cout << "Example 4. Show using high(10)/low(3) watermark for 25 messages." << std::endl;
143 example_setup(25);
144 r.add_credit(10);
145 break;
146
147 case 4:
148 if (received < 25) {
149 // Top up credit as needed.
150 uint32_t credit = r.credit();
151 if (credit <= 3) {
152 uint32_t new_credit = 10;
153 uint32_t remaining = 25 - received;
154 if (new_credit > remaining)
155 new_credit = remaining;
156 if (new_credit > credit) {
157 r.add_credit(new_credit - credit);
158 if (verbose)
159 std::cout << "flow_receiver adding credit for " << new_credit - credit
160 << " messages" << std::endl;
161 }
162 }
163 return;
164 }
165
166 verify(received == 25 && r.credit() == 0, "Example 4: high/low watermark");
167 r.connection().close();
168 break;
169
170 default:
171 throw std::runtime_error("run_stage sequencing error");
172 }
173 stage++;
174 }
175
176 void on_receiver_open(proton::receiver &r) override {
177 run_stage(r, "on_receiver_open");
178 }
179
180 void on_message(proton::delivery &d, proton::message &m) override {
181 if (verbose)
182 std::cout << "flow_receiver in \"on_message\" with " << m.body() << std::endl;
183 proton::receiver r(d.receiver());
184 received++;
185 run_stage(r, "on_message");
186 }
187
188 void on_receiver_drain_finish(proton::receiver &r) override {
189 if (verbose)
190 std::cout << "flow_receiver in \"on_receiver_drain_finish\"" << std::endl;
191 run_stage(r, "on_receiver_drain_finish");
192 }
193 };
194
195 class flow_listener : public proton::listen_handler {
196 proton::connection_options opts;
197 public:
198 flow_listener(flow_sender& sh) {
199 opts.handler(sh);
200 }
201
202 void on_open(proton::listener& l) override {
203 std::ostringstream url;
204 url << "//:" << l.port() << "/example"; // Connect to the actual listening port
205 l.container().connect(url.str());
206 }
207
208 proton::connection_options on_accept(proton::listener&) override { return opts; }
209 };
210
211 class flow_control : public proton::messaging_handler {
212 private:
213 proton::listener listener;
214 flow_sender send_handler;
215 flow_receiver receive_handler;
216 flow_listener listen_handler;
217
218 public:
219 flow_control() : receive_handler(send_handler), listen_handler(send_handler) {}
220
221 void on_container_start(proton::container &c) override {
222 // Listen on a dynamic port on the local host.
223 listener = c.listen("//:0", listen_handler);
224 }
225
226 void on_connection_open(proton::connection &c) override {
227 if (c.active()) {
228 // outbound connection
229 c.open_receiver("flow_example", proton::receiver_options().handler(receive_handler).credit_window(0));
230 }
231 }
232
233 void on_connection_close(proton::connection &) override {
234 listener.stop();
235 }
236 };
237
238 int main(int argc, char **argv) {
239 // Pick an "unusual" port since we are going to be talking to
240 // ourselves, not a broker.
241 bool quiet = false;
242
243 example::options opts(argc, argv);
244 opts.add_flag(quiet, 'q', "quiet", "suppress additional commentary of credit allocation and consumption");
245
246 try {
247 opts.parse();
248 if (quiet)
249 verbose = false;
250
251 flow_control fc;
252 proton::container(fc).run();
253
254 return 0;
255 } catch (const std::exception& e) {
256 std::cerr << e.what() << std::endl;
257 }
258
259 return 1;
260 }