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/ClientSession.hpp>
10 #include <anna/diameter.comm/Entity.hpp>
11 #include <anna/diameter.comm/Engine.hpp>
12 #include <anna/diameter.comm/OamModule.hpp>
13 #include <anna/diameter/codec/functions.hpp>
14 #include <anna/diameter/helpers/base/defines.hpp>
15 #include <anna/core/functions.hpp>
16 #include <anna/statistics/Engine.hpp>
17 #include <anna/time/functions.hpp>
19 #include <anna/core/tracing/Logger.hpp>
20 #include <anna/core/functions.hpp>
21 #include <anna/core/tracing/TraceMethod.hpp>
27 using namespace anna::diameter;
28 using namespace anna::diameter::comm;
31 void Server::initialize() throw() {
34 a_clientSessions.clear(); // importante (el recycler creo que no lo tocaba)
36 a_maxClientSessions = 1; // mono client connection
37 a_lastIncomingActivityTime = (anna::Millisecond)0;
38 a_lastOutgoingActivityTime = (anna::Millisecond)0;
39 a_statisticsAccumulator.reset();
40 a_lastUsedResource = NULL;
43 void Server::initializeStatisticConcepts() throw() {
45 anna::statistics::Engine& statsEngine = anna::statistics::Engine::instantiate();
46 // Concepts descriptions:
47 std::string serverAsString = anna::functions::socketLiteralAsString(a_socket.first, a_socket.second);
48 std::string c1desc = "Diameter processing time (for requests) at servers on "; c1desc += serverAsString;
49 std::string c2desc = "Diameter message sizes received from servers on "; c2desc += serverAsString;
51 a_processing_time__StatisticConceptId = statsEngine.addConcept(c1desc.c_str(), "ms", true/* integer values */);
52 a_received_message_size__StatisticConceptId = statsEngine.addConcept(c2desc.c_str(), "bytes", true/* integer values */);
55 void Server::resetStatistics() throw() {
56 a_statisticsAccumulator.reset();
59 void Server::updateProcessingTimeStatisticConcept(const double &value) throw() {
60 a_statisticsAccumulator.process(a_processing_time__StatisticConceptId, value);
61 LOGDEBUG(anna::Logger::debug(a_statisticsAccumulator.asString(), ANNA_FILE_LOCATION));
64 void Server::updateReceivedMessageSizeStatisticConcept(const double &value) throw() {
65 a_statisticsAccumulator.process(a_received_message_size__StatisticConceptId, value);
66 //LOGDEBUG(anna::Logger::debug(a_statisticsAccumulator.asString(), ANNA_FILE_LOCATION));
70 void Server::assertReady() throw(anna::RuntimeException) {
71 if(a_clientSessions.size() != a_maxClientSessions) {
72 std::string msg(asString());
73 msg += " | Non-configured server: you must add the remaining client-sessions before any operation (bind, send, etc.)";
74 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
79 void Server::addClientSession(int socketId)
80 throw(anna::RuntimeException) {
81 if(a_clientSessions.size() == a_maxClientSessions) {
84 std::string msg = "Maximum number of client-sessions reached for this server (";
85 msg += anna::functions::asString(a_maxClientSessions);
87 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
93 throw anna::RuntimeException("Invalid engine reference (NULL)", ANNA_FILE_LOCATION);
95 ClientSession *s = a_engine->createClientSession(this, socketId);
96 a_clientSessions.push_back(s);
99 int Server::getOTARequests() const throw() {
102 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
103 result += (*it)->getOTARequests();
109 bool Server::send(const Message* message, int socketId) throw(anna::RuntimeException) {
110 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "send", ANNA_FILE_LOCATION));
112 bool fixedSocket = (socketId != -1);
113 int clientSessions = getNumberOfClientSessions();
115 for(int k = 0; k < clientSessions; k++) { // try round-robin only over one cycle,
116 // no matter where you are: don't repeat same socket
118 a_lastUsedResource = a_engine->findClientSession(a_socket.first /*ip*/, a_socket.second /*port*/, socketId); // exception if not found
120 if(a_deliveryIterator == end()) a_deliveryIterator = begin();
122 a_lastUsedResource = (*a_deliveryIterator);
124 if(clientSessions > 1) a_deliveryIterator++; // Round robin
129 const Response* response = a_lastUsedResource->send(message);
130 return true; // no matter if response is NULL (answers, i.e.) or not.
131 } catch(anna::RuntimeException &ex) {
133 std::string msg = ex.getText();
134 msg += " | Send failed on socket id ";
135 msg += anna::functions::asString(a_lastUsedResource->getSocketId());
136 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
140 // Specific socket sending doesn't try
141 if(fixedSocket) return false;
148 bool Server::broadcast(const Message* message) throw(anna::RuntimeException) {
149 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "broadcast", ANNA_FILE_LOCATION));
151 const Response* response;
154 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++) {
156 response = (*it)->send(message);
157 } catch(anna::RuntimeException &ex) {
167 bool Server::bind() throw(anna::RuntimeException) {
168 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "bind", ANNA_FILE_LOCATION));
170 a_deliveryIterator = begin();
171 bool result = true; // all OK return
173 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++) {
176 } catch(anna::RuntimeException &ex) {
185 void Server::raiseAutoRecovery(bool autoRecovery) throw(anna::RuntimeException) {
186 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "raiseAutoRecovery", ANNA_FILE_LOCATION));
188 a_deliveryIterator = begin();
190 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
191 (*it)->setAutoRecovery(autoRecovery);
194 void Server::setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) throw() {
195 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "setClassCodeTimeout", ANNA_FILE_LOCATION));
198 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++) {
200 (*it)->setClassCodeTimeout(v, millisecond);
201 } catch(anna::RuntimeException &ex) {
208 // Private close/destroy method
209 void Server::close(bool destroy) throw(anna::RuntimeException) {
210 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "close", ANNA_FILE_LOCATION));
213 throw anna::RuntimeException("Invalid engine reference (NULL)", ANNA_FILE_LOCATION);
217 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
218 a_engine->closeClientSession(*it, destroy);
222 void Server::childIdle() const throw() {
223 // Check father entity idleness:
224 if(idle()) a_parent->childIdle();
228 void Server::hide() throw() {
229 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
233 void Server::show() throw() {
234 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
238 bool Server::hidden() const throw() {
239 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
240 if((*it)->shown()) return false;
244 bool Server::shown() const throw() {
245 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
246 if((*it)->hidden()) return false;
253 std::string Server::socketAsString() const throw() {
254 std::string result = getAddress();
256 result += anna::functions::asString(getPort());
261 std::string Server::asString() const throw() {
262 std::string result("diameter::comm::Server { ");
263 result += " | Parent Entity: ";
264 result += a_parent->getDescription();
265 result += " | Server Address: ";
266 result += a_socket.first;
267 result += " | Server Port: ";
268 result += anna::functions::asString(a_socket.second);
269 result += " | Available: ";
270 result += a_available ? "yes" : "no";
271 result += " | Max client-sessions supported: ";
272 result += anna::functions::asString(a_maxClientSessions);
273 result += " | Currently configured client-sessions: ";
274 result += anna::functions::asString(a_clientSessions.size());
275 result += anna::functions::asString(" | OTA requests: %d%s", getOTARequests(), idle() ? " (idle)" : "");
276 result += " | Last Incoming Activity Time: ";
277 result += a_lastIncomingActivityTime.asString();
278 result += " | Last Outgoing Activity Time: ";
279 result += a_lastOutgoingActivityTime.asString();
280 result += " | Hidden: ";
281 result += (hidden() ? "yes" : "no");
283 result += a_statisticsAccumulator.asString();
285 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++) {
287 result += (*it)->asString();
293 anna::xml::Node* Server::asXML(anna::xml::Node* parent) const throw() {
294 anna::xml::Node* result = parent->createChild("diameter.Server");
295 result->createAttribute("ParentEntity", a_parent->getDescription());
296 result->createAttribute("ServerAddress", a_socket.first);
297 result->createAttribute("ServerPort", anna::functions::asString(a_socket.second));
298 result->createAttribute("Available", a_available ? "yes" : "no");
299 result->createAttribute("MaxClientSessionsSupported", anna::functions::asString(a_maxClientSessions));
300 result->createAttribute("CurrentlyConfiguredClientSessions", anna::functions::asString(a_clientSessions.size()));
301 result->createAttribute("OTArequests", anna::functions::asString("%d%s", getOTARequests(), idle() ? " (idle)" : ""));
302 result->createAttribute("LastIncomingActivityTime", a_lastIncomingActivityTime.asString());
303 result->createAttribute("LastOutgoingActivityTime", a_lastOutgoingActivityTime.asString());
304 result->createAttribute("Hidden", hidden() ? "yes" : "no");
306 anna::xml::Node* stats = result->createChild("Statistics");
307 a_statisticsAccumulator.asXML(stats);
308 anna::xml::Node* clientSessions = result->createChild("Server.ClientSessions");
310 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
311 (*it)->asXML(clientSessions);
317 void Server::eventPeerShutdown(const ClientSession *clientSession) throw() {
318 // Inform father server:
319 a_parent->eventPeerShutdown(clientSession);
322 void Server::eventResponse(const Response& response) throw(anna::RuntimeException) {
323 // Inform father server:
324 a_parent->eventResponse(response);
327 void Server::eventRequest(ClientSession *clientSession, const anna::DataBlock & request) throw(anna::RuntimeException) {
328 // Inform father server:
329 a_parent->eventRequest(clientSession, request);
332 void Server::eventUnknownResponse(ClientSession *clientSession, const anna::DataBlock & response) throw(anna::RuntimeException) {
333 // Inform father server:
334 a_parent->eventUnknownResponse(clientSession, response);
337 void Server::eventDPA(ClientSession *clientSession, const anna::DataBlock & response) throw(anna::RuntimeException) {
338 // Inform father server:
339 a_parent->eventDPA(clientSession, response);
343 void Server::availabilityLost() throw() {
345 std::string socket = anna::functions::socketLiteralAsString(a_socket.first, a_socket.second);
347 std::string msg = "diameter::comm::Server { Socket: ";
349 msg += " } has lost its availability";
350 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
353 OamModule &oamModule = OamModule::instantiate();
354 oamModule.activateAlarm(OamModule::Alarm::c_LostAvailabilityOverServerDefinedAs__s__, socket.c_str());
355 oamModule.count(OamModule::Counter::LostAvailabilityOverServer);
356 a_engine->availabilityLost(this);
357 a_parent->refreshAvailability();
361 void Server::availabilityRecovered() throw() {
363 std::string socket = anna::functions::socketLiteralAsString(a_socket.first, a_socket.second);
365 std::string msg = "diameter::comm::Server { Socket: ";
367 msg += " } has recovered its availability";
368 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
371 OamModule &oamModule = OamModule::instantiate();
372 oamModule.cancelAlarm(OamModule::Alarm::c_LostAvailabilityOverServerDefinedAs__s__, socket.c_str());
373 oamModule.count(OamModule::Counter::RecoveredAvailabilityOverServer);
374 a_engine->availabilityRecovered(this);
375 a_parent->refreshAvailability();
380 bool Server::refreshAvailability() throw() {
382 if(a_available) { // check not-bound state for all client-sessions:
385 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
387 //if ((*it)->getState() == ClientSession::State::Bound) { isolate = false; break; }
388 if((*it)->getState() != ClientSession::State::Closed) { isolate = false; break; }
398 // Here not available
399 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
400 if((*it)->getState() == ClientSession::State::Bound) {
401 availabilityRecovered();
409 //------------------------------------------------------------------------------
410 //---------------------------------------- Server::updateIncomingActivityTime()
411 //------------------------------------------------------------------------------
412 void Server::updateIncomingActivityTime() throw() {
413 a_lastIncomingActivityTime = anna::functions::millisecond();
416 std::string msg = "Updated INCOMING activity on server (milliseconds unix): ";
417 msg += anna::functions::asString(a_lastIncomingActivityTime.getValue());
418 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
420 a_parent->updateIncomingActivityTime();
424 //------------------------------------------------------------------------------
425 //---------------------------------------- Server::updateOutgoingActivityTime()
426 //------------------------------------------------------------------------------
427 void Server::updateOutgoingActivityTime(void) throw() {
428 a_lastOutgoingActivityTime = anna::functions::millisecond();
431 std::string msg = "Updated OUTGOING activity on server (milliseconds unix): "; msg += anna::functions::asString(a_lastOutgoingActivityTime.getValue());
432 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
434 a_parent->updateOutgoingActivityTime();