Debian Codegraph @

cpp/examples/scheduled_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/container.hpp>
25 #include <proton/connection.hpp>
26 #include <proton/message.hpp>
27 #include <proton/messaging_handler.hpp>
28 #include <proton/sender.hpp>
29 #include <proton/tracker.hpp>
30 #include <proton/work_queue.hpp>
31
32 #include <iostream>
33
34 // Send messages at a constant rate one per interval. cancel after a timeout.
35 class scheduled_sender : public proton::messaging_handler {
36 private:
37 std::string url;
38 proton::sender sender;
39 proton::duration interval, timeout;
40 proton::work_queue* work_queue;
41 bool ready, canceled;
42
43 public:
44
45 scheduled_sender(const std::string &s, double d, double t) :
46 url(s),
47 interval(int(d*proton::duration::SECOND.milliseconds())), // Send interval.
48 timeout(int(t*proton::duration::SECOND.milliseconds())), // Cancel after timeout.
49 work_queue(0),
50 ready(true), // Ready to send.
51 canceled(false) // Canceled.
52 {}
53
54 // The awkward looking double lambda is necessary because the scheduled lambdas run in the container context
55 // and must arrange lambdas for send and close to happen in the connection context.
56 void on_container_start(proton::container &c) override {
57 c.open_sender(url);
58 }
59
60 void on_sender_open(proton::sender &s) override {
61 sender = s;
62 work_queue = &s.work_queue();
63 // Call this->cancel after timeout.
64 s.container().schedule(timeout, [this]() { this->work_queue->add( [this]() { this->cancel(); }); });
65 // Start regular ticks every interval.
66 s.container().schedule(interval, [this]() { this->work_queue->add( [this]() { this->tick(); }); });
67 }
68
69 void cancel() {
70 canceled = true;
71 sender.connection().close();
72 }
73
74 void tick() {
75 // Schedule the next tick unless we have been cancelled.
76 if (!canceled)
77 sender.container().schedule(interval, [this]() { this->work_queue->add( [this]() { this->tick(); }); });
78 if (sender.credit() > 0) // Only send if we have credit
79 send();
80 else
81 ready = true; // Set the ready flag, send as soon as we get credit.
82 }
83
84 void on_sendable(proton::sender &) override {
85 if (ready) // We have been ticked since the last send.
86 send();
87 }
88
89 void send() {
90 std::cout << "send" << std::endl;
91 sender.send(proton::message("ping"));
92 ready = false;
93 }
94 };
95
96
97 int main(int argc, char **argv) {
98 std::string address("127.0.0.1:5672/examples");
99 double interval = 1.0;
100 double timeout = 5.0;
101
102 example::options opts(argc, argv);
103
104 opts.add_value(address, 'a', "address", "connect and send to URL", "URL");
105 opts.add_value(interval, 'i', "interval", "send a message every INTERVAL seconds", "INTERVAL");
106 opts.add_value(timeout, 't', "timeout", "stop after T seconds", "T");
107
108 try {
109 opts.parse();
110 scheduled_sender h(address, interval, timeout);
111 proton::container(h).run();
112 return 0;
113 } catch (const example::bad_option& e) {
114 std::cout << opts << std::endl << e.what() << std::endl;
115 } catch (const std::exception& e) {
116 std::cerr << e.what() << std::endl;
117 }
118
119 return 1;
120 }