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
39 #include <anna/core/tracing/Logger.hpp>
41 #include <anna/app/Component.hpp>
42 #include <anna/app/functions.hpp>
44 #include <anna/xml/Node.hpp>
45 #include <anna/xml/Attribute.hpp>
50 app::Component::Component(const char* className) :
51 a_className(className),
52 a_state(State::Stopped) {
53 Application& app(app::functions::getApp());
55 if(app.a_running == false) {
58 } catch(RuntimeException& ex) {
61 } else if(Logger::isActive(Logger::Warning) == true) {
62 string msg(asString());
63 msg += " | Not connected to applicatino yet";
64 Logger::warning(msg, ANNA_FILE_LOCATION);
68 app::Component::~Component() {
70 functions::getApp().detach(this);
71 } catch(RuntimeException& ex) {
76 void app::Component::addPredecessor(const char* componentName)
78 const std::string name(componentName);
80 if((find(begin(), end(), name)) != end())
83 a_predecessors.push_back(name);
85 string msg("anna::app::Component::addPredecessor | ");
87 msg += " | Requires: ";
89 Logger::debug(msg, ANNA_FILE_LOCATION);
93 void app::Component::initialize()
94 throw(RuntimeException) {
95 if(a_state == State::Running)
98 a_state = State::Starting;
99 Application& app = functions::getApp();
100 Component* predecessor;
103 for(iterator ii = begin(), maxii = end(); ii != maxii; ii ++) {
104 const std::string& name = data(ii);
106 if((predecessor = app.find(name.c_str())) == NULL) {
107 string msg("anna::app::Component::initialize | ");
109 msg += " | Requires component '";
111 msg += "' undefined";
112 throw RuntimeException(msg, ANNA_FILE_LOCATION);
115 if(predecessor->a_state == State::Starting) {
116 string msg("anna::app::Component::initialize | ");
118 msg += " | Cyclic dependency with '";
121 throw RuntimeException(msg, ANNA_FILE_LOCATION);
124 predecessor->initialize();
128 a_state = State::Running;
129 } catch(RuntimeException&) {
130 a_state = State::Stopped;
135 void app::Component::attach()
136 throw(RuntimeException) {
137 app::functions::getApp().attach(this);
140 std::string app::Component::asString() const
142 std::string result("app::Component { Name: ");
143 result += a_className;
144 result += " | Reference: ";
145 result += functions::asHexString(anna_ptrnumber_cast(this));
146 return result += " }";
149 xml::Node* app::Component::asXML(xml::Node* parent) const
151 xml::Node* result = parent->createChild("app.Component");
152 result->createAttribute("Name", a_className);
153 result->createAttribute("Reference", functions::asHexString(anna_ptrnumber_cast(this)));