Debian Codegraph @

cpp/examples/simple_recv.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/delivery.hpp>
28 #include <proton/link.hpp>
29 #include <proton/message.hpp>
30 #include <proton/message_id.hpp>
31 #include <proton/messaging_handler.hpp>
32 #include <proton/value.hpp>
33
34 #include <iostream>
35 #include <map>
36
37
38 class simple_recv : public proton::messaging_handler {
39 private:
40 std::string url;
41 std::string user;
42 std::string password;
43 proton::receiver receiver;
44 int expected;
45 int received;
46
47 public:
48 simple_recv(const std::string &s, const std::string &u, const std::string &p, int c) :
49 url(s), user(u), password(p), expected(c), received(0) {}
50
51 void on_container_start(proton::container &c) override {
52 proton::connection_options co;
53 if (!user.empty()) co.user(user);
54 if (!password.empty()) co.password(password);
55 receiver = c.open_receiver(url, co);
56 }
57
58 void on_message(proton::delivery &d, proton::message &msg) override {
59 if (!msg.id().empty() && proton::coerce<int>(msg.id()) < received) {
60 return; // Ignore if no id or duplicate
61 }
62
63 if (expected == 0 || received < expected) {
64 std::cout << msg.body() << std::endl;
65 received++;
66
67 if (received == expected) {
68 d.receiver().close();
69 d.connection().close();
70 }
71 }
72 }
73 };
74
75 int main(int argc, char **argv) {
76 std::string address("127.0.0.1:5672/examples");
77 std::string user;
78 std::string password;
79 int message_count = 100;
80 example::options opts(argc, argv);
81
82 opts.add_value(address, 'a', "address", "connect to and receive from URL", "URL");
83 opts.add_value(message_count, 'm', "messages", "receive COUNT messages", "COUNT");
84 opts.add_value(user, 'u', "user", "authenticate as USER", "USER");
85 opts.add_value(password, 'p', "password", "authenticate with PASSWORD", "PASSWORD");
86
87
88 try {
89 opts.parse();
90
91 simple_recv recv(address, user, password, message_count);
92 proton::container(recv).run();
93
94 return 0;
95 } catch (const example::bad_option& e) {
96 std::cout << opts << std::endl << e.what() << std::endl;
97 } catch (const std::exception& e) {
98 std::cerr << e.what() << std::endl;
99 }
100
101 return 1;
102 }