-
-void MyCommunicator::terminate()
-throw() {
- if (hasRequestedStop() == true)
- return;
-
- requestStop();
-}
-
-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(
- 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());
- // Operation:
- std::string response_content;
-
- try {
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- my_app.eventOperation(body_content, response_content);
- } catch (RuntimeException &ex) {
- ex.trace();
- }
-
- anna::http::Response* response = allocateResponse();
- response->setStatusCode(200); // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
- anna::DataBlock db_content(true);
- db_content = response_content;
- response->setBody(db_content);
-// 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");
-
- try {
- clientSocket.send(*response);
- } catch (Exception& ex) {
- ex.trace();
- }
-}
-
-void Launcher::eventOperation(const std::string &operation, std::string &response_content) throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("Launcher", "eventOperation", ANNA_FILE_LOCATION));
- CommandLine& cl(anna::CommandLine::instantiate());
- LOGDEBUG(anna::Logger::debug(operation, ANNA_FILE_LOCATION));
- response_content = "Operation processed with exception. See traces\n"; // supposed
- std::string result = "";
- anna::DataBlock db_aux(true);
-
- ///////////////////////////////////////////////////////////////////
- // Simple operations without arguments:
-
- // Help:
- if (operation == "help") {
- std::string s_help = help();
- std::cout << s_help << std::endl;
- LOGINFORMATION(anna::Logger::information(s_help, ANNA_FILE_LOCATION));
- response_content = "Help dumped on stdout and information-level traces (launcher.traces file)\n";
- return;
- }
-
- // Reset performance data:
- if (operation == "collect") {
- resetCounters();
- resetStatistics();
- response_content = "All process counters & statistic information have been reset\n";
- return;
- }
- ///////////////////////////////////////////////////////////////////
-
- // Tokenize operation
- Tokenizer params;
- params.apply(operation, "|");
- int numParams = params.size() - 1;
-
- // No operation has more than 2 arguments ...
- if (numParams > 2) {
- LOGWARNING(anna::Logger::warning(help(), ANNA_FILE_LOCATION));
- throw anna::RuntimeException("Wrong body content format on HTTP Request", ANNA_FILE_LOCATION);
- }
-
- // Get the operation type:
- Tokenizer::const_iterator tok_iter = params.begin();
- std::string opType = Tokenizer::data(tok_iter);
-
- // Check the number of parameters:
- bool wrongBody = false;
- if ( ((opType == "code")||(opType == "decode")) && (numParams != 2) ) wrongBody = true;
- if ( ((opType == "sendxml") || (opType == "sendxml2e") || (opType == "sendhex") || (opType == "sendhex2e")) && (numParams != 1) ) wrongBody = true;
- if ( (opType == "burst") && (numParams < 1) ) wrongBody = true;
- if ( ((opType == "sendxml2c")||(opType == "sendhex2c")||(opType == "loadxml")||(opType == "diameterServerSessions")) && (numParams != 1) ) wrongBody = true;
- if (wrongBody) {
- // Launch exception
- std::string msg = "Wrong body content format on HTTP Request for '";
- msg += opType;
- msg += "' operation (missing parameter/s)";
- throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
- }
-
- // All seems ok:
- std::string param1, param2;
- if (numParams >= 1) { tok_iter++; param1 = Tokenizer::data(tok_iter); }
- if (numParams == 2) { tok_iter++; param2 = Tokenizer::data(tok_iter); }
-
-
- // Operations:
- if (opType == "code") {
- G_codecMsg.loadXML(param1);
- std::string hexString = anna::functions::asHexString(G_codecMsg.code());
- // write to outfile
- ofstream outfile(param2.c_str(), ifstream::out);
- outfile.write(hexString.c_str(), hexString.size());
- outfile.close();
-
- } else if (opType == "decode") {
- // Get DataBlock from file with hex content:
- if (!getDataBlockFromHexFile(param1, db_aux))
- throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
-
- // Decode
- try { G_codecMsg.decode(db_aux); } catch (anna::RuntimeException &ex) { ex.trace(); }
-
- std::string xmlString = G_codecMsg.asXMLString();
- // write to outfile
- ofstream outfile(param2.c_str(), ifstream::out);
- outfile.write(xmlString.c_str(), xmlString.size());
- outfile.close();
-
- } else if ((opType == "hide") || (opType == "show") || (opType == "hidden") || (opType == "shown")) {
- anna::diameter::comm::Entity *entity = getEntity();
- if (!entity) throw anna::RuntimeException("No entity configured to send messages", ANNA_FILE_LOCATION);
- if (param1 != "") {
- if (param2 != "") {
- std::string key = param1;
- key += "|";
- key += param2;
-
- if (opType == "hide") getMyDiameterEngine()->findClientSession(key)->hide();
- if (opType == "show") getMyDiameterEngine()->findClientSession(key)->show();
- if (opType == "hidden") result = getMyDiameterEngine()->findClientSession(key)->hidden() ? "true" : "false";
- if (opType == "shown") result = getMyDiameterEngine()->findClientSession(key)->shown() ? "true" : "false";
- } else {
- std::string address;
- int port;
- anna::functions::getAddressAndPortFromSocketLiteral(param1, address, port);
-
- if (opType == "hide") getMyDiameterEngine()->findServer(address, port)->hide();
- if (opType == "show") getMyDiameterEngine()->findServer(address, port)->show();
- if (opType == "hidden") result = getMyDiameterEngine()->findServer(address, port)->hidden() ? "true" : "false";
- if (opType == "shown") result = getMyDiameterEngine()->findServer(address, port)->shown() ? "true" : "false";
- }
- } else {
- if (opType == "hide") entity->hide();
- if (opType == "show") entity->show();
- if (opType == "hidden") result = entity->hidden() ? "true" : "false";
- if (opType == "shown") result = entity->shown() ? "true" : "false";
- }
- } else if ((opType == "sendxml") || (opType == "sendxml2e") || (opType == "sendhex") || (opType == "sendhex2e")) {
-
- anna::diameter::comm::Entity *entity = getEntity();
- if (!entity) throw anna::RuntimeException("No entity configured to send the message", ANNA_FILE_LOCATION);
-
- if ((opType == "sendxml") || (opType == "sendxml2e")) {
- G_codecMsg.loadXML(param1);
- G_commMsgSent2e.clearBody();
- try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher)
- G_commMsgSent2e.setBody(G_codecMsg.code());
- }
- else {
- // Get DataBlock from file with hex content:
- if (!getDataBlockFromHexFile(param1, db_aux))
- throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
- G_commMsgSent2e.setBody(db_aux);
- }
-
- bool success = entity->send(G_commMsgSent2e, cl.exists("balance"));
-
- // Detailed log:
- if (logEnabled()) {
- anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
- anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
- std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // esto no deberia ocurrir
- writeLogFile(G_codecMsg, (success ? "sent2e" : "send2eError"), detail);
- }
- } else if ((opType == "burst")) {
- anna::diameter::comm::Entity *entity = getEntity();
- if (!entity) throw anna::RuntimeException("No entity configured to use burst feature", ANNA_FILE_LOCATION);
-
- // burst|clear clears all loaded burst messages.
- // burst|load|<source_file> loads the next diameter message into launcher burst.
- // burst|start|<initial load> starts the message sending with a certain initial load.
- // burst|push|<load amount> sends specific non-aynchronous load.
- // burst|stop stops the burst cycle.
- // burst|repeat|[[yes]|no] restarts the burst launch when finish.
- // burst|send|<amount> send messages from burst list. The main difference with
- // start/push operations is that burst won't be awaken.
- // Externally we could control sending time (no request
- // will be sent for answers).
- // burst|goto|<order> Updates current burst pointer position.
- // burst|look|<order> Show programmed burst message for order provided.
-
- if (param1 == "clear") {
- result = "Removed ";
- result += anna::functions::asString(clearBurst());
- result += " elements.";
- } else if (param1 == "load") {
- if (param2 == "") throw anna::RuntimeException("Missing xml path file for burst load operation", ANNA_FILE_LOCATION);
-
- G_codecMsg.loadXML(param2);
-
- if (G_codecMsg.isAnswer()) throw anna::RuntimeException("Cannot load diameter answers for burst feature", ANNA_FILE_LOCATION);
- try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue loading (see validation mode configured in launcher)
-
- int position = loadBurstMessage(G_codecMsg.code());
- result = "Loaded '";
- result += param2;
- result += "' file into burst list position ";
- result += anna::functions::asString(position);
- } else if (param1 == "start") {
- if (param2 == "") throw anna::RuntimeException("Missing initial load for burst start operation", ANNA_FILE_LOCATION);
-
- int initialLoad = atoi(param2.c_str());
- int processed = startBurst(initialLoad);
-
- if (processed > 0) {
- result = "Initial load completed for ";
- result += anna::functions::entriesAsString(processed, "message");
- result += ".";
- }
- } else if (param1 == "push") {
- if (param2 == "") throw anna::RuntimeException("Missing load amount for burst push operation", ANNA_FILE_LOCATION);
-
- int pushed = pushBurst(atoi(param2.c_str()));
-
- if (pushed > 0) {
- result = "Pushed ";
- result += anna::functions::entriesAsString(pushed, "message");
- result += ".";
- }
- } else if (param1 == "pop") {
- if (param2 == "") throw anna::RuntimeException("Missing amount for burst pop operation", ANNA_FILE_LOCATION);
-
- int releaseLoad = atoi(param2.c_str());
- int popped = popBurst(releaseLoad);
-
- if (popped > 0) {
- result = "Burst popped for ";
- result += anna::functions::entriesAsString(popped, "message");
- result += ".";
- }
- } else if (param1 == "stop") {
- int left = stopBurst();
-
- if (left != -1) {
- result += anna::functions::entriesAsString(left, "message");
- result += " left to the end of the cycle.";
- }
- } else if (param1 == "repeat") {
- if (param2 == "") param2 = "yes";
-
- bool repeat = (param2 == "yes");
- repeatBurst(repeat);
- result += (repeat ? "Mode on." : "Mode off.");
- } else if (param1 == "send") {
- if (param2 == "") throw anna::RuntimeException("Missing amount for burst send operation", ANNA_FILE_LOCATION);
-
- int sent = sendBurst(atoi(param2.c_str()));
-
- if (sent > 0) {
- result = "Sent ";
- result += anna::functions::entriesAsString(sent, "message");
- result += ".";
- }
- } else if (param1 == "goto") {
- if (param2 == "") throw anna::RuntimeException("Missing order position for burst goto operation", ANNA_FILE_LOCATION);
-
- result = gotoBurst(atoi(param2.c_str()));
- result += ".";
- } else if (param1 == "look") {
- if (param2 == "") throw anna::RuntimeException("Missing order position for burst look operation", ANNA_FILE_LOCATION);
-
- result = "\n\n";
- result += lookBurst(atoi(param2.c_str()));
- result += "\n\n";
- } else {
- throw anna::RuntimeException("Wrong body content format on HTTP Request for 'burst' operation (unexpected action parameter). See help", ANNA_FILE_LOCATION);
- }
-
- } else if ((opType == "sendxml2c") || (opType == "sendhex2c")) {
- anna::diameter::comm::LocalServer *localServer = getDiameterLocalServer();
- if (!localServer) throw anna::RuntimeException("No local server configured to send the message", ANNA_FILE_LOCATION);
-
- if (opType == "sendxml2c") {
- G_codecMsg.loadXML(param1);
- G_commMsgSent2c.clearBody();
- try { G_codecMsg.valid(); } catch (anna::RuntimeException &ex) { ex.trace(); } // at least we need to see validation errors although it will continue sending (see validation mode configured in launcher)
- G_commMsgSent2c.setBody(G_codecMsg.code());
- }
- else {
- // Get DataBlock from file with hex content:
- if (!getDataBlockFromHexFile(param1, db_aux))
- throw anna::RuntimeException("Error reading hex file provided", ANNA_FILE_LOCATION);
- G_commMsgSent2c.setBody(db_aux);
- }
-
- bool success = localServer->send(G_commMsgSent2c);
-
- // Detailed log:
- if (logEnabled()) {
- anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource();
- std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
- writeLogFile(G_codecMsg, (success ? "sent2c" : "send2cError"), detail);
- }
-
- } else if (opType == "loadxml") {
- G_codecMsg.loadXML(param1);
- std::string xmlString = G_codecMsg.asXMLString();
- std::cout << xmlString << std::endl;
-
- } else if (opType == "diameterServerSessions") {
-
- int diameterServerSessions = atoi(param1.c_str());
-
- if (!getDiameterLocalServer())
- startDiameterServer(diameterServerSessions);
- else
- getDiameterLocalServer()->setMaxConnections(diameterServerSessions);
- } else if ((opType == "answerxml") || (opType == "answerxml2c") || (opType == "answerhex") || (opType == "answerhex2c")) {
- anna::diameter::comm::LocalServer *localServer = getDiameterLocalServer();
-
- if (!localServer)
- throw anna::RuntimeException("Operation not applicable (no own diameter server has been configured)", ANNA_FILE_LOCATION);
-
- if (param1 != "") {
- anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
- anna::diameter::codec::Message *message = engine->createMessage(param1);
- LOGDEBUG
- (
- anna::Logger::debug(message->asXMLString(), ANNA_FILE_LOCATION);
- );
-
- if (message->isRequest())
- throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION);
-
- int code = message->getId().first;
- reacting_answers_const_iterator it = G_reactingAnswers2C.find(code);
-
- if (it != G_reactingAnswers2C.end()) { // found: replace
- LOGDEBUG(anna::Logger::debug("Replacing formerly programed answer...", ANNA_FILE_LOCATION));
- engine->releaseMessage((*it).second);
- }
-
- G_reactingAnswers2C[code] = message;
- } else { // answers query on stdout
- std::cout << programmedAnswers2c() << std::endl;
- response_content = "Programmed answers dumped on stdout\n";
- return;
- }
- } else if ((opType == "answerxml2e") || (opType == "answerhex2e")) {
- anna::diameter::comm::Entity *entity = getEntity();
-
- if (!entity)
- throw anna::RuntimeException("Operation not applicable (no diameter entity has been configured)", ANNA_FILE_LOCATION);
-
- if (param1 != "") {
- anna::diameter::codec::Engine *engine = anna::functions::component <Engine> (ANNA_FILE_LOCATION);
- anna::diameter::codec::Message *message = engine->createMessage(param1);
- LOGDEBUG
- (
- anna::Logger::debug(message->asXMLString(), ANNA_FILE_LOCATION);
- );
-
- if (message->isRequest())
- throw anna::RuntimeException("Cannot program diameter requests. Answer type must be provided", ANNA_FILE_LOCATION);
-
- int code = message->getId().first;
- reacting_answers_const_iterator it = G_reactingAnswers2E.find(code);
-
- if (it != G_reactingAnswers2E.end()) { // found: replace
- LOGDEBUG(anna::Logger::debug("Replacing formerly programed answer...", ANNA_FILE_LOCATION));
- engine->releaseMessage((*it).second);
- }
-
- G_reactingAnswers2E[code] = message;
- } else { // answers query on stdout
- std::cout << programmedAnswers2e() << std::endl;
- response_content = "Programmed answers dumped on stdout\n";
- return;
- }
- } else {
- LOGWARNING(anna::Logger::warning(help(), ANNA_FILE_LOCATION));
- throw anna::RuntimeException("Wrong body content format on HTTP Request. Unsupported/unrecognized operation type", ANNA_FILE_LOCATION);
- }
-
- // HTTP response
- response_content = "Operation processed; ";
-
- if ((opType == "decode") || (opType == "code")) {
- response_content += "File '";
- response_content += param2;
- response_content += "' created.";
- response_content += "\n";
- } else if ((opType == "hide") || (opType == "show")) {
- response_content += "Resource '";
- response_content += ((param1 != "") ? param1 : "Entity");
-
- if (param2 != "") {
- response_content += "|";
- response_content += param2;
- }
-
- response_content += "' ";
-
- if (opType == "hide") response_content += "has been hidden.";
-
- if (opType == "show") response_content += "has been shown.";
-
- response_content += "\n";
- } else if ((opType == "hidden") || (opType == "shown")) {
- response_content += "Result: ";
- response_content += result;
- response_content += "\n";
- } else if ((opType == "sendxml") || (opType == "sendxml2e") || (opType == "sendhex") || (opType == "sendhex2e")) {
- response_content += "Message '";
- response_content += param1;
- response_content += "' sent to entity.";
- response_content += "\n";
- } else if (opType == "burst") {
- response_content += "Burst '";
- response_content += param1;
- response_content += "' executed. ";
- response_content += result;
- response_content += "\n";
- } else if ((opType == "sendxml2c")||(opType == "sendhex2c")) {
- response_content += "Message '";
- response_content += param1;
- response_content += "' sent to client.";
- response_content += "\n";
- } else if (opType == "loadxml") {
- response_content += "Message '";
- response_content += param1;
- response_content += "' loaded.";
- response_content += "\n";
- } else if ((opType == "answerxml") || (opType == "answerxml2c") || (opType == "answerhex") || (opType == "answerhex2c")) {
- response_content += "Answer to client '";
- response_content += param1;
- response_content += "' programmed.";
- response_content += "\n";
- } else if ((opType == "answerxml2e") || (opType == "answerhex2e")) {
- response_content += "Answer to entity '";
- response_content += param1;
- response_content += "' programmed.";
- response_content += "\n";
- } else if (opType == "diameterServerSessions") {
- response_content += "Maximum server socket connections updated to '";
- response_content += param1;
- response_content += "'.";
- response_content += "\n";
- }
-}
-
-
-int MyDiameterEntity::readSocketId(const anna::diameter::comm::Message* message, int maxClientSessions) const throw() {
- CommandLine& cl(anna::CommandLine::instantiate());
- std::string sessionBasedModelsType = (cl.exists("sessionBasedModelsClientSocketSelection") ? cl.getValue("sessionBasedModelsClientSocketSelection") : "SessionIdLowPart");
-
- if (sessionBasedModelsType == "RoundRobin") return -1; // IEC also would return -1
-
- try {
- // Service-Context-Id:
- anna::diameter::helpers::dcca::ChargingContext::_v chargingContext;
- std::string scid = anna::diameter::helpers::dcca::functions::getServiceContextId(message->getBody(), chargingContext);
-
- switch (chargingContext) {
- case anna::diameter::helpers::dcca::ChargingContext::Data:
- case anna::diameter::helpers::dcca::ChargingContext::Voice:
- case anna::diameter::helpers::dcca::ChargingContext::Content: {
- // Session-Id: '<DiameterIdentity>;<high 32 bits>;<low 32 bits>[;<optional value>="">]'
- std::string sid = anna::diameter::helpers::base::functions::getSessionId(message->getBody());
- std::string diameterIdentity, optional;
- anna::U32 high, low;
- anna::diameter::helpers::base::functions::decodeSessionId(sid, diameterIdentity, high, low /* context-teid */, optional);
-
- if (sessionBasedModelsType == "SessionIdLowPart") return (low % maxClientSessions);
-
- if (sessionBasedModelsType == "SessionIdHighPart") return (high % maxClientSessions);
-
- if (sessionBasedModelsType == "SessionIdOptionalPart") return (atoi(optional.c_str()) % maxClientSessions);
- }
- //case anna::diameter::helpers::dcca::ChargingContext::SMS:
- //case anna::diameter::helpers::dcca::ChargingContext::MMS:
- //default:
- // return -1; // IEC model and Unknown traffic types
- }
- } catch (anna::RuntimeException &ex) {
- LOGDEBUG(
- std::string msg = ex.getText();
- msg += " | Round-robin between sessions will be used to send";
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
- }
-
- return -1;
-}
-
-
-void MyDiameterEntity::eventRequest(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventRequest", ANNA_FILE_LOCATION));
- // Performance stats:
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- CommandLine& cl(anna::CommandLine::instantiate());
- // CommandId:
- anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
- LOGDEBUG
- (
- std::string msg = "Request received: ";
- msg += anna::diameter::functions::commandIdAsPairString(cid);
- msg += " | DiameterServer: ";
- msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
- msg += " | EventTime: ";
- msg += anna::time::functions::currentTimeAsString();
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- // Write reception
- if (my_app.logEnabled()) my_app.writeLogFile(message, "recvfe", clientSession->asString());
-
- // Lookup reacting answers list:
- int code = cid.first;
- reacting_answers_const_iterator it = G_reactingAnswers2E.find(code);
-
- if (it != G_reactingAnswers2E.end()) {
- anna::diameter::codec::Message *answer_message = (*it).second;
- // Prepare answer:
- my_app.getCommunicator()->prepareAnswer(answer_message, message);
-
- try {
- G_commMsgSent2e.setBody(answer_message->code());
- /* response = NULL =*/clientSession->send(&G_commMsgSent2e);
-
- if (my_app.logEnabled()) my_app.writeLogFile(*answer_message, "sent2e", clientSession->asString());
- } catch (anna::RuntimeException &ex) {
- ex.trace();
-
- if (my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2eError", clientSession->asString());
- }
- } else { // not found: forward to client (if exists)
- // Forward to client:
- anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
-
- if (localServer && (cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CER */) {
- try {
- anna::diameter::comm::Message *msg = G_commMessages.create();
- msg->setBody(message);
- msg->setRequestClientSessionKey(clientSession->getKey());
- bool success = localServer->send(msg);
-
- // Detailed log:
- if (my_app.logEnabled()) {
- anna::diameter::comm::ServerSession *usedServerSession = localServer->getLastUsedResource();
- std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
- my_app.writeLogFile(message, (success ? "fwd2c" : "fwd2cError"), detail);
- }
- } catch (anna::RuntimeException &ex) {
- ex.trace();
- }
- }
- }
-}
-
-
-void MyDiameterEntity::eventResponse(const anna::diameter::comm::Response &response)
-throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventResponse", ANNA_FILE_LOCATION));
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- CommandLine& cl(anna::CommandLine::instantiate());
- anna::diameter::comm::ClassCode::_v code = response.getClassCode();
- anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
- anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
- const anna::DataBlock* message = response.getMessage();
- const anna::diameter::comm::ClientSession *clientSession = static_cast<const anna::diameter::comm::ClientSession *>(response.getSession());
- bool isBindResponse = (code == anna::diameter::comm::ClassCode::Bind);
- bool isApplicationMessage = (code == anna::diameter::comm::ClassCode::ApplicationMessage);
- bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
- bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
- bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
- // CommandId:
- anna::diameter::CommandId request_cid = request->getCommandId();
- LOGDEBUG
- (
- std::string msg = "Response received for original diameter request: ";
- msg += anna::diameter::functions::commandIdAsPairString(request_cid);
- msg += " | Response: ";
- msg += response.asString();
- msg += " | DiameterServer: ";
- msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
- msg += " | EventTime: ";
- msg += anna::time::functions::currentTimeAsString();
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- if (isUnavailable) {
- //if (isApplicationMessage)
- LOGWARNING(anna::Logger::warning("Diameter entity unavailable for Diameter Request", ANNA_FILE_LOCATION));
- }
-
- if (contextExpired) {
- //if (isApplicationMessage)
- LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the entity", ANNA_FILE_LOCATION));
-
- if (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) { // don't trace CEA
- if (my_app.logEnabled()) my_app.writeLogFile(*request, "req2e-expired", clientSession->asString());
- }
- }
-
- if (isOK) {
- LOGDEBUG(
- std::string msg = "Received response for diameter message: ";
- msg += anna::diameter::functions::commandIdAsPairString(request_cid);
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
- // Write reception
- bool alreadyDecodedOnG_codecMsg = false;
-
- if (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) { // don't trace CEA
- if (my_app.logEnabled()) {
- my_app.writeLogFile(*message, "recvfe", clientSession->asString());
- alreadyDecodedOnG_codecMsg = true;
- }
- }
-
- // Forward to client:
- anna::diameter::comm::LocalServer *localServer = my_app.getDiameterLocalServer();
-
- if (localServer && (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) /* don't forward CEA */) {
- try {
- G_commMsgFwd2c.setBody(*message);
- bool success = localServer->send(&G_commMsgFwd2c, request->getRequestServerSessionKey());
- G_commMessages.release(request);
- // Detailed log:
- anna::diameter::comm::ServerSession *usedServerSession = my_app.getMyDiameterEngine()->findServerSession(request->getRequestServerSessionKey());
- std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // esto no deberia ocurrir
-
- if (my_app.logEnabled()) {
- if (alreadyDecodedOnG_codecMsg)
- my_app.writeLogFile(G_codecMsg, (success ? "fwd2c" : "fwd2cError"), detail);
- else
- my_app.writeLogFile(*message, (success ? "fwd2c" : "fwd2cError"), detail);
- }
- } catch (anna::RuntimeException &ex) {
- ex.trace();
- }
- }
- }
-
- // Triggering burst:
- if (isOK || contextExpired) my_app.sendBurstMessage();
-}
-
-
-void MyDiameterEntity::eventUnknownResponse(anna::diameter::comm::ClientSession *clientSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("launcher::MyDiameterEntity", "eventUnknownResponse", ANNA_FILE_LOCATION));
- // Performance stats:
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- // CommandId:
- anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
- LOGDEBUG
- (
- std::string msg = "Out-of-context response received from entity: ";
- msg += anna::diameter::functions::commandIdAsPairString(cid);
- msg += " | DiameterServer: ";
- msg += anna::functions::socketLiteralAsString(clientSession->getAddress(), clientSession->getPort());
- msg += " | EventTime: ";
- msg += anna::time::functions::currentTimeAsString();
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- // Write reception
- if (my_app.logEnabled()) my_app.writeLogFile(message, "recvfe-ans-unknown", clientSession->asString());
-}
-
-
-
-
-void MyLocalServer::eventRequest(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventRequest", ANNA_FILE_LOCATION));
- // Performance stats:
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- CommandLine& cl(anna::CommandLine::instantiate());
- // CommandId:
- anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
- LOGDEBUG
- (
- std::string msg = "Request received: ";
- msg += anna::diameter::functions::commandIdAsPairString(cid);
- msg += " | DiameterServer: ";
- msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
- msg += " | EventTime: ";
- msg += anna::time::functions::currentTimeAsString();
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- // Write reception
- if (my_app.logEnabled()) my_app.writeLogFile(message, "recvfc", serverSession->asString());
-
- // If no answer is programmed and entity is configured, the failed request would be forwarded even being wrong (delegates at the end point)
- int code = cid.first;
- reacting_answers_const_iterator it = G_reactingAnswers2C.find(code);
- bool programmed = (it != G_reactingAnswers2C.end());
- anna::diameter::comm::Entity *entity = my_app.getEntity();
-
- if (!programmed && entity) { // forward condition (no programmed answer + entity available)
- anna::diameter::comm::Message *msg = G_commMessages.create();
- msg->setBody(message);
- msg->setRequestServerSessionKey(serverSession->getKey());
- bool success = entity->send(msg, cl.exists("balance"));
-
- // Detailed log:
- if (my_app.logEnabled()) {
- anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
- anna::diameter::comm::ClientSession *usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
- std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // esto no deberia ocurrir
- my_app.writeLogFile(message, (success ? "fwd2e" : "fwd2eError"), detail); // forwarded
- }
-
- return;
- }
-
- // Error analisys:
- bool analysisOK = true; // by default
- anna::diameter::codec::Message *answer_message = NULL;
-
- if (!cl.exists("ignoreErrors")) { // Error analysis
- answer_message = (anna::diameter::codec::Message*) & G_codecAnsMsg;
- answer_message->clear();
-
- // Decode
- try { G_codecMsg.decode(message, answer_message); } catch (anna::RuntimeException &ex) { ex.trace(); }
-
- answer_message->setStandardToAnswer(G_codecMsg, my_app.getMyDiameterEngine()->getHost(), my_app.getMyDiameterEngine()->getRealm());
- analysisOK = (answer_message->getResultCode() == anna::diameter::helpers::base::AVPVALUES__Result_Code::DIAMETER_SUCCESS);
- }
-
- // Programmed answer only when all is ok
- if (analysisOK) {
- if (programmed) {
- answer_message = (*it).second;
- // Prepare answer:
- my_app.getCommunicator()->prepareAnswer(answer_message, message);
- } else return; // nothing done
- }
-
- anna::diameter::codec::Engine *codecEngine = (anna::functions::component <anna::diameter::codec::Engine> (ANNA_FILE_LOCATION));
- anna::diameter::codec::Engine::ValidationMode::_v backupVM = codecEngine->getValidationMode();
-
- if (!analysisOK)
- codecEngine->setValidationMode(anna::diameter::codec::Engine::ValidationMode::Never);
-
- try {
- G_commMsgSent2c.setBody(answer_message->code());
- /* response = NULL =*/serverSession->send(&G_commMsgSent2c);
-
- if (my_app.logEnabled()) my_app.writeLogFile(*answer_message, "sent2c", serverSession->asString());
- } catch (anna::RuntimeException &ex) {
- ex.trace();
-
- if (my_app.logEnabled()) my_app.writeLogFile(*answer_message, "send2cError", serverSession->asString());
- }
-
- // Restore validation mode
- codecEngine->setValidationMode(backupVM);
-}
-
-void MyLocalServer::eventResponse(const anna::diameter::comm::Response &response)
-throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventResponse", ANNA_FILE_LOCATION));
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- CommandLine& cl(anna::CommandLine::instantiate());
- anna::diameter::comm::ClassCode::_v code = response.getClassCode();
- anna::diameter::comm::Response::ResultCode::_v result = response.getResultCode();
- anna::diameter::comm::Message* request = const_cast<anna::diameter::comm::Message*>(response.getRequest());
- const anna::DataBlock* message = response.getMessage();
- const anna::diameter::comm::ServerSession *serverSession = static_cast<const anna::diameter::comm::ServerSession *>(response.getSession());
- bool isBindResponse = (code == anna::diameter::comm::ClassCode::Bind);
- bool isApplicationMessage = (code == anna::diameter::comm::ClassCode::ApplicationMessage);
- bool contextExpired = (result == anna::diameter::comm::Response::ResultCode::Timeout);
- bool isUnavailable = (result == anna::diameter::comm::Response::ResultCode::DiameterUnavailable);
- bool isOK = (result == anna::diameter::comm::Response::ResultCode::Success);
- // CommandId:
- anna::diameter::CommandId request_cid = request->getCommandId();
- LOGDEBUG
- (
- std::string msg = "Response received for original diameter request: ";
- msg += anna::diameter::functions::commandIdAsPairString(request_cid);
- msg += " | Response: ";
- msg += response.asString();
- msg += " | LocalServer: ";
- msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
- msg += " | EventTime: ";
- msg += anna::time::functions::currentTimeAsString();
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- if (isUnavailable) {
- //if (isApplicationMessage)
- LOGWARNING(anna::Logger::warning("Diameter client unavailable for Diameter Request", ANNA_FILE_LOCATION));
- }
-
- if (contextExpired) {
- //if (isApplicationMessage)
- LOGWARNING(anna::Logger::warning("Context Expired for Diameter Request which was sent to the client", ANNA_FILE_LOCATION));
-
- if (request_cid != anna::diameter::helpers::base::COMMANDID__Capabilities_Exchange_Request) { // don't trace CEA
- if (my_app.logEnabled()) my_app.writeLogFile(*request, "req2c-expired", serverSession->asString());
- }
- }
-
- if (isOK) {
- LOGDEBUG(
- std::string msg = "Received response for diameter message: ";
- msg += anna::diameter::functions::commandIdAsPairString(request_cid);
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- // Write reception
- if (my_app.logEnabled()) my_app.writeLogFile(*message, "recvfc", serverSession->asString());
-
- // This is not very usual, but answers could arrive from clients:
- anna::diameter::comm::Entity *entity = my_app.getEntity();
-
- if (entity) {
- anna::diameter::comm::ClientSession *usedClientSession = my_app.getMyDiameterEngine()->findClientSession(request->getRequestClientSessionKey());
- std::string detail;
-
- if (my_app.logEnabled()) detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // esto no deberia ocurrir
-
- try {
- G_commMsgFwd2e.setBody(*message);
-
- // Metodo 1:
- if (usedClientSession) /* response = NULL =*/usedClientSession->send(&G_commMsgFwd2e);
-
- // Metodo 2:
- //G_commMsgFwd2e.setRequestClientSessionKey(request->getRequestClientSessionKey());
- //bool success = entity->send(G_commMsgFwd2e);
- G_commMessages.release(request);
-
- if (my_app.logEnabled()) my_app.writeLogFile(*message, "fwd2e", detail); // forwarded
- } catch (anna::RuntimeException &ex) {
- ex.trace();
-
- if (my_app.logEnabled()) my_app.writeLogFile(*message, "fwd2eError", detail); // forwarded
- }
- }
- }
-}
-
-void MyLocalServer::eventUnknownResponse(anna::diameter::comm::ServerSession *serverSession, const anna::DataBlock &message)
-throw(anna::RuntimeException) {
- LOGMETHOD(anna::TraceMethod tm("launcher::MyLocalServer", "eventUnknownResponse", ANNA_FILE_LOCATION));
- // Performance stats:
- Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
- // CommandId:
- anna::diameter::CommandId cid = anna::diameter::codec::functions::getCommandId(message);
- LOGDEBUG
- (
- std::string msg = "Out-of-context response received from client: ";
- msg += anna::diameter::functions::commandIdAsPairString(cid);
- msg += " | DiameterServer: ";
- msg += anna::functions::socketLiteralAsString(serverSession->getAddress(), serverSession->getPort());
- msg += " | EventTime: ";
- msg += anna::time::functions::currentTimeAsString();
- anna::Logger::debug(msg, ANNA_FILE_LOCATION);
- );
-
- if (my_app.logEnabled()) my_app.writeLogFile(message, "recvfc-ans-unknown", serverSession->asString());
-}
-
-
-anna::xml::Node* Launcher::asXML(anna::xml::Node* parent) const
-throw() {
- anna::xml::Node* result = parent->createChild("launcher");
- anna::comm::Application::asXML(result);
- // Timming:
- result->createAttribute("StartTime", a_start_time.asString());
- result->createAttribute("SecondsLifeTime", anna::time::functions::lapsedMilliseconds() / 1000);
- // Diameter:
- (anna::functions::component <anna::diameter::codec::Engine> (ANNA_FILE_LOCATION))->asXML(result);
- // OAM:
- anna::diameter::comm::OamModule::instantiate().asXML(result);
- anna::diameter::codec::OamModule::instantiate().asXML(result);
- // Statistics:
- anna::statistics::Engine::instantiate().asXML(result);
- return result;
-}
-