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