1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
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
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.
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.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_app_Component_hpp
38 #define anna_app_Component_hpp
42 #include <anna/core/RuntimeException.hpp>
43 #include <anna/core/mt/Mutex.hpp>
45 #include <anna/app/Application.hpp>
56 Clase de la que heredan los componentes de aplicacion.
58 Solo deberia haber una instancia de cada uno de los componentes, pero no podemos
59 declararlos como heredados de anna::Singleton porque debemos dar la posiblidad de
60 que el programador re-escriba su comportamiento mediante herencia.
62 Todos los componentes se arrancan y paran automaticamente desde la aplicacion.
64 El siguiente ejemplo muestra como obtener la instancia de un componente asociado a nuestra aplicacion:
67 Clase* objeto = anna::app::functions::component <Clase> (FILE_LOCATION);
69 ..... uso del objeto ....
72 Si el componente \em 'Clase' no hubiera sido registrado (instanciado) en nuestra aplicacion el metodo
73 template anna::component lanzara una excepcion.
75 class Component : public Mutex {
83 Devuelve el nombre de esta clase indicado en el constructor.
84 \return El nombre de la clase indicado en el constructor.
86 const char* getClassName() const throw() { return a_className.c_str(); }
89 Conecta explicitamente este componente con la aplicacion. Sera necesario invocar a este metodo
90 cuando instanciemos un componentes despues de comenzar la ejecucion de nuestra aplicacion y
91 cuando el nuevo componente este completamente listo para su inicializacion.
93 void attach() throw(RuntimeException);
96 Devuelve una cadena con la informacion mas relevante de esta instancia.
97 \return Una cadena con la informacion mas relevante de esta instancia.
99 virtual std::string asString() const throw();
102 Devuelve un documento XML con la informacion mas relevante de esta instancia.
103 \param parent Nodo XML del que colgar la informacion referente a esta instancia.
104 \return Un documento XML con la informacion mas relevante de esta instancia.
106 virtual xml::Node* asXML(xml::Node* parent) const throw();
110 Estados en los que puede estar un componente.
113 struct State { enum _v { Stopped, Starting, Running }; };
117 @param className Nombre lgico asociado a esta clase.
119 explicit Component(const char* className);
122 Devuelve el estado de este componente.
123 \return el estado de este componente.
125 State::_v getState() const throw() { return a_state; }
128 Indica que el nombre de un componente que debe ser initializa antes que este.
129 \param componentName Nombre de componente requerido por esta instancia.
130 \warning Solo tendra efecto antes de inicializar el componente.
132 void addPredecessor(const char* componentName) throw();
135 metodo que debemos implementar si la clase heredada necesita algn tipo de inicializacin.
136 Este metodo se invocara automaticamente desde anna::Application::start.
138 void initialize() throw(RuntimeException);
141 metodo que debemos implementar en la clase heredada para implementar el proceso de parada de
144 Este metodo debe implementar un metodo de parada controlada. Se invocara automaticamente
147 void stop() throw() { a_state = State::Stopped; do_stop(); }
150 metodo que debemos implementar en la clase heredada para implementar el proceso de parada de
151 esta clase. Se invocara automaticamente desde el nucleo
153 Este metodo implementa un metodo de parada no controlada.
155 virtual void kill() throw() { stop(); }
158 typedef std::vector <std::string>::iterator iterator;
160 const std::string a_className;
162 std::vector <std::string> a_predecessors;
164 Component(const Component& other);
165 iterator begin() throw() { return a_predecessors.begin(); }
166 iterator end() throw() { return a_predecessors.end(); }
167 const std::string& data(iterator& ii) throw() { return *ii; }
170 metodo que debemos implementar si la clase heredada necesita algn tipo de inicializacin.
171 Este metodo se invocara automaticamente desde anna::Application::start.
173 virtual void do_initialize() throw(RuntimeException) = 0;
176 Metodo que debemos implementar en la clase heredada para tratar el proceso de parada de
178 Este metodo debe implementar un metodo de parada controlada. Se invocara automaticamente
181 virtual void do_stop() throw() = 0;
184 * Metodo que debemos implementar en la clase heredada para tratar el proceso de clonado de
185 * esta clase desde el punto de vista del proceso original.
186 * \warning Si se detecta algun error terminara abortara la ejecucion del proceso hijo.
188 virtual void do_cloneParent() throw(RuntimeException) { ; }
191 * Metodo que debemos implementar en la clase heredada para tratar el proceso de clonado de
192 * esta clase desde el punto de vista del proceso hijo.
193 * \warning Si se detecta algun error terminara abortara la ejecucion de este proceso.
195 virtual void do_cloneChild() throw(RuntimeException) { ; }
197 friend void Application::startComponents() throw(RuntimeException);
198 friend void Application::stopComponents() throw(RuntimeException);
199 friend void Application::attach(Component*) throw(RuntimeException);
200 friend Application& Application::clone() throw(RuntimeException);