1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
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 the copyright holder 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
46 #include <sys/types.h>
48 #include <anna/core/RuntimeException.hpp>
49 #include <anna/core/tracing/Logger.hpp>
50 #include <anna/core/functions.hpp>
51 #include <anna/core/tracing/TraceWriter.hpp>
52 #include <anna/core/tracing/TraceMethod.hpp>
53 #include <anna/core/internal/ModuleManager.hpp>
54 #include <anna/core/util/CommandLine.hpp>
56 #include <anna/app/Application.hpp>
57 #include <anna/app/Component.hpp>
58 #include <anna/app/internal/sccs.hpp>
59 #include <anna/app/functions.hpp>
61 #include <anna/xml/Node.hpp>
62 #include <anna/xml/Attribute.hpp>
63 #include <anna/xml/Compiler.hpp>
68 app::Application* app::Application::st_application = NULL;
70 app::Application::Application(const char* shortName, const char* title, const char* version, const char* date, const char* time) :
71 a_shortName(shortName),
76 sigset(SIGUSR1, handlerSignalUSR);
77 sigset(SIGUSR2, handlerSignalUSR);
78 sigset(SIGTERM, handlerSignalTerminate);
80 app::sccs::activate();
81 a_version += functions::getArchitecture();
84 if(st_application == NULL)
85 st_application = this;
87 cout << a_shortName << " - " << a_title << ". Version " << a_version << endl;
90 cout << "Revision date: ";
92 if(date) cout << date << " ";
94 if(time) cout << time;
99 cout << "(c) Copyright 2001-2006 Eduardo Ramos Testillano & Francisco Antonio Ruiz Rayo" << endl;
100 cout << " eduardo.ramos.testillano@gmail.com & cisco.tierra@gmail.com" << endl << endl;
103 app::Component* app::Application::find(const char* className)
105 Component* component;
107 for(iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
108 component = Application::component(ii);
110 if(anna_strcmp(className, component->getClassName()) == 0)
118 * (1) Si se ejecuta el metodo clone evita que los hijos que termina se queden como zombies.
120 void app::Application::start()
121 throw(RuntimeException) {
122 TraceMethod tm("app::Application", "start", ANNA_FILE_LOCATION);
123 ModuleManager& moduleManager = ModuleManager::instantiate();
125 if(a_running == true) {
126 string msg("app::Application { Name; ");
128 msg += " | Already on execution";
129 throw RuntimeException(msg, ANNA_FILE_LOCATION);
132 if(sigset(SIGCHLD, handlerChildTerminate) != 0) // (1)
133 throw RuntimeException("Error installing terminate handler", ANNA_FILE_LOCATION);
136 if(a_enableGPL == true) {
137 cout << "This program is free software: you can redistribute it and/or modify" << endl;
138 cout << "it under the terms of the GNU General Public License as published by" << endl;
139 cout << "the Free Software Foundation, either version 3 of the License, or" << endl;
140 cout << "(at your option) any later version." << endl << endl;
141 cout << "This program is distributed in the hope that it will be useful," << endl;
142 cout << "but WITHOUT ANY WARRANTY; without even the implied warranty of" << endl;
143 cout << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" << endl;
144 cout << "GNU General Public License for more details." << endl << endl;
145 cout << "You should have received a copy of the GNU General Public License" << endl;
146 cout << "along with this program. If not, see <http://www.gnu.org/licenses/>." << endl << endl;
149 cout << "ANNA Suite. Version " << functions::getVersion() << endl;
150 cout << "Revision date: " << __DATE__ << " " << __TIME__ << endl;
151 cout << "(c) Copyright 2001-2006 Eduardo Ramos Testillano & Francisco Antonio Ruiz Rayo" << endl;
152 cout << " eduardo.ramos.testillano@gmail.com & cisco.tierra@gmail.com" << endl << endl;
154 cout << "Activating application modules ....................." << endl;
156 for(ModuleManager::const_iterator ii = moduleManager.begin(), maxii = moduleManager.end(); ii != maxii; ii ++) {
157 cout << "\t Module " << ModuleManager::module(ii) << endl;
166 } catch(RuntimeException& ex) {
173 void app::Application::startComponents()
174 throw(RuntimeException) {
175 LOGMETHOD(TraceMethod tm("app::Application", "startComponents", ANNA_FILE_LOCATION));
176 Component* component;
178 for(iterator ii = begin(); ii != end(); ii ++) {
179 component = Application::component(ii);
181 std::string msg("Starting component | ");
182 msg += component->asString();
183 Logger::write(Logger::Information, msg, ANNA_FILE_LOCATION);
185 component->initialize();
189 void app::Application::stopComponents()
190 throw(RuntimeException) {
191 LOGMETHOD(TraceMethod tm("app::Application", "stopComponents", ANNA_FILE_LOCATION));
192 Component* component;
194 for(iterator ii = begin(); ii != end(); ii ++) {
195 component = Application::component(ii);
197 std::string msg("Stopping component | ");
198 msg += component->asString();
199 Logger::write(Logger::Information, msg, ANNA_FILE_LOCATION);
206 * (1) Este el codigo que ejecuta la copia que acabamos de crear.
207 * (1.1) Limpia la lista de hijos que pudiera tener el padre.
209 app::Application& app::Application::clone()
210 throw(RuntimeException) {
211 if(anna::functions::supportMultithread() == true) {
212 string msg("app::Application::clone | pid: ");
213 msg += functions::asString((int) a_pid);
214 msg += " | Application clone is only supported for ST applications";
215 throw RuntimeException(msg, errno, ANNA_FILE_LOCATION);
218 const pid_t pid = ::fork();
221 string msg("app::Application::clone | pid: ");
222 msg += functions::asString((int) a_pid);
223 throw RuntimeException(msg, errno, ANNA_FILE_LOCATION);
226 Component* component;
228 if(pid == 0) { // (1)
229 Logger::showPID(true);
231 a_pids.clear(); // (1.1)
234 for(iterator ii = begin(); ii != end(); ii ++) {
235 component = Application::component(ii);
237 std::string msg("Cloning component on child | ");
238 msg += component->asString();
239 Logger::write(Logger::Information, msg, ANNA_FILE_LOCATION);
241 component->do_cloneChild();
245 } catch(RuntimeException& ex) {
251 string msg("app::Application::clone | Parent pid: ");
252 msg += functions::asString((int) a_pid);
253 msg += " | Child pid: ";
254 msg += functions::asString((int) pid);
255 Logger::warning(msg, ANNA_FILE_LOCATION);
259 for(iterator ii = begin(); ii != end(); ii ++) {
260 component = Application::component(ii);
262 std::string msg("Cloning component on parent | ");
263 msg += component->asString();
264 Logger::write(Logger::Information, msg, ANNA_FILE_LOCATION);
266 component->do_cloneParent();
270 a_pids.push_back(pid);
271 } catch(RuntimeException& ex) {
272 string msg("app::Application::clone | Abort execution: ");
273 msg += msg += functions::asString((int) pid);
274 Logger::error(msg, ANNA_FILE_LOCATION);
283 //---------------------------------------------------------------------------------------
284 // (1) Se invoca desde los constructores de los componentes => no se puede invocar a
285 // ningn m�odo virtual => obliga a que est� definidos antes de comenzar la
286 // ejecucin de la aplicacin => se inicilizar� en el Application::start ya que en
287 // ese momento los objetos est� completamente creados.
288 //---------------------------------------------------------------------------------------
289 void app::Application::attach(app::Component* component)
290 throw(RuntimeException) {
291 if(component == NULL)
292 throw RuntimeException("Cannot attach a NULL component", ANNA_FILE_LOCATION);
294 iterator ii = std::find(begin(), end(), component);
298 string msg((*ii)->asString());
299 msg += " | Was previously attached";
300 Logger::write(Logger::Information, msg, ANNA_FILE_LOCATION);
306 string msg("app::Application::attach | ");
307 msg += component->asString();
308 Logger::debug(msg, ANNA_FILE_LOCATION);
310 a_components.push_back(component);
314 std::string msg("Starting component | ");
315 msg += component->asString();
316 Logger::write(Logger::Information, msg, ANNA_FILE_LOCATION);
318 component->initialize();
322 void app::Application::detach(app::Component* component)
323 throw(RuntimeException) {
324 LOGMETHOD(TraceMethod tm("app::Application", "detach(component)", ANNA_FILE_LOCATION));
326 if(component == NULL)
327 throw RuntimeException("Cannot detach a NULL component", ANNA_FILE_LOCATION);
330 string msg("app::Application::detach | ");
331 msg += component->asString();
332 Logger::debug(msg, ANNA_FILE_LOCATION);
334 iterator ii = std::find(begin(), end(), component);
337 LOGDEBUG(Logger::write(Logger::Debug, "Detached", (*ii)->asString(), ANNA_FILE_LOCATION));
338 a_components.erase(ii);
342 void app::Application::writeContext(const std::string& file)
343 throw(RuntimeException) {
345 out.open(file.c_str());
347 if(out.is_open() == false) {
348 string msg("Error opening '");
351 throw RuntimeException(msg, ANNA_FILE_LOCATION);
355 string msg("app::Application::writeContext | File: ");
357 Logger::notice(msg, ANNA_FILE_LOCATION);
359 xml::Node root("Application");
360 out << xml::Compiler().apply(asXML(&root)) << endl;
364 xml::Node* app::Application::asXML(xml::Node* app) const
366 xml::Node* node(NULL);
367 app->createAttribute("Name", getShortName());
368 app->createAttribute("Version", getVersion());
369 app->createAttribute("pid", getPid());
371 CommandLine::instantiate().asXML(app);
372 node = app->createChild("app.Clones");
374 for(const_pid_iterator ii = pid_begin(), maxii = pid_end(); ii != maxii; ii ++)
375 node->createChild("Clone")->createAttribute("PID", pid(ii));
377 ModuleManager& moduleManager = ModuleManager::instantiate();
378 node = app->createChild("app.Modules");
380 for(ModuleManager::const_iterator ii = moduleManager.begin(), maxii = moduleManager.end(); ii != maxii; ii ++)
381 node->createChild("Module")->createAttribute("Id", ModuleManager::module(ii));
384 node = app->createChild("app.Components");
386 for(const_iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
387 Guard guard(cc = component(ii));
394 void app::Application::signalUSR(int signal)
395 throw(RuntimeException) {
396 if (signal == SIGUSR1) signalUSR1();
397 else if (signal == SIGUSR2) signalUSR2();
400 void app::Application::signalUSR1()
401 throw(RuntimeException) {
402 writeContext(anna::functions::asString("/var/tmp/anna.context.%05d", getPid()));
405 void app::Application::signalUSR2()
406 throw(RuntimeException) {
407 Logger::notice("Captured signal SIGUSR2. Nothing implemented at the moment", ANNA_FILE_LOCATION);
410 void app::Application::sendSignalToChilds(const int signal)
412 for(pid_iterator ii = pid_begin(), maxii = pid_end(); ii != maxii; ii ++)
413 kill(pid(ii), signal);
416 void app::Application::handlerSignalUSR(int signal)
421 Application& app = anna::app::functions::getApp();
422 app.sendSignalToChilds(signal);
423 app.signalUSR(signal);
424 } catch(Exception& ex) {
428 sigset(signal, handlerSignalUSR);
431 void app::Application::handlerSignalTerminate(int)
437 Logger::write(Logger::Warning, "app::Application | Received SIGTERM", ANNA_FILE_LOCATION);
439 Application& app = anna::app::functions::getApp();
440 app.sendSignalToChilds(SIGTERM);
441 app.signalTerminate();
442 } catch(Exception& ex) {
447 sigset(SIGTERM, handlerSignalTerminate);
451 * Se recoge el estado de terminacion del hijo para que no se quede considerado como un zombie y se
452 * elimina de la lista de procesos hijos.
454 void app::Application::handlerChildTerminate(int sig)
458 pid_t pid = wait(&status);
460 if(st_application == NULL)
463 pid_iterator maxii = st_application->pid_end();
464 pid_iterator ii = ::find(st_application->pid_begin(), maxii, pid);
467 st_application->a_pids.erase(ii);