Salome HOME
390dd350f8d334a42f49995d263fd3d2afc7257e
[modules/yacs.git] / src / runtime / SalomeContainer.cxx
1 // Copyright (C) 2006-2020  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 //#define REFCNT
21 //
22 #ifdef REFCNT
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
27 #endif
28
29 #include "RuntimeSALOME.hxx"
30 #include "SalomeContainer.hxx"
31 #include "SalomeComponent.hxx"
32 #include "ServiceNode.hxx"
33 #include "Proc.hxx"
34
35 #include "SALOME_NamingService.hxx"
36 #include "SALOME_LifeCycleCORBA.hxx"
37 #include "SALOME_ContainerManager.hxx"
38 #include "Basics_Utils.hxx"
39 #include "OpUtil.hxx"
40
41 #include <sstream>
42 #include <iostream>
43
44 #ifdef WIN32
45 #include <process.h>
46 #define getpid _getpid
47 #endif
48
49 //#define _DEVDEBUG_
50 #include "YacsTrace.hxx"
51
52 using namespace YACS::ENGINE;
53 using namespace std;
54
55 const char SalomeContainer::KIND[]="Salome";
56
57 const char SalomeContainer::TYPE_PROPERTY_STR[]="type";
58
59 SalomeContainer::SalomeContainer():_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
60 {
61 }
62
63 SalomeContainer::SalomeContainer(const SalomeContainer& other)
64 : Container(other),_componentNames(other._componentNames),
65   _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()),
66   _shutdownLevel(other._shutdownLevel),
67   _sct(other._sct)
68 {
69 }
70
71 SalomeContainer::SalomeContainer(const Container& other, const SalomeContainerTools& sct, const SalomeContainerHelper *lmt,
72                                  const std::vector<std::string>& componentNames, int shutdownLev):Container(other),_componentNames(componentNames),
73                                      _launchModeType(const_cast<SalomeContainerHelper *>(lmt)),_shutdownLevel(shutdownLev),_sct(sct)
74 {
75   if(lmt)
76     lmt->incrRef();
77 }
78
79 SalomeContainer::~SalomeContainer()
80 {
81   _launchModeType->decrRef();
82 }
83
84 void SalomeContainer::lock()
85 {
86   _mutex.lock();
87 }
88
89 void SalomeContainer::unLock()
90 {
91   _mutex.unLock();
92 }
93
94 std::string SalomeContainer::getKind() const
95 {
96   return KIND;
97 }
98
99 Container *SalomeContainer::clone() const
100 {
101   if(_isAttachedOnCloning)
102     {
103       incrRef();
104       return (Container*) (this);
105     }
106   else
107     return new SalomeContainer(*this);
108 }
109
110 Container *SalomeContainer::cloneAlways() const
111 {
112   return new SalomeContainer(*this);
113 }
114
115 void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const
116 {
117   if(inst->getKind()!=SalomeComponent::KIND)
118     throw Exception("SalomeContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
119 }
120
121 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
122 {
123   if (name == AOC_ENTRY)
124     {
125       std::istringstream iss(value);
126       int val;
127       iss >> val;
128       setAttachOnCloningStatus((bool)val);
129     }
130   else if (name == TYPE_PROPERTY_STR)
131     {
132       if (value == SalomeContainerMonoHelper::TYPE_NAME)
133         {
134           _launchModeType->decrRef();
135           _launchModeType=new SalomeContainerMonoHelper;
136         }
137       else if (value == SalomeContainerMultiHelper::TYPE_NAME)
138         {
139           _launchModeType->decrRef();
140           _launchModeType=new SalomeContainerMultiHelper;
141         }
142       else
143         throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value);
144     }
145   _sct.setProperty(name,value);
146 }
147
148 std::string SalomeContainer::getProperty(const std::string& name) const
149 {
150   if (name == TYPE_PROPERTY_STR)
151     return _launchModeType->getType();
152   if (name==AOC_ENTRY)
153     {
154       int reti(_isAttachedOnCloning);
155       std::ostringstream oss; oss << reti;
156       return oss.str();
157     }
158   return _sct.getProperty(name);
159 }
160
161 void SalomeContainer::clearProperties()
162 {
163   _sct.clearProperties();
164 }
165
166 void SalomeContainer::addComponentName(const std::string& name)
167 {
168   _componentNames.push_back(name);
169 }
170
171 void SalomeContainer::addToResourceList(const std::string& name)
172 {
173   _sct.addToResourceList(name);
174 }
175
176 //! Load a component instance in this container
177 /*!
178  * \param inst the component instance to load
179  */
180 CORBA::Object_ptr SalomeContainer::loadComponent(Task *askingNode)
181 {
182   return SalomeContainerTools::LoadComponent(_launchModeType,this,askingNode);
183 }
184
185 //! Get the container placement id for a component instance
186 /*!
187  * \param inst the component instance
188  * \return the placement id
189  */
190 std::string SalomeContainer::getPlacementId(const Task *askingNode) const
191 {
192   return SalomeContainerTools::GetPlacementId(_launchModeType,this,askingNode);
193 }
194
195 //! Get the container full path for a component instance
196 /*!
197  * \param inst the component instance
198  * \return the full placement id
199  */
200 std::string SalomeContainer::getFullPlacementId(const Task *askingNode) const
201 {
202   return SalomeContainerTools::GetFullPlacementId(_launchModeType,this,askingNode);
203 }
204
205 //! Check if the component instance container is already started
206 /*!
207  * \param inst the component instance
208  * \return true, if the container is already started, else false
209  */
210 bool SalomeContainer::isAlreadyStarted(const Task *askingNode) const
211 {
212   return _launchModeType->isAlreadyStarted(askingNode);
213 }
214
215 Engines::Container_ptr SalomeContainer::getContainerPtr(const Task *askingNode) const
216 {
217   return Engines::Container::_duplicate(_launchModeType->getContainer(askingNode));
218 }
219
220 //! Start a salome container (true salome container not yacs one) with given ContainerParameters (_params)
221 /*!
222  * \param inst the component instance
223  */
224 void SalomeContainer::start(const Task *askingNode)
225 {
226   SalomeContainerTools::Start(_componentNames,_launchModeType,_sct,_shutdownLevel,this,askingNode);
227 }
228
229 void SalomeContainer::start(const Task *askingNode,
230                             const std::string& resource_name,
231                             const std::string& container_name)
232 {
233   if(canAcceptImposedResource()
234     && askingNode != nullptr
235     && askingNode->hasImposedResource())
236   {
237     SalomeContainerTools tempSct = _sct;
238     tempSct.setProperty("name", resource_name);
239     tempSct.setProperty("container_name", container_name);
240     //SalomeContainerTools::Start(_componentNames,_launchModeType,tempSct,_shutdownLevel,this,askingNode);
241     // components are not supported yet on this kind of start
242     std::vector<std::string> noComponentNames;
243     int shutdownLevel = 999;
244     SalomeContainerTools::Start(noComponentNames,_launchModeType,tempSct,shutdownLevel,this,askingNode);
245   }
246   else
247     start(askingNode);
248 }
249
250 bool SalomeContainer::canAcceptImposedResource()
251 {
252   return _launchModeType->getType() == SalomeContainerMultiHelper::TYPE_NAME;
253 }
254
255 void SalomeContainer::shutdown(int level)
256 {
257   DEBTRACE("SalomeContainer::shutdown: " << _name << "," << level << "," << _shutdownLevel);
258   if(level < _shutdownLevel)
259     return;
260
261   _shutdownLevel=999;
262   //shutdown the SALOME containers
263   _launchModeType->shutdown();
264 }
265
266 std::map<std::string,std::string> SalomeContainer::getResourceProperties(const std::string& name) const
267 {
268   return _sct.getResourceProperties(name);
269 }
270
271 std::map<std::string,std::string> SalomeContainer::getProperties() const
272 {
273   return _sct.getProperties();
274 }