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