Salome HOME
Save/Load manages HPContainers.
[modules/yacs.git] / src / runtime / SalomeContainer.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 //#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 SalomeContainer::SalomeContainer():_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
58 {
59 }
60
61 SalomeContainer::SalomeContainer(const SalomeContainer& other)
62 : Container(other),_componentNames(other._componentNames),
63   _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()),
64   _shutdownLevel(other._shutdownLevel),
65   _sct(other._sct)
66 {
67 }
68
69 SalomeContainer::SalomeContainer(const Container& other, const SalomeContainerTools& sct, const SalomeContainerHelper *lmt,
70                                  const std::vector<std::string>& componentNames, int shutdownLev):Container(other),_componentNames(componentNames),
71                                      _launchModeType(const_cast<SalomeContainerHelper *>(lmt)),_shutdownLevel(shutdownLev),_sct(sct)
72 {
73   if(lmt)
74     lmt->incrRef();
75 }
76
77 SalomeContainer::~SalomeContainer()
78 {
79   _launchModeType->decrRef();
80 }
81
82 void SalomeContainer::lock()
83 {
84   _mutex.lock();
85 }
86
87 void SalomeContainer::unLock()
88 {
89   _mutex.unLock();
90 }
91
92 std::string SalomeContainer::getKind() const
93 {
94   return KIND;
95 }
96
97 Container *SalomeContainer::clone() const
98 {
99   if(_isAttachedOnCloning)
100     {
101       incrRef();
102       return (Container*) (this);
103     }
104   else
105     return new SalomeContainer(*this);
106 }
107
108 Container *SalomeContainer::cloneAlways() const
109 {
110   return new SalomeContainer(*this);
111 }
112
113 void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
114 {
115   if(inst->getKind()!=SalomeComponent::KIND)
116     throw Exception("SalomeContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
117 }
118
119 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
120 {
121   if (name == "type")
122     {
123       if (value == SalomeContainerMonoHelper::TYPE_NAME)
124         {
125           _launchModeType->decrRef();
126           _launchModeType=new SalomeContainerMonoHelper;
127         }
128       else if (value == SalomeContainerMultiHelper::TYPE_NAME)
129         {
130           _launchModeType->decrRef();
131           _launchModeType=new SalomeContainerMultiHelper;
132         }
133       else
134         throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value);
135       return ;
136     }
137   _sct.setProperty(name,value);
138 }
139
140 std::string SalomeContainer::getProperty(const std::string& name) const
141 {
142   return _sct.getProperty(name);
143 }
144
145 void SalomeContainer::clearProperties()
146 {
147   _sct.clearProperties();
148 }
149
150 void SalomeContainer::addComponentName(const std::string& name)
151 {
152   _componentNames.push_back(name);
153 }
154
155 void SalomeContainer::addToResourceList(const std::string& name)
156 {
157   _sct.addToResourceList(name);
158 }
159
160 //! Load a component instance in this container
161 /*!
162  * \param inst the component instance to load
163  */
164 CORBA::Object_ptr SalomeContainer::loadComponent(Task *askingNode)
165 {
166   return SalomeContainerTools::LoadComponent(_launchModeType,this,askingNode);
167 }
168
169 //! Get the container placement id for a component instance
170 /*!
171  * \param inst the component instance
172  * \return the placement id
173  */
174 std::string SalomeContainer::getPlacementId(const Task *askingNode) const
175 {
176   return SalomeContainerTools::GetPlacementId(_launchModeType,this,askingNode);
177 }
178
179 //! Get the container full path for a component instance
180 /*!
181  * \param inst the component instance
182  * \return the full placement id
183  */
184 std::string SalomeContainer::getFullPlacementId(const Task *askingNode) const
185 {
186   return SalomeContainerTools::GetFullPlacementId(_launchModeType,this,askingNode);
187 }
188
189 //! Check if the component instance container is already started
190 /*!
191  * \param inst the component instance
192  * \return true, if the container is already started, else false
193  */
194 bool SalomeContainer::isAlreadyStarted(const Task *askingNode) const
195 {
196   return _launchModeType->isAlreadyStarted(askingNode);
197 }
198
199 Engines::Container_ptr SalomeContainer::getContainerPtr(const Task *askingNode) const
200 {
201   return Engines::Container::_duplicate(_launchModeType->getContainer(askingNode));
202 }
203
204 //! Start a salome container (true salome container not yacs one) with given ContainerParameters (_params)
205 /*!
206  * \param inst the component instance
207  */
208 void SalomeContainer::start(const Task *askingNode) throw(YACS::Exception)
209 {
210   SalomeContainerTools::Start(_componentNames,_launchModeType,_sct,_shutdownLevel,this,askingNode);
211 }
212
213 void SalomeContainer::shutdown(int level)
214 {
215   DEBTRACE("SalomeContainer::shutdown: " << _name << "," << level << "," << _shutdownLevel);
216   if(level < _shutdownLevel)
217     return;
218
219   _shutdownLevel=999;
220   //shutdown the SALOME containers
221   _launchModeType->shutdown();
222 }
223
224 std::map<std::string,std::string> SalomeContainer::getResourceProperties(const std::string& name) const
225 {
226   return _sct.getResourceProperties(name);
227 }
228
229 std::map<std::string,std::string> SalomeContainer::getProperties() const
230 {
231   return _sct.getProperties();
232 }