| 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/container.hpp> |
| 26 |
#include <proton/listen_handler.hpp> |
| 27 |
#include <proton/listener.hpp> |
| 28 |
#include <proton/message.hpp> |
| 29 |
#include <proton/message_id.hpp> |
| 30 |
#include <proton/messaging_handler.hpp> |
| 31 |
#include <proton/value.hpp> |
| 32 |
#include <proton/tracker.hpp> |
| 33 |
#include <proton/types.hpp> |
| 34 |
|
| 35 |
#include <iostream> |
| 36 |
#include <map> |
| 37 |
|
| 38 |
|
| 39 |
class simple_send : public proton::messaging_handler { |
| 40 |
private: |
| 41 |
class listener_ready_handler : public proton::listen_handler { |
| 42 |
void on_open(proton::listener& l) override { |
| 43 |
std::cout << "listening on " << l.port() << std::endl; |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
std::string url; |
| 48 |
proton::listener listener; |
| 49 |
listener_ready_handler listen_handler; |
| 50 |
int sent; |
| 51 |
int confirmed; |
| 52 |
int total; |
| 53 |
|
| 54 |
public: |
| 55 |
simple_send(const std::string &s, int c) : url(s), sent(0), confirmed(0), total(c) {} |
| 56 |
|
| 57 |
void on_container_start(proton::container &c) override { |
| 58 |
listener = c.listen(url, listen_handler); |
| 59 |
} |
| 60 |
|
| 61 |
void on_sendable(proton::sender &sender) override { |
| 62 |
while (sender.credit() && sent < total) { |
| 63 |
proton::message msg; |
| 64 |
std::map<std::string, int> m; |
| 65 |
m["sequence"] = sent + 1; |
| 66 |
|
| 67 |
msg.id(sent + 1); |
| 68 |
msg.body(m); |
| 69 |
|
| 70 |
sender.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 |
listener.stop(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void on_transport_close(proton::transport &) override { |
| 86 |
sent = confirmed; |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
int main(int argc, char **argv) { |
| 91 |
std::string address("127.0.0.1:5672/examples"); |
| 92 |
int message_count = 100; |
| 93 |
example::options opts(argc, argv); |
| 94 |
|
| 95 |
opts.add_value(address, 'a', "address", "listen and send on URL", "URL"); |
| 96 |
opts.add_value(message_count, 'm', "messages", "send COUNT messages", "COUNT"); |
| 97 |
|
| 98 |
try { |
| 99 |
opts.parse(); |
| 100 |
|
| 101 |
simple_send send(address, message_count); |
| 102 |
proton::container(send).run(); |
| 103 |
return 0; |
| 104 |
} catch (const example::bad_option& e) { |
| 105 |
std::cout << opts << std::endl << e.what() << std::endl; |
| 106 |
} catch (const std::exception& e) { |
| 107 |
std::cerr << e.what() << std::endl; |
| 108 |
} |
| 109 |
|
| 110 |
return 1; |
| 111 |
} |