First commit
[anna.git] / include / anna / core / oam / CounterScope.hpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 //     * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //     * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 //     * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_core_oam_CounterScope_hpp
38 #define anna_core_oam_CounterScope_hpp
39
40 #include <anna/core/functions.hpp>
41 #include <anna/core/RuntimeException.hpp>
42 #include <anna/core/mt/Mutex.hpp>
43
44 #include <anna/core/oam/Counter.hpp>
45
46 namespace anna {
47
48 class Guard;
49
50 namespace xml {
51 class Node;
52 }
53
54 namespace timex {
55 class Engine;
56 }
57
58 namespace oam {
59
60 class CounterManager;
61
62 /**
63    Ámbito de contadores. Contiene un grupo logico de contadores.
64 */
65 class CounterScope  : public Mutex {
66 public:
67   /**
68    * Accesor que permite modificar el valor de los contadores asociados a un ámbito de forma segura.
69    * \see anna::oam::CounterScope.
70    */
71   class Safe {
72   public:
73     /**
74      * Constructor.
75      * \param counterScope Ámbito de contadores a bloquear para aplicar cambios.
76      * \param Texto que identifica el punto de bloqueo.
77      */
78     Safe(timex::Engine* ttcc, CounterScope& counterScope, const char* whatis);
79
80     /**
81      * Destructor.
82      */
83     ~Safe() throw();
84
85     /**
86      * Incrementa el contador recibido como parámetro.
87      * \warning La invocación a este método deberá hacerse sobre una sección crítica que proteja
88      * a este ámbito.
89      * \return El valor actual del contador.
90      */
91     Counter::type_t increment(const int counter, const Counter::type_t value) throw(RuntimeException) { return a_counterScope.increment(counter, value); }
92
93     /**
94      * Establece el valor del contador recibido como parámetro.
95      * \warning La invocación a este método deberá hacerse sobre una sección crítica que proteja
96      * a este ámbito.
97      * \return El valor actual del contador.
98      */
99     Counter::type_t assign(const int counter, const Counter::type_t value) throw(RuntimeException) { return a_counterScope.assign(counter, value); }
100
101   private:
102     CounterScope& a_counterScope;
103     Guard* a_guards [2];
104   };
105
106   static const int MaxCounter = 1000; /**< Numero maximo de contador por cada ambito */
107
108   /**
109      Destructor.
110   */
111   ~CounterScope();
112
113   /**
114      Devuelve el nombre logico del contador, que coincidira con el indicado a la hora
115      de crear el contador mediante el metodo CounterScope::create.
116      \return El nombre logico del contador.
117   */
118   const std::string& getName() const throw() { return a_name; }
119
120   /**
121      Devuelve el identificador del ambito, que coincidira con el indicado a la hora
122      de crear el contador mediante el metodo CounterManager::create.
123      \return El identificador del ambito.
124   */
125   const int getId() const throw() { return a_id; }
126
127   /**
128      Devuelve el gestor de contadores asociado a este ambito.
129      \return El gestor de contadores asociado a este ambito.
130   */
131   CounterManager& getCounterManager() throw() { return a_counterManager; }
132
133   /**
134      Crea un nuevo contador.
135      \param counter Numero logico del contador a crear. Debera ser menor de MaxCounter.
136      \param name Nombre logico del ambito.
137   */
138   void create(const int counter, const char* name) throw(RuntimeException);
139
140   /**
141      Devuelve una cadena con la informacion relevante de este objeto.
142      \return Una cadena con la informacion relevante de este objeto.
143   */
144   std::string asString() const throw();
145
146   /**
147    * Devuelve la información relevante de esta instancia en un documento XML.
148    * \return la información relevante de esta instancia en un documento XML.
149    */
150   xml::Node* asXML(xml::Node* parent) const throw(RuntimeException);
151
152 protected:
153   /**
154    * Incrementa el contador recibido como parámetro.
155    * \warning La invocación a este método deberá hacerse sobre una sección crítica que proteja
156    * a este ámbito.
157    * \return El valor actual del contador.
158    */
159   Counter::type_t increment(const int counter, const Counter::type_t value) throw(RuntimeException);
160
161   /**
162    * Establece el valor del contador recibido como parámetro.
163    * \warning La invocación a este método deberá hacerse sobre una sección crítica que proteja
164    * a este ámbito.
165    * \return El valor actual del contador.
166    */
167   Counter::type_t assign(const int counter, const Counter::type_t value) throw(RuntimeException);
168
169   /**
170    * Devuelve el valor actual del contador pasado como parámetro.
171    * \param counter Identificador del contedor cuyo valor queremos obtener.
172    */
173   Counter::type_t getValue(const int counter) const throw(RuntimeException);
174
175   /**
176    * Devuelve la instancia del contador. Puede ser NULL.
177    * \return la instancia del contador.
178    */
179   const Counter* getCounter(const int counter) const throw(RuntimeException);
180
181 public:
182
183   /**
184    * Devuelve el valor actual acumulado del contador pasado como parámetro.
185    * \param counter Identificador del contedor cuyo valor acumulado queremos obtener.
186    */
187   Unsigned64 getAccValue(const int counter) const throw(RuntimeException);
188
189   /**
190    * Resetea los valores acumulados totales de los contadores incluidos en el ámbito.
191    * \return Numero de contadores afectados que tenian un acumulado no nulo.
192    */
193   int resetAccValues() throw(RuntimeException);
194
195 private:
196   CounterManager& a_counterManager;
197   const int a_id;
198   const std::string a_name;
199   Counter* a_counters [MaxCounter];
200
201   CounterScope(CounterManager& counterManager, const int id, const char* name) :
202     Mutex(Mutex::Mode::Recursive),
203     a_counterManager(counterManager),
204     a_id(id),
205     a_name(name) {
206     anna_memset(a_counters, 0, sizeof(a_counters));
207   }
208
209   friend class Safe;
210   friend class CounterManager;
211   // CounterScope
212 };
213
214 }
215 }
216
217 #endif
218