From 95bb2236ac69b3ae54bb84f91a58a89491b94240 Mon Sep 17 00:00:00 2001 From: Comment Date: Wed, 13 Feb 2013 17:11:43 +0000 Subject: initial commit --- rotord/rotord.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 rotord/rotord.cpp (limited to 'rotord/rotord.cpp') diff --git a/rotord/rotord.cpp b/rotord/rotord.cpp new file mode 100644 index 0000000..ccb770b --- /dev/null +++ b/rotord/rotord.cpp @@ -0,0 +1,88 @@ +#include "rotord.h" + +RotorRequestHandler::RotorRequestHandler(const std::string& format): _format(format){ +} + +void RotorRequestHandler::handleRequest(HTTPServerRequest& request,HTTPServerResponse& response) { + + Timestamp now; + std::string dt(DateTimeFormatter::format(now, _format)); + + response.setChunkedTransferEncoding(true); + response.setContentType("text/html"); + + std::ostream& ostr = response.send(); + ostr << "RotorServer powered by " + "POCO C++ Libraries"; + ostr << ""; + ostr << "

"; + ostr << dt; + ostr << "

"; +} + + +RotorRequestHandlerFactory::RotorRequestHandlerFactory(const std::string& format):_format(format){ +} + +HTTPRequestHandler* RotorRequestHandlerFactory::createRequestHandler(const HTTPServerRequest& request){ + Application& app = Application::instance(); + app.logger().information("Request from "+ request.clientAddress().toString()+": "+request.getURI()); + if (request.getURI() == "/") + return new RotorRequestHandler(_format); + else + return 0; +} + + +RotorServer::RotorServer(): _helpRequested(false) +{ +} + +RotorServer::~RotorServer() +{ +} + +void RotorServer::initialize(Application& self){ + loadConfiguration(); + ServerApplication::initialize(self); +} + +void RotorServer::uninitialize(){ + ServerApplication::uninitialize(); +} + +void RotorServer::defineOptions(OptionSet& options) { + ServerApplication::defineOptions(options); + options.addOption( + Option("help", "h", "display argument help information") + .required(false) + .repeatable(false) + .callback(OptionCallback(this, &RotorServer::handleHelp) + ) + ); +} + +void RotorServer::handleHelp(const std::string& name, const std::string& value){ + HelpFormatter helpFormatter(options()); + helpFormatter.setCommand(commandName()); + helpFormatter.setUsage("OPTIONS"); + helpFormatter.setHeader( + "Rotor"); + helpFormatter.format(std::cout); + stopOptionsProcessing(); + _helpRequested = true; +} + +int RotorServer::main(const std::vector& args){ + if (!_helpRequested) { + unsigned short port = (unsigned short) config().getInt("port", 9980); + std::string format(config().getString("format", DateTimeFormat::SORTABLE_FORMAT)); + ServerSocket svs(port); + HTTPServer srv(new RotorRequestHandlerFactory(format), svs, new HTTPServerParams); + srv.start(); + waitForTerminationRequest(); + srv.stop(); + } + return Application::EXIT_OK; +} -- cgit v1.2.3