From fa7b5992ceade15172227d7a2aa9218fedc9b544 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 31 Jul 2014 11:42:35 +0200 Subject: [PATCH] some factorizations before introducing new type of yacs container. --- src/engine/Container.cxx | 22 +- src/engine/Container.hxx | 13 +- src/engine/Test/ContainerTest.hxx | 14 + src/runtime/CMakeLists.txt | 6 +- src/runtime/CppContainer.hxx | 24 +- src/runtime/SalomeContainer.cxx | 379 ++++---------------------- src/runtime/SalomeContainer.hxx | 26 +- src/runtime/SalomeContainerHelper.cxx | 164 +++++++++++ src/runtime/SalomeContainerHelper.hxx | 86 ++++++ src/runtime/SalomeContainerTools.cxx | 263 ++++++++++++++++++ src/runtime/SalomeContainerTools.hxx | 57 ++++ src/runtime/SalomeHPContainer.hxx | 77 ++++++ 12 files changed, 761 insertions(+), 370 deletions(-) create mode 100644 src/runtime/SalomeContainerHelper.cxx create mode 100644 src/runtime/SalomeContainerHelper.hxx create mode 100644 src/runtime/SalomeContainerTools.cxx create mode 100644 src/runtime/SalomeContainerTools.hxx create mode 100644 src/runtime/SalomeHPContainer.hxx diff --git a/src/engine/Container.cxx b/src/engine/Container.cxx index 282753669..133c5784a 100644 --- a/src/engine/Container.cxx +++ b/src/engine/Container.cxx @@ -64,26 +64,10 @@ bool Container::isSupportingRTODefNbOfComp() const return true; } -void Container::setProperty(const std::string& name, const std::string& value) +void Container::setProperties(const std::map& properties) { - DEBTRACE("Container::setProperty " << name << " " << value); - _propertyMap[name]=value; -} - -std::string Container::getProperty(const std::string& name) -{ - DEBTRACE("Container::getProperty " << name ); - return _propertyMap[name]; -} - -void Container::setProperties(std::map properties) -{ - _propertyMap.clear(); - std::map::iterator it; - for (it = properties.begin(); it != properties.end(); ++it) - { - setProperty((*it).first, (*it).second); // setProperty virtual and derived - } + for (std::map::const_iterator it=properties.begin();it!=properties.end();++it) + setProperty((*it).first,(*it).second); } void Container::shutdown(int level) diff --git a/src/engine/Container.hxx b/src/engine/Container.hxx index 9cbe7bb27..5e37e4a60 100644 --- a/src/engine/Container.hxx +++ b/src/engine/Container.hxx @@ -59,22 +59,21 @@ namespace YACS virtual Container *cloneAlways() const = 0; virtual bool isSupportingRTODefNbOfComp() const; virtual void checkCapabilityToDealWith(const ComponentInstance *inst) const throw(Exception) = 0; - virtual void setProperty(const std::string& name,const std::string& value); - virtual std::string getProperty(const std::string& name); - std::map getProperties() { return _propertyMap; }; - virtual void setProperties(std::map properties); + virtual void setProperty(const std::string& name,const std::string& value) = 0; + virtual std::string getProperty(const std::string& name) const = 0; + virtual void clearProperties() = 0; + virtual std::map getProperties() const = 0; + virtual std::map getResourceProperties(const std::string& name) const = 0; + virtual void setProperties(const std::map& properties); std::string getName() const { return _name; }; //! \b WARNING ! name is used in edition to identify different containers, it is not the runtime name of the container void setName(std::string name) { _name = name; }; void setProc(Proc* proc) { _proc = proc; }; Proc* getProc() { return _proc; }; virtual void shutdown(int level); - virtual std::map getResourceProperties(const std::string& name) { return _propertyMap; }; - protected: std::string _name; mutable bool _isAttachedOnCloning; - std::map _propertyMap; Proc* _proc; }; } diff --git a/src/engine/Test/ContainerTest.hxx b/src/engine/Test/ContainerTest.hxx index 291a9feb7..9dc1f3d7e 100644 --- a/src/engine/Test/ContainerTest.hxx +++ b/src/engine/Test/ContainerTest.hxx @@ -36,6 +36,13 @@ namespace YACS void start(const ComponentInstance *inst) throw(Exception); Container *clone() const; Container *cloneAlways() const; + // + void clearProperties() { } + void setProperty(const std::string& name,const std::string& value) { } + std::string getProperty(const std::string& name) const { } + std::map getResourceProperties(const std::string& name) const { return std::map(); } + std::map getProperties() const { return std::map(); } + // std::string getPlacementId(const ComponentInstance *inst) const { return ""; } std::string getFullPlacementId(const ComponentInstance *inst) const { return ""; } static void initAllContainers(); @@ -57,6 +64,13 @@ namespace YACS void start(const ComponentInstance *inst) throw(Exception); Container *clone() const; Container *cloneAlways() const; + // + void clearProperties() { } + void setProperty(const std::string& name,const std::string& value) { } + std::string getProperty(const std::string& name) const { } + std::map getResourceProperties(const std::string& name) const { return std::map(); } + std::map getProperties() const { return std::map(); } + // std::string getPlacementId(const ComponentInstance *inst) const { return ""; } std::string getFullPlacementId(const ComponentInstance *inst) const { return ""; } static void initAllContainers(); diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt index 451934b05..c5d9ef548 100644 --- a/src/runtime/CMakeLists.txt +++ b/src/runtime/CMakeLists.txt @@ -116,7 +116,9 @@ SET(YACSRuntimeSALOME_HEADERS PythonXMLConv.hxx RuntimeSALOME.hxx SalomeComponent.hxx - SalomeContainer.hxx + SalomeContainer.hxx + SalomeContainerTools.hxx + SalomeContainerHelper.hxx SALOMEDispatcher.hxx SalomeProc.hxx SalomePythonComponent.hxx @@ -167,6 +169,8 @@ SET(YACSRuntimeSALOME_SOURCES DistributedPythonNode.cxx SalomePythonComponent.cxx SalomeContainer.cxx + SalomeContainerTools.cxx + SalomeContainerHelper.cxx PythonPorts.cxx XMLNode.cxx XMLPorts.cxx diff --git a/src/runtime/CppContainer.hxx b/src/runtime/CppContainer.hxx index ada94cc26..012e8d17a 100644 --- a/src/runtime/CppContainer.hxx +++ b/src/runtime/CppContainer.hxx @@ -100,13 +100,11 @@ namespace YACS }; - class CppContainer : public Container { - + class CppContainer : public Container + { friend class CppComponent; friend class LocalContainer; - public: - CppContainer(); virtual ~CppContainer(); bool isAlreadyStarted(const ComponentInstance *inst) const; @@ -117,19 +115,25 @@ namespace YACS Container *cloneAlways() const; void lock(); void unLock(); - + void checkCapabilityToDealWith(const ComponentInstance *inst) const throw (YACS::Exception); bool loadComponentLibrary(const std::string & componentName) throw (YACS::Exception); CppComponent * createComponentInstance(const std::string & componentName, int studyID = 0); void createInternalInstance(const std::string & componentName, void *& obj, RunFunction &r, TerminateFunction &t); void unregisterComponentInstance(CppComponent * C); - - protected: - YACS::BASES::Mutex _mutex; - LocalContainer * _trueCont; + // + void setProperty(const std::string& name,const std::string& value) { } + std::string getProperty(const std::string& name) const { return std::string(); } + void clearProperties() { } + std::map getProperties() const { return std::map(); } + std::map getResourceProperties(const std::string& name) const { return std::map(); } + // + protected: + YACS::BASES::Mutex _mutex; + LocalContainer * _trueCont; - }; + }; }; }; diff --git a/src/runtime/SalomeContainer.cxx b/src/runtime/SalomeContainer.cxx index 1557a53d1..cea49d6a0 100644 --- a/src/runtime/SalomeContainer.cxx +++ b/src/runtime/SalomeContainer.cxx @@ -51,24 +51,22 @@ using namespace YACS::ENGINE; using namespace std; -SalomeContainer::SalomeContainer():_trueCont(Engines::Container::_nil()),_type("mono"),_shutdownLevel(999) +SalomeContainer::SalomeContainer():_launchMode("start"),_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999) { - /* Init ContainerParameters */ - SALOME_LifeCycleCORBA::preSet(_params); - _params.mode= "start"; } SalomeContainer::SalomeContainer(const SalomeContainer& other) : Container(other), - _trueCont(Engines::Container::_nil()), - _type(other._type), + _launchMode(other._launchMode), + _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()), _shutdownLevel(other._shutdownLevel), - _params(other._params) + _sct(other._sct) { } SalomeContainer::~SalomeContainer() { + delete _launchModeType; } void SalomeContainer::lock() @@ -105,142 +103,33 @@ void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) c void SalomeContainer::setProperty(const std::string& name, const std::string& value) { - DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value); - - // Container Part - if (name == "container_name") - _params.container_name = CORBA::string_dup(value.c_str()); - else if (name == "type") - { - if (value == "mono") - _params.mode = "start"; - else if (value == "multi") - _params.mode = "getorstart"; - else - throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value); - _type=value; - } - else if (name == "workingdir") - _params.workingdir = CORBA::string_dup(value.c_str()); - else if (name == "nb_parallel_procs") - { - std::istringstream iss(value); - if (!(iss >> _params.nb_proc)) - throw Exception("salomecontainer::setproperty : params.nb_proc value not correct : " + value); - } - else if (name == "isMPI") - { - if (value == "true") - _params.isMPI = true; - else if (value == "false") - _params.isMPI = false; - else - throw Exception("SalomeContainer::setProperty : params.isMPI value not correct : " + value); - } - else if (name == "parallelLib") - _params.parallelLib = CORBA::string_dup(value.c_str()); - - // Resource part - else if (name == "name") - _params.resource_params.name = CORBA::string_dup(value.c_str()); - else if (name == "hostname") - _params.resource_params.hostname = CORBA::string_dup(value.c_str()); - else if (name == "OS") - _params.resource_params.OS = CORBA::string_dup(value.c_str()); - else if (name == "nb_resource_procs") - { - std::istringstream iss(value); - if (!(iss >> _params.resource_params.nb_proc)) - throw Exception("salomecontainer::setproperty : params.resource_params.nb_proc value not correct : " + value); - } - else if (name == "mem_mb") - { - std::istringstream iss(value); - if (!(iss >> _params.resource_params.mem_mb)) - throw Exception("salomecontainer::setproperty : params.resource_params.mem_mb value not correct : " + value); - } - else if (name == "cpu_clock") - { - std::istringstream iss(value); - if (!(iss >> _params.resource_params.cpu_clock)) - throw Exception("salomecontainer::setproperty : params.resource_params.cpu_clock value not correct : " + value); - } - else if (name == "nb_node") - { - std::istringstream iss(value); - if (!(iss >> _params.resource_params.nb_node)) - throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value); - } - else if (name == "nb_proc_per_node") - { - std::istringstream iss(value); - if (!(iss >> _params.resource_params.nb_proc_per_node)) - throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value); - } - else if (name == "policy") - _params.resource_params.policy = CORBA::string_dup(value.c_str()); - else if (name == "component_list") - { - std::string clean_value(value); - - // Step 1: remove blanks - while(clean_value.find(" ") != std::string::npos) - clean_value = clean_value.erase(clean_value.find(" "), 1); - - // Step 2: get values - while(!clean_value.empty()) + if (name == "type") { - std::string result(""); - std::string::size_type loc = clean_value.find(",", 0); - if (loc != std::string::npos) - { - result = clean_value.substr(0, loc); - clean_value = clean_value.erase(0, loc+1); - } + if (value == SalomeContainerMonoHelper::TYPE_NAME) + { + delete _launchModeType; + _launchModeType=new SalomeContainerMonoHelper; + } + else if (value == SalomeContainerMultiHelper::TYPE_NAME) + { + delete _launchModeType; + _launchModeType=new SalomeContainerMultiHelper; + } else - { - result = clean_value; - clean_value.erase(); - } - if (result != "," && result != "") - { - addToComponentList(result); - } + throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value); + return ; } + _sct.setProperty(name,value); +} - } - else if (name == "resource_list") - { - std::string clean_value(value); - - // Step 1: remove blanks - while(clean_value.find(" ") != std::string::npos) - clean_value = clean_value.erase(clean_value.find(" "), 1); - - // Step 2: get values - while(!clean_value.empty()) - { - std::string result(""); - std::string::size_type loc = clean_value.find(",", 0); - if (loc != std::string::npos) - { - result = clean_value.substr(0, loc); - clean_value = clean_value.erase(0, loc+1); - } - else - { - result = clean_value; - clean_value.erase(); - } - if (result != "," && result != "") - { - addToResourceList(result); - } - } +std::string SalomeContainer::getProperty(const std::string& name) const +{ + return _sct.getProperty(name); +} - } - // End - Container::setProperty(name, value); +void SalomeContainer::clearProperties() +{ + _sct.clearProperties(); } void SalomeContainer::addComponentName(std::string name) @@ -248,6 +137,11 @@ void SalomeContainer::addComponentName(std::string name) _componentNames.push_back(name); } +void SalomeContainer::addToResourceList(const std::string& name) +{ + _sct.addToResourceList(name); +} + //! Load a component instance in this container /*! * \param inst the component instance to load @@ -273,11 +167,7 @@ CORBA::Object_ptr SalomeContainer::loadComponent(ComponentInstance *inst) CORBA::Object_ptr objComponent=CORBA::Object::_nil(); std::string compoName=inst->getCompoName(); const char* componentName=compoName.c_str(); - Engines::Container_var container; - if(_type=="multi") - container=_trueContainers[inst]; - else - container=_trueCont; + Engines::Container_var container(_launchModeType->getContainer(inst)); char* reason; @@ -339,12 +229,7 @@ std::string SalomeContainer::getPlacementId(const ComponentInstance *inst) const if(isAlreadyStarted(inst)) { - Engines::Container_var container=_trueCont; - if(_type=="multi") - { - std::map::const_iterator found = _trueContainers.find(inst); - container=found->second; - } + Engines::Container_var container(_launchModeType->getContainer(inst)); const char *what="/"; CORBA::String_var corbaStr=container->name(); string ret(corbaStr); @@ -370,12 +255,7 @@ std::string SalomeContainer::getFullPlacementId(const ComponentInstance *inst) c if(isAlreadyStarted(inst)) { - Engines::Container_var container=_trueCont; - if(_type=="multi") - { - std::map::const_iterator found = _trueContainers.find(inst); - container=found->second; - } + Engines::Container_var container(_launchModeType->getContainer(inst)); try { CORBA::String_var corbaStr=container->name(); @@ -398,41 +278,12 @@ std::string SalomeContainer::getFullPlacementId(const ComponentInstance *inst) c */ bool SalomeContainer::isAlreadyStarted(const ComponentInstance *inst) const { - if(_type=="mono") - { - if(CORBA::is_nil(_trueCont)) - return false; - else - return true; - } - else - { - if(_trueContainers.count(inst)==0) - return false; - else - return true; - } + return _launchModeType->isAlreadyStarted(inst); } Engines::Container_ptr SalomeContainer::getContainerPtr(const ComponentInstance *inst) const { - if(_type=="mono") - { - if(CORBA::is_nil(_trueCont)) - return Engines::Container::_nil(); - else - return Engines::Container::_duplicate(_trueCont); - } - else - { - if(_trueContainers.count(inst)==0) - return Engines::Container::_nil(); - else - { - std::map::const_iterator iter=_trueContainers.find(inst); - return Engines::Container::_duplicate(iter->second); - } - } + return Engines::Container::_duplicate(_launchModeType->getContainer(inst)); } //! Start a salome container (true salome container not yacs one) with given ContainerParameters (_params) @@ -441,7 +292,7 @@ Engines::Container_ptr SalomeContainer::getContainerPtr(const ComponentInstance */ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception) { - CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb(); + CORBA::ORB_ptr orb(getSALOMERuntime()->getOrb()); SALOME_NamingService ns; try { @@ -451,17 +302,17 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception { throw Exception("SalomeContainer::start : Unable to contact the SALOME Naming Service"); } - CORBA::Object_var obj=ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS); - Engines::ContainerManager_var contManager=Engines::ContainerManager::_narrow(obj); + CORBA::Object_var obj(ns.Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS)); + Engines::ContainerManager_var contManager(Engines::ContainerManager::_narrow(obj)); - std::string str(_params.container_name); - DEBTRACE("SalomeContainer::start " << str <<";"<<_params.resource_params.hostname <<";"<<_type); + std::string str(_sct.getContainerName()); + DEBTRACE("SalomeContainer::start " << str <<";"<< _sct.getHostName() <<";"<<_type); // Finalize parameters with components found in the container std::vector::iterator iter; for(CORBA::ULong i=0; i < _componentNames.size();i++) - addToComponentList(_componentNames[i]); - Engines::ContainerParameters myparams = _params; + _sct.addToComponentList(_componentNames[i]); + Engines::ContainerParameters myparams(_sct.getParameters()); bool namedContainer=false; if(str != "") @@ -469,7 +320,7 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception //If a container_name is given try to find an already existing container in naming service //If not found start a new container with the given parameters - if (_type=="mono" && str != "") + if (dynamic_cast(_launchModeType) && str != "") { myparams.mode="getorstart"; } @@ -488,7 +339,7 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception _shutdownLevel=1; } - _trueCont=Engines::Container::_nil(); + Engines::Container_var trueCont(Engines::Container::_nil()); if(namedContainer && _shutdownLevel==999) { //Make this only the first time start is called (_shutdownLevel==999) @@ -498,7 +349,7 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception myparams.mode="get"; try { - _trueCont=contManager->GiveContainer(myparams); + trueCont=contManager->GiveContainer(myparams); } catch( const SALOME::SALOME_Exception& ex ) { @@ -511,7 +362,7 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception { } - if(!CORBA::is_nil(_trueCont)) + if(!CORBA::is_nil(trueCont)) { _shutdownLevel=3; DEBTRACE( "container found: " << str << " " << _shutdownLevel ); @@ -524,12 +375,12 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception } } - if(CORBA::is_nil(_trueCont)) + if(CORBA::is_nil(trueCont)) try { // --- GiveContainer is used in batch mode to retreive launched containers, // and is equivalent to StartContainer when not in batch. - _trueCont=contManager->GiveContainer(myparams); + trueCont=contManager->GiveContainer(myparams); } catch( const SALOME::SALOME_Exception& ex ) { @@ -547,23 +398,13 @@ void SalomeContainer::start(const ComponentInstance *inst) throw(YACS::Exception throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected"); } - if(CORBA::is_nil(_trueCont)) + if(CORBA::is_nil(trueCont)) throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file"); - _trueContainers[inst]=_trueCont; - - CORBA::String_var containerName=_trueCont->name(); - CORBA::String_var hostName=_trueCont->getHostName(); - std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << _trueCont->getPID() << std::endl; + _launchModeType->setContainer(inst,trueCont); -#ifdef REFCNT - DEBTRACE(_trueCont->_PR_getobj()->pd_refCount ); - std::map::const_iterator it; - for(it = _trueContainers.begin(); it != _trueContainers.end(); ++it) - { - DEBTRACE(it->second->_PR_getobj()->pd_refCount ); - } -#endif + CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName()); + std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl; } void SalomeContainer::shutdown(int level) @@ -574,117 +415,15 @@ void SalomeContainer::shutdown(int level) _shutdownLevel=999; //shutdown the SALOME containers - if(_type=="multi") - { - std::map::const_iterator it; - for(it = _trueContainers.begin(); it != _trueContainers.end(); ++it) - { - try - { - DEBTRACE("shutdown SALOME container: " ); - CORBA::String_var containerName=it->second->name(); - DEBTRACE(containerName); - it->second->Shutdown(); - std::cerr << "shutdown SALOME container: " << containerName << std::endl; - } - catch(CORBA::Exception&) - { - DEBTRACE("Unexpected CORBA failure detected." ); - } - catch(...) - { - DEBTRACE("Unknown exception ignored." ); - } - } - _trueContainers.clear(); - } - else - { - try - { - DEBTRACE("shutdown SALOME container: " ); - CORBA::String_var containerName=_trueCont->name(); - DEBTRACE(containerName); - _trueCont->Shutdown(); - std::cerr << "shutdown SALOME container: " << containerName << std::endl; - } - catch(...) - { - DEBTRACE("Unknown exception ignored." ); - } - _trueCont=Engines::Container::_nil(); - } + _launchModeType->shutdown(); } -void -SalomeContainer::addToComponentList(const std::string & name) +std::map SalomeContainer::getResourceProperties(const std::string& name) const { - // Search if name is already in the list - for (CORBA::ULong i = 0; i < _params.resource_params.componentList.length(); i++) - { - std::string component_name = _params.resource_params.componentList[i].in(); - if (component_name == name) - return; - } - - // Add name to list - CORBA::ULong lgth = _params.resource_params.componentList.length(); - _params.resource_params.componentList.length(lgth + 1); - _params.resource_params.componentList[lgth] = CORBA::string_dup(name.c_str()); -} - -void -SalomeContainer::addToResourceList(const std::string & name) -{ - // Search if name is already in the list - for (CORBA::ULong i = 0; i < _params.resource_params.resList.length(); i++) - { - std::string component_name = _params.resource_params.resList[i].in(); - if (component_name == name) - return; - } - - // Add name to list - CORBA::ULong lgth = _params.resource_params.resList.length(); - _params.resource_params.resList.length(lgth + 1); - _params.resource_params.resList[lgth] = CORBA::string_dup(name.c_str()); + return _sct.getResourceProperties(name); } -std::map SalomeContainer::getResourceProperties(const std::string& name) +std::map SalomeContainer::getProperties() const { - std::map properties; - - YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime(); - CORBA::ORB_ptr orb = runTime->getOrb(); - if (!orb) return properties; - SALOME_NamingService namingService(orb); - SALOME_LifeCycleCORBA lcc(&namingService); - CORBA::Object_var obj = namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS); - if (CORBA::is_nil(obj)) return properties; - Engines::ResourcesManager_var resManager = Engines::ResourcesManager::_narrow(obj); - if (CORBA::is_nil(resManager)) return properties; - - std::ostringstream value; - Engines::ResourceDefinition_var resource_definition = resManager->GetResourceDefinition(name.c_str()); - properties["hostname"]=resource_definition->hostname.in(); - properties["OS"]=resource_definition->OS.in(); - value.str(""); value << resource_definition->mem_mb; - properties["mem_mb"]=value.str(); - value.str(""); value << resource_definition->cpu_clock; - properties["cpu_clock"]=value.str(); - value.str(""); value << resource_definition->nb_node; - properties["nb_node"]=value.str(); - value.str(""); value << resource_definition->nb_proc_per_node; - properties["nb_proc_per_node"]=value.str(); - /* - properties["component_list"]=""; - for(CORBA::ULong i=0; i < resource_definition->componentList.length(); i++) - { - if(i > 0) - properties["component_list"]=properties["component_list"]+","; - properties["component_list"]=properties["component_list"]+resource_definition->componentList[i].in(); - } - */ - - return properties; + return _sct.getProperties(); } diff --git a/src/runtime/SalomeContainer.hxx b/src/runtime/SalomeContainer.hxx index 49f718788..fc6d6b2aa 100644 --- a/src/runtime/SalomeContainer.hxx +++ b/src/runtime/SalomeContainer.hxx @@ -22,12 +22,12 @@ #include "YACSRuntimeSALOMEExport.hxx" #include "Container.hxx" +#include "SalomeContainerTools.hxx" +#include "SalomeContainerHelper.hxx" #include "Mutex.hxx" #include #include -#include #include CORBA_CLIENT_HEADER(SALOME_Component) -#include CORBA_CLIENT_HEADER(SALOME_ContainerManager) namespace YACS { @@ -53,14 +53,16 @@ namespace YACS std::string getPlacementId(const ComponentInstance *inst) const; std::string getFullPlacementId(const ComponentInstance *inst) const; void checkCapabilityToDealWith(const ComponentInstance *inst) const throw (Exception); - virtual void setProperty(const std::string& name, const std::string& value); - virtual void addComponentName(std::string name); + void setProperty(const std::string& name, const std::string& value); + std::string getProperty(const std::string& name) const; + void clearProperties(); + void addComponentName(std::string name); + void addToResourceList(const std::string& name); virtual CORBA::Object_ptr loadComponent(ComponentInstance *inst); - virtual void shutdown(int level); + void shutdown(int level); // Helper methods - void addToComponentList(const std::string & name); - void addToResourceList(const std::string & name); - virtual std::map getResourceProperties(const std::string& name); + std::map getResourceProperties(const std::string& name) const; + std::map getProperties() const; protected: #ifndef SWIG virtual ~SalomeContainer(); @@ -68,13 +70,11 @@ namespace YACS protected: //! thread safety in Salome ??? YACS::BASES::Mutex _mutex; - Engines::Container_var _trueCont; std::vector _componentNames; - std::map _trueContainers; - std::string _type; + std::string _launchMode; + SalomeContainerHelper *_launchModeType; int _shutdownLevel; - public: - Engines::ContainerParameters _params; + SalomeContainerTools _sct; }; } } diff --git a/src/runtime/SalomeContainerHelper.cxx b/src/runtime/SalomeContainerHelper.cxx new file mode 100644 index 000000000..3afa30894 --- /dev/null +++ b/src/runtime/SalomeContainerHelper.cxx @@ -0,0 +1,164 @@ +// Copyright (C) 2006-2014 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "SalomeContainerHelper.hxx" + +#include "YacsTrace.hxx" + +#include +#include + +using namespace YACS::ENGINE; +using namespace std; + +const char SalomeContainerMonoHelper::TYPE_NAME[]="mono"; + +const char SalomeContainerMonoHelper::DFT_LAUNCH_MODE[]="start"; + +const char SalomeContainerMultiHelper::TYPE_NAME[]="multi"; + +const char SalomeContainerMultiHelper::DFT_LAUNCH_MODE[]="getorstart"; + +SalomeContainerHelper::~SalomeContainerHelper() +{ +} + +SalomeContainerMonoHelper::SalomeContainerMonoHelper():_trueCont(Engines::Container::_nil()) +{ + +} + +std::string SalomeContainerMonoHelper::getType() const +{ + return TYPE_NAME; +} + +SalomeContainerMonoHelper *SalomeContainerMonoHelper::deepCpyOnlyStaticInfo() const +{ + return new SalomeContainerMonoHelper; +} + +Engines::Container_var SalomeContainerMonoHelper::getContainer(const ComponentInstance *inst) const +{ + return _trueCont; +} + +bool SalomeContainerMonoHelper::isAlreadyStarted(const ComponentInstance *inst) const +{ + if(CORBA::is_nil(_trueCont)) + return false; + else + return true; +} + +void SalomeContainerMonoHelper::setContainer(const ComponentInstance *inst, Engines::Container_var cont) +{ + _trueCont=cont; +#ifdef REFCNT + DEBTRACE(_trueCont->_PR_getobj()->pd_refCount ); +#endif +} + +void SalomeContainerMonoHelper::shutdown() +{ + try + { + DEBTRACE("shutdown SALOME container: " ); + CORBA::String_var containerName=_trueCont->name(); + DEBTRACE(containerName); + _trueCont->Shutdown(); + std::cerr << "shutdown SALOME container: " << containerName << std::endl; + } + catch(...) + { + DEBTRACE("Unknown exception ignored." ); + } + _trueCont=Engines::Container::_nil(); +} + +SalomeContainerMonoHelper::~SalomeContainerMonoHelper() +{ +} + +std::string SalomeContainerMultiHelper::getType() const +{ + return TYPE_NAME; +} + +SalomeContainerMultiHelper *SalomeContainerMultiHelper::deepCpyOnlyStaticInfo() const +{ + return new SalomeContainerMultiHelper; +} + +Engines::Container_var SalomeContainerMultiHelper::getContainer(const ComponentInstance *inst) const +{ + std::map::const_iterator it(_trueContainers.find(inst)); + if(it!=_trueContainers.end()) + return (*it).second; + else + return Engines::Container::_nil(); +} + +bool SalomeContainerMultiHelper::isAlreadyStarted(const ComponentInstance *inst) const +{ + if(_trueContainers.count(inst)==0) + return false; + else + return true; +} + +void SalomeContainerMultiHelper::setContainer(const ComponentInstance *inst, Engines::Container_var cont) +{ + _trueContainers[inst]=cont; +#ifdef REFCNT + std::map::const_iterator it; + for(it = _trueContainers.begin(); it != _trueContainers.end(); ++it) + { + DEBTRACE(it->second->_PR_getobj()->pd_refCount ); + } +#endif +} + +void SalomeContainerMultiHelper::shutdown() +{ + for(std::map::const_iterator it = _trueContainers.begin(); it != _trueContainers.end(); ++it) + { + try + { + DEBTRACE("shutdown SALOME container: " ); + CORBA::String_var containerName=it->second->name(); + DEBTRACE(containerName); + it->second->Shutdown(); + std::cerr << "shutdown SALOME container: " << containerName << std::endl; + } + catch(CORBA::Exception&) + { + DEBTRACE("Unexpected CORBA failure detected." ); + } + catch(...) + { + DEBTRACE("Unknown exception ignored." ); + } + } + _trueContainers.clear(); +} + +SalomeContainerMultiHelper::~SalomeContainerMultiHelper() +{ +} diff --git a/src/runtime/SalomeContainerHelper.hxx b/src/runtime/SalomeContainerHelper.hxx new file mode 100644 index 000000000..1ae0601ee --- /dev/null +++ b/src/runtime/SalomeContainerHelper.hxx @@ -0,0 +1,86 @@ +// Copyright (C) 2006-2014 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef __SALOMECONTAINERHELPER_HXX__ +#define __SALOMECONTAINERHELPER_HXX__ + +#include "YACSRuntimeSALOMEExport.hxx" + +#include "SALOMEconfig.h" +#include CORBA_CLIENT_HEADER(SALOME_Component) + +#include +#include + +namespace YACS +{ + namespace ENGINE + { + class ComponentInstance; + + class SalomeContainerHelper + { + public: + virtual std::string getType() const = 0; + virtual SalomeContainerHelper *deepCpyOnlyStaticInfo() const = 0; + virtual Engines::Container_var getContainer(const ComponentInstance *inst) const = 0; + virtual bool isAlreadyStarted(const ComponentInstance *inst) const = 0; + virtual void setContainer(const ComponentInstance *inst, Engines::Container_var cont) = 0; + virtual void shutdown() = 0; + virtual ~SalomeContainerHelper(); + }; + + class SalomeContainerMonoHelper : public SalomeContainerHelper + { + public: + SalomeContainerMonoHelper(); + std::string getType() const; + SalomeContainerMonoHelper *deepCpyOnlyStaticInfo() const; + Engines::Container_var getContainer(const ComponentInstance *inst) const; + bool isAlreadyStarted(const ComponentInstance *inst) const; + void setContainer(const ComponentInstance *inst, Engines::Container_var cont); + void shutdown(); + ~SalomeContainerMonoHelper(); + public: + static const char TYPE_NAME[]; + static const char DFT_LAUNCH_MODE[]; + private: + Engines::Container_var _trueCont; + }; + + class SalomeContainerMultiHelper : public SalomeContainerHelper + { + public: + std::string getType() const; + SalomeContainerMultiHelper *deepCpyOnlyStaticInfo() const; + Engines::Container_var getContainer(const ComponentInstance *inst) const; + bool isAlreadyStarted(const ComponentInstance *inst) const; + void setContainer(const ComponentInstance *inst, Engines::Container_var cont); + void shutdown(); + ~SalomeContainerMultiHelper(); + public: + static const char TYPE_NAME[]; + static const char DFT_LAUNCH_MODE[]; + private: + std::map _trueContainers; + }; + } +} + +#endif diff --git a/src/runtime/SalomeContainerTools.cxx b/src/runtime/SalomeContainerTools.cxx new file mode 100644 index 000000000..8eb41946e --- /dev/null +++ b/src/runtime/SalomeContainerTools.cxx @@ -0,0 +1,263 @@ +// Copyright (C) 2006-2014 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#include "SalomeContainerTools.hxx" +#include "SALOME_LifeCycleCORBA.hxx" +#include "SALOME_NamingService.hxx" +#include "SALOME_ResourcesManager.hxx" + +#include "RuntimeSALOME.hxx" +#include "Exception.hxx" + +#include + +using namespace YACS::ENGINE; + +SalomeContainerTools::SalomeContainerTools() +{ + /* Init ContainerParameters */ + SALOME_LifeCycleCORBA::preSet(_params); +} + +SalomeContainerTools::SalomeContainerTools(const SalomeContainerTools& other):_params(other._params),_propertyMap(other._propertyMap) +{ +} + +void SalomeContainerTools::clearProperties() +{ + _propertyMap.clear(); + _params=Engines::ContainerParameters(); +} + +std::string SalomeContainerTools::getProperty(const std::string& name) const +{ + std::map::const_iterator it(_propertyMap.find(name)); + if(it!=_propertyMap.end()) + return (*it).second; + else + return std::string(); +} + +void SalomeContainerTools::setProperty(const std::string& name, const std::string& value) +{ + //DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value); + // Container Part + if (name == "container_name") + _params.container_name = CORBA::string_dup(value.c_str()); + else if (name == "workingdir") + _params.workingdir = CORBA::string_dup(value.c_str()); + else if (name == "nb_parallel_procs") + { + std::istringstream iss(value); + if (!(iss >> _params.nb_proc)) + throw Exception("salomecontainer::setproperty : params.nb_proc value not correct : " + value); + } + else if (name == "isMPI") + { + if (value == "true") + _params.isMPI = true; + else if (value == "false") + _params.isMPI = false; + else + throw Exception("SalomeContainer::setProperty : params.isMPI value not correct : " + value); + } + else if (name == "parallelLib") + _params.parallelLib = CORBA::string_dup(value.c_str()); + + // Resource part + else if (name == "name") + _params.resource_params.name = CORBA::string_dup(value.c_str()); + else if (name == "hostname") + _params.resource_params.hostname = CORBA::string_dup(value.c_str()); + else if (name == "OS") + _params.resource_params.OS = CORBA::string_dup(value.c_str()); + else if (name == "nb_resource_procs") + { + std::istringstream iss(value); + if (!(iss >> _params.resource_params.nb_proc)) + throw Exception("salomecontainer::setproperty : params.resource_params.nb_proc value not correct : " + value); + } + else if (name == "mem_mb") + { + std::istringstream iss(value); + if (!(iss >> _params.resource_params.mem_mb)) + throw Exception("salomecontainer::setproperty : params.resource_params.mem_mb value not correct : " + value); + } + else if (name == "cpu_clock") + { + std::istringstream iss(value); + if (!(iss >> _params.resource_params.cpu_clock)) + throw Exception("salomecontainer::setproperty : params.resource_params.cpu_clock value not correct : " + value); + } + else if (name == "nb_node") + { + std::istringstream iss(value); + if (!(iss >> _params.resource_params.nb_node)) + throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value); + } + else if (name == "nb_proc_per_node") + { + std::istringstream iss(value); + if (!(iss >> _params.resource_params.nb_proc_per_node)) + throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value); + } + else if (name == "policy") + _params.resource_params.policy = CORBA::string_dup(value.c_str()); + else if (name == "component_list") + { + std::string clean_value(value); + + // Step 1: remove blanks + while(clean_value.find(" ") != std::string::npos) + clean_value = clean_value.erase(clean_value.find(" "), 1); + + // Step 2: get values + while(!clean_value.empty()) + { + std::string result(""); + std::string::size_type loc = clean_value.find(",", 0); + if (loc != std::string::npos) + { + result = clean_value.substr(0, loc); + clean_value = clean_value.erase(0, loc+1); + } + else + { + result = clean_value; + clean_value.erase(); + } + if (result != "," && result != "") + { + addToComponentList(result); + } + } + + } + else if (name == "resource_list") + { + std::string clean_value(value); + + // Step 1: remove blanks + while(clean_value.find(" ") != std::string::npos) + clean_value = clean_value.erase(clean_value.find(" "), 1); + + // Step 2: get values + while(!clean_value.empty()) + { + std::string result(""); + std::string::size_type loc = clean_value.find(",", 0); + if (loc != std::string::npos) + { + result = clean_value.substr(0, loc); + clean_value = clean_value.erase(0, loc+1); + } + else + { + result = clean_value; + clean_value.erase(); + } + if (result != "," && result != "") + { + addToResourceList(result); + } + } + + } + _propertyMap[name]=value; +} + +void SalomeContainerTools::addToComponentList(const std::string& name) +{ + // Search if name is already in the list + for (CORBA::ULong i = 0; i < _params.resource_params.componentList.length(); i++) + { + std::string component_name = _params.resource_params.componentList[i].in(); + if (component_name == name) + return; + } + // Add name to list + CORBA::ULong lgth = _params.resource_params.componentList.length(); + _params.resource_params.componentList.length(lgth + 1); + _params.resource_params.componentList[lgth] = CORBA::string_dup(name.c_str()); +} + +void SalomeContainerTools::addToResourceList(const std::string& name) +{ + // Search if name is already in the list + for (CORBA::ULong i = 0; i < _params.resource_params.resList.length(); i++) + { + std::string component_name = _params.resource_params.resList[i].in(); + if (component_name == name) + return; + } + // Add name to list + CORBA::ULong lgth = _params.resource_params.resList.length(); + _params.resource_params.resList.length(lgth + 1); + _params.resource_params.resList[lgth] = CORBA::string_dup(name.c_str()); +} + +std::string SalomeContainerTools::getContainerName() const +{ + return std::string(_params.container_name); +} + +std::string SalomeContainerTools::getHostName() const +{ + return std::string(_params.resource_params.hostname); +} + +std::map SalomeContainerTools::getResourceProperties(const std::string& name) const +{ + std::map properties; + + YACS::ENGINE::RuntimeSALOME* runTime = YACS::ENGINE::getSALOMERuntime(); + CORBA::ORB_ptr orb = runTime->getOrb(); + if (!orb) return properties; + SALOME_NamingService namingService(orb); + SALOME_LifeCycleCORBA lcc(&namingService); + CORBA::Object_var obj = namingService.Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS); + if (CORBA::is_nil(obj)) + return properties; + Engines::ResourcesManager_var resManager = Engines::ResourcesManager::_narrow(obj); + if (CORBA::is_nil(resManager)) + return properties; + + std::ostringstream value; + Engines::ResourceDefinition_var resource_definition = resManager->GetResourceDefinition(name.c_str()); + properties["hostname"]=resource_definition->hostname.in(); + properties["OS"]=resource_definition->OS.in(); + value.str(""); value << resource_definition->mem_mb; + properties["mem_mb"]=value.str(); + value.str(""); value << resource_definition->cpu_clock; + properties["cpu_clock"]=value.str(); + value.str(""); value << resource_definition->nb_node; + properties["nb_node"]=value.str(); + value.str(""); value << resource_definition->nb_proc_per_node; + properties["nb_proc_per_node"]=value.str(); + /* + properties["component_list"]=""; + for(CORBA::ULong i=0; i < resource_definition->componentList.length(); i++) + { + if(i > 0) + properties["component_list"]=properties["component_list"]+","; + properties["component_list"]=properties["component_list"]+resource_definition->componentList[i].in(); + } + */ + return properties; +} diff --git a/src/runtime/SalomeContainerTools.hxx b/src/runtime/SalomeContainerTools.hxx new file mode 100644 index 000000000..0713e6d62 --- /dev/null +++ b/src/runtime/SalomeContainerTools.hxx @@ -0,0 +1,57 @@ +// Copyright (C) 2006-2014 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef __SALOMECONTAINERTOOLS_HXX__ +#define __SALOMECONTAINERTOOLS_HXX__ + +#include "YACSRuntimeSALOMEExport.hxx" +#include "SALOMEconfig.h" +#include CORBA_CLIENT_HEADER(SALOME_ContainerManager) + +#include +#include + +namespace YACS +{ + namespace ENGINE + { + class YACSRUNTIMESALOME_EXPORT SalomeContainerTools + { + public: + SalomeContainerTools(); + SalomeContainerTools(const SalomeContainerTools& other); + std::string getProperty(const std::string& name) const; + void setProperty(const std::string& name, const std::string& value); + const std::map& getProperties() const { return _propertyMap; } + void clearProperties(); + std::map getResourceProperties(const std::string& name) const; + void addToComponentList(const std::string& name); + void addToResourceList(const std::string& name); + public: + std::string getContainerName() const; + std::string getHostName() const; + Engines::ContainerParameters getParameters() const { return _params; } + protected: + std::map _propertyMap; + Engines::ContainerParameters _params; + }; + } +} + +#endif diff --git a/src/runtime/SalomeHPContainer.hxx b/src/runtime/SalomeHPContainer.hxx new file mode 100644 index 000000000..016ed66ed --- /dev/null +++ b/src/runtime/SalomeHPContainer.hxx @@ -0,0 +1,77 @@ +// Copyright (C) 2006-2014 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef __SALOMEHPCONTAINER_HXX__ +#define __SALOMEHPCONTAINER_HXX__ + +#include "YACSRuntimeSALOMEExport.hxx" +#include "HomogeneousPoolContainer.hxx" +#include "Mutex.hxx" +#include +#include +#include +#include CORBA_CLIENT_HEADER(SALOME_Component) +#include CORBA_CLIENT_HEADER(SALOME_ContainerManager) + +namespace YACS +{ + namespace ENGINE + { + class SalomeComponent; + + class YACSRUNTIMESALOME_EXPORT SalomeHPContainer : public HomogeneousPoolContainer + { + public: + SalomeHPContainer(); + SalomeHPContainer(const SalomeContainer& other); + bool isAlreadyStarted(const ComponentInstance *inst) const; + Engines::Container_ptr getContainerPtr(const ComponentInstance *inst) const; + void start(const ComponentInstance *inst) throw (Exception); + Container *clone() const; + Container *cloneAlways() const; + std::string getPlacementId(const ComponentInstance *inst) const; + std::string getFullPlacementId(const ComponentInstance *inst) const; + void checkCapabilityToDealWith(const ComponentInstance *inst) const throw (Exception); + virtual void setProperty(const std::string& name, const std::string& value); + virtual void addComponentName(std::string name); + virtual CORBA::Object_ptr loadComponent(ComponentInstance *inst); + virtual void shutdown(int level); + // Helper methods + void addToComponentList(const std::string & name); + void addToResourceList(const std::string & name); + virtual std::map getResourceProperties(const std::string& name); + protected: +#ifndef SWIG + virtual ~SalomeHPContainer(); +#endif + protected: + //! thread safety in Salome ??? + YACS::BASES::Mutex _mutex; + Engines::Container_var _trueCont; + std::vector _componentNames; + std::vector _trueContainers; + std::string _type; + int _shutdownLevel; + public: + Engines::ContainerParameters _params; + }; + } +} + +#endif -- 2.39.2