Salome HOME
B4 an API modification to generalize launching of kernel containers.
[modules/yacs.git] / src / runtime / SalomeContainer.cxx
index cea49d6a02a88905a987acdc5d3bc3b4c9b9d563..ef67c063149b2a1a3f852cfb7b047c650c0dfd5b 100644 (file)
 using namespace YACS::ENGINE;
 using namespace std;
 
-SalomeContainer::SalomeContainer():_launchMode("start"),_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
+SalomeContainer::SalomeContainer():_launchModeType(new SalomeContainerMonoHelper),_shutdownLevel(999)
 {
 }
 
 SalomeContainer::SalomeContainer(const SalomeContainer& other)
 : Container(other),
-  _launchMode(other._launchMode),
   _launchModeType(other._launchModeType->deepCpyOnlyStaticInfo()),
   _shutdownLevel(other._shutdownLevel),
   _sct(other._sct)
@@ -132,7 +131,7 @@ void SalomeContainer::clearProperties()
   _sct.clearProperties();
 }
 
-void SalomeContainer::addComponentName(std::string name)
+void SalomeContainer::addComponentName(const std::string& name)
 {
   _componentNames.push_back(name);
 }
@@ -148,75 +147,7 @@ void SalomeContainer::addToResourceList(const std::string& name)
  */
 CORBA::Object_ptr SalomeContainer::loadComponent(ComponentInstance *inst)
 {
-  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,inst);
 }
 
 //! Get the container placement id for a component instance
@@ -226,23 +157,7 @@ CORBA::Object_ptr SalomeContainer::loadComponent(ComponentInstance *inst)
  */
 std::string SalomeContainer::getPlacementId(const ComponentInstance *inst) 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,inst);
 }
 
 //! Get the container full path for a component instance
@@ -252,23 +167,7 @@ std::string SalomeContainer::getPlacementId(const ComponentInstance *inst) const
  */
 std::string SalomeContainer::getFullPlacementId(const ComponentInstance *inst) 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,inst);
 }
 
 //! Check if the component instance container is already started
@@ -292,119 +191,7 @@ Engines::Container_ptr SalomeContainer::getContainerPtr(const ComponentInstance
  */
 void SalomeContainer::start(const ComponentInstance *inst) 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,inst);
 }
 
 void SalomeContainer::shutdown(int level)