| 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/container.hpp> |
| 25 |
#include <proton/listen_handler.hpp> |
| 26 |
#include <proton/listener.hpp> |
| 27 |
#include <proton/message.hpp> |
| 28 |
#include <proton/message_id.hpp> |
| 29 |
#include <proton/messaging_handler.hpp> |
| 30 |
#include <proton/sender.hpp> |
| 31 |
#include <proton/sender_options.hpp> |
| 32 |
#include <proton/source_options.hpp> |
| 33 |
#include <proton/tracker.hpp> |
| 34 |
|
| 35 |
#include <iostream> |
| 36 |
#include <map> |
| 37 |
#include <string> |
| 38 |
#include <sstream> |
| 39 |
#include <cctype> |
| 40 |
|
| 41 |
|
| 42 |
class server : public proton::messaging_handler { |
| 43 |
private: |
| 44 |
class listener_ready_handler : public proton::listen_handler { |
| 45 |
void on_open(proton::listener& l) override { |
| 46 |
std::cout << "listening on " << l.port() << std::endl; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
typedef std::map<std::string, proton::sender> sender_map; |
| 51 |
listener_ready_handler listen_handler; |
| 52 |
std::string url; |
| 53 |
sender_map senders; |
| 54 |
int address_counter; |
| 55 |
|
| 56 |
public: |
| 57 |
server(const std::string &u) : url(u), address_counter(0) {} |
| 58 |
|
| 59 |
void on_container_start(proton::container &c) override { |
| 60 |
c.listen(url, listen_handler); |
| 61 |
} |
| 62 |
|
| 63 |
std::string to_upper(const std::string &s) { |
| 64 |
std::string uc(s); |
| 65 |
size_t l = uc.size(); |
| 66 |
|
| 67 |
for (size_t i=0; i<l; i++) |
| 68 |
uc[i] = static_cast<char>(std::toupper(uc[i])); |
| 69 |
|
| 70 |
return uc; |
| 71 |
} |
| 72 |
|
| 73 |
std::string generate_address() { |
| 74 |
std::ostringstream addr; |
| 75 |
addr << "server" << address_counter++; |
| 76 |
|
| 77 |
return addr.str(); |
| 78 |
} |
| 79 |
|
| 80 |
void on_sender_open(proton::sender &sender) override { |
| 81 |
if (sender.source().dynamic()) { |
| 82 |
std::string addr = generate_address(); |
| 83 |
sender.open(proton::sender_options().source(proton::source_options().address(addr))); |
| 84 |
senders[addr] = sender; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void on_message(proton::delivery &, proton::message &m) override { |
| 89 |
std::cout << "Received " << m.body() << std::endl; |
| 90 |
|
| 91 |
std::string reply_to = m.reply_to(); |
| 92 |
sender_map::iterator it = senders.find(reply_to); |
| 93 |
|
| 94 |
if (it == senders.end()) { |
| 95 |
std::cout << "No link for reply_to: " << reply_to << std::endl; |
| 96 |
} else { |
| 97 |
proton::sender sender = it->second; |
| 98 |
proton::message reply; |
| 99 |
|
| 100 |
reply.to(reply_to); |
| 101 |
reply.body(to_upper(proton::get<std::string>(m.body()))); |
| 102 |
reply.correlation_id(m.correlation_id()); |
| 103 |
|
| 104 |
sender.send(reply); |
| 105 |
} |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
int main(int argc, char **argv) { |
| 110 |
std::string address("amqp://127.0.0.1:5672/examples"); |
| 111 |
example::options opts(argc, argv); |
| 112 |
|
| 113 |
opts.add_value(address, 'a', "address", "listen on URL", "URL"); |
| 114 |
|
| 115 |
try { |
| 116 |
opts.parse(); |
| 117 |
|
| 118 |
server srv(address); |
| 119 |
proton::container(srv).run(); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} catch (const example::bad_option& e) { |
| 123 |
std::cout << opts << std::endl << e.what() << std::endl; |
| 124 |
} catch (const std::exception& e) { |
| 125 |
std::cerr << e.what() << std::endl; |
| 126 |
} |
| 127 |
|
| 128 |
return 1; |
| 129 |
} |