Salome HOME
Merge branch 'V7_dev'
[modules/yacs.git] / src / runtime / SalomeContainer.cxx
index cea49d6a02a88905a987acdc5d3bc3b4c9b9d563..70b405be81ec278df2a943cf15461b75d9e080d7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2014  CEA/DEN, EDF R&D
+// 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
@@ -29,6 +29,7 @@
 #include "RuntimeSALOME.hxx"
 #include "SalomeContainer.hxx"
 #include "SalomeComponent.hxx"
+#include "ServiceNode.hxx"
 #include "Proc.hxx"
 
 #include "SALOME_NamingService.hxx"
 using namespace YACS::ENGINE;
 using namespace std;
 
-SalomeContainer::SalomeContainer():_launchMode("start"),_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
+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),
-  _launchMode(other._launchMode),
+: Container(other),_componentNames(other._componentNames),
   _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()),
   _shutdownLevel(other._shutdownLevel),
   _sct(other._sct)
 {
 }
 
+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)
+{
+  if(lmt)
+    lmt->incrRef();
+}
+
 SalomeContainer::~SalomeContainer()
 {
-  delete _launchModeType;
+  _launchModeType->decrRef();
 }
 
 void SalomeContainer::lock()
@@ -76,7 +88,12 @@ void SalomeContainer::lock()
 
 void SalomeContainer::unLock()
 {
-  _mutex.unlock();
+  _mutex.unLock();
+}
+
+std::string SalomeContainer::getKind() const
+{
+  return KIND;
 }
 
 Container *SalomeContainer::clone() const
@@ -103,27 +120,41 @@ void SalomeContainer::checkCapabilityToDealWith(const ComponentInstance *inst) c
 
 void SalomeContainer::setProperty(const std::string& name, const std::string& value)
 {
-  if (name == "type")
+  if (name == AOC_ENTRY)
+    {
+      std::istringstream iss(value);
+      int val;
+      iss >> val;
+      setAttachOnCloningStatus((bool)val);
+    }
+  else if (name == TYPE_PROPERTY_STR)
     {
       if (value == SalomeContainerMonoHelper::TYPE_NAME)
         {
-          delete _launchModeType;
+          _launchModeType->decrRef();
           _launchModeType=new SalomeContainerMonoHelper;
         }
       else if (value == SalomeContainerMultiHelper::TYPE_NAME)
         {
-          delete _launchModeType;
+          _launchModeType->decrRef();
           _launchModeType=new SalomeContainerMultiHelper;
         }
       else
         throw Exception("SalomeContainer::setProperty : type value is not correct (mono or multi): " + value);
-      return ;
     }
   _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)
+    {
+      int reti(_isAttachedOnCloning);
+      std::ostringstream oss; oss << reti;
+      return oss.str();
+    }
   return _sct.getProperty(name);
 }
 
@@ -132,7 +163,7 @@ void SalomeContainer::clearProperties()
   _sct.clearProperties();
 }
 
-void SalomeContainer::addComponentName(std::string name)
+void SalomeContainer::addComponentName(const std::string& name)
 {
   _componentNames.push_back(name);
 }
@@ -146,77 +177,9 @@ void SalomeContainer::addToResourceList(const std::string& name)
 /*!
  * \param inst the component instance to load
  */
-CORBA::Object_ptr SalomeContainer::loadComponent(ComponentInstance *inst)
+CORBA::Object_ptr SalomeContainer::loadComponent(Task *askingNode)
 {
-  DEBTRACE("SalomeContainer::loadComponent ");
-  lock();//To be sure
-  if(!isAlreadyStarted(inst))
-    {
-      try
-        {
-          start(inst);
-        }
-      catch(Exception& e)
-        {
-          unLock();
-          throw e;
-        }
-    }
-  unLock();
-  lock();//To be sure
-  CORBA::Object_ptr objComponent=CORBA::Object::_nil();
-  std::string compoName=inst->getCompoName();
-  const char* componentName=compoName.c_str();
-  Engines::Container_var container(_launchModeType->getContainer(inst));
-
-  char* reason;
-
-  bool isLoadable = container->load_component_Library(componentName, reason);
-  if (isLoadable)
-    {
-      CORBA::string_free(reason);
-      int studyid=1;
-      Proc* p=getProc();
-      if(p)
-        {
-          std::string value=p->getProperty("DefaultStudyID");
-          if(!value.empty())
-            studyid= atoi(value.c_str());
-        }
-      // prepare component instance properties
-      Engines::FieldsDict_var env = new Engines::FieldsDict;
-      std::map<std::string, std::string> properties = inst->getProperties();
-      if(p)
-        {
-          std::map<std::string,std::string> procMap=p->getProperties();
-          properties.insert(procMap.begin(),procMap.end());
-        }
-
-      std::map<std::string, std::string>::const_iterator itm;
-      env->length(properties.size());
-      int item=0;
-      for(itm = properties.begin(); itm != properties.end(); ++itm, item++)
-        {
-          DEBTRACE("envname="<<itm->first<<" envvalue="<< itm->second);
-          env[item].key= CORBA::string_dup(itm->first.c_str());
-          env[item].value <<= itm->second.c_str();
-        }
-
-      objComponent=container->create_component_instance_env(componentName, studyid, env, reason);
-    }
-
-  if(CORBA::is_nil(objComponent))
-    {
-      unLock();
-      std::string text="Error while trying to create a new component: component '"+ compoName;
-      text=text+"' is not installed or it's a wrong name";
-      text += '\n';
-      text += reason;
-      CORBA::string_free(reason);
-      throw Exception(text);
-    }
-  unLock();
-  return objComponent;
+  return SalomeContainerTools::LoadComponent(_launchModeType,this,askingNode);
 }
 
 //! Get the container placement id for a component instance
@@ -224,25 +187,9 @@ CORBA::Object_ptr SalomeContainer::loadComponent(ComponentInstance *inst)
  * \param inst the component instance
  * \return the placement id
  */
-std::string SalomeContainer::getPlacementId(const ComponentInstance *inst) const
+std::string SalomeContainer::getPlacementId(const Task *askingNode) const
 {
-
-  if(isAlreadyStarted(inst))
-    {
-      Engines::Container_var container(_launchModeType->getContainer(inst));
-      const char *what="/";
-      CORBA::String_var corbaStr=container->name();
-      string ret(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 SalomeContainerTools::GetPlacementId(_launchModeType,this,askingNode);
 }
 
 //! Get the container full path for a component instance
@@ -250,25 +197,9 @@ std::string SalomeContainer::getPlacementId(const ComponentInstance *inst) const
  * \param inst the component instance
  * \return the full placement id
  */
-std::string SalomeContainer::getFullPlacementId(const ComponentInstance *inst) const
+std::string SalomeContainer::getFullPlacementId(const Task *askingNode) const
 {
-
-  if(isAlreadyStarted(inst))
-    {
-      Engines::Container_var container(_launchModeType->getContainer(inst));
-      try
-        {
-          CORBA::String_var corbaStr=container->name();
-          string ret(corbaStr);
-          return ret;
-        }
-      catch(...)
-        {
-          return "Unknown_placement";
-        }
-    }
-  else
-    return "Not_placed_yet";
+  return SalomeContainerTools::GetFullPlacementId(_launchModeType,this,askingNode);
 }
 
 //! Check if the component instance container is already started
@@ -276,135 +207,23 @@ std::string SalomeContainer::getFullPlacementId(const ComponentInstance *inst) c
  * \param inst the component instance
  * \return true, if the container is already started, else false
  */
-bool SalomeContainer::isAlreadyStarted(const ComponentInstance *inst) const
+bool SalomeContainer::isAlreadyStarted(const Task *askingNode) const
 {
-  return _launchModeType->isAlreadyStarted(inst);
+  return _launchModeType->isAlreadyStarted(askingNode);
 }
 
-Engines::Container_ptr SalomeContainer::getContainerPtr(const ComponentInstance *inst) const
+Engines::Container_ptr SalomeContainer::getContainerPtr(const Task *askingNode) const
 {
-  return Engines::Container::_duplicate(_launchModeType->getContainer(inst));
+  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 ComponentInstance *inst) throw(YACS::Exception)
+void SalomeContainer::start(const Task *askingNode) throw(YACS::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(_sct.getContainerName());
-  DEBTRACE("SalomeContainer::start " << str <<";"<< _sct.getHostName() <<";"<<_type);
-
-  // Finalize parameters with components found in the container
-  std::vector<std::string>::iterator iter;
-  for(CORBA::ULong i=0; i < _componentNames.size();i++)
-    _sct.addToComponentList(_componentNames[i]);
-  Engines::ContainerParameters myparams(_sct.getParameters());
-
-  bool namedContainer=false;
-  if(str != "")
-    namedContainer=true;
-
-  //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 (dynamic_cast<SalomeContainerMonoHelper *>(_launchModeType) && str != "")
-    {
-      myparams.mode="getorstart";
-    }
-
-  if (str == "")
-  {
-    //give a almost unique name to the container : Pid_Name_Addr
-    std::ostringstream stream;
-    stream << getpid();
-    stream << "_";
-    stream << _name;
-    stream << "_";
-    stream << (void *)(this);
-    DEBTRACE("container_name="<<stream.str());
-    myparams.container_name=CORBA::string_dup(stream.str().c_str());
-    _shutdownLevel=1;
-  }
-
-  Engines::Container_var trueCont(Engines::Container::_nil());
-  if(namedContainer && _shutdownLevel==999)
-    {
-      //Make this only the first time start is called (_shutdownLevel==999)
-      //If the container is named, first try to get an existing container
-      //If there is an existing container use it and set the shutdown level to 3
-      //If there is no existing container, try to launch a new one and set the shutdown level to 2
-      myparams.mode="get";
-      try
-        { 
-          trueCont=contManager->GiveContainer(myparams);
-        }
-      catch( const SALOME::SALOME_Exception& ex )
-        {
-          std::string msg="SalomeContainer::start : no existing container : ";
-          msg += '\n';
-          msg += ex.details.text.in();
-          DEBTRACE( msg );
-        }
-      catch(...)
-        {
-        }
-
-      if(!CORBA::is_nil(trueCont))
-        {
-          _shutdownLevel=3;
-          DEBTRACE( "container found: " << str << " " << _shutdownLevel );
-        }
-      else
-        {
-          _shutdownLevel=2;
-          myparams.mode="start";
-          DEBTRACE( "container not found: " << str << " " << _shutdownLevel);
-        }
-    }
-
-  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);
-      }
-    catch( const SALOME::SALOME_Exception& ex )
-      {
-        std::string msg="SalomeContainer::start : Unable to launch container in Salome : ";
-        msg += '\n';
-        msg += ex.details.text.in();
-        throw Exception(msg);
-      }
-    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");
-
-  _launchModeType->setContainer(inst,trueCont);
-
-  CORBA::String_var containerName(trueCont->name()),hostName(trueCont->getHostName());
-  std::cerr << "SalomeContainer launched : " << containerName << " " << hostName << " " << trueCont->getPID() << std::endl;
+  SalomeContainerTools::Start(_componentNames,_launchModeType,_sct,_shutdownLevel,this,askingNode);
 }
 
 void SalomeContainer::shutdown(int level)