Menu Search

simple_connect.cpp

#include "options.hpp"

#include <proton/connection.hpp>
#include <proton/connection_options.hpp>
#include <proton/container.hpp>
#include <proton/messaging_handler.hpp>

#include <iostream>

#include "fake_cpp11.hpp"

class simple_connect : public proton::messaging_handler {
  private:
    std::string url;
    std::string user;
    std::string password;
    bool sasl;
    std::string mechs;
    proton::connection connection;

  public:
    simple_connect(const std::string &a, const std::string &u, const std::string &p, bool s, const std::string& ms) :
        url(a), user(u), password(p), sasl(s), mechs(ms) {}

    void on_container_start(proton::container &c) OVERRIDE {
        proton::connection_options co;
        if (!user.empty()) co.user(user);
        if (!password.empty()) co.password(password);
        if (sasl) co.sasl_enabled(true);
        if (!mechs.empty()) co.sasl_allowed_mechs(mechs);
        connection = c.connect(url, co);
    }

    void on_connection_open(proton::connection &c) OVERRIDE {
        c.close();
    }
};

int main(int argc, char **argv) {
    std::string address("127.0.0.1:5672/examples");
    std::string user;
    std::string password;
    std::string mechs;
    bool sasl = false;
    example::options opts(argc, argv);

    opts.add_value(address, 'a', "address", "connect and send to URL", "URL");
    opts.add_value(user, 'u', "user", "authenticate as USER", "USER");
    opts.add_value(password, 'p', "password", "authenticate with PASSWORD", "PASSWORD");
    opts.add_flag(sasl,'s', "sasl", "force SASL authentication with no user specified (Use for Kerberos/GSSAPI)");
    opts.add_value(mechs, 'm', "mechs", "allowed SASL mechanisms", "MECHS");

    try {
        opts.parse();

        simple_connect connect(address, user, password, sasl, mechs);
        proton::container(connect).run();

        return 0;
    } catch (const example::bad_option& e) {
        std::cout << opts << std::endl << e.what() << std::endl;
    } catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
    }

    return 1;
}

Download this file