]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/ServiceNode.cxx
Salome HOME
Addition of a new type of SalomeComponent to take advantage of HPContainer.
[modules/yacs.git] / src / engine / ServiceNode.cxx
1 // Copyright (C) 2006-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ServiceNode.hxx"
21 #include "Visitor.hxx"
22 #include "ComponentInstance.hxx"
23 #include "ComposedNode.hxx"
24 #include "Runtime.hxx"
25 #include <iostream>
26 #include <cassert>
27
28 //#define _DEVDEBUG_
29 #include "YacsTrace.hxx"
30
31 using namespace YACS::ENGINE;
32
33 /*! \class YACS::ENGINE::ServiceNode
34  *  \brief Class for calculation node associated with a component service
35  *
36  * \ingroup Nodes
37  *
38  * \see InlineNode
39  * \see ElementaryNode
40  */
41
42 const char ServiceNode::KIND[]="";
43
44 //! Return the service node kind
45 /*!
46  * A runtime can provide several implementations of a service node.
47  * Each implementation has a different kind. A ComponentInstance can be
48  * associated to a ServiceNode with the same kind.
49  */
50 std::string ServiceNode::getKind() const
51 {
52   return KIND;
53 }
54
55 ServiceNode::ServiceNode(const std::string& name):ElementaryNode(name),_component(0)
56
57 }
58
59 ServiceNode::ServiceNode(const ServiceNode& other, ComposedNode *father)
60            :ElementaryNode(other,father),_method(other._method),_component(0)
61 {
62 }
63
64 void ServiceNode::performDuplicationOfPlacement(const Node& other)
65 {
66   const ServiceNode &otherC=*(dynamic_cast<const ServiceNode *>(&other));
67   //if other has no component don't clone: this will not have one
68   if(otherC._component)
69     _component=otherC._component->clone();
70 }
71
72 ServiceNode::~ServiceNode()
73 {
74   if(_component)
75     _component->decrRef();
76 }
77
78 //! Load the component associated to the node
79 void ServiceNode::load()
80 {
81   if(_component)
82     {
83       if(!_component->isLoaded(this))
84         {
85           try
86             {
87               _component->load(this);
88             }
89           catch(Exception& e)
90             {
91               _errorDetails=e.what();
92               throw e;
93             }
94         }
95     }
96   else
97     {
98       std::string what("ServiceNode::load : a load operation requested on ServiceNode called \"");
99       what+=_name; what+="\" with no component specified.";
100       _errorDetails=what;
101       throw Exception(what);
102     }
103 }
104
105 void ServiceNode::accept(Visitor *visitor)
106 {
107   visitor->visitServiceNode(this);
108 }
109
110 //! Return the associated component instance
111 ComponentInstance *ServiceNode::getComponent()
112 {
113   return _component;
114 }
115
116 const ComponentInstance *ServiceNode::getComponent() const
117 {
118   return _component;
119 }
120
121 //! Return the associated container
122 Container *ServiceNode::getContainer()
123 {
124   if(_component)return _component->getContainer();
125   return 0;
126 }
127
128 //! By definition of ServiceNode class.
129 bool ServiceNode::isDeployable() const
130 {
131   return true;
132 }
133
134 //! Associate an existing component instance to this service node \b AND check the consistency regarding the deployment from root node point of view.
135 void ServiceNode::setComponent(ComponentInstance* compo) throw(YACS::Exception)
136 {
137   DEBTRACE("ServiceNode::setComponent " << compo);
138   if(compo)
139     {
140       DEBTRACE(compo->getInstanceName());
141       if(compo->getKindForNode() != this->getKind())
142         {
143           //Not allowed
144           std::string what("ServiceNode::setComponent : component instance kind not allowed ");
145           throw Exception(what);
146         }
147     }
148
149   ComponentInstance* oldcompo=_component;
150   std::string oldref=_ref;
151
152   _component=compo;
153   _ref=compo->getCompoName();
154   DEBTRACE(_component->getInstanceName());
155   if(_component)
156     {
157       if(_father)
158         try
159           {
160             getRootNode()->checkDeploymentTree(false);
161           }
162         catch(Exception& e)
163           {
164             // Impossible to associate compo to this node. Keep the old component instance and throws exception
165             _component=oldcompo;
166             _ref=oldref;
167             throw e;
168           }
169       _component->incrRef();
170     }
171
172   if(oldcompo)
173     oldcompo->decrRef();
174 }
175
176 //! Associate a new component instance to this service node
177 /*!
178  * A new component instance with type name ref is created (from runtime
179  * factory createComponentInstance) and associated to the node.
180  *
181  */
182 void ServiceNode::setRef(const std::string& ref)
183 {
184   _ref = ref;
185   if(_component)
186     {
187       //The node is already associated with a component instance
188       _component->decrRef();
189       //Don't forget to unassociate
190     }
191   _component= getRuntime()->createComponentInstance(ref,getKind());
192   YASSERT(_component);
193 }
194
195 std::string ServiceNode::getRef()
196 {
197   return _ref;
198 }