2 //=============================================================================
3 // File : SALOME_LifeCycleCORBA.cxx
4 // Created : jeu jui 12 14:55:50 CEST 2001
5 // Author : Paul RASCLE, EDF
7 // Copyright : EDF 2001
9 //=============================================================================
17 #include "utilities.h"
19 #include <ServiceUnreachable.hxx>
21 #include "SALOME_LifeCycleCORBA.hxx"
22 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
23 #include "SALOME_NamingService.hxx"
25 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA()
28 _FactoryServer = NULL ;
31 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
34 _FactoryServer = NULL ;
37 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
41 string SALOME_LifeCycleCORBA::ContainerName(
42 const char * aComputerContainer ,
43 string * theComputer ,
44 string * theContainer ) {
45 char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
46 strcpy( ContainerName , aComputerContainer ) ;
47 string theComputerContainer("/Containers/");
48 char * slash = strchr( ContainerName , '/' ) ;
50 *theComputer = GetHostname() ;
51 theComputerContainer += *theComputer ;
52 theComputerContainer += "/" ;
53 *theContainer = ContainerName ;
54 theComputerContainer += *theContainer ;
59 *theContainer = slash ;
60 if ( !strcmp( ContainerName , "localhost" ) ) {
61 *theComputer = GetHostname() ;
64 *theComputer = ContainerName ;
66 theComputerContainer += *theComputer ;
67 theComputerContainer += "/" ;
68 theComputerContainer += *theContainer ;
70 return theComputerContainer ;
73 string SALOME_LifeCycleCORBA::ComputerPath(
74 const char * theComputer ) {
75 CORBA::String_var path;
76 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
77 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
78 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
80 path = Catalog->GetPathPrefix( theComputer );
82 catch (SALOME_ModuleCatalog::NotFound&) {
83 MESSAGE("GetPathPrefix(" << theComputer << ") not found!");
87 return CORBA::string_dup( path ) ;
90 Engines::Container_var SALOME_LifeCycleCORBA::FindContainer(const char *containerName ) {
93 if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
96 cont = ContainerName( containerName , &theComputer , &theContainer ) ;
99 cont = containerName ;
105 CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
106 if( !CORBA::is_nil( obj ) ) {
107 return Engines::Container::_narrow( obj ) ;
110 catch (ServiceUnreachable&) {
111 INFOS("Caught exception: Naming Service Unreachable");
114 INFOS("Caught unknown exception.");
116 return Engines::Container::_nil();
119 Engines::Container_var SALOME_LifeCycleCORBA::FindOrStartContainer(
120 const string aComputerContainer ,
121 const string theComputer ,
122 const string theContainer ) {
123 Engines::Container_var aContainer = FindContainer( aComputerContainer.c_str() ) ;
124 Engines::Container_var aFactoryServer ;
125 SCRUTE( aComputerContainer ) ;
126 SCRUTE( theComputer ) ;
127 SCRUTE( theContainer ) ;
128 bool pyCont = false ;
129 int len = theContainer.length() ;
130 if ( !strcmp( &theContainer.c_str()[len-2] , "Py" ) ) {
133 if ( !CORBA::is_nil( aContainer ) ) {
137 string FactoryServer = theComputer ;
139 FactoryServer += "/FactoryServerPy" ;
142 FactoryServer += "/FactoryServer" ;
144 aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
145 if ( CORBA::is_nil( aFactoryServer ) ) {
146 // rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 &
148 if ( theComputer!= GetHostname() ) {
153 string path = ComputerPath( theComputer.c_str() ) ;
155 if ( path[0] != '\0' ) {
159 rsh += "runSession " ;
161 rsh += "SALOME_ContainerPy.py " ;
162 rsh += "FactoryServerPy -" ;
165 rsh += "SALOME_Container " ;
166 rsh += "FactoryServer -" ;
168 string omniORBcfg( getenv( "HOME" ) ) ;
169 omniORBcfg += "/.omniORB.cfg" ;
170 ifstream omniORBfile( omniORBcfg.c_str() ) ;
171 char ORBInitRef[12] ;
172 char nameservice[132] ;
173 omniORBfile >> ORBInitRef ;
176 omniORBfile >> nameservice ;
177 omniORBfile.close() ;
178 char * bsn = strchr( nameservice , '\n' ) ;
184 rsh += " > /tmp/FactoryServerPy_" ;
187 rsh += " > /tmp/FactoryServer_" ;
190 rsh += ".log 2>&1 &" ;
192 int status = system( rsh.c_str() ) ;
194 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status -1)") ;
196 else if (status == 217) {
197 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status 217)") ;
201 while ( CORBA::is_nil( aFactoryServer ) && count ) {
205 MESSAGE( count << ". Waiting for FactoryServer on " << theComputer)
206 aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
208 if ( CORBA::is_nil( aFactoryServer ) ) {
209 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ;
211 else if ( strcmp( theComputer.c_str() , GetHostname().c_str() ) ) {
212 _FactoryServer = aFactoryServer ;
216 if ( !CORBA::is_nil( aFactoryServer ) ) {
217 if ( strcmp( theContainer.c_str() , "FactoryServer" ) ||
218 strcmp( theContainer.c_str() , "FactoryServerPy" ) ) {
219 MESSAGE("Container not found ! trying to start " << aComputerContainer);
220 Engines::Container_var myContainer = aFactoryServer->start_impl( theContainer.c_str() ) ;
221 if ( !CORBA::is_nil( myContainer ) ) {
222 MESSAGE("Container " << aComputerContainer << " started");
226 MESSAGE("Container " << aComputerContainer << " NOT started");
230 MESSAGE("Container " << aComputerContainer << " started");
231 return aFactoryServer ;
235 return Engines::Container::_nil();
238 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
239 (const char *containerName,
240 const char *componentName,
241 const char *implementation)
243 BEGIN_OF("FindOrLoad_Component(1)");
246 string theContainer ;
247 string theComputerContainer = ContainerName( containerName ,
250 Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
253 // ASSERT(!CORBA::is_nil(cont));
255 string path( theComputerContainer );
257 path = path + componentName;
261 CORBA::Object_var obj = _NS->Resolve(path.c_str());
262 if (CORBA::is_nil(obj))
264 MESSAGE("Component not found ! trying to load " << path);
265 Engines::Component_var compo
266 = cont->load_impl(componentName, implementation);
267 // ASSERT(!CORBA::is_nil(compo));
268 MESSAGE("Component launched !" << path);
273 MESSAGE("Component found !" << path);
274 Engines::Component_var compo = Engines::Component::_narrow(obj);
275 // ASSERT(!CORBA::is_nil(compo));
280 catch (CORBA::COMM_FAILURE&)
282 INFOS("Caught CORBA::SystemException CommFailure. Engine "
283 << path << "does not respond" );
288 catch (ServiceUnreachable&)
290 INFOS("Caught exception: Naming Service Unreachable");
294 INFOS("Caught unknown exception.");
296 return Engines::Component::_nil();
299 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
300 (const char *containerName,
301 const char *componentName)
303 // BEGIN_OF("FindOrLoad_Component(2)");
306 string theContainer ;
307 string theComputerContainer = ContainerName( containerName ,
310 Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
314 if ( CORBA::is_nil( cont ) ) {
315 MESSAGE("Container not found ! " << theComputerContainer );
316 return Engines::Component::_nil();
319 // char * machine = cont->machineName() ;
320 const char * machine = theComputer.c_str() ;
322 string path( theComputerContainer );
324 path += componentName;
328 CORBA::Object_var obj = _NS->Resolve(path.c_str());
329 if ( CORBA::is_nil( obj ) ) {
330 MESSAGE("Component not found ! trying to load " << path);
331 CORBA::Object_var obj2 = _NS->Resolve("/Kernel/ModulCatalog");
332 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
333 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj2);
335 SALOME_ModuleCatalog::Acomponent_ptr compoInfo =
336 Catalog->GetComponent(componentName);
337 if (CORBA::is_nil (compoInfo))
339 INFOS("Catalog Error : Component not found in the catalog")
340 return Engines::Component::_nil();
347 path = compoInfo->GetPathPrefix( machine ) ;
350 catch (SALOME_ModuleCatalog::NotFound&)
352 MESSAGE("GetPathPrefix(" << machine << ") not found!"
353 << "trying localhost");
355 path = compoInfo->GetPathPrefix("localhost") ;
358 catch (SALOME_ModuleCatalog::NotFound&) {
359 MESSAGE("GetPathPrefix(localhost) not found!") ;
365 string implementation(path);
366 implementation += "lib";
367 implementation += componentName;
368 implementation += "Engine.so";
370 Engines::Component_var compo
371 = cont->load_impl(componentName, implementation.c_str());
373 // ASSERT(!CORBA::is_nil(compo));
374 // MESSAGE("Component launched !" << path);
379 MESSAGE("Component found !" << path);
380 Engines::Component_var compo = Engines::Component::_narrow(obj);
381 // ASSERT(!CORBA::is_nil(compo));
384 string instanceName = compo->instanceName();
386 catch (CORBA::COMM_FAILURE&)
388 INFOS("Caught CORBA::SystemException CommFailure. Engine "
389 << path << "does not respond" );
394 catch (ServiceUnreachable&)
396 INFOS("Caught exception: Naming Service Unreachable");
400 INFOS("Caught unknown exception.");
402 return Engines::Component::_nil();