First commit
[anna.git] / include / anna / timex / TimeEvent.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_timex_TimeEvent_hpp
38 #define anna_timex_TimeEvent_hpp
39
40 #include <anna/core/RuntimeException.hpp>
41 #include <anna/config/defines.hpp>
42 #include <anna/core/util/Millisecond.hpp>
43
44 namespace anna {
45
46 namespace timex {
47
48 class Engine;
49 class TimeEventObserver;
50
51 /**
52    Clase base de los eventos de los eventos de tiempo.
53 */
54 class TimeEvent {
55 public:
56   /**
57      Sinonimo usado para el identificador de eventos de tiempo.
58
59      Se define como un ptrnumber para facilitar la construccion de instancias sin tener que indicar
60      un Id en particular, ya que se usara su direccion de memoria como identificador.
61   */
62   typedef anna::ptrnumber Id;
63
64   /**
65      Destructor.
66      Si un evento de tiempo esta activo y se invoca a su destructor se invocaria automaticamente a
67      Engine::cancel para terminar con la ejecucin de este evento.
68   */
69   virtual ~TimeEvent();
70
71   /**
72      Devuelve el identificador de este evento.
73      \return El identificador de este evento.
74   */
75   Id getId() const throw() { return a_id; }
76
77   /**
78      Devuelve la duracion maxima a este evento indicada en milisegundos.
79      @return La duracion maxima de este evento indicada en milisegundos.
80   */
81   const Millisecond & getTimeout() const throw() { return a_timeout; }
82
83   /**
84      Devuelve la instancia del objeto encargado de gestionar el espacio asociado a
85      esta instancia. Puede ser NULL.
86      \return La instancia del objeto encargado de gestionar el espacio asociado a
87      esta instancia.
88   */
89   const TimeEventObserver* getObserver() const throw() { return a_observer; }
90
91   /**
92      Establece el identificador de este evento de tiempo.
93      \param id El identificador de evento para esta instancia.
94
95      \warning Exclusivamente uso interno.
96   */
97   void setId(const Id id) throw() { a_id = id; }
98
99   /**
100      Establece la duracion de este evento indicada en milisegundos.
101      \param timeout Establece la duracion de este evento.
102   */
103   void setTimeout(const Millisecond & timeout) throw() { a_timeout = timeout; }
104
105   /**
106      Establece la instancia de objeto encargado de gestionar el espacio asociado a esta
107      instancia. Si no es NULL sera invocado cuando el timex::Engine caduque o
108      cancele este evento temporal.
109
110      \param observer Instancia del objeto encargada de gestional el espacio asignado a esta
111      instancia.
112   */
113   void setObserver(TimeEventObserver* observer) throw() { a_observer = observer; }
114
115   /**
116      Devuelve el estado de activacin de este evento de tiempo.
117      @return \em false si el evento esta activado o \em false en otro caso.
118   */
119   bool isActive() const throw() { return (a_controller != NULL); }
120
121   /**
122      Devuelve una cadena con la informacion sobre este evento.
123      \return Una cadena con la informacion sobre este evento.
124   */
125   virtual std::string asString() const throw();
126
127 protected:
128   /**
129      Contructor.
130   */
131   TimeEvent() :
132     a_id(0),
133     a_timeout(0),
134     a_controller(NULL),
135     a_observer(NULL)
136   {;}
137
138   /**
139      Contructor.
140
141      @param id Identificador de este evento.
142      @param timeout La duracion de este evento indicada en milisegundos.
143   */
144   TimeEvent(const Id id, const Millisecond & timeout) :
145     a_id(id),
146     a_timeout(timeout),
147     a_controller(NULL),
148     a_observer(NULL) {}
149
150   /**
151      metodo que debemos re-escribir para particularizar el comportamiento de nuestra clase cuando
152      el Engine notifica que se ha sobrepasado la duracion maxima del evento si que se invoque
153      a ninguno de los metodos que lo cancelarian. Una vez invocado a este metodo el evento se considera
154      cancelado y todos sus recursos son liberados automaticamente por Engine.
155
156      @param timeController Controlador de tiempo asociado al evento que esta expirando.
157   */
158   virtual void expire(Engine* timeController) throw(RuntimeException) = 0;
159
160   /**
161      metodo que debemos re-escribir para particularizar el comportamiento de nuestra clase cuando
162      el Engine notifica que se ha parado el sistema de control de tiempos. Una vez invocado a este
163      metodo el evento se considera cancelado y todos sus recursos son liberados automaticamente
164      por Engine.
165
166      Las acciones realizadas nunca deber�n generar nuevos eventos. Por defecto no hace nada.
167   */
168   virtual void stop() throw(RuntimeException) {;}
169
170 private:
171   Id a_id;
172   Millisecond a_timeout;
173   Engine* a_controller;
174   TimeEventObserver* a_observer;
175
176   friend class Engine;
177 };
178
179 }
180 }
181
182 #endif
183
184