#include <proton/connection.hpp>
#include <proton/container.hpp>
#include <proton/delivery.hpp>
#include <proton/message.hpp>
#include <proton/messaging_handler.hpp>
#include <proton/receiver.hpp>
#include <proton/source.hpp>
#include <iostream>
#include <string>
struct receive_handler : public proton::messaging_handler {
std::string conn_url_ {};
std::string address_ {};
int desired_ {0};
int received_ {0};
void on_container_start(proton::container& cont) override {
cont.connect(conn_url_);
// To connect with a user and password:
//
// proton::connection_options opts {};
// opts.user("<user>");
// opts.password("<password>");
//
// cont.connect(conn_url_, opts);
}
void on_connection_open(proton::connection& conn) override {
conn.open_receiver(address_);
}
void on_receiver_open(proton::receiver& rcv) override {
std::cout << "RECEIVE: Opened receiver for source address '"
<< rcv.source().address() << "'\n";
}
void on_message(proton::delivery& dlv, proton::message& msg) override {
std::cout << "RECEIVE: Received message '" << msg.body() << "'\n";
received_++;
if (received_ == desired_) {
dlv.receiver().close();
dlv.connection().close();
}
}
};
int main(int argc, char** argv) {
if (argc != 3 && argc != 4) {
std::cerr << "Usage: receive <connection-url> <address> [<message-count>]\n";
return 1;
}
receive_handler handler {};
handler.conn_url_ = argv[1];
handler.address_ = argv[2];
if (argc == 4) {
handler.desired_ = std::stoi(argv[3]);
}
proton::container cont {handler};
try {
cont.run();
} catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
return 0;
}
#include <proton/connection.hpp>
#include <proton/container.hpp>
#include <proton/delivery.hpp>
#include <proton/message.hpp>
#include <proton/messaging_handler.hpp>
#include <proton/receiver.hpp>
#include <proton/source.hpp>
#include <iostream>
#include <string>
struct receive_handler : public proton::messaging_handler {
std::string conn_url_ {};
std::string address_ {};
int desired_ {0};
int received_ {0};
void on_container_start(proton::container& cont) override {
cont.connect(conn_url_);
// To connect with a user and password:
//
// proton::connection_options opts {};
// opts.user("<user>");
// opts.password("<password>");
//
// cont.connect(conn_url_, opts);
}
void on_connection_open(proton::connection& conn) override {
conn.open_receiver(address_);
}
void on_receiver_open(proton::receiver& rcv) override {
std::cout << "RECEIVE: Opened receiver for source address '"
<< rcv.source().address() << "'\n";
}
void on_message(proton::delivery& dlv, proton::message& msg) override {
std::cout << "RECEIVE: Received message '" << msg.body() << "'\n";
received_++;
if (received_ == desired_) {
dlv.receiver().close();
dlv.connection().close();
}
}
};
int main(int argc, char** argv) {
if (argc != 3 && argc != 4) {
std::cerr << "Usage: receive <connection-url> <address> [<message-count>]\n";
return 1;
}
receive_handler handler {};
handler.conn_url_ = argv[1];
handler.address_ = argv[2];
if (argc == 4) {
handler.desired_ = std::stoi(argv[3]);
}
proton::container cont {handler};
try {
cont.run();
} catch (const std::exception& e) {
std::cerr << e.what() << "\n";
return 1;
}
return 0;
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow