Salome HOME
Merge branch 'V7_dev'
[modules/yacs.git] / src / runtime / SalomeContainer.cxx
index 0887dda78c802e0c9cb0a9cf24ef21693078dcf1..70b405be81ec278df2a943cf15461b75d9e080d7 100644 (file)
@@ -1,4 +1,24 @@
+// Copyright (C) 2006-2016  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
+//
+
 //#define REFCNT
+//
 #ifdef REFCNT
 #define private public
 #define protected public
 #include "RuntimeSALOME.hxx"
 #include "SalomeContainer.hxx"
 #include "SalomeComponent.hxx"
+#include "ServiceNode.hxx"
+#include "Proc.hxx"
 
 #include "SALOME_NamingService.hxx"
 #include "SALOME_LifeCycleCORBA.hxx"
 #include "SALOME_ContainerManager.hxx"
+#include "Basics_Utils.hxx"
 #include "OpUtil.hxx"
 
 #include <sstream>
 #include <iostream>
 
+#ifdef WIN32
+#include <process.h>
+#define getpid _getpid
+#endif
+
 //#define _DEVDEBUG_
 #include "YacsTrace.hxx"
 
 using namespace YACS::ENGINE;
 using namespace std;
 
-SalomeContainer::SalomeContainer():_trueCont(Engines::Container::_nil())
+const char SalomeContainer::KIND[]="Salome";
+
+const char SalomeContainer::TYPE_PROPERTY_STR[]="type";
+
+SalomeContainer::SalomeContainer():_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
+{
+}
+
+SalomeContainer::SalomeContainer(const SalomeContainer& other)
+: Container(other),_componentNames(other._componentNames),
+  _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()),
+  _shutdownLevel(other._shutdownLevel),
+  _sct(other._sct)
 {
-  /* Init MachinesParameters */
-  _params.container_name = "";
-  _params.hostname = "";
-  _params.OS = "";
-  _params.mem_mb = 0;
-  _params.cpu_clock = 0;
-  _params.nb_proc_per_node = 0;
-  _params.nb_node = 0;
-  _params.isMPI = false;
-  _params.parallelLib = "";
-  _params.nb_component_nodes = 0;
 }
 
-SalomeContainer::SalomeContainer(const SalomeContainer& other):Container(other),_trueCont(Engines::Container::_nil())
+SalomeContainer::SalomeContainer(const Container& other, const SalomeContainerTools& sct, const SalomeContainerHelper *lmt,
+                                 const std::vector<std::string>& componentNames, int shutdownLev):Container(other),_componentNames(componentNames),
+                                     _launchModeType(const_cast<SalomeContainerHelper *>(lmt)),_shutdownLevel(shutdownLev),_sct(sct)
 {
-  _params.container_name = CORBA::string_dup(other._params.container_name);
-  _params.hostname = CORBA::string_dup(other._params.hostname);
-  _params.OS = CORBA::string_dup(other._params.OS);
-  _params.mem_mb = other._params.mem_mb;
-  _params.cpu_clock = other._params.cpu_clock;
-  _params.nb_proc_per_node = other._params.nb_proc_per_node;
-  _params.nb_node = other._params.nb_node;
-  _params.isMPI = other._params.isMPI;
-  _params.parallelLib = CORBA::string_dup(other._params.parallelLib);
-  _params.nb_component_nodes = other._params.nb_component_nodes;
-  _params.workingdir= CORBA::string_dup(other._params.workingdir);
+  if(lmt)
+    lmt->incrRef();
 }
 
 SalomeContainer::~SalomeContainer()
 {
+  _launchModeType->decrRef();
 }
 
 void SalomeContainer::lock()
@@ -65,94 +88,12 @@ void SalomeContainer::lock()
 
 void SalomeContainer::unLock()
 {
-  _mutex.unlock();
+  _mutex.unLock();
 }
 
-bool SalomeContainer::isAlreadyStarted() const
+std::string SalomeContainer::getKind() const
 {
-  if(CORBA::is_nil(_trueCont))
-    return false;
-  else
-    return true;
-}
-
-void SalomeContainer::start() throw (Exception)
-{
-  CORBA::ORB_ptr orb=getSALOMERuntime()->getOrb();
-  SALOME_NamingService ns;
-  try
-    {
-      ns.init_orb(orb);
-    }
-  catch(SALOME_Exception& e)
-    {
-      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);
-
-  std::string str(_params.container_name);
-  //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 (str != "")
-    {
-      std::string machine(_params.hostname);
-      if(machine == "" || machine == "localhost")
-        machine=GetHostname();
-      std::string ContainerNameInNS=ns.BuildContainerNameForNS(_params,machine.c_str());
-      obj=ns.Resolve(ContainerNameInNS.c_str());
-      if(!CORBA::is_nil(obj))
-        {
-          std::cerr << "Container already exists: " << ContainerNameInNS << std::endl;
-          _trueCont=Engines::Container::_narrow(obj);
-          return;
-        }
-    }
-
-  if (str == "") 
-    {
-      std::ostringstream stream;
-      stream << (void *)(this);
-      _params.container_name=CORBA::string_dup(stream.str().c_str());
-    }
-  Engines::CompoList compolist;
-  compolist.length(_componentNames.size());
-  std::vector<std::string>::iterator iter;
-  for(CORBA::ULong i=0; i < _componentNames.size();i++)
-    {
-      compolist[i]=CORBA::string_dup(_componentNames[i].c_str());
-    }
-
-  try
-    { 
-      // --- GiveContainer is used in batch mode to retreive launched containers,
-      //     and is equivalent to StartContainer when not in batch.
-      std::string policy=getProperty("policy");
-      if(policy=="best")
-        _trueCont=contManager->GiveContainer(_params,Engines::P_BEST,compolist);
-      else if(policy=="first")
-        _trueCont=contManager->GiveContainer(_params,Engines::P_FIRST,compolist);
-      else
-        _trueCont=contManager->GiveContainer(_params,Engines::P_CYCL,compolist);
-    }
-  catch(CORBA::COMM_FAILURE&)
-    {
-      throw Exception("SalomeContainer::start : Unable to launch container in Salome : CORBA Comm failure detected");
-    }
-  catch(CORBA::Exception&)
-    {
-      throw Exception("SalomeContainer::start : Unable to launch container in Salome : Unexpected CORBA failure detected");
-    }
-  if(CORBA::is_nil(_trueCont))
-    throw Exception("SalomeContainer::start : Unable to launch container in Salome. Check your CatalogResources.xml file");
-
-  CORBA::String_var containerName=_trueCont->name();
-  CORBA::String_var hostName=_trueCont->getHostName();
-  std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << _trueCont->getPID() << std::endl;
-
-#ifdef REFCNT
-    DEBTRACE(_trueCont->_PR_getobj()->pd_refCount );
-#endif
+  return KIND;
 }
 
 Container *SalomeContainer::clone() const
@@ -166,26 +107,12 @@ Container *SalomeContainer::clone() const
     return new SalomeContainer(*this);
 }
 
-std::string SalomeContainer::getPlacementId() const
+Container *SalomeContainer::cloneAlways() const
 {
-  if(isAlreadyStarted())
-    {
-      const char *what="/";
-      char *corbaStr=_trueCont->name();
-      string ret(corbaStr);
-      CORBA::string_free(corbaStr);
-      //Salome FOREVER ...
-      std::string::size_type i=ret.find_first_of(what,0);
-      i=ret.find_first_of(what, i==std::string::npos ? i:i+1);
-      if(i!=std::string::npos)
-       return ret.substr(i+1);
-      return ret;
-    }
-  else
-    return "Not placed yet !!!";
+  return new SalomeContainer(*this);
 }
 
-void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw (Exception)
+void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) const throw(YACS::Exception)
 {
   if(inst->getKind()!=SalomeComponent::KIND)
     throw Exception("SalomeContainer::checkCapabilityToDealWith : SalomeContainer is not able to deal with this type of ComponentInstance.");
@@ -193,70 +120,129 @@ void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) c
 
 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
 {
-  DEBTRACE("SalomeContainer::setProperty : " << name << " ; " << value);
-  
-  if (name == "container_name")
-    _params.container_name = CORBA::string_dup(value.c_str());
-  else if (name == "hostname")
-    _params.hostname = CORBA::string_dup(value.c_str());
-  else if (name == "OS")
-    _params.OS = CORBA::string_dup(value.c_str());
-  else if (name == "parallelLib")
-    _params.parallelLib = CORBA::string_dup(value.c_str());
-  else if (name == "workingdir")
-    _params.workingdir = CORBA::string_dup(value.c_str());
-  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 == "mem_mb")
-    {
-      std::istringstream iss(value);
-      if (!(iss >> _params.mem_mb))
-        throw Exception("salomecontainer::setproperty : params.mem_mb value not correct : " + value);
-    }
-  else if (name == "cpu_clock")
+  if (name == AOC_ENTRY)
     {
       std::istringstream iss(value);
-      if (!(iss >> _params.cpu_clock))
-        throw Exception("salomecontainer::setproperty : params.cpu_clock value not correct : " + value);
+      int val;
+      iss >> val;
+      setAttachOnCloningStatus((bool)val);
     }
-  else if (name == "nb_proc_per_node")
+  else if (name == TYPE_PROPERTY_STR)
     {
-      std::istringstream iss(value);
-      if (!(iss >> _params.nb_proc_per_node))
-        throw Exception("salomecontainer::setproperty : params.nb_proc_per_node value not correct : " + value);
-    }
-  else if (name == "nb_node")
-    {
-      std::istringstream iss(value);
-      if (!(iss >> _params.nb_node))
-        throw Exception("salomecontainer::setproperty : params.nb_node value not correct : " + value);
+      if (value == SalomeContainerMonoHelper::TYPE_NAME)
+        {
+          _launchModeType->decrRef();
+          _launchModeType=new SalomeContainerMonoHelper;
+        }
+      else if (value == SalomeContainerMultiHelper::TYPE_NAME)
+        {
+          _launchModeType->decrRef();
+          _launchModeType=new SalomeContainerMultiHelper;
+        }
+      else
+        throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value);
     }
-  else if (name == "nb_component_nodes")
+  _sct.setProperty(name,value);
+}
+
+std::string SalomeContainer::getProperty(const std::string& name) const
+{
+  if (name == TYPE_PROPERTY_STR)
+    return _launchModeType->getType();
+  if (name==AOC_ENTRY)
     {
-      std::istringstream iss(value);
-      if (!(iss >> _params.nb_component_nodes))
-        throw Exception("salomecontainer::setproperty : params.nb_component_nodes value not correct : " + value);
+      int reti(_isAttachedOnCloning);
+      std::ostringstream oss; oss << reti;
+      return oss.str();
     }
-  Container::setProperty(name, value);
+  return _sct.getProperty(name);
 }
 
-bool SalomeContainer::isAPaCOContainer() const
+void SalomeContainer::clearProperties()
 {
-  bool result = false;
-  string parallelLib(_params.parallelLib);
-  if (parallelLib != "")
-    result = true;
-  return result;
+  _sct.clearProperties();
 }
 
-void SalomeContainer::addComponentName(std::string name)
+void SalomeContainer::addComponentName(const 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
+ */
+CORBA::Object_ptr SalomeContainer::loadComponent(Task *askingNode)
+{
+  return SalomeContainerTools::LoadComponent(_launchModeType,this,askingNode);
+}
+
+//! Get the container placement id for a component instance
+/*!
+ * \param inst the component instance
+ * \return the placement id
+ */
+std::string SalomeContainer::getPlacementId(const Task *askingNode) const
+{
+  return SalomeContainerTools::GetPlacementId(_launchModeType,this,askingNode);
+}
+
+//! Get the container full path for a component instance
+/*!
+ * \param inst the component instance
+ * \return the full placement id
+ */
+std::string SalomeContainer::getFullPlacementId(const Task *askingNode) const
+{
+  return SalomeContainerTools::GetFullPlacementId(_launchModeType,this,askingNode);
+}
+
+//! Check if the component instance container is already started
+/*!
+ * \param inst the component instance
+ * \return true, if the container is already started, else false
+ */
+bool SalomeContainer::isAlreadyStarted(const Task *askingNode) const
+{
+  return _launchModeType->isAlreadyStarted(askingNode);
+}
+
+Engines::Container_ptr SalomeContainer::getContainerPtr(const Task *askingNode) const
+{
+  return Engines::Container::_duplicate(_launchModeType->getContainer(askingNode));
+}
+
+//! Start a salome container (true salome container not yacs one) with given ContainerParameters (_params)
+/*!
+ * \param inst the component instance
+ */
+void SalomeContainer::start(const Task *askingNode) throw(YACS::Exception)
+{
+  SalomeContainerTools::Start(_componentNames,_launchModeType,_sct,_shutdownLevel,this,askingNode);
+}
+
+void SalomeContainer::shutdown(int level)
+{
+  DEBTRACE("SalomeContainer::shutdown: " << _name << "," << level << "," << _shutdownLevel);
+  if(level < _shutdownLevel)
+    return;
+
+  _shutdownLevel=999;
+  //shutdown the SALOME containers
+  _launchModeType->shutdown();
+}
+
+std::map<std::string,std::string> SalomeContainer::getResourceProperties(const std::string& name) const
+{
+  return _sct.getResourceProperties(name);
+}
+
+std::map<std::string,std::string> SalomeContainer::getProperties() const
+{
+  return _sct.getProperties();
+}