Debian Codegraph @

cpp/examples/selected_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 <proton/connection.hpp>
23 #include <proton/container.hpp>
24 #include <proton/message.hpp>
25 #include <proton/messaging_handler.hpp>
26 #include <proton/receiver_options.hpp>
27 #include <proton/source_options.hpp>
28
29 #include <iostream>
30
31
32 namespace {
33
34 // Example custom function to configure an AMQP filter,
35 // specifically an APACHE.ORG:SELECTOR
36 // (http://www.amqp.org/specification/1.0/filters)
37
38 void set_filter(proton::source_options &opts, const std::string& selector_str) {
39 proton::source::filter_map map;
40 proton::symbol filter_key("selector");
41 proton::value filter_value;
42 // The value is a specific AMQP "described type": binary string with symbolic descriptor
43 proton::codec::encoder enc(filter_value);
44 enc << proton::codec::start::described()
45 << proton::symbol("apache.org:selector-filter:string")
46 << selector_str
47 << proton::codec::finish();
48 // In our case, the map has this one element
49 map.put(filter_key, filter_value);
50 opts.filters(map);
51 }
52 }
53
54
55 class selected_recv : public proton::messaging_handler {
56 std::string conn_url_;
57 std::string addr_;
58
59 public:
60 selected_recv(const std::string& u, const std::string& a) :
61 conn_url_(u), addr_(a) {}
62
63 void on_container_start(proton::container &c) override {
64 proton::source_options opts;
65 set_filter(opts, "colour = 'green'");
66 proton::connection conn = c.connect(conn_url_);
67 conn.open_receiver(addr_, proton::receiver_options().source(opts));
68 }
69
70 void on_message(proton::delivery &, proton::message &m) override {
71 std::cout << m.body() << std::endl;
72 }
73 };
74
75 int main(int argc, char **argv) {
76 try {
77 std::string conn_url = argc > 1 ? argv[1] : "//127.0.0.1:5672";
78 std::string addr = argc > 2 ? argv[2] : "examples";
79
80 selected_recv recv(conn_url, addr);
81 proton::container(recv).run();
82
83 return 0;
84 } catch (const std::exception& e) {
85 std::cerr << e.what() << std::endl;
86 }
87
88 return 1;
89 }