Debian Codegraph @

cpp/examples/helloworld.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/delivery.hpp>
25 #include <proton/message.hpp>
26 #include <proton/messaging_handler.hpp>
27 #include <proton/tracker.hpp>
28
29 #include <iostream>
30
31
32 class hello_world : public proton::messaging_handler {
33 std::string conn_url_;
34 std::string addr_;
35
36 public:
37 hello_world(const std::string& u, const std::string& a) :
38 conn_url_(u), addr_(a) {}
39
40 void on_container_start(proton::container& c) override {
41 c.connect(conn_url_);
42 }
43
44 void on_connection_open(proton::connection& c) override {
45 c.open_receiver(addr_);
46 c.open_sender(addr_);
47 }
48
49 void on_sendable(proton::sender &s) override {
50 proton::message m("Hello World!");
51 s.send(m);
52 s.close();
53 }
54
55 void on_message(proton::delivery &d, proton::message &m) override {
56 std::cout << m.body() << std::endl;
57 d.connection().close();
58 }
59 };
60
61 int main(int argc, char **argv) {
62 try {
63 std::string conn_url = argc > 1 ? argv[1] : "//127.0.0.1:5672";
64 std::string addr = argc > 2 ? argv[2] : "examples";
65
66 hello_world hw(conn_url, addr);
67 proton::container(hw).run();
68
69 return 0;
70 } catch (const std::exception& e) {
71 std::cerr << e.what() << std::endl;
72 }
73
74 return 1;
75 }