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_lastUsedResource = NULL;
42 void Server::initializeStatisticResources() throw() {
43 std::string accName = "remote server '";
44 accName += anna::functions::socketLiteralAsString(a_socket.first, a_socket.second);
45 accName += "' from origin-realm '";
46 accName += a_engine ? a_engine->getOriginRealmName() : "unknown"; // it should be known (createServer)
47 accName += "' and origin-host '";
48 accName += a_engine ? a_engine->getOriginHostName() : "unknown"; // it should be known (createServer)
50 a_messageStatistics.initialize(accName);
53 void Server::resetStatistics() throw() {
54 a_messageStatistics.getAccumulator()->reset();
57 void Server::updateProcessingTimeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) throw() {
58 a_messageStatistics.process(MessageStatistics::ConceptType::SentRequestProcessingTime, cid, value);
59 LOGDEBUG(anna::Logger::debug(a_messageStatistics.getAccumulator()->asString(), ANNA_FILE_LOCATION));
62 void Server::updateReceivedMessageSizeStatisticConcept(const double &value, const anna::diameter::CommandId &cid) throw() {
63 a_messageStatistics.process(MessageStatistics::ConceptType::ReceivedMessageSize, cid, value);
64 LOGDEBUG(anna::Logger::debug(a_messageStatistics.getAccumulator()->asString(), ANNA_FILE_LOCATION));
68 void Server::assertReady() throw(anna::RuntimeException) {
69 if(a_clientSessions.size() != a_maxClientSessions) {
70 std::string msg(asString());
71 msg += " | Non-configured server: you must add the remaining client-sessions before any operation (bind, send, etc.)";
72 throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
77 void Server::addClientSession(int socketId)
78 throw(anna::RuntimeException) {
79 if(a_clientSessions.size() == a_maxClientSessions) {
82 std::string msg = "Maximum number of client-sessions reached for this server (";
83 msg += anna::functions::asString(a_maxClientSessions);
85 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
91 throw anna::RuntimeException("Invalid engine reference (NULL)", ANNA_FILE_LOCATION);
93 ClientSession *s = a_engine->createClientSession(this, socketId);
94 a_clientSessions.push_back(s);
97 int Server::getOTARequests() const throw() {
100 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
101 result += (*it)->getOTARequests();
107 bool Server::send(const Message* message, int socketId) throw(anna::RuntimeException) {
108 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "send", ANNA_FILE_LOCATION));
110 bool fixedSocket = (socketId != -1);
111 int clientSessions = getNumberOfClientSessions();
113 for(int k = 0; k < clientSessions; k++) { // try round-robin only over one cycle,
114 // no matter where you are: don't repeat same socket
116 a_lastUsedResource = a_engine->findClientSession(a_socket.first /*ip*/, a_socket.second /*port*/, socketId); // exception if not found
118 if(a_deliveryIterator == end()) a_deliveryIterator = begin();
120 a_lastUsedResource = (*a_deliveryIterator);
122 if(clientSessions > 1) a_deliveryIterator++; // Round robin
127 a_lastUsedResource->send(message);
128 return true; // no matter if response is NULL (answers, i.e.) or not.
129 } catch(anna::RuntimeException &ex) {
131 std::string msg = ex.getText();
132 msg += " | Send failed on socket id ";
133 msg += anna::functions::asString(a_lastUsedResource->getSocketId());
134 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
138 // Specific socket sending doesn't try
139 if(fixedSocket) return false;
146 bool Server::broadcast(const Message* message) throw(anna::RuntimeException) {
147 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "broadcast", ANNA_FILE_LOCATION));
151 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++) {
153 (*it)->send(message);
154 } catch(anna::RuntimeException &ex) {
164 bool Server::bind() throw(anna::RuntimeException) {
165 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "bind", ANNA_FILE_LOCATION));
167 a_deliveryIterator = begin();
168 bool result = true; // all OK return
170 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++) {
173 } catch(anna::RuntimeException &ex) {
182 void Server::raiseAutoRecovery(bool autoRecovery) throw(anna::RuntimeException) {
183 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "raiseAutoRecovery", ANNA_FILE_LOCATION));
185 a_deliveryIterator = begin();
187 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
188 (*it)->setAutoRecovery(autoRecovery);
191 void Server::setClassCodeTimeout(const ClassCode::_v v, const anna::Millisecond & millisecond) throw() {
192 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "setClassCodeTimeout", ANNA_FILE_LOCATION));
195 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++) {
197 (*it)->setClassCodeTimeout(v, millisecond);
198 } catch(anna::RuntimeException &ex) {
205 // Private close/destroy method
206 void Server::close(bool destroy) throw(anna::RuntimeException) {
207 LOGMETHOD(anna::TraceMethod tttm("diameter::comm::Server", "close", ANNA_FILE_LOCATION));
210 throw anna::RuntimeException("Invalid engine reference (NULL)", ANNA_FILE_LOCATION);
214 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
215 a_engine->closeClientSession(*it, destroy);
219 void Server::childIdle() const throw() {
220 // Check father entity idleness:
221 if(idle()) a_parent->childIdle();
225 void Server::hide() throw() {
226 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
230 void Server::show() throw() {
231 for(std::vector<ClientSession*>::iterator it = begin(); it != end(); it++)
235 bool Server::hidden() const throw() {
236 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
237 if((*it)->shown()) return false;
241 bool Server::shown() const throw() {
242 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
243 if((*it)->hidden()) return false;
250 std::string Server::socketAsString() const throw() {
251 std::string result = getAddress();
253 result += anna::functions::asString(getPort());
258 std::string Server::asString() const throw() {
259 std::string result("diameter::comm::Server { ");
260 result += " | Parent Entity: ";
261 result += a_parent->getDescription();
262 result += " | Server Address: ";
263 result += a_socket.first;
264 result += " | Server Port: ";
265 result += anna::functions::asString(a_socket.second);
266 result += " | Available: ";
267 result += a_available ? "yes" : "no";
268 result += " | Max client-sessions supported: ";
269 result += anna::functions::asString(a_maxClientSessions);
270 result += " | Currently configured client-sessions: ";
271 result += anna::functions::asString(a_clientSessions.size());
272 result += anna::functions::asString(" | OTA requests: %d%s", getOTARequests(), idle() ? " (idle)" : "");
273 result += " | Last Incoming Activity Time: ";
274 result += a_lastIncomingActivityTime.asString();
275 result += " | Last Outgoing Activity Time: ";
276 result += a_lastOutgoingActivityTime.asString();
277 result += " | Hidden: ";
278 result += (hidden() ? "yes" : "no");
280 result += a_messageStatistics.getAccumulator()->asString();
282 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++) {
284 result += (*it)->asString();
290 anna::xml::Node* Server::asXML(anna::xml::Node* parent) const throw() {
291 anna::xml::Node* result = parent->createChild("diameter.Server");
292 result->createAttribute("ParentEntity", a_parent->getDescription());
293 result->createAttribute("ServerAddress", a_socket.first);
294 result->createAttribute("ServerPort", anna::functions::asString(a_socket.second));
295 result->createAttribute("Available", a_available ? "yes" : "no");
296 result->createAttribute("MaxClientSessionsSupported", anna::functions::asString(a_maxClientSessions));
297 result->createAttribute("CurrentlyConfiguredClientSessions", anna::functions::asString(a_clientSessions.size()));
298 result->createAttribute("OTArequests", anna::functions::asString("%d%s", getOTARequests(), idle() ? " (idle)" : ""));
299 result->createAttribute("LastIncomingActivityTime", a_lastIncomingActivityTime.asString());
300 result->createAttribute("LastOutgoingActivityTime", a_lastOutgoingActivityTime.asString());
301 result->createAttribute("Hidden", hidden() ? "yes" : "no");
303 anna::xml::Node* stats = result->createChild("Statistics");
304 a_messageStatistics.getAccumulator()->asXML(stats);
305 anna::xml::Node* clientSessions = result->createChild("Server.ClientSessions");
307 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
308 (*it)->asXML(clientSessions);
314 void Server::eventPeerShutdown(const ClientSession *clientSession) throw() {
315 // Inform father entity:
316 a_parent->eventPeerShutdown(clientSession);
319 void Server::eventRequestRetransmission(const ClientSession* clientSession, Message *request) throw() {
320 // Inform father entity:
321 a_parent->eventRequestRetransmission(clientSession, request);
324 void Server::eventResponse(const Response& response) throw(anna::RuntimeException) {
325 // Inform father entity:
326 a_parent->eventResponse(response);
329 void Server::eventRequest(ClientSession *clientSession, const anna::DataBlock & request) throw(anna::RuntimeException) {
330 // Inform father entity:
331 a_parent->eventRequest(clientSession, request);
334 void Server::eventUnknownResponse(ClientSession *clientSession, const anna::DataBlock & response) throw(anna::RuntimeException) {
335 // Inform father entity:
336 a_parent->eventUnknownResponse(clientSession, response);
339 void Server::eventDPA(ClientSession *clientSession, const anna::DataBlock & response) throw(anna::RuntimeException) {
340 // Inform father entity:
341 a_parent->eventDPA(clientSession, response);
345 void Server::availabilityLost() throw() {
347 std::string socket = anna::functions::socketLiteralAsString(a_socket.first, a_socket.second);
349 std::string msg = "diameter::comm::Server { Socket: ";
351 msg += " } has lost its availability";
352 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
355 OamModule &oamModule = OamModule::instantiate();
356 oamModule.activateAlarm(OamModule::Alarm::c_LostAvailabilityOverServerDefinedAs__s__, socket.c_str());
357 oamModule.count(OamModule::Counter::LostAvailabilityOverServer);
358 a_engine->availabilityLost(this);
359 a_parent->refreshAvailability();
363 void Server::availabilityRecovered() throw() {
365 std::string socket = anna::functions::socketLiteralAsString(a_socket.first, a_socket.second);
367 std::string msg = "diameter::comm::Server { Socket: ";
369 msg += " } has recovered its availability";
370 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
373 OamModule &oamModule = OamModule::instantiate();
374 oamModule.cancelAlarm(OamModule::Alarm::c_LostAvailabilityOverServerDefinedAs__s__, socket.c_str());
375 oamModule.count(OamModule::Counter::RecoveredAvailabilityOverServer);
376 a_engine->availabilityRecovered(this);
377 a_parent->refreshAvailability();
382 bool Server::refreshAvailability() throw() {
384 if(a_available) { // check not-bound state for all client-sessions:
387 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
389 //if ((*it)->getState() == ClientSession::State::Bound) { isolate = false; break; }
390 if((*it)->getState() != ClientSession::State::Closed) { isolate = false; break; }
400 // Here not available
401 for(std::vector<ClientSession*>::const_iterator it = begin(); it != end(); it++)
402 if((*it)->getState() == ClientSession::State::Bound) {
403 availabilityRecovered();
411 //------------------------------------------------------------------------------
412 //---------------------------------------- Server::updateIncomingActivityTime()
413 //------------------------------------------------------------------------------
414 void Server::updateIncomingActivityTime() throw() {
415 a_lastIncomingActivityTime = anna::functions::millisecond();
418 std::string msg = "Updated INCOMING activity on server (milliseconds unix): ";
419 msg += anna::functions::asString(a_lastIncomingActivityTime.getValue());
420 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
422 a_parent->updateIncomingActivityTime();
426 //------------------------------------------------------------------------------
427 //---------------------------------------- Server::updateOutgoingActivityTime()
428 //------------------------------------------------------------------------------
429 void Server::updateOutgoingActivityTime(void) throw() {
430 a_lastOutgoingActivityTime = anna::functions::millisecond();
433 std::string msg = "Updated OUTGOING activity on server (milliseconds unix): "; msg += anna::functions::asString(a_lastOutgoingActivityTime.getValue());
434 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
436 a_parent->updateOutgoingActivityTime();