Debian Codegraph @

cpp/examples/server.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/message_id.hpp>
26 #include <proton/messaging_handler.hpp>
27
28 #include <iostream>
29 #include <map>
30 #include <string>
31 #include <cctype>
32
33
34 class server : public proton::messaging_handler {
35 std::string conn_url_;
36 std::string addr_;
37 proton::connection conn_;
38 std::map<std::string, proton::sender> senders_;
39
40 public:
41 server(const std::string& u, const std::string& a) :
42 conn_url_(u), addr_(a) {}
43
44 void on_container_start(proton::container& c) override {
45 conn_ = c.connect(conn_url_);
46 conn_.open_receiver(addr_);
47
48 std::cout << "Server connected to " << conn_url_ << std::endl;
49 }
50
51 std::string to_upper(const std::string& s) {
52 std::string uc(s);
53 size_t l = uc.size();
54
55 for (size_t i=0; i<l; i++) {
56 uc[i] = static_cast<char>(std::toupper(uc[i]));
57 }
58
59 return uc;
60 }
61
62 void on_message(proton::delivery&, proton::message& m) override {
63 std::cout << "Received " << m.body() << std::endl;
64
65 std::string reply_to = m.reply_to();
66 proton::message reply;
67
68 reply.to(reply_to);
69 reply.body(to_upper(proton::get<std::string>(m.body())));
70 reply.correlation_id(m.correlation_id());
71
72 if (!senders_[reply_to]) {
73 senders_[reply_to] = conn_.open_sender(reply_to);
74 }
75
76 senders_[reply_to].send(reply);
77 }
78 };
79
80 int main(int argc, char** argv) {
81 try {
82 std::string conn_url = argc > 1 ? argv[1] : "//127.0.0.1:5672";
83 std::string addr = argc > 2 ? argv[2] : "examples";
84
85 server srv(conn_url, addr);
86 proton::container(srv).run();
87
88 return 0;
89 } catch (const std::exception& e) {
90 std::cerr << e.what() << std::endl;
91 }
92
93 return 1;
94 }