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