A variant of the server part of a request-response example that accepts incoming connections and does not need an intermediary. Much like the original server, it receives incoming requests, converts the body to uppercase and sends the result back to the indicated reply address. Can be used in conjunction with any of the client alternatives.
#include "options.hpp"
#include "proton/acceptor.hpp"
#include "proton/container.hpp"
#include "proton/event.hpp"
#include "proton/handler.hpp"
#include "proton/url.hpp"
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <cctype>
private:
typedef std::map<std::string, proton::sender> sender_map;
sender_map senders;
int address_counter;
public:
server(const std::string &u) : url(u), address_counter(0) {}
std::cout << "server listening on " << url << std::endl;
}
std::string to_upper(const std::string &s) {
std::string uc(s);
size_t l = uc.size();
for (size_t i=0; i<l; i++) uc[i] = std::toupper(uc[i]);
return uc;
}
std::string generate_address() {
std::ostringstream addr;
addr << "server" << address_counter++;
return addr.str();
}
}
}
std::cout <<
"Received " << e.
message().
body() << std::endl;
std::string reply_to = e.
message().
reply_to();
sender_map::iterator it = senders.find(reply_to);
if (it == senders.end()) {
std::cout << "No link for reply_to: " << reply_to << std::endl;
} else {
reply.
correlation_id(e.
message().correlation_id());
}
}
};
int main(int argc, char **argv) {
std::string address("amqp://127.0.0.1:5672/examples");
options opts(argc, argv);
opts.add_value(address, 'a', "address", "listen on URL", "URL");
try {
opts.parse();
server srv(address);
return 0;
} catch (const bad_option& e) {
std::cout << opts << std::endl << e.what() << std::endl;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 1;
}