-// ANNA - Anna is Not 'N' Anymore
-//
-// (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
-//
-// https://bitbucket.org/testillano/anna
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions
-// are met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-//
-// Authors: eduardo.ramos.testillano@gmail.com
-// cisco.tierra@gmail.com
-
-
-#include <anna/core/tracing/TraceMethod.hpp>
-#include <anna/core/tracing/Logger.hpp>
-
-#include <anna/xml/Node.hpp>
-#include <anna/xml/Attribute.hpp>
-
-#include <anna/timex/Engine.hpp>
-
-#include <anna/core/oam/CounterManager.hpp>
-#include <anna/core/oam/CounterRecorder.hpp>
-#include <anna/core/oam/CounterSummarizer.hpp>
-
-
-using namespace std;
-using namespace anna;
-
-oam::CounterManager::CounterManager() :
- a_timer(*this),
- a_timeController(NULL),
- a_counterRecorder(NULL),
- a_counterSummarizer(NULL),
- a_recording(false) {
- anna_memset(a_scopes, 0, sizeof(a_scopes));
-}
-
-oam::CounterManager::~CounterManager() {
- for(register int i = 0; i < MaxScope; i ++)
- delete a_scopes [i];
-}
-
-oam::CounterScope& oam::CounterManager::create(const int scope, const char* name, bool reuse)
-throw(RuntimeException) {
- if(scope >= MaxScope) {
- string msg(CounterScope(*this, scope, name).asString());
- msg += " | Out of range";
- throw RuntimeException(msg, ANNA_FILE_LOCATION);
- }
-
- bool exists = (a_scopes [scope] != NULL);
-
- if(exists) {
- string msg(CounterScope(*this, scope, name).asString());
- msg += " | Id was already registered as ";
- msg += a_scopes [scope]->asString();
-
- if(!reuse)
- throw RuntimeException(msg, ANNA_FILE_LOCATION);
-
- LOGWARNING(Logger::warning(msg, ANNA_FILE_LOCATION));
- } else
- a_scopes [scope] = new CounterScope(*this, scope, name);
-
- return *(a_scopes [scope]);
-}
-
-oam::CounterScope& oam::CounterManager::find(const int scope)
-throw(RuntimeException) {
- if(scope >= MaxScope) {
- string msg(CounterScope(*this, scope, "(none)").asString());
- msg += " | Out of range";
- throw RuntimeException(msg, ANNA_FILE_LOCATION);
- }
-
- if(a_scopes [scope] == NULL) {
- string msg(CounterScope(*this, scope, "(none)").asString());
- msg += " | Was not created";
- throw RuntimeException(msg, ANNA_FILE_LOCATION);
- }
-
- return *(a_scopes [scope]);
-}
-
-const oam::CounterScope& oam::CounterManager::find(const int scope) const
-throw(RuntimeException) {
- const CounterScope& result = const_cast <oam::CounterManager*>(this)->find(scope);
- return result;
-}
-
-
-// Se invoca desde oam::CounterScope
-void oam::CounterManager::activateTimer()
-throw(RuntimeException) {
- static bool warning = false;
-
- if(a_timeController == NULL) {
- if(warning == false) {
- LOGWARNING(
- Logger::write(
- Logger::Warning,
- "anna::oam::CounterManager has no time manager associated. Counters won't be dumped",
- ANNA_FILE_LOCATION
- );
- )
- warning = true;
- }
-
- return;
- }
-
- if(a_counterRecorder == NULL) {
- LOGWARNING(
- Logger::write(
- Logger::Warning,
- "anna::oam::CounterManager has no counter recorder associated. Ignore record operation.",
- ANNA_FILE_LOCATION
- );
- )
- return;
- }
-
- if(a_timer.isActive() == false && a_recording == false) {
- try {
- a_timeController->activate(a_timer);
- } catch(RuntimeException& ex) {
- ex.trace();
- }
- }
-}
-
-// Al invocar a este método el timex::Engine tiene activa una SSCC
-void oam::CounterManager::record()
-throw(RuntimeException) {
- LOGMETHOD(TraceMethod traceMethod("anna::oam::CounterManager", "record", ANNA_FILE_LOCATION));
-
- if(a_counterRecorder == NULL)
- throw RuntimeException("anna::oam::CounterManager has no counter recorder associated", ANNA_FILE_LOCATION);
-
- LOGDEBUG(
- string msg("Recording counters | ");
- msg += a_counterRecorder->asString();
- Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION)
- );
- RecordingGuard guard(this);
-
- if(a_counterSummarizer != NULL)
- a_counterSummarizer->apply();
-
- a_counterRecorder->open();
- Counter* counter = NULL;
- CounterScope* scope = NULL;
-
- try {
- for(register int iscope = 0; iscope < MaxScope; iscope ++) {
- if((scope = a_scopes [iscope]) == NULL) continue;
-
- CounterScope& counterScope(*a_scopes [iscope]);
- // Al invocar a este método el timex::Engine ya está protegido => no hace falta volver a hacerlo
- CounterScope::Safe locker(NULL, counterScope, "oam::CounterManager::record");
-
- for(register int icounter = 0; icounter < CounterScope::MaxCounter; icounter ++) {
- Counter* counter = counterScope.a_counters [icounter];
-
- if(counter == NULL)
- continue;
-
- if(counter->getValue() == 0)
- continue;
-
- a_counterRecorder->apply(*counter);
- counter->reset();
- }
- }
-
- a_counterRecorder->close();
- } catch(RuntimeException&) {
- a_counterRecorder->close();
- throw;
- }
-}
-
-xml::Node* oam::CounterManager::asXML(xml::Node* parent) const
-throw(RuntimeException) {
- xml::Node* result = parent->createChild("oam.CounterManager");
-
- for(int ii = 0; ii < MaxScope; ii ++) {
- if(a_scopes [ii] != NULL) {
- // Observar que el Scope no conoce SU id.
- a_scopes [ii]->asXML(result)->createAttribute("Id", ii);
- }
- }
-
- return result;
-}
-
-oam::CounterManager::RecordingGuard::RecordingGuard(CounterManager* counterManager) :
- a_counterManager(counterManager) {
- counterManager->a_recording = true;
-}
-
-oam::CounterManager::RecordingGuard::~RecordingGuard() {
- a_counterManager->a_recording = false;
-}
-
-//static
-void oam::CounterManager::count(const int scope, const int counter, const oam::Counter::type_t value)
-throw() {
- CounterManager& ccmm = CounterManager::instantiate();
-
- try {
- oam::CounterScope::Safe counterScope(ccmm.a_timeController, ccmm.find(scope), "oam::CounterManager::count");
- counterScope.increment(counter, value);
- } catch(Exception& ex) {
- ex.trace();
- }
-}