Salome HOME
Merge from V5_1_3_BR 07/12/2009
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_LifeCycleCORBA.cxx
index e6e5936e76fe3ef36c45a13c509aa9439193f020..53455360982754f42b8ebcf52db9da14789e35d7 100644 (file)
@@ -51,6 +51,7 @@
 #include "SALOME_ContainerManager.hxx"
 #include "SALOME_Component_i.hxx"
 #include "SALOME_NamingService.hxx"
+#include "SALOME_FileTransferCORBA.hxx"
 
 using namespace std;
 
@@ -64,6 +65,11 @@ IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
 {
 }
 
+/*! \class SALOME_LifeCycleCORBA
+    \brief A class to manage life cycle of SALOME components.
+
+*/
+
 //=============================================================================
 /*! 
  *  Constructor
@@ -79,9 +85,11 @@ SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
   char **argv = &xargv;
   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
+  _NSnew=0;
   if (!ns)
     {
       _NS = new SALOME_NamingService(orb);
+      _NSnew=_NS;
     }
   else _NS = ns;
   //add try catch
@@ -109,18 +117,18 @@ SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
 
 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
 {
+  if(_NSnew)delete _NSnew;
 }
 
 //=============================================================================
-/*! Public - 
- *  Find and aready existing and registered component instance.
+/*! \brief Find an already existing and registered component instance.
+ *
  *  \param params         machine parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
  *  \return a CORBA reference of the component instance, or _nil if not found
  */
 //=============================================================================
-
 Engines::Component_ptr
 SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
                                     const char *componentName,
@@ -129,13 +137,12 @@ SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
   if (! isKnownComponentClass(componentName))
     return Engines::Component::_nil();
 
-  Engines::CompoList clist;
-  clist.length(1);
-  clist[0] = componentName;
-  Engines::MachineList_var listOfMachines =
-    _ResManager->GetFittingResources(params, clist);
+  Engines::MachineParameters parms(params);
+  parms.componentList.length(1);
+  parms.componentList[0] = componentName;
+  Engines::MachineList_var listOfMachines = _ResManager->GetFittingResources(parms);
 
-  Engines::Component_var compo = _FindComponent(params,
+  Engines::Component_var compo = _FindComponent(parms,
                                                componentName,
                                                studyId,
                                                listOfMachines);
@@ -144,8 +151,8 @@ SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
 }
 
 //=============================================================================
-/*! Public - 
- *  Load a component instance on a container defined by machine parameters
+/*! \brief Load a component instance on a container defined by machine parameters
+ *
  *  \param params         machine parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
@@ -163,24 +170,24 @@ SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params,
   if (! isKnownComponentClass(componentName))
     return Engines::Component::_nil();
 
-  Engines::CompoList clist;
-  clist.length(1);
-  clist[0] = componentName;
-  Engines::MachineList_var listOfMachines =
-    _ResManager->GetFittingResources(params, clist);
+  Engines::MachineParameters parms(params);
+  parms.componentList.length(1);
+  parms.componentList[0] = componentName;
+
+  Engines::MachineList_var listOfMachines = _ResManager->GetFittingResources(parms);
+  parms.computerList=listOfMachines;
 
-  Engines::Component_var compo = _LoadComponent(params,
+  Engines::Component_var compo = _LoadComponent(parms,
                                                componentName,
-                                               studyId,
-                                               listOfMachines);
+                                               studyId);
 
   return compo._retn();
 }
 
 //=============================================================================
-/*! Public - 
- *  Find and aready existing and registered component instance or load a new
- *  component instance on a container defined by machine parameters.
+/*! \brief Find an already existing and registered component instance or load a new
+ *         component instance on a container defined by machine parameters.
+ *
  *  \param params         machine parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
@@ -199,30 +206,31 @@ FindOrLoad_Component(const Engines::MachineParameters& params,
   if (! isKnownComponentClass(componentName))
     return Engines::Component::_nil();
 
-  Engines::CompoList clist;
-  clist.length(1);
-  clist[0] = componentName;
-  Engines::MachineList_var listOfMachines =
-    _ResManager->GetFittingResources(params,clist);
+  Engines::MachineParameters parms(params);
+  parms.componentList.length(1);
+  parms.componentList[0] = componentName;
+  Engines::MachineList_var listOfMachines = _ResManager->GetFittingResources(parms);
 
-  Engines::Component_var compo = _FindComponent(params,
+  Engines::Component_var compo = _FindComponent(parms,
                                                componentName,
                                                studyId,
                                                listOfMachines);
 
   if(CORBA::is_nil(compo))
-    compo = _LoadComponent(params,
+    {
+      parms.computerList=listOfMachines;
+      compo = _LoadComponent(parms,
                           componentName,
-                          studyId,
-                          listOfMachines);
+                          studyId);
+    }
 
   return compo._retn();
 }
 
 //=============================================================================
-/*! Public - 
- *  Find and aready existing and registered component instance or load a new
- *  component instance on a container defined by name
+/*! \brief Find an already existing and registered component instance or load a new
+ *         component instance on a container defined by name
+ *
  *  \param containerName  the name of container, under one of the forms
  *           - 1 aContainer (local container)
  *           - 2 machine/aContainer (container on hostname = machine)
@@ -269,7 +277,7 @@ SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
     {
       // containerName doesn't contain "/" => Local container
       params->container_name=CORBA::string_dup(stContainer);
-      params->hostname=CORBA::string_dup(Kernel_Utils::GetHostname().c_str());
+      params->hostname="";
     }
   else 
     {
@@ -292,8 +300,8 @@ SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
 }
 
 //=============================================================================
-/*! Public -
- *  Check if the component class is known in module catalog
+/*! \brief Check if the component class is known in module catalog
+ *
  *  \param componentName  the name of component class
  *  \return true if found, false otherwise
  */
@@ -330,7 +338,7 @@ bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
 }
 
 //=============================================================================
-/*! Public -
+/*! 
  *  Not so complex... useful ?
  */
 //=============================================================================
@@ -347,9 +355,8 @@ SALOME_LifeCycleCORBA::isMpiContainer(const Engines::MachineParameters& params)
 
 
 //=============================================================================
-/*! Public -
- *  Pre initialisation of a given Engines::MachineParameters with default
- *  values.
+/*! \brief Initialisation of a given Engines::MachineParameters with default values.
+ *
  *  - container_name = ""  : not relevant
  *  - hostname = ""        : not relevant
  *  - OS = ""              : not relevant
@@ -365,19 +372,23 @@ void SALOME_LifeCycleCORBA::preSet( Engines::MachineParameters& params)
 {
   params.container_name = "";
   params.hostname = "";
+  //param.componentList = 0;
+  //param.computerList = 0;
   params.OS = "";
   params.mem_mb = 0;
   params.cpu_clock = 0;
   params.nb_proc_per_node = 0;
   params.nb_node = 0;
   params.isMPI = false;
-
+  params.workingdir = "";
+  params.mode = "";
+  params.policy = "";
   params.parallelLib = "";
   params.nb_component_nodes = 0;
 }
 
 //=============================================================================
-/*! Public -
+/*! 
  *  \return a number of processors not 0, only for MPI containers
  */
 //=============================================================================
@@ -397,7 +408,8 @@ int SALOME_LifeCycleCORBA::NbProc(const Engines::MachineParameters& params)
 }
 
 //=============================================================================
-/*! Public -
+/*! \brief Get the container manager
+ *
  *  \return the container Manager
  */
 //=============================================================================
@@ -410,7 +422,8 @@ Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
 }
 
 //=============================================================================
-/*! Public -
+/*! \brief Get the resources manager
+ *
  *  \return the container Manager
  */
 //=============================================================================
@@ -423,8 +436,7 @@ Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
 }
 
 //=============================================================================
-/*! Public -
- *  shutdown all the SALOME servers except SALOME_Session_Server, omniNames and notifd
+/*! \brief shutdown all the SALOME servers except SALOME_Session_Server, omniNames and notifd
  */
 //=============================================================================
 
@@ -516,8 +528,7 @@ void SALOME_LifeCycleCORBA::shutdownServers()
 }
 
 //=============================================================================
-/*! Public -
- *  shutdown  omniNames and notifd
+/*! \brief shutdown  omniNames and notifd
  */
 //=============================================================================
 
@@ -526,6 +537,8 @@ void SALOME_LifeCycleCORBA::killOmniNames()
   string portNumber (::getenv ("NSPORT") );
   if ( !portNumber.empty() ) 
     {
+#ifdef WNT
+#else
       string cmd ;
       cmd = string( "ps -eo pid,command | grep -v grep | grep -E \"omniNames.*")
         + portNumber
@@ -536,6 +549,7 @@ void SALOME_LifeCycleCORBA::killOmniNames()
       }
       catch ( ... ) {
       }
+#endif
     }
   
   // NPAL 18309  (Kill Notifd)
@@ -550,15 +564,16 @@ void SALOME_LifeCycleCORBA::killOmniNames()
 }
 
 //=============================================================================
-/*! Protected -
- *  Find and aready existing and registered component instance.
+/*! \brief Find an already existing and registered component instance.
+ *
+ * - build a list of machines on which an instance of the component is running,
+ * - find the best machine among the list
+ *
  *  \param params         machine parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
  *  \param listOfMachines list of machine address
  *  \return a CORBA reference of the component instance, or _nil if not found
- * - build a list of machines on which an instance of the component is running,
- * - find the best machine among the list
  */
 //=============================================================================
 
@@ -610,16 +625,16 @@ _FindComponent(const Engines::MachineParameters& params,
 }
 
 //=============================================================================
-/*! Protected -
- *  Load a component instance.
+/*! \brief  Load a component instance.
+ *
+ *  - Finds a container in the list of machine or start one.
+ *  - Try to load the component library in the container,
+ *  - then create an instance of the component.
+ *
  *  \param params         machine parameters like type or name...
  *  \param componentName  the name of component class
  *  \param studyId        default = 0  : multistudy instance
- *  \param listOfMachines list of machine address
  *  \return a CORBA reference of the component instance, or _nil if problem
- *  - Finds a container in the list of machine or start one.
- *  - Try to load the component library in the container,
- *  - then create an instance of the component.
  */
 //=============================================================================
 
@@ -627,15 +642,12 @@ Engines::Component_ptr
 SALOME_LifeCycleCORBA::
 _LoadComponent(const Engines::MachineParameters& params, 
              const char *componentName,
-             int studyId,
-             const Engines::MachineList& listOfMachines)
+             int studyId)
 {
   MESSAGE("_LoadComponent, required " << params.container_name <<
          " " << componentName << " " << NbProc(params));
 
-  Engines::Container_var cont =
-    _ContManager->FindOrStartContainer(params,
-                                      listOfMachines);
+  Engines::Container_var cont = _ContManager->FindOrStartContainer(params);
   if (CORBA::is_nil(cont)) return Engines::Component::_nil();
 
   bool isLoadable = cont->load_component_Library(componentName);
@@ -646,6 +658,15 @@ _LoadComponent(const Engines::MachineParameters& params,
   return myInstance._retn();
 }
 
+//=============================================================================
+/*! \brief  Load a parallel component instance.
+ *
+ *  \param params         machine parameters like type or name...
+ *  \param componentName  the name of component class
+ *  \param studyId        default = 0  : multistudy instance
+ *  \return a CORBA reference of the parallel component instance, or _nil if problem
+ */
+//=============================================================================
 Engines::Component_ptr
 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::MachineParameters& params,
                                               const char *componentName,
@@ -658,19 +679,12 @@ SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::MachineParameters&
   MESSAGE("Number of component nodes : " << params.nb_component_nodes);
   MESSAGE("Component Name : " << componentName);*/
 
-  Engines::CompoList clist;
-  clist.length(1);
-  clist[0] = componentName;
-  MESSAGE("Building a list of machines");
-  Engines::MachineList_var listOfMachines = _ResManager->GetFittingResources(params, clist);
-  if (listOfMachines->length() == 0)
-  {
-    INFOS("No matching machines founded !");
-    return Engines::Component::_nil();
-  }
+  Engines::MachineParameters parms(params);
+  parms.componentList.length(1);
+  parms.componentList[0] = componentName;
 
   MESSAGE("Starting Parallel Container");
-  Engines::Container_var cont = _ContManager->FindOrStartParallelContainer(params, listOfMachines);
+  Engines::Container_var cont = _ContManager->StartParallelContainer(parms);
   if (CORBA::is_nil(cont)) {
     INFOS("FindOrStartParallelContainer() returns a NULL container !");
     return Engines::Component::_nil();
@@ -686,10 +700,58 @@ SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::MachineParameters&
   MESSAGE("Creating component instance");
   // @PARALLEL@ permits to identify that the component requested
   // is a parallel component.
-  string name = string(componentName) + string("@PARALLEL@");
+  string name = string(componentName);
   Engines::Component_var myInstance = cont->create_component_instance(name.c_str(), studyId);
   if (CORBA::is_nil(myInstance))
     INFOS("create_component_instance returns a NULL component !");
   return myInstance._retn();
 }
 
+/*! \brief copy a file from a source host to a destination host
+ * \param hostSrc the source host
+ * \param fileSrc the file to copy from the source host to the destination host
+ * \param hostDest the destination host
+ * \param fileDest the destination file
+ */
+void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
+{
+  if(strcmp(hostDest,"localhost") == 0)
+    {
+      //if localhost use a shortcut
+      SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
+      transfer.getLocalFile(fileDest);
+      return;
+    }
+
+  Engines::ContainerManager_var contManager = getContainerManager();
+
+  Engines::MachineParameters params;
+  preSet(params);
+
+  params.hostname = hostDest;
+  Engines::Container_var containerDest = contManager->FindOrStartContainer(params);
+
+  params.hostname = hostSrc;
+  Engines::Container_var containerSrc = contManager->FindOrStartContainer(params);
+
+  containerDest->copyFile(containerSrc,fileSrc,fileDest);
+}
+
+/*! \brief get the naming service used by the life cycle
+ *
+ *  \return the naming service
+ */
+SALOME_NamingService * SALOME_LifeCycleCORBA::namingService()
+{
+  return _NS;
+}
+
+/*! \brief get the orb used by the life cycle
+ *
+ *  \return the orb
+ */
+CORBA::ORB_ptr SALOME_LifeCycleCORBA::orb()
+{
+  return _NS->orb();
+}
+