Debian Codegraph @

cpp/examples/simple_connect.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/connection.hpp>
25 #include <proton/connection_options.hpp>
26 #include <proton/container.hpp>
27 #include <proton/messaging_handler.hpp>
28 #include <proton/reconnect_options.hpp>
29
30 #include <iostream>
31
32
33 class simple_connect : public proton::messaging_handler {
34 private:
35 std::string url;
36 std::string user;
37 std::string password;
38 bool reconnect;
39 bool sasl;
40 std::string mechs;
41 bool insecure;
42 proton::connection connection;
43
44 public:
45 simple_connect(const std::string &a, const std::string &u, const std::string &p,
46 bool r, bool s, const std::string& ms, bool in) :
47 url(a), user(u), password(p),
48 reconnect(r), sasl(s), mechs(ms), insecure(in) {}
49
50 void on_container_start(proton::container &c) override {
51 proton::connection_options co;
52 if (!user.empty()) co.user(user);
53 if (!password.empty()) co.password(password);
54 if (reconnect) co.reconnect(proton::reconnect_options());
55 if (sasl) co.sasl_enabled(true);
56 //
57 // NB: We only set sasl options if they are not default to avoid
58 // forcing SASL negotiation on when it's not needed.
59 //
60 // This is because the SASL negotiation is turned off unless
61 // it is needed. Setting a username/password or any SASL option will
62 // force the SASL negotiation to be turned on.
63 //
64 if (!mechs.empty()) co.sasl_allowed_mechs(mechs);
65 if (insecure) co.sasl_allow_insecure_mechs(true);
66 connection = c.connect(url, co);
67 }
68
69 void on_connection_open(proton::connection &c) override {
70 if (!reconnect) c.close();
71 }
72 };
73
74 int main(int argc, char **argv) {
75 std::string address("127.0.0.1:5672/examples");
76 std::string user;
77 std::string password;
78 bool reconnect = false;
79 bool sasl = false;
80 std::string mechs;
81 bool insecure = false;
82 example::options opts(argc, argv);
83
84 opts.add_value(address, 'a', "address", "connect and send to URL", "URL");
85 opts.add_value(user, 'u', "user", "authenticate as USER", "USER");
86 opts.add_value(password, 'p', "password", "authenticate with PASSWORD", "PASSWORD");
87 opts.add_flag(reconnect, 'r', "reconnect", "reconnect on connection failure");
88 opts.add_flag(sasl,'s', "sasl", "force SASL authentication with no user specified (Use for Kerberos/GSSAPI)");
89 opts.add_value(mechs, 'm', "mechs", "allowed SASL mechanisms", "MECHS");
90 opts.add_flag(insecure, 'i', "insecure", "allow clear-text passwords");
91
92 try {
93 opts.parse();
94
95 simple_connect connect(address, user, password, reconnect, sasl, mechs, insecure);
96 proton::container(connect).run();
97
98 return 0;
99 } catch (const example::bad_option& e) {
100 std::cout << opts << std::endl << e.what() << std::endl;
101 } catch (const std::exception& e) {
102 std::cerr << e.what() << std::endl;
103 }
104
105 return 1;
106 }