Updated license
[anna.git] / include / anna / core / oam / CounterScope.hpp
1 // ANNA - Anna is Not Nothingness 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 oam {
55
56
57 /**
58    Ámbito de contadores. Contiene un grupo logico de contadores.
59 */
60 class CounterScope  : public Mutex {
61 public:
62
63   static const int MaxCounter = 1000; /**< Numero maximo de contador por cada ambito */
64
65   /**
66      Destructor.
67   */
68   ~CounterScope();
69
70   /**
71      Devuelve el nombre logico del contador, que coincidira con el indicado a la hora
72      de crear el contador mediante el metodo CounterScope::create.
73      \return El nombre logico del contador.
74   */
75   const std::string& getName() const throw() { return a_name; }
76
77   /**
78      Devuelve el identificador del ambito
79      \return El identificador del ambito.
80   */
81   const int getId() const throw() { return a_id; }
82
83   /**
84      Crea un nuevo contador.
85      \param counter Numero logico del contador a crear. Debera ser menor de MaxCounter.
86      \param name Nombre logico del ambito.
87   */
88   void create(const int counter, const char* name) throw(RuntimeException);
89
90   /**
91      Devuelve una cadena con la informacion relevante de este objeto.
92      \return Una cadena con la informacion relevante de este objeto.
93   */
94   std::string asString() const throw();
95
96   /**
97    * Devuelve la información relevante de esta instancia en un documento XML.
98    * \return la información relevante de esta instancia en un documento XML.
99    */
100   xml::Node* asXML(xml::Node* parent) const throw(RuntimeException);
101
102 protected:
103   /**
104    * Incrementa el contador recibido como parámetro.
105    * \warning La invocación a este método deberá hacerse sobre una sección crítica que proteja
106    * a este ámbito.
107    * \return El valor actual del contador.
108    */
109   Counter::type_t increment(const int counter, const Counter::type_t value) throw(RuntimeException);
110
111   /**
112    * Establece el valor del contador recibido como parámetro.
113    * \warning La invocación a este método deberá hacerse sobre una sección crítica que proteja
114    * a este ámbito.
115    * \return El valor actual del contador.
116    */
117   Counter::type_t assign(const int counter, const Counter::type_t value) throw(RuntimeException);
118
119   /**
120    * Devuelve el valor actual del contador pasado como parámetro.
121    * \param counter Identificador del contedor cuyo valor queremos obtener.
122    */
123   Counter::type_t getValue(const int counter) const throw(RuntimeException);
124
125   /**
126    * Devuelve la instancia del contador. Puede ser NULL.
127    * \return la instancia del contador.
128    */
129   const Counter* getCounter(const int counter) const throw(RuntimeException);
130
131 public:
132
133   /**
134    * Devuelve el valor actual acumulado del contador pasado como parámetro.
135    * \param counter Identificador del contedor cuyo valor acumulado queremos obtener.
136    */
137   U64 getAccValue(const int counter) const throw(RuntimeException);
138
139   /**
140    * Resetea los valores acumulados totales de los contadores incluidos en el ámbito.
141    * \return Numero de contadores afectados que tenian un acumulado no nulo.
142    */
143   int resetAccValues() throw(RuntimeException);
144
145 private:
146   const int a_id;
147   const std::string a_name;
148   Counter* a_counters [MaxCounter];
149
150   CounterScope(const int id, const char* name) :
151     Mutex(Mutex::Mode::Recursive),
152     a_id(id),
153     a_name(name) {
154     anna_memset(a_counters, 0, sizeof(a_counters));
155   }
156
157   friend class Safe;
158   friend class Handler;
159   friend class Module;
160 };
161
162 }
163 }
164
165 #endif
166