X-Git-Url: https://git.teslayout.com/public/public/public/?a=blobdiff_plain;f=example%2Fdiameter%2Flauncher%2FMyHandler.cpp;h=6638fc37985cd2587ccda64f2357f75c2aa6b85e;hb=ea1b80f8fd0c1b806460f98f4e4cf433698857d9;hp=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391;hpb=68a8fe342d8337ec1a86b57920bc7dcf7faf2413;p=anna.git diff --git a/example/diameter/launcher/MyHandler.cpp b/example/diameter/launcher/MyHandler.cpp index e69de29..6638fc3 100644 --- a/example/diameter/launcher/MyHandler.cpp +++ b/example/diameter/launcher/MyHandler.cpp @@ -0,0 +1,83 @@ +// ANNA - Anna is Not Nothingness Anymore // +// // +// (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo // +// // +// See project site at http://redmine.teslayout.com/projects/anna-suite // +// See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE // + + +// Standard +#include +#include +#include + +// Project +#include + +// Process +#include +#include +#include + + +void MyHandler::evRequest(anna::comm::ClientSocket& clientSocket, const anna::http::Request& request) +throw(anna::RuntimeException) { + const anna::DataBlock& body = request.getBody(); + + if(body.getSize() == 0) + throw anna::RuntimeException("Missing operation parameters on HTTP request", ANNA_FILE_LOCATION); + + LOGINFORMATION( + std::string msg("Received body: "); + msg += anna::functions::asString(body); + anna::Logger::information(msg, ANNA_FILE_LOCATION); + ); + std::string body_content; + body_content.assign(body.getData(), body.getSize()); + auto json_body = nlohmann::json::parse(body_content); + + // Operation: + std::string response_content; + + bool opOk{}; + + try { + Launcher& my_app = static_cast (anna::app::functions::getApp()); + opOk = my_app.eventOperation(EventOperation::json2piped(json_body), response_content); + } catch(RuntimeException &ex) { + ex.trace(); + opOk = false; + } + + anna::http::Response* response = allocateResponse(); + response->setStatusCode(200); // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes + +// EXAMPLES TO SET HEADERS: +// response->find(anna::http::Header::Type::Date)->setValue("Mon, 30 Jan 2006 14:36:18 GMT"); +// anna::http::Header* keepAlive = response->find("Keep-Alive"); +// +// if (keepAlive == NULL) +// keepAlive = response->createHeader("Keep-Alive"); +// +// keepAlive->setValue("Verificacion del cambio 1.0.7"); + + // Content-Type header: + anna::http::Header* contentType = response->find("Content-Type"); + if (contentType == NULL) + contentType = response->createHeader("Content-Type"); + contentType->setValue("application/json"); + + // Json Body for response: + std::stringstream ss; + + ss << R"({ "result":")" << (opOk ? "true":"false") << R"(", "data": )" << std::quoted(response_content) << R"( })"; + anna::DataBlock db_content(true); + db_content = ss.str(); + response->setBody(db_content); + + try { + clientSocket.send(*response); + } catch(Exception& ex) { + ex.trace(); + } +}