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 <iostream>
#include <map>
#include <string>
#include <sstream>
#include <cctype>
#include "fake_cpp11.hpp"
private:
std::cout <<
"listening on " << l.
port() << std::endl;
}
};
typedef std::map<std::string, proton::sender> sender_map;
listener_ready_handler listen_handler;
std::string url;
sender_map senders;
int address_counter;
public:
server(const std::string &u) : url(u), address_counter(0) {}
c.
listen(url, listen_handler);
}
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] = static_cast<char>(std::toupper(uc[i]));
return uc;
}
std::string generate_address() {
std::ostringstream addr;
addr << "server" << address_counter++;
return addr.str();
}
std::string addr = generate_address();
senders[addr] = sender;
}
}
std::cout <<
"Received " << m.
body() << std::endl;
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.
body(to_upper(proton::get<std::string>(m.
body())));
}
}
};
int main(int argc, char **argv) {
std::string address("amqp://127.0.0.1:5672/examples");
example::options opts(argc, argv);
opts.add_value(address, 'a', "address", "listen on URL", "URL");
try {
opts.parse();
server srv(address);
return 0;
} catch (const example::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;
}