1 // Copyright (C) 2006-2014 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
29 #include "RuntimeSALOME.hxx"
30 #include "SalomeContainer.hxx"
31 #include "SalomeComponent.hxx"
34 #include "SALOME_NamingService.hxx"
35 #include "SALOME_LifeCycleCORBA.hxx"
36 #include "SALOME_ContainerManager.hxx"
37 #include "Basics_Utils.hxx"
45 #define getpid _getpid
49 #include "YacsTrace.hxx"
51 using namespace YACS::ENGINE;
54 SalomeContainer::SalomeContainer():_launchMode("start"),_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
58 SalomeContainer::SalomeContainer(const SalomeContainer& other)
60 _launchMode(other._launchMode),
61 _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()),
62 _shutdownLevel(other._shutdownLevel),
67 SalomeContainer::~SalomeContainer()
69 delete _launchModeType;
72 void SalomeContainer::lock()
77 void SalomeContainer::unLock()
82 Container *SalomeContainer::clone() const
84 if(_isAttachedOnCloning)
87 return (Container*) (this);
90 return new SalomeContainer(*this);
93 Container *SalomeContainer::cloneAlways() const
95 return new SalomeContainer(*this);
98 void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
100 if(inst->getKind()!=SalomeComponent::KIND)
101 throw Exception("SalomeContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
104 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
108 if (value == SalomeContainerMonoHelper::TYPE_NAME)
110 delete _launchModeType;
111 _launchModeType=new SalomeContainerMonoHelper;
113 else if (value == SalomeContainerMultiHelper::TYPE_NAME)
115 delete _launchModeType;
116 _launchModeType=new SalomeContainerMultiHelper;
119 throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value);
122 _sct.setProperty(name,value);
125 std::string SalomeContainer::getProperty(const std::string& name) const
127 return _sct.getProperty(name);
130 void SalomeContainer::clearProperties()
132 _sct.clearProperties();
135 void SalomeContainer::addComponentName(std::string name)
137 _componentNames.push_back(name);
140 void SalomeContainer::addToResourceList(const std::string& name)
142 _sct.addToResourceList(name);
145 //! Load a component instance in this container
147 * \param inst the component instance to load
149 CORBA::Object_ptr SalomeContainer::loadComponent(ComponentInstance *inst)
151 DEBTRACE("SalomeContainer::loadComponent ");
153 if(!isAlreadyStarted(inst))
167 CORBA::Object_ptr objComponent=CORBA::Object::_nil();
168 std::string compoName=inst->getCompoName();
169 const char* componentName=compoName.c_str();
170 Engines::Container_var container(_launchModeType->getContainer(inst));
174 bool isLoadable = container->load_component_Library(componentName, reason);
177 CORBA::string_free(reason);
182 std::string value=p->getProperty("DefaultStudyID");
184 studyid= atoi(value.c_str());
186 // prepare component instance properties
187 Engines::FieldsDict_var env = new Engines::FieldsDict;
188 std::map<std::string, std::string> properties = inst->getProperties();
191 std::map<std::string,std::string> procMap=p->getProperties();
192 properties.insert(procMap.begin(),procMap.end());
195 std::map<std::string, std::string>::const_iterator itm;
196 env->length(properties.size());
198 for(itm = properties.begin(); itm != properties.end(); ++itm, item++)
200 DEBTRACE("envname="<<itm->first<<" envvalue="<< itm->second);
201 env[item].key= CORBA::string_dup(itm->first.c_str());
202 env[item].value <<= itm->second.c_str();
205 objComponent=container->create_component_instance_env(componentName, studyid, env, reason);
208 if(CORBA::is_nil(objComponent))
211 std::string text="Error while trying to create a new component: component '"+ compoName;
212 text=text+"' is not installed or it's a wrong name";
215 CORBA::string_free(reason);
216 throw Exception(text);
222 //! Get the container placement id for a component instance
224 * \param inst the component instance
225 * \return the placement id
227 std::string SalomeContainer::getPlacementId(const ComponentInstance *inst) const
230 if(isAlreadyStarted(inst))
232 Engines::Container_var container(_launchModeType->getContainer(inst));
233 const char *what="/";
234 CORBA::String_var corbaStr=container->name();
235 string ret(corbaStr);
238 std::string::size_type i=ret.find_first_of(what,0);
239 i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
240 if(i!=std::string::npos)
241 return ret.substr(i+1);
245 return "Not placed yet !!!";
248 //! Get the container full path for a component instance
250 * \param inst the component instance
251 * \return the full placement id
253 std::string SalomeContainer::getFullPlacementId(const ComponentInstance *inst) const
256 if(isAlreadyStarted(inst))
258 Engines::Container_var container(_launchModeType->getContainer(inst));
261 CORBA::String_var corbaStr=container->name();
262 string ret(corbaStr);
267 return "Unknown_placement";
271 return "Not_placed_yet";
274 //! Check if the component instance container is already started
276 * \param inst the component instance
277 * \return true, if the container is already started, else false
279 bool SalomeContainer::isAlreadyStarted(const ComponentInstance *inst) const
281 return _launchModeType->isAlreadyStarted(inst);
284 Engines::Container_ptr SalomeContainer::getContainerPtr(const ComponentInstance *inst) const
286 return Engines::Container::_duplicate(_launchModeType->getContainer(inst));
289 //! Start a salome container (true salome container not yacs one) with given ContainerParameters (_params)
291 * \param inst the component instance
293 void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception)
295 CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb());
296 SALOME_NamingService ns;
301 catch(SALOME_Exception& e)
303 throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service");
305 CORBA::Object_var obj(ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS));
306 Engines::ContainerManager_var contManager(Engines::ContainerManager::_narrow(obj));
308 std::string str(_sct.getContainerName());
309 DEBTRACE("SalomeContainer::start " << str <<";"<< _sct.getHostName() <<";"<<_type);
311 // Finalize parameters with components found in the container
312 std::vector<std::string>::iterator iter;
313 for(CORBA::ULong i=0; i < _componentNames.size();i++)
314 _sct.addToComponentList(_componentNames[i]);
315 Engines::ContainerParameters myparams(_sct.getParameters());
317 bool namedContainer=false;
321 //If a container_name is given try to find an already existing container in naming service
322 //If not found start a new container with the given parameters
323 if (dynamic_cast<SalomeContainerMonoHelper *>(_launchModeType) && str != "")
325 myparams.mode="getorstart";
330 //give a almost unique name to the container : Pid_Name_Addr
331 std::ostringstream stream;
336 stream << (void *)(this);
337 DEBTRACE("container_name="<<stream.str());
338 myparams.container_name=CORBA::string_dup(stream.str().c_str());
342 Engines::Container_var trueCont(Engines::Container::_nil());
343 if(namedContainer && _shutdownLevel==999)
345 //Make this only the first time start is called (_shutdownLevel==999)
346 //If the container is named, first try to get an existing container
347 //If there is an existing container use it and set the shutdown level to 3
348 //If there is no existing container, try to launch a new one and set the shutdown level to 2
352 trueCont=contManager->GiveContainer(myparams);
354 catch( const SALOME::SALOME_Exception& ex )
356 std::string msg="SalomeContainer::start : no existing container : ";
358 msg += ex.details.text.in();
365 if(!CORBA::is_nil(trueCont))
368 DEBTRACE( "container found: " << str << " " << _shutdownLevel );
373 myparams.mode="start";
374 DEBTRACE( "container not found: " << str << " " << _shutdownLevel);
378 if(CORBA::is_nil(trueCont))
381 // --- GiveContainer is used in batch mode to retreive launched containers,
382 // and is equivalent to StartContainer when not in batch.
383 trueCont=contManager->GiveContainer(myparams);
385 catch( const SALOME::SALOME_Exception& ex )
387 std::string msg="SalomeContainer::start : Unable to launch container in Salome : ";
389 msg += ex.details.text.in();
390 throw Exception(msg);
392 catch(CORBA::COMM_FAILURE&)
394 throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
396 catch(CORBA::Exception&)
398 throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
401 if(CORBA::is_nil(trueCont))
402 throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
404 _launchModeType->setContainer(inst,trueCont);
406 CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName());
407 std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl;
410 void SalomeContainer::shutdown(int level)
412 DEBTRACE("SalomeContainer::shutdown: " << _name << "," << level << "," << _shutdownLevel);
413 if(level < _shutdownLevel)
417 //shutdown the SALOME containers
418 _launchModeType->shutdown();
421 std::map<std::string,std::string> SalomeContainer::getResourceProperties(const std::string& name) const
423 return _sct.getResourceProperties(name);
426 std::map<std::string,std::string> SalomeContainer::getProperties() const
428 return _sct.getProperties();