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