Salome HOME
YACS in SSL mode
[modules/kernel.git] / src / LifeCycleCORBA / SALOME_LifeCycleCORBA.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++
24 //  File   : SALOME_LifeCycleCORBA.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <iomanip>
32
33 #include <time.h>
34 #ifndef WIN32
35   #include <sys/time.h>
36   #include <unistd.h>
37 #endif
38
39 #include "Basics_Utils.hxx"
40 #include "utilities.h"
41
42 #include <ServiceUnreachable.hxx>
43
44 #include "SALOME_LifeCycleCORBA.hxx"
45 #include "SALOME_ResourcesManager.hxx"
46 #include "SALOMESDS_DataServerManager.hxx"
47 #include "SALOME_ExternalServerLauncher.hxx"
48
49 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
50 #include CORBA_CLIENT_HEADER(SALOME_Session)
51 #include CORBA_CLIENT_HEADER(DSC_Engines)
52 #include CORBA_CLIENT_HEADER(SALOME_Registry)
53 #include CORBA_CLIENT_HEADER(SALOMEDS)
54 #include CORBA_CLIENT_HEADER(SALOME_SDS)
55 #include CORBA_CLIENT_HEADER(Logger)
56 #include CORBA_CLIENT_HEADER(SALOME_Launcher)
57
58 #include "SALOME_ResourcesManager.hxx"
59 #include "SALOME_ContainerManager.hxx"
60 #include "SALOME_Component_i.hxx"
61 #include "SALOME_NamingService.hxx"
62 #include "SALOME_FileTransferCORBA.hxx"
63
64 IncompatibleComponent::IncompatibleComponent( void ):
65   SALOME_Exception( "IncompatibleComponent" )
66 {
67 }
68
69 IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
70   SALOME_Exception( ex )
71 {
72 }
73
74 /*! \class SALOME_LifeCycleCORBA
75     \brief A class to manage life cycle of SALOME components.
76
77 */
78
79 //=============================================================================
80 /*!
81  *  Constructor
82  */
83 //=============================================================================
84
85 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService_Abstract *ns)
86 {
87   // be sure to have an instance of traceCollector, when used via SWIG
88   // in a Python module
89   int argc = 0;
90   char *xargv = (char*)"";
91   char **argv = &xargv;
92   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
93   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
94   _NSnew=0;
95   if (!ns)
96     {
97       _NS = new SALOME_NamingService(orb);
98       _NSnew=_NS;
99     }
100   else _NS = ns;
101   //add try catch
102   _NS->Change_Directory("/"); // mpv 250105: current directory may be not root
103                               // (in SALOMEDS for an example)
104   // not enough: set a current directory in naming service is not thread safe
105   // if naming service instance is shared among several threads...
106   // ==> always use absolute path and don't rely on current directory!
107   //if( dynamic_cast<SALOME_NamingService *>(_NS) )
108   {
109     CORBA::Object_var obj =
110       _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
111     if (CORBA::is_nil(obj))
112       throw SALOME_Exception("Error: Cannot resolve ContainerManager in Naming Service");
113     _ContManager=Engines::ContainerManager::_narrow(obj);
114
115     obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
116     if (CORBA::is_nil(obj))
117       throw SALOME_Exception("Error: Cannot resolve ResourceManager in Naming Service");
118     _ResManager=Engines::ResourcesManager::_narrow(obj);
119   }
120 }
121
122 //=============================================================================
123 /*!
124  *  Destructor
125  */
126 //=============================================================================
127
128 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
129 {
130   if(_NSnew)delete _NSnew;
131 }
132
133 //=============================================================================
134 /*! \brief Find an already existing and registered component instance.
135  *
136  *  \param params         container parameters like type or name...
137  *  \param componentName  the name of component class
138  *  \return a CORBA reference of the component instance, or _nil if not found
139  */
140 //=============================================================================
141 Engines::EngineComponent_ptr
142 SALOME_LifeCycleCORBA::FindComponent(const Engines::ContainerParameters& params,
143                                      const char *componentName)
144 {
145   if (! isKnownComponentClass(componentName))
146     return Engines::EngineComponent::_nil();
147
148   Engines::ContainerParameters new_params(params);
149   new_params.resource_params.componentList.length(1);
150   new_params.resource_params.componentList[0] = componentName;
151   new_params.resource_params.can_run_containers = true;
152   Engines::ResourceList_var listOfResources;
153   try
154     {
155       listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
156     }
157   catch( const SALOME::SALOME_Exception& /*ex*/ ) //!< TODO: unused variable
158     {
159       return Engines::EngineComponent::_nil();
160     }
161
162   Engines::EngineComponent_var compo = _FindComponent(new_params,
163                                                 componentName,
164                                                 listOfResources);
165
166   return compo._retn();
167 }
168
169 //=============================================================================
170 /*! \brief Load a component instance on a container defined by its parameters
171  *
172  *  \param params         container parameters like type or name...
173  *  \param componentName  the name of component class
174  *  \return a CORBA reference of the component instance, or _nil if problem
175  */
176 //=============================================================================
177
178 Engines::EngineComponent_ptr
179 SALOME_LifeCycleCORBA::LoadComponent(const Engines::ContainerParameters& params,
180                                      const char *componentName)
181 {
182   // --- Check if Component Name is known in ModuleCatalog
183
184   if (! isKnownComponentClass(componentName))
185     return Engines::EngineComponent::_nil();
186
187   Engines::ContainerParameters new_params(params);
188   new_params.resource_params.componentList.length(1);
189   new_params.resource_params.componentList[0] = componentName;
190   new_params.resource_params.can_run_containers = true;
191
192   Engines::ResourceList_var listOfResources;
193   try
194     {
195       listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
196     }
197   catch( const SALOME::SALOME_Exception& /*ex*/ ) //!< TODO: unused variable
198     {
199       return Engines::EngineComponent::_nil();
200     }
201   new_params.resource_params.resList = listOfResources;
202
203   Engines::EngineComponent_var compo = _LoadComponent(new_params,
204                                                       componentName);
205
206   return compo._retn();
207 }
208
209 //=============================================================================
210 /*! \brief Find an already existing and registered component instance or load a new
211  *         component instance on a container defined by its parameters.
212  *
213  *  \param params         container parameters like type or name...
214  *  \param componentName  the name of component class
215  *  \return a CORBA reference of the component instance, or _nil if problem
216  */
217 //=============================================================================
218
219 Engines::EngineComponent_ptr
220 SALOME_LifeCycleCORBA::
221 FindOrLoad_Component(const Engines::ContainerParameters& params,
222                      const char *componentName)
223 {
224   // --- Check if Component Name is known in ModuleCatalog
225
226   if (! isKnownComponentClass(componentName))
227     return Engines::EngineComponent::_nil();
228
229   Engines::ContainerParameters new_params(params);
230   new_params.resource_params.componentList.length(1);
231   new_params.resource_params.componentList[0] = componentName;
232   new_params.resource_params.can_run_containers = true;
233
234   Engines::ResourceList_var listOfResources;
235   try
236     {
237       listOfResources = _ResManager->GetFittingResources(new_params.resource_params);
238     }
239   catch( const SALOME::SALOME_Exception& /*ex*/ ) //!< TODO: unused variable
240     {
241       return Engines::EngineComponent::_nil();
242     }
243
244   Engines::EngineComponent_var compo = _FindComponent(new_params,
245                                                       componentName,
246                                                       listOfResources);
247
248   if(CORBA::is_nil(compo))
249   {
250     new_params.resource_params.resList = listOfResources;
251     compo = _LoadComponent(new_params,
252                            componentName);
253   }
254
255   return compo._retn();
256 }
257
258 //=============================================================================
259 /*! \brief Find an already existing and registered component instance or load a new
260  *         component instance on a container defined by name
261  *
262  *  \param containerName  the name of container, under one of the forms
263  *           - 1 aContainer (local container)
264  *           - 2 machine/aContainer (container on hostname = machine)
265  *  \param componentName  the name of component class
266  *  \return a CORBA reference of the component instance, or _nil if problem
267  */
268 //=============================================================================
269
270 Engines::EngineComponent_ptr
271 SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
272                                             const char *componentName)
273 {
274   MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component INTERACTIF " << containerName << " " << componentName ) ;
275
276   // --- Check if Component Name is known in ModuleCatalog
277   if (! isKnownComponentClass(componentName))
278     return Engines::EngineComponent::_nil();
279
280   // --- Check if containerName contains machine name (if yes: rg>0)
281   char *stContainer=strdup(containerName);
282   std::string st2Container(stContainer);
283   size_t rg=st2Container.find("/");
284
285   Engines::ContainerParameters params;
286   preSet(params);
287   if (rg == std::string::npos)
288   {
289     // containerName doesn't contain "/" => Local container
290     params.container_name = CORBA::string_dup(stContainer);
291   }
292   else
293   {
294     stContainer[rg]='\0';
295     params.container_name = CORBA::string_dup(stContainer+rg+1);
296     params.resource_params.hostname = CORBA::string_dup(stContainer);
297   }
298   params.isMPI = false;
299   SCRUTE(params.container_name);
300   free(stContainer);
301   return FindOrLoad_Component(params, componentName);
302 }
303
304 //=============================================================================
305 /*! \brief Check if the component class is known in module catalog
306  *
307  *  \param componentName  the name of component class
308  *  \return true if found, false otherwise
309  */
310 //=============================================================================
311
312 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
313 {
314   try
315   {
316     CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
317     SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
318       SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
319     ASSERT(! CORBA::is_nil(Catalog));
320     SALOME_ModuleCatalog::Acomponent_var compoInfo =
321       Catalog->GetComponent(componentName);
322     if (CORBA::is_nil (compoInfo))
323     {
324       MESSAGE("Catalog Error: Component not found in the catalog " << componentName);
325       return false;
326     }
327     else return true;
328   }
329   catch (ServiceUnreachable&)
330   {
331     INFOS("Caught exception: Naming Service Unreachable");
332   }
333   catch (...)
334   {
335     INFOS("Caught unknown exception.");
336   }
337   return false;
338 }
339
340 //=============================================================================
341 /*! \brief Initialisation of a given Engines::ResourceParameters with default values.
342  */
343 //=============================================================================
344
345 void
346 SALOME_LifeCycleCORBA::preSet(Engines::ResourceParameters& params)
347 {
348   params.name = "";
349   params.hostname = "";
350   params.OS = "";
351   params.nb_proc = 0;
352   params.mem_mb = 0;
353   params.cpu_clock = 0;
354   params.nb_node = 0;
355   params.nb_proc_per_node = 0;
356   params.policy = "";
357   params.can_launch_batch_jobs = false;
358   params.can_run_containers = false;
359 }
360
361 //=============================================================================
362 /*! \brief Initialisation of a given Engines::ContainerParameters with default values.
363  */
364 //=============================================================================
365
366 void SALOME_LifeCycleCORBA::preSet( Engines::ContainerParameters& params)
367 {
368   params.container_name = "";
369   params.mode = "";
370   params.workingdir = "";
371   params.nb_proc = 0;
372   params.isMPI = false;
373   params.parallelLib = "";
374   SALOME_LifeCycleCORBA::preSet(params.resource_params);
375 }
376
377 //=============================================================================
378 /*!
379  *  \return a number of processors not 0, only for MPI containers
380  */
381 //=============================================================================
382
383 int SALOME_LifeCycleCORBA::NbProc(const Engines::ContainerParameters& params)
384 {
385   if( !params.isMPI )
386     return 0;
387   else if( params.nb_proc <= 0 )
388     return 1;
389   else
390     return params.nb_proc;
391 }
392
393 //=============================================================================
394 /*! \brief Get the container manager
395  *
396  *  \return the container Manager
397  */
398 //=============================================================================
399
400 Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
401 {
402  Engines::ContainerManager_var contManager =
403    Engines::ContainerManager::_duplicate(_ContManager);
404  return contManager._retn();
405 }
406
407 //=============================================================================
408 /*! \brief Get the resources manager
409  *
410  *  \return the container Manager
411  */
412 //=============================================================================
413
414 Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
415 {
416  Engines::ResourcesManager_var resManager =
417    Engines::ResourcesManager::_duplicate(_ResManager);
418  return resManager._retn();
419 }
420
421 //=============================================================================
422 /*! \brief shutdown all the SALOME servers except SALOME_Session_Server and omniNames
423  */
424 //=============================================================================
425
426 void SALOME_LifeCycleCORBA::shutdownServers(bool shutdownLauncher)
427 {
428   // get each Container from NamingService => shutdown it
429   // (the order is inverse to the order of servers initialization)
430
431   SALOME::Session_var session = SALOME::Session::_nil();
432   CORBA::Long pid = 0;
433   CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
434   if (!CORBA::is_nil(objS))
435   {
436     session = SALOME::Session::_narrow(objS);
437     if (!CORBA::is_nil(session))
438     {
439       pid = session->getPID();
440       session->Shutdown();
441     }
442   }
443
444   std::string hostname = Kernel_Utils::GetHostname();
445
446   // 1) ConnectionManager
447   try
448     {
449       CORBA::Object_var objCnM=_NS->Resolve("/ConnectionManager");
450       Engines::ConnectionManager_var connMan=Engines::ConnectionManager::_narrow(objCnM);
451       if ( !CORBA::is_nil(connMan) && ( pid != connMan->getPID() ) )
452         connMan->ShutdownWithExit();
453     }
454   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
455     {
456        // ignore and continue
457     }
458
459   timespec ts_req;
460   ts_req.tv_nsec=100000000;
461   ts_req.tv_sec=0;
462
463 //Wait some time so that ConnectionManager be completely shutdown
464 #ifndef WIN32
465   nanosleep(&ts_req,0);
466 #endif
467
468   // 2) SALOMEDS
469   try
470     {
471       CORBA::Object_var objSDS = _NS->Resolve("/Study");
472       SALOMEDS::Study_var study = SALOMEDS::Study::_narrow(objSDS) ;
473       if ( !CORBA::is_nil(study) && ( pid != study->getPID() ) )
474         study->Shutdown();
475       _NS->Destroy_Name("/Study");
476     }
477   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
478     {
479        // ignore and continue
480     }
481
482 //Wait some time so that study be completely shutdown
483 #ifndef WIN32
484   nanosleep(&ts_req,0);
485 #endif
486
487   // 3) ModuleCatalog
488   try
489     {
490       CORBA::Object_var objMC=_NS->Resolve("/Kernel/ModulCatalog");
491       SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objMC);
492       if ( !CORBA::is_nil(catalog) && ( pid != catalog->getPID() ) )
493         catalog->shutdown();
494       _NS->Destroy_Name("/Kernel/ModulCatalog");
495     }
496   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
497     {
498        // ignore and continue
499     }
500
501 //Wait some time so that ModulCatalog be completely shutdown
502 #ifndef WIN32
503   nanosleep(&ts_req,0);
504 #endif
505   // 4 ) Remote ScopeServer (the DataServer is hosted by SalomeLauncher shutdown right after on point 6)
506   try
507     {
508       CORBA::Object_var objDSM(_NS->Resolve(SALOMESDS::DataServerManager::NAME_IN_NS));
509       SALOME::DataServerManager_var dsm(SALOME::DataServerManager::_narrow(objDSM));
510       if ( !CORBA::is_nil(dsm) )
511         dsm->shutdownScopes();
512     }
513   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
514     {
515        // ignore and continue
516     }
517   // 5) External server launcher (the ExternalServer is hosted by SalomeLauncher shutdown right after on point 6)
518   try
519     {
520       CORBA::Object_var objDSM(_NS->Resolve(SALOME_ExternalServerLauncher::NAME_IN_NS));
521       SALOME::ExternalServerLauncher_var dsm(SALOME::ExternalServerLauncher::_narrow(objDSM));
522       if ( !CORBA::is_nil(dsm) )
523         dsm->shutdownServers();
524     }
525   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
526     {
527        // ignore and continue
528     }
529   // 6) SalomeLauncher
530   try
531     {
532       if(shutdownLauncher){
533         CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
534         Engines::SalomeLauncher_var launcher = Engines::SalomeLauncher::_narrow(objSL);
535         if (!CORBA::is_nil(launcher) && (pid != launcher->getPID()))
536           launcher->Shutdown();
537       }
538     }
539   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
540     {
541        // ignore and continue
542     }
543
544 //Wait some time so that launcher be completely shutdown
545 #ifndef WIN32
546   nanosleep(&ts_req,0);
547 #endif
548
549   // 6) Registry
550   try
551     {
552       CORBA::Object_var objR = _NS->Resolve("/Registry");
553       Registry::Components_var registry = Registry::Components::_narrow(objR);
554       if ( !CORBA::is_nil(registry) && ( pid != registry->getPID() ) )
555           registry->Shutdown();
556       _NS->Destroy_Name("/Registry");
557     }
558   catch(const CORBA::Exception& /*e*/) //!< TODO: unused variable
559     {
560        // ignore and continue
561     }
562
563   // 7) Logger
564   int argc = 0;
565   char *xargv = (char*)"";
566   char **argv = &xargv;
567   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
568
569   CORBA::Object_var objLog = CORBA::Object::_nil();
570   CosNaming::NamingContext_var inc;
571   CORBA::Object_var theObj = CORBA::Object::_nil();
572   std::string stdname = "Logger";
573   CosNaming::Name name;
574   name.length(1);
575   name[0].id = CORBA::string_dup(stdname.c_str());
576   try
577   {
578     if(!CORBA::is_nil(orb))
579       theObj = orb->resolve_initial_references("NameService");
580     if (!CORBA::is_nil(theObj))
581       inc = CosNaming::NamingContext::_narrow(theObj);
582   }
583   catch(...)
584   {
585   }
586   if(!CORBA::is_nil(inc))
587   {
588     try
589     {
590       objLog = inc->resolve(name);
591       SALOME_Logger::Logger_var logger = SALOME_Logger::Logger::_narrow(objLog);
592       if ( !CORBA::is_nil(logger) )
593         logger->shutdown();
594     }
595     catch(...)
596     {
597     }
598   }
599 }
600
601 //=============================================================================
602 /*! \brief shutdown  omniNames
603  */
604 //=============================================================================
605
606 void SALOME_LifeCycleCORBA::killOmniNames()
607 {
608   std::string portNumber (::getenv ("NSPORT") );
609   std::string python_exe;
610
611   python_exe = std::string("python3");
612
613   if ( !portNumber.empty() )
614   {
615     std::string cmd;
616
617     cmd  = std::string("from salome_utils import killOmniNames; ");
618     cmd += std::string("killOmniNames(") + portNumber + "); ";
619     cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
620     MESSAGE(cmd);
621     system( cmd.c_str() );
622
623     cmd  = std::string("from killSalomeWithPort import cleanApplication; ");
624     cmd += std::string("cleanApplication(") + portNumber + "); ";
625     cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
626     MESSAGE(cmd);
627     system( cmd.c_str() );
628   }
629
630   // shutdown portmanager
631   if ( !portNumber.empty() )
632   {
633     std::string cmd;
634
635     cmd  = std::string("from PortManager import releasePort; ");
636     cmd += std::string("releasePort(") + portNumber + "); ";
637     cmd  = python_exe + std::string(" -c \"") + cmd +"\"";
638     MESSAGE(cmd);
639     system( cmd.c_str() );
640   }
641 }
642
643 //=============================================================================
644 /*! \brief Find an already existing and registered component instance.
645  *
646  * - build a list of machines on which an instance of the component is running,
647  * - find the best machine among the list
648  *
649  *  \param params         machine parameters like type or name...
650  *  \param componentName  the name of component class
651  *  \param listOfMachines list of machine address
652  *  \return a CORBA reference of the component instance, or _nil if not found
653  */
654 //=============================================================================
655
656 Engines::EngineComponent_ptr
657 SALOME_LifeCycleCORBA::
658 _FindComponent(const Engines::ContainerParameters& params,
659                const char *componentName,
660                const Engines::ResourceList& listOfResources)
661 {
662   // --- build the list of machines on which the component is already running
663   const char *containerName = params.container_name;
664   int nbproc = NbProc(params);
665
666   Engines::ResourceList_var resourcesOK = new Engines::ResourceList;
667
668   unsigned int lghtOfresourcesOK = 0;
669   resourcesOK->length(listOfResources.length());
670
671   for(unsigned int i=0; i < listOfResources.length(); i++)
672   {
673     const char * currentResource = listOfResources[i];
674     Engines::ResourceDefinition_var resource_definition =
675         _ResManager->GetResourceDefinition(currentResource);
676     CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
677                                                   containerName,
678                                                   componentName,
679                                                   nbproc);
680     if (!CORBA::is_nil(obj))
681       resourcesOK[lghtOfresourcesOK++] = CORBA::string_dup(currentResource);
682   }
683
684   // --- find the best machine among the list
685   if(lghtOfresourcesOK != 0)
686   {
687     resourcesOK->length(lghtOfresourcesOK);
688     CORBA::String_var bestResource = _ResManager->FindFirst(resourcesOK);
689     Engines::ResourceDefinition_var resource_definition =
690         _ResManager->GetResourceDefinition(bestResource);
691     CORBA::Object_var obj = _NS->ResolveComponent(resource_definition->hostname.in(),
692                                                   containerName,
693                                                   componentName,
694                                                   nbproc);
695     return Engines::EngineComponent::_narrow(obj);
696   }
697   else
698     return Engines::EngineComponent::_nil();
699 }
700
701 //=============================================================================
702 /*! \brief  Load a component instance.
703  *
704  *  - Finds a container in the list of machine or start one.
705  *  - Try to load the component library in the container,
706  *  - then create an instance of the component.
707  *
708  *  \param params         machine parameters like type or name...
709  *  \param componentName  the name of component class
710  *  \return a CORBA reference of the component instance, or _nil if problem
711  */
712 //=============================================================================
713
714 Engines::EngineComponent_ptr
715 SALOME_LifeCycleCORBA::
716 _LoadComponent(const Engines::ContainerParameters& params,
717               const char *componentName)
718 {
719   MESSAGE("_LoadComponent, required " << params.container_name <<
720           " " << componentName << " " << NbProc(params));
721
722   Engines::ContainerParameters local_params(params);
723   local_params.mode = CORBA::string_dup("findorstart");
724   Engines::Container_var cont = _ContManager->GiveContainer(local_params);
725   if (CORBA::is_nil(cont)) return Engines::EngineComponent::_nil();
726
727   char* reason;
728   bool isLoadable = cont->load_component_Library(componentName,reason);
729   if (!isLoadable)
730     {
731       //std::cerr << reason << std::endl;
732       CORBA::string_free(reason);
733       return Engines::EngineComponent::_nil();
734     }
735   CORBA::string_free(reason);
736
737   Engines::EngineComponent_var myInstance =
738     cont->create_component_instance(componentName);
739   return myInstance._retn();
740 }
741
742 //=============================================================================
743 /*! \brief  Load a parallel component instance.
744  *
745  *  \param params         machine parameters like type or name...
746  *  \param componentName  the name of component class
747  *  \return a CORBA reference of the parallel component instance, or _nil if problem
748  */
749 //=============================================================================
750 Engines::EngineComponent_ptr
751 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::ContainerParameters& params,
752                                               const char *componentName)
753 {
754   MESSAGE("Entering LoadParallelComponent");
755
756 /*MESSAGE("Parameters : ");
757   MESSAGE("Container name : " << params.container_name);
758   MESSAGE("Number of component nodes : " << params.nb_component_nodes);
759   MESSAGE("Component Name : " << componentName);*/
760
761   Engines::ContainerParameters parms(params);
762   parms.resource_params.componentList.length(1);
763   parms.resource_params.componentList[0] = componentName;
764   parms.mode = CORBA::string_dup("findorstart");
765
766   MESSAGE("Starting Parallel Container");
767   Engines::Container_var cont = _ContManager->GiveContainer(parms);
768   if (CORBA::is_nil(cont)) {
769     INFOS("FindOrStartParallelContainer() returns a NULL container !");
770     return Engines::EngineComponent::_nil();
771   }
772
773   MESSAGE("Loading component library");
774   char* reason;
775   bool isLoadable = cont->load_component_Library(componentName,reason);
776   if (!isLoadable) {
777     INFOS(componentName <<" library is not loadable !");
778     //std::cerr << reason << std::endl;
779     CORBA::string_free(reason);
780     return Engines::EngineComponent::_nil();
781   }
782   CORBA::string_free(reason);
783
784   MESSAGE("Creating component instance");
785   // @PARALLEL@ permits to identify that the component requested
786   // is a parallel component.
787   std::string name = std::string(componentName);
788   Engines::EngineComponent_var myInstance = cont->create_component_instance(name.c_str());
789   if (CORBA::is_nil(myInstance))
790     INFOS("create_component_instance returns a NULL component !");
791   return myInstance._retn();
792 }
793
794 /*! \brief copy a file from a source host to a destination host
795  * \param hostSrc the source host
796  * \param fileSrc the file to copy from the source host to the destination host
797  * \param hostDest the destination host
798  * \param fileDest the destination file
799  */
800 void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
801 {
802   if(strcmp(hostDest,"localhost") == 0)
803     {
804       //if localhost use a shortcut
805       SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
806       transfer.getLocalFile(fileDest);
807       return;
808     }
809
810   Engines::ContainerManager_var contManager = getContainerManager();
811
812   Engines::ContainerParameters params;
813   preSet(params);
814
815   params.resource_params.hostname = hostDest;
816   params.mode = CORBA::string_dup("findorstart");
817   Engines::Container_var containerDest = contManager->GiveContainer(params);
818
819   params.resource_params.hostname = hostSrc;
820   Engines::Container_var containerSrc = contManager->GiveContainer(params);
821
822   containerDest->copyFile(containerSrc,fileSrc,fileDest);
823 }
824
825 /*! \brief get the naming service used by the life cycle
826  *
827  *  \return the naming service
828  */
829 SALOME_NamingService_Abstract * SALOME_LifeCycleCORBA::namingService()
830 {
831   return _NS;
832 }
833
834 /*! \brief get the orb used by the life cycle
835  *
836  *  \return the orb
837  */
838 CORBA::ORB_ptr SALOME_LifeCycleCORBA::orb()
839 {
840   SALOME_NamingService *NSC = dynamic_cast<SALOME_NamingService *>(_NS);
841   if(!_NS)
842     THROW_SALOME_EXCEPTION("SALOME_LifeCycleCORBA::orb : not a CORBA SALOME_NamingService ");
843   return NSC->orb();
844 }