1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++
23 // File : SALOME_LifeCycleCORBA.cxx
24 // Author : Paul RASCLE, EDF
38 #include "Basics_Utils.hxx"
39 #include "utilities.h"
41 #include <ServiceUnreachable.hxx>
43 #include "SALOME_LifeCycleCORBA.hxx"
44 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
45 #include CORBA_CLIENT_HEADER(SALOME_Session)
46 #include CORBA_CLIENT_HEADER(DSC_Engines)
47 #include CORBA_CLIENT_HEADER(SALOME_Registry)
48 #include CORBA_CLIENT_HEADER(SALOMEDS)
49 #include CORBA_CLIENT_HEADER(Logger)
51 #include "SALOME_ContainerManager.hxx"
52 #include "SALOME_Component_i.hxx"
53 #include "SALOME_NamingService.hxx"
54 #include "SALOME_FileTransferCORBA.hxx"
58 IncompatibleComponent::IncompatibleComponent( void ):
59 SALOME_Exception( "IncompatibleComponent" )
63 IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
64 SALOME_Exception( ex )
68 /*! \class SALOME_LifeCycleCORBA
69 \brief A class to manage life cycle of SALOME components.
73 //=============================================================================
77 //=============================================================================
79 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
81 // be sure to have an instance of traceCollector, when used via SWIG
84 char *xargv = (char*)"";
86 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
87 // LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
90 _NS = new SALOME_NamingService(orb);
94 _NS->Change_Directory("/"); // mpv 250105: current directory may be not root
95 // (in SALOMEDS for an example)
96 // not enough: set a current directory in naming service is not thread safe
97 // if naming service instance is shared among several threads...
98 // ==> allways use absolute path and dot rely on current directory!
100 CORBA::Object_var obj =
101 _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
102 ASSERT( !CORBA::is_nil(obj));
103 _ContManager=Engines::ContainerManager::_narrow(obj);
105 obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
106 ASSERT( !CORBA::is_nil(obj));
107 _ResManager=Engines::ResourcesManager::_narrow(obj);
110 //=============================================================================
114 //=============================================================================
116 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
120 //=============================================================================
121 /*! \brief Find an already existing and registered component instance.
123 * \param params machine parameters like type or name...
124 * \param componentName the name of component class
125 * \param studyId default = 0 : multistudy instance
126 * \return a CORBA reference of the component instance, or _nil if not found
128 //=============================================================================
129 Engines::Component_ptr
130 SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
131 const char *componentName,
134 if (! isKnownComponentClass(componentName))
135 return Engines::Component::_nil();
137 Engines::CompoList clist;
139 clist[0] = componentName;
140 Engines::MachineList_var listOfMachines =
141 _ResManager->GetFittingResources(params, clist);
143 Engines::Component_var compo = _FindComponent(params,
148 return compo._retn();
151 //=============================================================================
152 /*! \brief Load a component instance on a container defined by machine parameters
154 * \param params machine parameters like type or name...
155 * \param componentName the name of component class
156 * \param studyId default = 0 : multistudy instance
157 * \return a CORBA reference of the component instance, or _nil if problem
159 //=============================================================================
161 Engines::Component_ptr
162 SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params,
163 const char *componentName,
166 // --- Check if Component Name is known in ModuleCatalog
168 if (! isKnownComponentClass(componentName))
169 return Engines::Component::_nil();
171 Engines::CompoList clist;
173 clist[0] = componentName;
174 Engines::MachineList_var listOfMachines =
175 _ResManager->GetFittingResources(params, clist);
177 Engines::Component_var compo = _LoadComponent(params,
182 return compo._retn();
185 //=============================================================================
186 /*! \brief Find an already existing and registered component instance or load a new
187 * component instance on a container defined by machine parameters.
189 * \param params machine parameters like type or name...
190 * \param componentName the name of component class
191 * \param studyId default = 0 : multistudy instance
192 * \return a CORBA reference of the component instance, or _nil if problem
194 //=============================================================================
196 Engines::Component_ptr
197 SALOME_LifeCycleCORBA::
198 FindOrLoad_Component(const Engines::MachineParameters& params,
199 const char *componentName,
202 // --- Check if Component Name is known in ModuleCatalog
204 if (! isKnownComponentClass(componentName))
205 return Engines::Component::_nil();
207 Engines::CompoList clist;
209 clist[0] = componentName;
210 Engines::MachineList_var listOfMachines =
211 _ResManager->GetFittingResources(params,clist);
213 Engines::Component_var compo = _FindComponent(params,
218 if(CORBA::is_nil(compo))
219 compo = _LoadComponent(params,
224 return compo._retn();
227 //=============================================================================
228 /*! \brief Find an already existing and registered component instance or load a new
229 * component instance on a container defined by name
231 * \param containerName the name of container, under one of the forms
232 * - 1 aContainer (local container)
233 * - 2 machine/aContainer (container on hostname = machine)
234 * \param componentName the name of component class
235 * \return a CORBA reference of the component instance, or _nil if problem
237 //=============================================================================
239 Engines::Component_ptr
240 SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
241 const char *componentName)
243 char *valenv=getenv("SALOME_BATCH");
245 if (strcmp(valenv,"1")==0)
247 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component BATCH " << containerName << " " << componentName ) ;
248 _NS->Change_Directory("/Containers");
249 CORBA::Object_ptr obj=_NS->Resolve(containerName);
250 Engines::Container_var cont=Engines::Container::_narrow(obj);
251 bool isLoadable = cont->load_component_Library(componentName);
252 if (!isLoadable) return Engines::Component::_nil();
254 Engines::Component_ptr myInstance =
255 cont->create_component_instance(componentName, 0);
258 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component INTERACTIF " << containerName << " " << componentName ) ;
260 // --- Check if Component Name is known in ModuleCatalog
262 if (! isKnownComponentClass(componentName))
263 return Engines::Component::_nil();
265 // --- Check if containerName contains machine name (if yes: rg>0)
267 char *stContainer=strdup(containerName);
268 string st2Container(stContainer);
269 int rg=st2Container.find("/");
271 Engines::MachineParameters_var params=new Engines::MachineParameters;
275 // containerName doesn't contain "/" => Local container
276 params->container_name=CORBA::string_dup(stContainer);
277 params->hostname=CORBA::string_dup(Kernel_Utils::GetHostname().c_str());
281 stContainer[rg]='\0';
282 params->container_name=CORBA::string_dup(stContainer+rg+1);
283 params->hostname=CORBA::string_dup(stContainer);
285 params->isMPI = false;
286 SCRUTE(params->container_name);
287 // SCRUTE(params->hostname);
288 // SCRUTE(params->OS);
289 // SCRUTE(params->mem_mb);
290 // SCRUTE(params->cpu_clock);
291 // SCRUTE(params->nb_proc_per_node);
292 // SCRUTE(params->nb_node);
293 // SCRUTE(params->isMPI);
295 return FindOrLoad_Component(params,componentName);
299 //=============================================================================
300 /*! \brief Check if the component class is known in module catalog
302 * \param componentName the name of component class
303 * \return true if found, false otherwise
305 //=============================================================================
307 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
312 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
313 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
314 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
315 ASSERT(! CORBA::is_nil(Catalog));
316 SALOME_ModuleCatalog::Acomponent_var compoInfo =
317 Catalog->GetComponent(componentName);
318 if (CORBA::is_nil (compoInfo))
320 INFOS("Catalog Error: Component not found in the catalog" );
321 INFOS( componentName );
326 catch (ServiceUnreachable&)
328 INFOS("Caught exception: Naming Service Unreachable");
332 INFOS("Caught unknown exception.");
337 //=============================================================================
339 * Not so complex... useful ?
341 //=============================================================================
344 SALOME_LifeCycleCORBA::isMpiContainer(const Engines::MachineParameters& params)
345 throw(IncompatibleComponent)
354 //=============================================================================
355 /*! \brief Initialisation of a given Engines::MachineParameters with default values.
357 * - container_name = "" : not relevant
358 * - hostname = "" : not relevant
359 * - OS = "" : not relevant
360 * - mem_mb = 0 : not relevant
361 * - cpu_clock = 0 : not relevant
362 * - nb_proc_per_node = 0 : not relevant
363 * - nb_node = 0 : not relevant
364 * - isMPI = false : standard components
366 //=============================================================================
368 void SALOME_LifeCycleCORBA::preSet( Engines::MachineParameters& params)
370 params.container_name = "";
371 params.hostname = "";
373 params.protocol = "";
374 params.username = "";
375 params.applipath = "";
376 //param.componentList = 0;
377 //param.computerList = 0;
380 params.cpu_clock = 0;
381 params.nb_proc_per_node = 0;
383 params.isMPI = false;
386 params.workingdir = "";
389 params.parallelLib = "";
390 params.nb_component_nodes = 0;
393 //=============================================================================
395 * \return a number of processors not 0, only for MPI containers
397 //=============================================================================
399 int SALOME_LifeCycleCORBA::NbProc(const Engines::MachineParameters& params)
401 if( !isMpiContainer(params) )
403 else if( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
405 else if( params.nb_node == 0 )
406 return params.nb_proc_per_node;
407 else if( params.nb_proc_per_node == 0 )
408 return params.nb_node;
410 return params.nb_node * params.nb_proc_per_node;
413 //=============================================================================
414 /*! \brief Get the container manager
416 * \return the container Manager
418 //=============================================================================
420 Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
422 Engines::ContainerManager_var contManager =
423 Engines::ContainerManager::_duplicate(_ContManager);
424 return contManager._retn();
427 //=============================================================================
428 /*! \brief Get the resources manager
430 * \return the container Manager
432 //=============================================================================
434 Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
436 Engines::ResourcesManager_var resManager =
437 Engines::ResourcesManager::_duplicate(_ResManager);
438 return resManager._retn();
441 //=============================================================================
442 /*! \brief shutdown all the SALOME servers except SALOME_Session_Server, omniNames and notifd
444 //=============================================================================
446 void SALOME_LifeCycleCORBA::shutdownServers()
448 // get each Container from NamingService => shutdown it
449 // (the order is inverse to the order of servers initialization)
451 SALOME::Session_var session = SALOME::Session::_nil();
453 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
454 if (!CORBA::is_nil(objS))
456 session = SALOME::Session::_narrow(objS);
457 if (!CORBA::is_nil(session))
459 pid = session->getPID();
464 string hostname = Kernel_Utils::GetHostname();
467 CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
468 Engines::SalomeLauncher_var launcher = Engines::SalomeLauncher::_narrow(objSL);
469 if (!CORBA::is_nil(launcher) && (pid != launcher->getPID()))
470 launcher->Shutdown();
472 // 2) ConnectionManager
473 CORBA::Object_var objCnM=_NS->Resolve("/ConnectionManager");
474 Engines::ConnectionManager_var connMan=Engines::ConnectionManager::_narrow(objCnM);
475 if ( !CORBA::is_nil(connMan) && ( pid != connMan->getPID() ) )
476 connMan->ShutdownWithExit();
479 CORBA::Object_var objSDS = _NS->Resolve("/myStudyManager");
480 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(objSDS) ;
481 if ( !CORBA::is_nil(studyManager) && ( pid != studyManager->getPID() ) )
482 studyManager->Shutdown();
485 CORBA::Object_var objMC=_NS->Resolve("/Kernel/ModulCatalog");
486 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objMC);
487 if ( !CORBA::is_nil(catalog) && ( pid != catalog->getPID() ) )
491 CORBA::Object_var objR = _NS->Resolve("/Registry");
492 Registry::Components_var registry = Registry::Components::_narrow(objR);
493 if ( !CORBA::is_nil(registry) && ( pid != registry->getPID() ) )
494 registry->Shutdown();
498 char *xargv = (char*)"";
499 char **argv = &xargv;
500 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
502 CORBA::Object_var objLog = CORBA::Object::_nil();
503 CosNaming::NamingContext_var inc;
504 CORBA::Object_var theObj = CORBA::Object::_nil();
505 std::string stdname = "Logger";
506 CosNaming::Name name;
508 name[0].id = CORBA::string_dup(stdname.c_str());
511 if(!CORBA::is_nil(orb))
512 theObj = orb->resolve_initial_references("NameService");
513 if (!CORBA::is_nil(theObj))
514 inc = CosNaming::NamingContext::_narrow(theObj);
519 if(!CORBA::is_nil(inc)) {
522 objLog = inc->resolve(name);
523 SALOME_Logger::Logger_var logger = SALOME_Logger::Logger::_narrow(objLog);
524 if ( !CORBA::is_nil(logger) )
533 //=============================================================================
534 /*! \brief shutdown omniNames and notifd
536 //=============================================================================
538 void SALOME_LifeCycleCORBA::killOmniNames()
540 string portNumber (::getenv ("NSPORT") );
541 if ( !portNumber.empty() )
544 cmd = string( "ps -eo pid,command | grep -v grep | grep -E \"omniNames.*")
546 + string("\" | awk '{cmd=sprintf(\"kill -9 %s\",$1); system(cmd)}'" );
549 system ( cmd.c_str() );
555 // NPAL 18309 (Kill Notifd)
556 if ( !portNumber.empty() )
558 string cmd = ("from killSalomeWithPort import killNotifdAndClean; ");
559 cmd += string("killNotifdAndClean(") + portNumber + "); ";
560 cmd = string("python -c \"") + cmd +"\" >& /dev/null";
562 system( cmd.c_str() );
566 //=============================================================================
567 /*! \brief Find an already existing and registered component instance.
569 * - build a list of machines on which an instance of the component is running,
570 * - find the best machine among the list
572 * \param params machine parameters like type or name...
573 * \param componentName the name of component class
574 * \param studyId default = 0 : multistudy instance
575 * \param listOfMachines list of machine address
576 * \return a CORBA reference of the component instance, or _nil if not found
578 //=============================================================================
580 Engines::Component_ptr
581 SALOME_LifeCycleCORBA::
582 _FindComponent(const Engines::MachineParameters& params,
583 const char *componentName,
585 const Engines::MachineList& listOfMachines)
587 // --- build the list of machines on which the component is already running
589 const char *containerName = params.container_name;
590 int nbproc = NbProc(params);
591 // MESSAGE("_FindComponent, required " << containerName <<
592 // " " << componentName << " " << nbproc);
594 Engines::MachineList_var machinesOK = new Engines::MachineList;
596 unsigned int lghtOfmachinesOK = 0;
597 machinesOK->length(listOfMachines.length());
599 for(unsigned int i=0; i<listOfMachines.length(); i++)
601 const char *currentMachine=listOfMachines[i];
602 // MESSAGE("_FindComponent, look at " << currentMachine);
603 CORBA::Object_var obj = _NS->ResolveComponent(currentMachine,
607 if (!CORBA::is_nil(obj))
608 machinesOK[lghtOfmachinesOK++] = CORBA::string_dup(currentMachine);
611 // --- find the best machine among the list
613 if(lghtOfmachinesOK != 0)
615 machinesOK->length(lghtOfmachinesOK);
616 CORBA::String_var bestMachine = _ResManager->FindFirst(machinesOK);
617 CORBA::Object_var obj = _NS->ResolveComponent(bestMachine,
621 return Engines::Component::_narrow(obj);
624 return Engines::Component::_nil();
627 //=============================================================================
628 /*! \brief Load a component instance.
630 * - Finds a container in the list of machine or start one.
631 * - Try to load the component library in the container,
632 * - then create an instance of the component.
634 * \param params machine parameters like type or name...
635 * \param componentName the name of component class
636 * \param studyId default = 0 : multistudy instance
637 * \param listOfMachines list of machine address
638 * \return a CORBA reference of the component instance, or _nil if problem
640 //=============================================================================
642 Engines::Component_ptr
643 SALOME_LifeCycleCORBA::
644 _LoadComponent(const Engines::MachineParameters& params,
645 const char *componentName,
647 const Engines::MachineList& listOfMachines)
649 MESSAGE("_LoadComponent, required " << params.container_name <<
650 " " << componentName << " " << NbProc(params));
652 Engines::Container_var cont =
653 _ContManager->FindOrStartContainer(params,
655 if (CORBA::is_nil(cont)) return Engines::Component::_nil();
657 bool isLoadable = cont->load_component_Library(componentName);
658 if (!isLoadable) return Engines::Component::_nil();
660 Engines::Component_var myInstance =
661 cont->create_component_instance(componentName, studyId);
662 return myInstance._retn();
665 //=============================================================================
666 /*! \brief Load a parallel component instance.
668 * \param params machine parameters like type or name...
669 * \param componentName the name of component class
670 * \param studyId default = 0 : multistudy instance
671 * \return a CORBA reference of the parallel component instance, or _nil if problem
673 //=============================================================================
674 Engines::Component_ptr
675 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::MachineParameters& params,
676 const char *componentName,
679 MESSAGE("Entering LoadParallelComponent");
681 /*MESSAGE("Parameters : ");
682 MESSAGE("Container name : " << params.container_name);
683 MESSAGE("Number of component nodes : " << params.nb_component_nodes);
684 MESSAGE("Component Name : " << componentName);*/
686 Engines::CompoList clist;
688 clist[0] = componentName;
689 MESSAGE("Building a list of machines");
690 Engines::MachineList_var listOfMachines = _ResManager->GetFittingResources(params, clist);
691 if (listOfMachines->length() == 0)
693 INFOS("No matching machines founded !");
694 return Engines::Component::_nil();
697 MESSAGE("Starting Parallel Container");
698 Engines::Container_var cont = _ContManager->FindOrStartParallelContainer(params, listOfMachines);
699 if (CORBA::is_nil(cont)) {
700 INFOS("FindOrStartParallelContainer() returns a NULL container !");
701 return Engines::Component::_nil();
704 MESSAGE("Loading component library");
705 bool isLoadable = cont->load_component_Library(componentName);
707 INFOS(componentName <<" library is not loadable !");
708 return Engines::Component::_nil();
711 MESSAGE("Creating component instance");
712 // @PARALLEL@ permits to identify that the component requested
713 // is a parallel component.
714 string name = string(componentName) + string("@PARALLEL@");
715 Engines::Component_var myInstance = cont->create_component_instance(name.c_str(), studyId);
716 if (CORBA::is_nil(myInstance))
717 INFOS("create_component_instance returns a NULL component !");
718 return myInstance._retn();
721 /*! \brief copy a file from a source host to a destination host
722 * \param hostSrc the source host
723 * \param fileSrc the file to copy from the source host to the destination host
724 * \param hostDest the destination host
725 * \param fileDest the destination file
727 void SALOME_LifeCycleCORBA::copyFile(const char* hostSrc, const char* fileSrc, const char* hostDest, const char* fileDest)
729 if(strcmp(hostDest,"localhost") == 0)
731 //if localhost use a shortcut
732 SALOME_FileTransferCORBA transfer(hostSrc,fileSrc);
733 transfer.getLocalFile(fileDest);
737 Engines::ContainerManager_var contManager = getContainerManager();
739 Engines::MachineParameters params;
742 Engines::MachineList listOfMachines;
743 listOfMachines.length(1);
745 params.hostname = hostDest;
746 listOfMachines[0] = hostDest;
747 Engines::Container_var containerDest = contManager->FindOrStartContainer(params, listOfMachines);
749 params.hostname = hostSrc;
750 listOfMachines[0] = hostSrc;
751 Engines::Container_var containerSrc = contManager->FindOrStartContainer(params, listOfMachines);
753 containerDest->copyFile(containerSrc,fileSrc,fileDest);