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