]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/SalomeHPComponent.cxx
Salome HOME
Addition of a new type of SalomeComponent to take advantage of HPContainer.
[modules/yacs.git] / src / runtime / SalomeHPComponent.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 "SalomeHPComponent.hxx"
21 #include "RuntimeSALOME.hxx"
22 #include "SalomeContainer.hxx"
23 #include "SalomeHPContainer.hxx"
24 #include "SalomeComponent.hxx" // for KIND
25 #include "CORBANode.hxx"
26 #include "AutoLocker.hxx"
27
28 #ifdef SALOME_KERNEL
29 #include "SALOME_NamingService.hxx"
30 #include "SALOME_LifeCycleCORBA.hxx"
31 #endif
32
33 #include <omniORB4/CORBA.h>
34 #include <iostream>
35 #include <sstream>
36
37 //#define _DEVDEBUG_
38 #include "YacsTrace.hxx"
39
40
41 namespace YACS
42 {
43   namespace ENGINE
44   {
45     class SalomeContainerTmpForHP : public SalomeContainer
46     {
47     public:
48       SalomeContainerTmpForHP(const Container& other, const SalomeContainerTools& sct, SalomeContainerHelper *lmt,
49                               const std::vector<std::string>& componentNames, int shutdownLev,
50                               const SalomeHPContainer *zeOriginCont, std::size_t pos):SalomeContainer(other,sct,lmt,componentNames,shutdownLev),_zeOriginCont(zeOriginCont),_pos(pos) { }
51       std::string getDiscreminantStrOfThis() const { std::ostringstream oss; oss << _zeOriginCont << "_" << _pos; return oss.str(); }
52       CORBA::Object_ptr loadComponent(Task *inst);
53     private:
54       const SalomeHPContainer *_zeOriginCont;
55       std::size_t _pos;
56     };
57   }
58 }
59
60 using namespace YACS::ENGINE;
61 using namespace std;
62
63 const char SalomeHPComponent::KIND[]="HPSalome";
64
65 CORBA::Object_ptr SalomeContainerTmpForHP::loadComponent(Task *askingNode)
66 {
67   const ComponentInstance *inst(askingNode?askingNode->getComponent():0);
68   if(!inst)
69     throw Exception("SalomeContainerTmpForHP::loadComponent : asking to load a component on the given task whereas this task has no component !");
70   std::string compoName(inst->getCompoName());
71   {
72     YACS::BASES::AutoLocker<Container> alck(this);//To be sure
73     if(!this->isAlreadyStarted(askingNode))
74       this->start(askingNode);
75   }
76   CORBA::Object_ptr objComponent=CORBA::Object::_nil();
77   {
78     YACS::BASES::AutoLocker<Container> alck(this);//To be sure
79     std::string compoName(inst->getCompoName());
80     Engines::Container_var container(_launchModeType->getContainer(askingNode));
81     objComponent=container->find_component_instance(compoName.c_str(),0);
82     if(CORBA::is_nil(objComponent))
83       {
84         char *reason;
85         bool isLoadable(container->load_component_Library(compoName.c_str(), reason));
86         if(isLoadable)
87           objComponent=SalomeContainerTools::CreateComponentInstance(this,container,inst);
88       }
89   }
90   return objComponent;
91 }
92
93 SalomeHPComponent::SalomeHPComponent(const std::string& name): ComponentInstance(name)
94 {
95   _objComponent=CORBA::Object::_nil();
96 }
97
98 SalomeHPComponent::SalomeHPComponent(const SalomeHPComponent& other):ComponentInstance(other)
99 {
100   _objComponent=CORBA::Object::_nil();
101 }
102
103 SalomeHPComponent::~SalomeHPComponent()
104 {
105 }
106
107 std::string SalomeHPComponent::getKind() const
108 {
109   return KIND;
110 }
111
112 std::string SalomeHPComponent::getKindForNode() const
113 {
114   return SalomeComponent::KIND;
115 }
116
117 //! Unload the component 
118 void SalomeHPComponent::unload(Task *askingNode)
119 {
120   //Not implemented
121   std::cerr << "SalomeHPComponent::unload : not implemented " << std::endl;
122 }
123
124 //! Is the component instance already loaded ?
125 bool SalomeHPComponent::isLoaded(Task *askingNode) const
126 {
127   return false;
128 }
129
130 //#ifdef SALOME_KERNEL
131 //! Load the component 
132 void SalomeHPComponent::load(Task *askingNode)
133 {
134   if(_container)
135     {
136       SalomeHPContainer *salomeContainer(dynamic_cast<SalomeHPContainer *>(_container));
137       if(salomeContainer)
138         {
139           SalomeContainerHelper *lmt(0);
140           std::size_t posIn(0);
141           {
142             YACS::BASES::AutoLocker<Container> altck(salomeContainer);
143             lmt=salomeContainer->getHelperOfTask(askingNode);
144             posIn=salomeContainer->locateTask(askingNode);
145           }
146           SalomeContainerTmpForHP tmpCont(*salomeContainer,salomeContainer->getContainerInfo(),lmt,
147                                           salomeContainer->getComponentNames(),salomeContainer->getShutdownLev(),salomeContainer,posIn);
148           _objComponent=tmpCont.loadComponent(askingNode);
149           return ;
150         }
151       throw Exception("Unrecognized type of Container ! Only Salome and HPSalome container are supported by the Salome components !");
152     }
153   else
154     throw Exception("No container on HP component ! Impossible to load !");
155 }
156 /*#else
157 void SalomeComponent::load(Task *askingNode)
158 {
159   throw Exception("YACS has been built without SALOME support");
160 }
161 #endif*/
162
163 //! Create a ServiceNode with this component instance and no input or output port
164 /*!
165  *   \param name : node name
166  *   \return       a new SalomeNode node
167  */
168 ServiceNode* SalomeHPComponent::createNode(const std::string& name)
169 {
170   SalomeNode* node(new SalomeNode(name));
171    node->setComponent(this);
172    return node;
173 }
174
175 //! Clone the component instance
176 ComponentInstance* SalomeHPComponent::clone() const
177 {
178   if(_isAttachedOnCloning)
179     {
180       incrRef();
181       return (ComponentInstance*) (this);
182     }
183   else
184     return new SalomeHPComponent(*this);
185 }
186
187 std::string SalomeHPComponent::getFileRepr() const
188 {
189   ostringstream stream;
190   stream << "<component>" << getCompoName() << "</component>";
191   return stream.str();
192 }
193
194 bool SalomeHPComponent::setContainer(Container *cont)
195 {
196   if(!dynamic_cast<SalomeHPContainer *>(cont))
197     throw Exception("SalomeHPComponent::setContainer : a Salome HP component must be attached to a Salome HP container !");
198   if(ComponentInstance::setContainer(cont))
199     {
200       if(_container)
201         _container->addComponentName(_compoName);
202       return true;
203     }
204   else
205     return false;
206 }
207
208 void SalomeHPComponent::shutdown(int level)
209 {
210   DEBTRACE("SalomeHPComponent::shutdown " << level);
211   if(_container)
212     _container->shutdown(level);
213 }