1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <anna/diameter.comm/OamModule.hpp>
10 #include <anna/diameter.comm/Engine.hpp>
11 #include <anna/diameter.comm/LocalServer.hpp>
12 #include <anna/diameter.comm/ServerSession.hpp>
13 #include <anna/diameter.comm/ServerSocket.hpp>
14 #include <anna/core/functions.hpp>
15 #include <anna/statistics/Engine.hpp>
16 #include <anna/diameter.comm/TimerManager.hpp>
18 #include <anna/core/tracing/Logger.hpp>
19 #include <anna/core/tracing/TraceMethod.hpp>
20 #include <anna/core/functions.hpp>
21 #include <anna/app/functions.hpp>
22 #include <anna/xml/Node.hpp>
23 #include <anna/comm/Communicator.hpp>
24 #include <anna/comm/Network.hpp>
25 #include <anna/comm/Host.hpp>
26 #include <anna/comm/ClientSocket.hpp>
33 using namespace anna::diameter::comm;
35 LocalServer::LocalServer() :
39 a_currentConnections(0),
40 a_allowedInactivityTime(ServerSession::DefaultAllowedInactivityTime),
46 a_lastUsedResource(NULL) {}
49 void LocalServer::initializeStatisticResources() throw() {
50 std::string accName = "sent from local server '";
51 accName += anna::functions::socketLiteralAsString(a_key.first, a_key.second);
52 accName += "' on origin-realm '";
53 accName += a_engine ? a_engine->getOriginRealm() : "unknown"; // it should be known (createServer)
54 accName += "' and origin-host '";
55 accName += a_engine ? a_engine->getOriginHost() : "unknown"; // it should be known (createServer)
57 a_messageStatistics.initialize(accName);
60 void LocalServer::resetStatistics() throw() {
61 a_messageStatistics.getAccumulator()->reset();
64 void LocalServer::updateProcessingTimeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) throw() {
65 a_messageStatistics.process(MessageStatistics::ConceptType::SentRequestProcessingTime, cid, value);
66 LOGDEBUG(anna::Logger::debug(a_messageStatistics.getAccumulator()->asString(), ANNA_FILE_LOCATION));
69 void LocalServer::updateReceivedMessageSizeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) throw() {
70 a_messageStatistics.process(MessageStatistics::ConceptType::ReceivedMessageSize, cid, value);
71 LOGDEBUG(anna::Logger::debug(a_messageStatistics.getAccumulator()->asString(), ANNA_FILE_LOCATION));
74 ServerSession* LocalServer::allocateServerSession() throw() { return a_serverSessionsRecycler.create(); }
75 void LocalServer::releaseServerSession(ServerSession *serverSession) throw() { a_serverSessionsRecycler.release(serverSession); }
78 LocalServer::serverSession_iterator LocalServer::serverSession_find(const serverSession_key &key) throw() {
79 return a_serverSessions.find(key);
83 LocalServer::serverSession_key LocalServer::getServerSessionKey(const anna::comm::ClientSocket &clientSocket) const throw() {
84 return (anna::functions::exclusiveHash(clientSocket.getRemoteAccessPoint().getINetAddress().serialize()));
88 void LocalServer::availabilityLost() throw() {
90 std::string socket = anna::functions::socketLiteralAsString(a_key.first, a_key.second);
92 std::string msg = "diameter::comm::LocalServer { Socket: ";
94 msg += " } has lost its availability";
95 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
98 OamModule &oamModule = OamModule::instantiate();
99 oamModule.activateAlarm(OamModule::Alarm::c_LostAvailabilityOverLocalServerDefinedAs__s__, socket.c_str());
100 oamModule.count(OamModule::Counter::LostAvailabilityOverLocalServer);
101 a_engine->availabilityLost(this);
102 a_engine->refreshAvailabilityForLocalServers();
106 void LocalServer::availabilityRecovered() throw() {
108 std::string socket = anna::functions::socketLiteralAsString(a_key.first, a_key.second);
110 std::string msg = "diameter::comm::LocalServer { Socket: ";
112 msg += " } has recovered its availability";
113 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
116 OamModule &oamModule = OamModule::instantiate();
117 oamModule.cancelAlarm(OamModule::Alarm::c_LostAvailabilityOverLocalServerDefinedAs__s__, socket.c_str());
118 oamModule.count(OamModule::Counter::RecoveredAvailabilityOverLocalServer);
119 a_engine->availabilityRecovered(this);
120 a_engine->refreshAvailabilityForLocalServers();
125 bool LocalServer::refreshAvailability() throw() {
127 if(a_available) { // check not-bound state for all server-sessions:
128 // bool isolate = true;
130 // for (const_serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++)
131 // if (serverSession(it)->getState() != ServerSession::State::Closed) { isolate = false; break; }
134 // El problema de lo anterior, es que cuando se acepta una conexion, aun no ha llegado el CER (receive). Un server session
135 // esta en estado "Bound" cuando llega dicho CER y consecuentemente envio un CEA. Nos basaremos en 'a_currentConnections':
136 if(a_currentConnections == 0) {
144 // Here not available
145 // for (const_serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++)
146 // if (serverSession(it)->getState() == ServerSession::State::Bound) {
147 if(a_currentConnections > 0) { // really == 0
148 availabilityRecovered();
156 void LocalServer::enable(bool unlock) throw(anna::RuntimeException) {
158 if(unlock) a_lock = false;
162 if(a_serverSocket && a_serverSocket->isOpened()) return; // communicator attach twice gets poll bad file descriptor and application stops !
164 // Resolve local address:
165 anna::comm::Network& network = anna::comm::Network::instantiate();
167 anna::comm::Host *host = network.resolve(a_key.first /* addr */);
168 const anna::comm::Device *device = *(host->device_begin());
169 anna::comm::INetAddress localAddress(device, a_key.second /* port */);
170 // Create server socket and assign receiver factory
171 a_serverSocket = new ServerSocket(localAddress, this);
172 a_serverSocket->setCategory(a_category);
176 void LocalServer::attach() throw() {
178 // Attach to communicator
179 anna::comm::Communicator * communicator = anna::app::functions::component <anna::comm::Communicator> (ANNA_FILE_LOCATION);
180 communicator->attach((anna::comm::ServerSocket*)a_serverSocket); // invokes handler insert and then initialize -> server socket bind (*)
182 OamModule &oamModule = OamModule::instantiate();
183 oamModule.count(OamModule::Counter::ServerSocketsOpened);
184 } catch(anna::RuntimeException& ex) {
185 ex.trace(); // fails on (*) (i.e. Address already in use), within communicator attach
187 a_serverSocket->close();
191 void LocalServer::attachPlanning() throw() {
192 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::LocalServer", "attachPlanning", ANNA_FILE_LOCATION));
195 TimerManager::instantiate().createTimer(this);
196 } catch(anna::RuntimeException& ex) {
198 anna::Logger::error("CAPTURED EXCEPTION activating attachPlanning timer", ANNA_FILE_LOCATION);
203 void LocalServer::disable(bool lock) throw(anna::RuntimeException) {
206 anna::comm::Communicator * communicator = anna::app::functions::component <anna::comm::Communicator> (ANNA_FILE_LOCATION);
207 communicator->detach((anna::comm::ServerSocket*)a_serverSocket);
208 //delete(a_serverSocket);
210 OamModule &oamModule = OamModule::instantiate();
211 oamModule.count(OamModule::Counter::ServerSocketsClosed);
215 void LocalServer::lostConnection() throw() {
216 a_currentConnections--;
221 void LocalServer::newConnection() throw(anna::RuntimeException) {
222 a_currentConnections++;
225 if(a_currentConnections == a_maxConnections) {
226 LOGWARNING(anna::Logger::warning("The maximum number of connections allowed over diameter server socket have already been served", ANNA_FILE_LOCATION));
230 // Inform local server (availability changes):
231 bool changes = refreshAvailability();
233 OamModule &oamModule = OamModule::instantiate();
234 oamModule.count(OamModule::Counter::CreatedConnectionForServerSession);
239 ServerSession *LocalServer::createServerSession(const anna::comm::ClientSocket &clientSocket) throw(anna::RuntimeException) {
240 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::LocalServer", "createServerSession", ANNA_FILE_LOCATION));
241 ServerSession* result(NULL);
242 // First erase deprecated ones:
243 std::vector<const ServerSession*> deprecated_server_sessions;
244 const ServerSession* ss;
246 for(const_serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++) {
247 ss = serverSession(it);
250 deprecated_server_sessions.push_back(ss);
253 std::vector<const ServerSession*>::iterator dc_ncit;
254 std::vector<const ServerSession*>::iterator dc_min(deprecated_server_sessions.begin());
255 std::vector<const ServerSession*>::iterator dc_max(deprecated_server_sessions.end());
256 serverSession_iterator ii;
258 for(dc_ncit = dc_min; dc_ncit != dc_max; dc_ncit++) {
259 ii = serverSession_find((*dc_ncit)->getSocketId());
260 a_serverSessions.erase(ii);
263 // End erase deprecated server sessions
265 if((result = allocateServerSession()) == NULL)
266 throw anna::RuntimeException("diameter::comm::LocalServer::allocateServerSession returns NULL", ANNA_FILE_LOCATION);
269 result->initialize(); // warning: recycler does not initialize its objects and at least...
270 // Assignments (it could be done at allocate):
271 serverSession_key key = getServerSessionKey(clientSocket);
272 result->setAllowedInactivityTime(getAllowedInactivityTime());
273 result->setClientSocket((anna::comm::ClientSocket*)(&clientSocket));
274 result->a_parent = this;
275 result->a_socketId = key; // de momento...
276 result->initializeSequences(); // despu�s de asignar el LocalServer y el socketId (*)
277 // (*) Las secuencias se basan en la semilla: srand(::time(NULL) + anna::functions::exclusiveHash(anna::functions::asString("%s:%d|%d", getAddress().c_str(), getPort(), a_socketId)));
278 result->a_engine = a_engine;
279 a_serverSessions.insert(serverSession_value_type(key, result));
281 a_deliveryIterator = serverSession_begin();
287 void LocalServer::closeServerSession(ServerSession* serverSession)
288 throw(anna::RuntimeException) {
289 if(serverSession == NULL)
293 std::string msg("diameter::comm::LocalServer::closeServerSession | ");
294 msg += serverSession->asString();
295 // msg += " | Destroy: ";
296 // msg += (destroy ? "yes" : "no");
297 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
299 serverSession_iterator ii = serverSession_find(serverSession->getSocketId());
301 if(ii == serverSession_end())
304 // Remove origin-realm / origin-host for server session in delivery map
305 // This is related to http://redmine.teslayout.com/issues/41
306 a_engine->manageDrDhServerSession(serverSession, false /* desregister */);
309 //serverSession->setState(ServerSession::State::Closing); NOT MANAGED WITH SERVER SESSIONS
310 serverSession->unbind(true /* always forceDisconnect on server sessions ... */);
311 releaseServerSession(serverSession);
312 } catch(anna::RuntimeException& ex) {
316 //a_serverSessions.erase(ii); // IMPORTANTE: posible fuente de cores de este tipo, en relacion con ServerSession::finalize() => delete(this)
317 // #0 0x0000003ca1c2e26d in raise () from /lib64/tls/libc.so.6
319 // #0 0x0000003ca1c2e26d in raise () from /lib64/tls/libc.so.6
320 // #1 0x0000003ca1c2fa6e in abort () from /lib64/tls/libc.so.6
321 // #2 0x0000003ca8cb1148 in __gnu_cxx::__verbose_terminate_handler () from /usr/lib64/libstdc++.so.6
322 // #3 0x0000003ca8caf176 in __cxa_call_unexpected () from /usr/lib64/libstdc++.so.6
323 // #4 0x0000003ca8caf1a3 in std::terminate () from /usr/lib64/libstdc++.so.6
324 // #5 0x0000003ca8caf1b6 in std::terminate () from /usr/lib64/libstdc++.so.6
325 // #6 0x0000003ca8caf0c8 in __cxa_call_unexpected () from /usr/lib64/libstdc++.so.6
326 // #7 0x000000000047a4a7 in anna::diameter::comm::LocalServer::lostConnection (this=0x8aeb10) at comm.db/diameter.comm.LocalServer.cc:200
327 // #8 0x000000000047a9e6 in anna::diameter::comm::LocalServer::closeServerSession (this=0x8aeb10, serverSession=0xc37a00)
328 // at comm.db/diameter.comm.LocalServer.cc:275
329 // #9 0x000000000048d288 in anna::diameter::comm::ServerSession::finalize (this=0xc37a00) at comm.db/diameter.comm.ServerSession.cc:510
330 // #10 0x0000000000494e4f in anna::diameter::comm::ServerSessionReceiver::eventBreakLocalConnection (this=0xc119c0, clientSocket=@0xb0ea00)
331 // SOLUCION: no borrar aqui, marcar como "deprecated". Este estado no se necesita realmente puesto que nadie volvera a usar este recurso.
332 // Pero simplemente se podria usar para purgar mediante temporizacion (entonces s� se har�a el erase)
333 serverSession->a_deprecated = true;
334 // WE WILL ERASE AT createServerSession
335 a_deliveryIterator = serverSession_begin();
340 ServerSession* LocalServer::findServerSession(int socketId, anna::Exception::Mode::_v emode)
341 throw(anna::RuntimeException) {
342 serverSession_iterator ii = serverSession_find(socketId);
344 if(ii != serverSession_end())
345 return serverSession(ii);
347 if(emode != anna::Exception::Mode::Ignore) {
348 std::string msg("diameter::comm::LocalServer::findServerSession | SocketId: ");
349 msg += anna::functions::asString(socketId);
350 msg += " | ServerSession not found";
351 anna::RuntimeException ex(msg, ANNA_FILE_LOCATION);
353 if(emode == anna::Exception::Mode::Throw)
362 ServerSession* LocalServer::findServerSession(const anna::comm::ClientSocket &clientSocket, anna::Exception::Mode::_v emode)
363 throw(anna::RuntimeException) {
364 return findServerSession(getServerSessionKey(clientSocket), emode);
368 int LocalServer::getOTARequests() const throw() {
371 for(const_serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++)
372 result += serverSession(it)->getOTARequests();
377 void LocalServer::close() throw(anna::RuntimeException) {
378 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::LocalServer", "close", ANNA_FILE_LOCATION));
379 // Close listener (permanently to avoid reopening when local connections are being deleted):
380 disable(true /* lock */);
382 for(serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++)
383 closeServerSession(serverSession(it));
386 void LocalServer::setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) throw() {
387 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::LocalServer", "setClassCodeTimeout", ANNA_FILE_LOCATION));
389 for(serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++) {
391 serverSession(it)->setClassCodeTimeout(v, millisecond);
392 } catch(anna::RuntimeException &ex) {
398 void LocalServer::setMaxConnections(int maxConnections) throw(anna::RuntimeException) {
399 LOGMETHOD(anna::TraceMethod tttm("anna::diameter::comm::LocalServer", "setMaxConnections", ANNA_FILE_LOCATION));
401 // Negative & initial
402 if(maxConnections < 0) {
403 LOGDEBUG(anna::Logger::debug("Provided negative value means no limit accepting connections over server socket. Opening listen port (if closed)...", ANNA_FILE_LOCATION));
404 a_maxConnections = -1;
410 if(maxConnections == a_maxConnections) {
411 LOGDEBUG(anna::Logger::debug("Provided equal to current. Ignore operation", ANNA_FILE_LOCATION));
416 if(maxConnections < a_currentConnections) {
417 std::string msg = "There are more current connections (";
418 msg += anna::functions::entriesAsString(a_currentConnections);
419 msg += ") than provided maximum (";
420 msg += anna::functions::entriesAsString(maxConnections);
421 msg += "). Command rejected (you should release connections before logical limitation)";
422 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
426 if(maxConnections > a_currentConnections) {
427 LOGDEBUG(anna::Logger::debug("Increasing connection margin (new limit is greater than current connections). Opening listen port (if closed)...", ANNA_FILE_LOCATION));
429 } else { // maxConnections == a_currentConnections: listen port must be closed if it is opened
432 if(maxConnections == 0)
433 anna::Logger::debug("Provided zero value means disabling diameter server", ANNA_FILE_LOCATION);
434 anna::Logger::debug("Zeroing connections margin (new limit is equal to current connections). Closing listen port (if opened)...", ANNA_FILE_LOCATION);
442 std::string msg("Updating max connections from ");
443 msg += (a_maxConnections == -1) ? "'no limit'" : anna::functions::asString(a_maxConnections);
445 msg += (maxConnections == -1) ? "'no limit'" : anna::functions::asString(maxConnections);
446 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
449 a_maxConnections = maxConnections;
453 bool LocalServer::send(const Message* message, int socketId) throw(anna::RuntimeException) {
454 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::LocalServer", "send", ANNA_FILE_LOCATION));
458 std::string msg = "The local server ";
460 if(a_description != "") {
462 msg += a_description;
465 msg += "is currently unavailable (no server sessions to send the message)";
466 anna::Logger::warning(msg, ANNA_FILE_LOCATION);
471 if(socketId != -1) { // socket id provided
472 // Send (it was a request because of key (socketId) != -1, we forward the answer):
474 ServerSession * fixedServerSession = a_engine->findServerSession(socketId); // exception if not found
475 fixedServerSession->send(message);
477 } catch(anna::RuntimeException &ex) {
478 std::string msg = "Cannot deliver answer through a fixed server session (socket id ";
479 msg += anna::functions::asString(socketId);
480 msg += "). Perhaps it existed but not now. Ignore";
481 anna::Logger::error(msg, ANNA_FILE_LOCATION);
487 // Socket is not provided: use readSocketId
488 socketId = (a_currentConnections > 1) ? readSocketId(message) : -1; // optimization
490 if(a_deliveryIterator == serverSession_end()) a_deliveryIterator = serverSession_begin();
492 a_lastUsedResource = (*a_deliveryIterator).second;
495 a_lastUsedResource = findServerSession(socketId); // exception if not found
496 } else { // Round Robin delivery between client-sessions
497 if(getCurrentConnections() != 1) { // optimize
498 // Next server-session:
499 a_deliveryIterator++;
505 const Response* response = a_lastUsedResource->send(message);
506 return true; // no matter if response is NULL (answers, i.e.) or not.
507 } catch(anna::RuntimeException &ex) {
511 // Here, sent has failed:
513 OamModule &oamModule = OamModule::instantiate();
514 std::string socket = anna::functions::socketLiteralAsString(getKey().first, getKey().second);
515 oamModule.activateAlarm(OamModule::Alarm::UnableToDeliverDiameterMessageToClientFromLocalServer__s__, socket.c_str());
516 oamModule.count(OamModule::Counter::UnableToDeliverToClient);
521 bool LocalServer::broadcast(const Message* message) throw(anna::RuntimeException) {
522 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::LocalServer", "broadcast", ANNA_FILE_LOCATION));
523 const Response* response;
526 for(serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++) {
528 response = serverSession(it)->send(message);
529 } catch(anna::RuntimeException &ex) {
538 void LocalServer::eventPeerShutdown(const ServerSession* serverSession) throw() {
540 std::string msg(serverSession->asString());
541 msg += " | eventPeerShutdown";
542 anna::Logger::warning(msg, ANNA_FILE_LOCATION);
546 void LocalServer::eventRequestRetransmission(const ServerSession* serverSession, Message *request) throw() {
548 std::string msg(serverSession->asString());
549 msg += " | eventRequestRetransmission";
550 anna::Logger::warning(msg, ANNA_FILE_LOCATION);
554 std::string LocalServer::asString() const throw() {
555 std::string result("diameter::comm::LocalServer { ");
556 result += "Description: ";
557 result += (a_description != "") ? a_description : "undefined";
558 result += " | Available (any server session bound): ";
559 result += a_available ? "yes" : "no";
560 result += " | Max Connections: ";
561 result += anna::functions::asString(a_maxConnections);
562 result += " | Current Connections: ";
563 result += anna::functions::asString(a_currentConnections);
564 // Current connections ??
565 result += " | Allowed inactivity time for server sessions: ";
566 result += a_allowedInactivityTime.asString();
567 result += " | Server socket: ";
568 result += a_serverSocket ? a_serverSocket->asString() : "closed";
569 result += anna::functions::asString(" | OTA requests: %d%s", getOTARequests(), idle() ? " (idle)" : "");
570 result += " | Last Incoming Activity Time: ";
571 result += a_lastIncomingActivityTime.asString();
572 result += " | Last Outgoing Activity Time: ";
573 result += a_lastOutgoingActivityTime.asString();
575 // result += a_messageStatistics.getAccumulator()->asString();
576 // ServerSessions only in xml
577 return result += " }";
581 anna::xml::Node* LocalServer::asXML(anna::xml::Node* parent) const throw() {
582 anna::xml::Node* result = parent->createChild("diameter.LocalServer");
583 result->createAttribute("Description", (a_description != "") ? a_description : "undefined");
584 result->createAttribute("Available", a_available ? "yes" : "no");
585 result->createAttribute("MaxConnections", a_maxConnections);
586 result->createAttribute("CurrentConnections", a_currentConnections);
587 // Current connections ??
588 result->createAttribute("AllowedInactivityTimeForServerSessions", a_allowedInactivityTime.asString());
591 a_serverSocket->asXML(result);
593 result->createAttribute("ServerSocket", "closed");
595 result->createAttribute("OTArequests", anna::functions::asString("%d%s", getOTARequests(), idle() ? " (idle)" : ""));
596 result->createAttribute("LastIncomingActivityTime", a_lastIncomingActivityTime.asString());
597 result->createAttribute("LastOutgoingActivityTime", a_lastOutgoingActivityTime.asString());
599 anna::xml::Node* stats = result->createChild("MessageStatistics");
600 a_messageStatistics.getAccumulator()->asXML(stats);
601 anna::xml::Node* serverSessions = result->createChild("ServerSessions"); // LocalServer.ServerSessions
603 for(const_serverSession_iterator it = serverSession_begin(); it != serverSession_end(); it++)
604 serverSession(it)->asXML(serverSessions);
609 //------------------------------------------------------------------------------
610 //------------------------------------ LocalServer::updateIncomingActivityTime()
611 //------------------------------------------------------------------------------
612 void LocalServer::updateIncomingActivityTime() throw() {
613 a_lastIncomingActivityTime = anna::functions::millisecond();
616 std::string msg = "Updated INCOMING activity on local server (milliseconds unix): ";
617 msg += anna::functions::asString(a_lastIncomingActivityTime.getValue());
618 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
623 //------------------------------------------------------------------------------
624 //------------------------------------ LocalServer::updateOutgoingActivityTime()
625 //------------------------------------------------------------------------------
626 void LocalServer::updateOutgoingActivityTime(void) throw() {
627 a_lastOutgoingActivityTime = anna::functions::millisecond();
630 std::string msg = "Updated OUTGOING activity on local server (milliseconds unix): ";
631 msg += anna::functions::asString(a_lastOutgoingActivityTime.getValue());
632 anna::Logger::debug(msg, ANNA_FILE_LOCATION);