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"
57 IncompatibleComponent::IncompatibleComponent( void ):
58 SALOME_Exception( "IncompatibleComponent" )
62 IncompatibleComponent::IncompatibleComponent(const IncompatibleComponent &ex):
63 SALOME_Exception( ex )
67 /*! \class SALOME_LifeCycleCORBA
68 \brief A class to manage life cycle of SALOME components.
72 //=============================================================================
76 //=============================================================================
78 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
80 // be sure to have an instance of traceCollector, when used via SWIG
83 char *xargv = (char*)"";
85 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
86 // LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
89 _NS = new SALOME_NamingService(orb);
93 _NS->Change_Directory("/"); // mpv 250105: current directory may be not root
94 // (in SALOMEDS for an example)
95 // not enough: set a current directory in naming service is not thread safe
96 // if naming service instance is shared among several threads...
97 // ==> allways use absolute path and dot rely on current directory!
99 CORBA::Object_var obj =
100 _NS->Resolve(SALOME_ContainerManager::_ContainerManagerNameInNS);
101 ASSERT( !CORBA::is_nil(obj));
102 _ContManager=Engines::ContainerManager::_narrow(obj);
104 obj = _NS->Resolve(SALOME_ResourcesManager::_ResourcesManagerNameInNS);
105 ASSERT( !CORBA::is_nil(obj));
106 _ResManager=Engines::ResourcesManager::_narrow(obj);
109 //=============================================================================
113 //=============================================================================
115 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
119 //=============================================================================
120 /*! \brief Find an already existing and registered component instance.
122 * \param params machine parameters like type or name...
123 * \param componentName the name of component class
124 * \param studyId default = 0 : multistudy instance
125 * \return a CORBA reference of the component instance, or _nil if not found
127 //=============================================================================
128 Engines::Component_ptr
129 SALOME_LifeCycleCORBA::FindComponent(const Engines::MachineParameters& params,
130 const char *componentName,
133 if (! isKnownComponentClass(componentName))
134 return Engines::Component::_nil();
136 Engines::CompoList clist;
138 clist[0] = componentName;
139 Engines::MachineList_var listOfMachines =
140 _ResManager->GetFittingResources(params, clist);
142 Engines::Component_var compo = _FindComponent(params,
147 return compo._retn();
150 //=============================================================================
151 /*! \brief Load a component instance on a container defined by machine parameters
153 * \param params machine parameters like type or name...
154 * \param componentName the name of component class
155 * \param studyId default = 0 : multistudy instance
156 * \return a CORBA reference of the component instance, or _nil if problem
158 //=============================================================================
160 Engines::Component_ptr
161 SALOME_LifeCycleCORBA::LoadComponent(const Engines::MachineParameters& params,
162 const char *componentName,
165 // --- Check if Component Name is known in ModuleCatalog
167 if (! isKnownComponentClass(componentName))
168 return Engines::Component::_nil();
170 Engines::CompoList clist;
172 clist[0] = componentName;
173 Engines::MachineList_var listOfMachines =
174 _ResManager->GetFittingResources(params, clist);
176 Engines::Component_var compo = _LoadComponent(params,
181 return compo._retn();
184 //=============================================================================
185 /*! \brief Find an already existing and registered component instance or load a new
186 * component instance on a container defined by machine parameters.
188 * \param params machine parameters like type or name...
189 * \param componentName the name of component class
190 * \param studyId default = 0 : multistudy instance
191 * \return a CORBA reference of the component instance, or _nil if problem
193 //=============================================================================
195 Engines::Component_ptr
196 SALOME_LifeCycleCORBA::
197 FindOrLoad_Component(const Engines::MachineParameters& params,
198 const char *componentName,
201 // --- Check if Component Name is known in ModuleCatalog
203 if (! isKnownComponentClass(componentName))
204 return Engines::Component::_nil();
206 Engines::CompoList clist;
208 clist[0] = componentName;
209 Engines::MachineList_var listOfMachines =
210 _ResManager->GetFittingResources(params,clist);
212 Engines::Component_var compo = _FindComponent(params,
217 if(CORBA::is_nil(compo))
218 compo = _LoadComponent(params,
223 return compo._retn();
226 //=============================================================================
227 /*! \brief Find an already existing and registered component instance or load a new
228 * component instance on a container defined by name
230 * \param containerName the name of container, under one of the forms
231 * - 1 aContainer (local container)
232 * - 2 machine/aContainer (container on hostname = machine)
233 * \param componentName the name of component class
234 * \return a CORBA reference of the component instance, or _nil if problem
236 //=============================================================================
238 Engines::Component_ptr
239 SALOME_LifeCycleCORBA::FindOrLoad_Component(const char *containerName,
240 const char *componentName)
242 char *valenv=getenv("SALOME_BATCH");
244 if (strcmp(valenv,"1")==0)
246 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component BATCH " << containerName << " " << componentName ) ;
247 _NS->Change_Directory("/Containers");
248 CORBA::Object_ptr obj=_NS->Resolve(containerName);
249 Engines::Container_var cont=Engines::Container::_narrow(obj);
250 bool isLoadable = cont->load_component_Library(componentName);
251 if (!isLoadable) return Engines::Component::_nil();
253 Engines::Component_ptr myInstance =
254 cont->create_component_instance(componentName, 0);
257 MESSAGE("SALOME_LifeCycleCORBA::FindOrLoad_Component INTERACTIF " << containerName << " " << componentName ) ;
259 // --- Check if Component Name is known in ModuleCatalog
261 if (! isKnownComponentClass(componentName))
262 return Engines::Component::_nil();
264 // --- Check if containerName contains machine name (if yes: rg>0)
266 char *stContainer=strdup(containerName);
267 string st2Container(stContainer);
268 int rg=st2Container.find("/");
270 Engines::MachineParameters_var params=new Engines::MachineParameters;
274 // containerName doesn't contain "/" => Local container
275 params->container_name=CORBA::string_dup(stContainer);
276 params->hostname=CORBA::string_dup(Kernel_Utils::GetHostname().c_str());
280 stContainer[rg]='\0';
281 params->container_name=CORBA::string_dup(stContainer+rg+1);
282 params->hostname=CORBA::string_dup(stContainer);
284 params->isMPI = false;
285 SCRUTE(params->container_name);
286 // SCRUTE(params->hostname);
287 // SCRUTE(params->OS);
288 // SCRUTE(params->mem_mb);
289 // SCRUTE(params->cpu_clock);
290 // SCRUTE(params->nb_proc_per_node);
291 // SCRUTE(params->nb_node);
292 // SCRUTE(params->isMPI);
294 return FindOrLoad_Component(params,componentName);
298 //=============================================================================
299 /*! \brief Check if the component class is known in module catalog
301 * \param componentName the name of component class
302 * \return true if found, false otherwise
304 //=============================================================================
306 bool SALOME_LifeCycleCORBA::isKnownComponentClass(const char *componentName)
311 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
312 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
313 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
314 ASSERT(! CORBA::is_nil(Catalog));
315 SALOME_ModuleCatalog::Acomponent_var compoInfo =
316 Catalog->GetComponent(componentName);
317 if (CORBA::is_nil (compoInfo))
319 INFOS("Catalog Error: Component not found in the catalog" );
320 INFOS( componentName );
325 catch (ServiceUnreachable&)
327 INFOS("Caught exception: Naming Service Unreachable");
331 INFOS("Caught unknown exception.");
336 //=============================================================================
338 * Not so complex... useful ?
340 //=============================================================================
343 SALOME_LifeCycleCORBA::isMpiContainer(const Engines::MachineParameters& params)
344 throw(IncompatibleComponent)
353 //=============================================================================
354 /*! \brief Initialisation of a given Engines::MachineParameters with default values.
356 * - container_name = "" : not relevant
357 * - hostname = "" : not relevant
358 * - OS = "" : not relevant
359 * - mem_mb = 0 : not relevant
360 * - cpu_clock = 0 : not relevant
361 * - nb_proc_per_node = 0 : not relevant
362 * - nb_node = 0 : not relevant
363 * - isMPI = false : standard components
365 //=============================================================================
367 void SALOME_LifeCycleCORBA::preSet( Engines::MachineParameters& params)
369 params.container_name = "";
370 params.hostname = "";
373 params.cpu_clock = 0;
374 params.nb_proc_per_node = 0;
376 params.isMPI = false;
378 params.parallelLib = "";
379 params.nb_component_nodes = 0;
382 //=============================================================================
384 * \return a number of processors not 0, only for MPI containers
386 //=============================================================================
388 int SALOME_LifeCycleCORBA::NbProc(const Engines::MachineParameters& params)
390 if( !isMpiContainer(params) )
392 else if( (params.nb_node <= 0) && (params.nb_proc_per_node <= 0) )
394 else if( params.nb_node == 0 )
395 return params.nb_proc_per_node;
396 else if( params.nb_proc_per_node == 0 )
397 return params.nb_node;
399 return params.nb_node * params.nb_proc_per_node;
402 //=============================================================================
403 /*! \brief Get the container manager
405 * \return the container Manager
407 //=============================================================================
409 Engines::ContainerManager_ptr SALOME_LifeCycleCORBA::getContainerManager()
411 Engines::ContainerManager_var contManager =
412 Engines::ContainerManager::_duplicate(_ContManager);
413 return contManager._retn();
416 //=============================================================================
417 /*! \brief Get the resources manager
419 * \return the container Manager
421 //=============================================================================
423 Engines::ResourcesManager_ptr SALOME_LifeCycleCORBA::getResourcesManager()
425 Engines::ResourcesManager_var resManager =
426 Engines::ResourcesManager::_duplicate(_ResManager);
427 return resManager._retn();
430 //=============================================================================
431 /*! \brief shutdown all the SALOME servers except SALOME_Session_Server, omniNames and notifd
433 //=============================================================================
435 void SALOME_LifeCycleCORBA::shutdownServers()
437 // get each Container from NamingService => shutdown it
438 // (the order is inverse to the order of servers initialization)
440 SALOME::Session_var session = SALOME::Session::_nil();
442 CORBA::Object_var objS = _NS->Resolve("/Kernel/Session");
443 if (!CORBA::is_nil(objS))
445 session = SALOME::Session::_narrow(objS);
446 if (!CORBA::is_nil(session))
448 pid = session->getPID();
453 string hostname = Kernel_Utils::GetHostname();
456 CORBA::Object_var objSL = _NS->Resolve("/SalomeLauncher");
457 Engines::SalomeLauncher_var launcher = Engines::SalomeLauncher::_narrow(objSL);
458 if (!CORBA::is_nil(launcher) && (pid != launcher->getPID()))
459 launcher->Shutdown();
461 // 2) ConnectionManager
462 CORBA::Object_var objCnM=_NS->Resolve("/ConnectionManager");
463 Engines::ConnectionManager_var connMan=Engines::ConnectionManager::_narrow(objCnM);
464 if ( !CORBA::is_nil(connMan) && ( pid != connMan->getPID() ) )
465 connMan->ShutdownWithExit();
468 CORBA::Object_var objSDS = _NS->Resolve("/myStudyManager");
469 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(objSDS) ;
470 if ( !CORBA::is_nil(studyManager) && ( pid != studyManager->getPID() ) )
471 studyManager->Shutdown();
474 CORBA::Object_var objMC=_NS->Resolve("/Kernel/ModulCatalog");
475 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objMC);
476 if ( !CORBA::is_nil(catalog) && ( pid != catalog->getPID() ) )
480 CORBA::Object_var objR = _NS->Resolve("/Registry");
481 Registry::Components_var registry = Registry::Components::_narrow(objR);
482 if ( !CORBA::is_nil(registry) && ( pid != registry->getPID() ) )
483 registry->Shutdown();
487 char *xargv = (char*)"";
488 char **argv = &xargv;
489 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
491 CORBA::Object_var objLog = CORBA::Object::_nil();
492 CosNaming::NamingContext_var inc;
493 CORBA::Object_var theObj = CORBA::Object::_nil();
494 std::string stdname = "Logger";
495 CosNaming::Name name;
497 name[0].id = CORBA::string_dup(stdname.c_str());
500 if(!CORBA::is_nil(orb))
501 theObj = orb->resolve_initial_references("NameService");
502 if (!CORBA::is_nil(theObj))
503 inc = CosNaming::NamingContext::_narrow(theObj);
508 if(!CORBA::is_nil(inc)) {
511 objLog = inc->resolve(name);
512 SALOME_Logger::Logger_var logger = SALOME_Logger::Logger::_narrow(objLog);
513 if ( !CORBA::is_nil(logger) )
522 //=============================================================================
523 /*! \brief shutdown omniNames and notifd
525 //=============================================================================
527 void SALOME_LifeCycleCORBA::killOmniNames()
529 string portNumber (::getenv ("NSPORT") );
530 if ( !portNumber.empty() )
533 cmd = string( "ps -eo pid,command | grep -v grep | grep -E \"omniNames.*")
535 + string("\" | awk '{cmd=sprintf(\"kill -9 %s\",$1); system(cmd)}'" );
538 system ( cmd.c_str() );
544 // NPAL 18309 (Kill Notifd)
545 if ( !portNumber.empty() )
547 string cmd = ("from killSalomeWithPort import killNotifdAndClean; ");
548 cmd += string("killNotifdAndClean(") + portNumber + "); ";
549 cmd = string("python -c \"") + cmd +"\" >& /dev/null";
551 system( cmd.c_str() );
555 //=============================================================================
556 /*! \brief Find an already existing and registered component instance.
558 * - build a list of machines on which an instance of the component is running,
559 * - find the best machine among the list
561 * \param params machine parameters like type or name...
562 * \param componentName the name of component class
563 * \param studyId default = 0 : multistudy instance
564 * \param listOfMachines list of machine address
565 * \return a CORBA reference of the component instance, or _nil if not found
567 //=============================================================================
569 Engines::Component_ptr
570 SALOME_LifeCycleCORBA::
571 _FindComponent(const Engines::MachineParameters& params,
572 const char *componentName,
574 const Engines::MachineList& listOfMachines)
576 // --- build the list of machines on which the component is already running
578 const char *containerName = params.container_name;
579 int nbproc = NbProc(params);
580 // MESSAGE("_FindComponent, required " << containerName <<
581 // " " << componentName << " " << nbproc);
583 Engines::MachineList_var machinesOK = new Engines::MachineList;
585 unsigned int lghtOfmachinesOK = 0;
586 machinesOK->length(listOfMachines.length());
588 for(unsigned int i=0; i<listOfMachines.length(); i++)
590 const char *currentMachine=listOfMachines[i];
591 // MESSAGE("_FindComponent, look at " << currentMachine);
592 CORBA::Object_var obj = _NS->ResolveComponent(currentMachine,
596 if (!CORBA::is_nil(obj))
597 machinesOK[lghtOfmachinesOK++] = CORBA::string_dup(currentMachine);
600 // --- find the best machine among the list
602 if(lghtOfmachinesOK != 0)
604 machinesOK->length(lghtOfmachinesOK);
605 CORBA::String_var bestMachine = _ResManager->FindFirst(machinesOK);
606 CORBA::Object_var obj = _NS->ResolveComponent(bestMachine,
610 return Engines::Component::_narrow(obj);
613 return Engines::Component::_nil();
616 //=============================================================================
617 /*! \brief Load a component instance.
619 * - Finds a container in the list of machine or start one.
620 * - Try to load the component library in the container,
621 * - then create an instance of the component.
623 * \param params machine parameters like type or name...
624 * \param componentName the name of component class
625 * \param studyId default = 0 : multistudy instance
626 * \param listOfMachines list of machine address
627 * \return a CORBA reference of the component instance, or _nil if problem
629 //=============================================================================
631 Engines::Component_ptr
632 SALOME_LifeCycleCORBA::
633 _LoadComponent(const Engines::MachineParameters& params,
634 const char *componentName,
636 const Engines::MachineList& listOfMachines)
638 MESSAGE("_LoadComponent, required " << params.container_name <<
639 " " << componentName << " " << NbProc(params));
641 Engines::Container_var cont =
642 _ContManager->FindOrStartContainer(params,
644 if (CORBA::is_nil(cont)) return Engines::Component::_nil();
646 bool isLoadable = cont->load_component_Library(componentName);
647 if (!isLoadable) return Engines::Component::_nil();
649 Engines::Component_var myInstance =
650 cont->create_component_instance(componentName, studyId);
651 return myInstance._retn();
654 //=============================================================================
655 /*! \brief Load a parallel component instance.
657 * \param params machine parameters like type or name...
658 * \param componentName the name of component class
659 * \param studyId default = 0 : multistudy instance
660 * \return a CORBA reference of the parallel component instance, or _nil if problem
662 //=============================================================================
663 Engines::Component_ptr
664 SALOME_LifeCycleCORBA::Load_ParallelComponent(const Engines::MachineParameters& params,
665 const char *componentName,
668 MESSAGE("Entering LoadParallelComponent");
670 /*MESSAGE("Parameters : ");
671 MESSAGE("Container name : " << params.container_name);
672 MESSAGE("Number of component nodes : " << params.nb_component_nodes);
673 MESSAGE("Component Name : " << componentName);*/
675 Engines::CompoList clist;
677 clist[0] = componentName;
678 MESSAGE("Building a list of machines");
679 Engines::MachineList_var listOfMachines = _ResManager->GetFittingResources(params, clist);
680 if (listOfMachines->length() == 0)
682 INFOS("No matching machines founded !");
683 return Engines::Component::_nil();
686 MESSAGE("Starting Parallel Container");
687 Engines::Container_var cont = _ContManager->FindOrStartParallelContainer(params, listOfMachines);
688 if (CORBA::is_nil(cont)) {
689 INFOS("FindOrStartParallelContainer() returns a NULL container !");
690 return Engines::Component::_nil();
693 MESSAGE("Loading component library");
694 bool isLoadable = cont->load_component_Library(componentName);
696 INFOS(componentName <<" library is not loadable !");
697 return Engines::Component::_nil();
700 MESSAGE("Creating component instance");
701 // @PARALLEL@ permits to identify that the component requested
702 // is a parallel component.
703 string name = string(componentName) + string("@PARALLEL@");
704 Engines::Component_var myInstance = cont->create_component_instance(name.c_str(), studyId);
705 if (CORBA::is_nil(myInstance))
706 INFOS("create_component_instance returns a NULL component !");
707 return myInstance._retn();