1 // SALOME LifeCycleCORBA : implementation of containers and engines life cycle both in Python and C++
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_LifeCycleCORBA.cxx
25 // Author : Paul RASCLE, EDF
36 #include "utilities.h"
38 #include <ServiceUnreachable.hxx>
40 #include "SALOME_LifeCycleCORBA.hxx"
41 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
42 #include "SALOME_NamingService.hxx"
44 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA()
47 _FactoryServer = NULL ;
50 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
53 _FactoryServer = NULL ;
56 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
60 string SALOME_LifeCycleCORBA::ContainerName(
61 const char * aComputerContainer ,
62 string * theComputer ,
63 string * theContainer ) {
64 char * ContainerName = new char [ strlen( aComputerContainer ) + 1 ] ;
65 strcpy( ContainerName , aComputerContainer ) ;
66 string theComputerContainer("/Containers/");
67 char * slash = strchr( ContainerName , '/' ) ;
69 *theComputer = GetHostname() ;
70 theComputerContainer += *theComputer ;
71 theComputerContainer += "/" ;
72 *theContainer = ContainerName ;
73 theComputerContainer += *theContainer ;
78 *theContainer = slash ;
79 if ( !strcmp( ContainerName , "localhost" ) ) {
80 *theComputer = GetHostname() ;
83 *theComputer = ContainerName ;
85 theComputerContainer += *theComputer ;
86 theComputerContainer += "/" ;
87 theComputerContainer += *theContainer ;
89 return theComputerContainer ;
92 string SALOME_LifeCycleCORBA::ComputerPath(
93 const char * theComputer ) {
94 CORBA::String_var path;
95 CORBA::Object_var obj = _NS->Resolve("/Kernel/ModulCatalog");
96 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
97 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
99 path = Catalog->GetPathPrefix( theComputer );
101 catch (SALOME_ModuleCatalog::NotFound&) {
102 MESSAGE("GetPathPrefix(" << theComputer << ") not found!");
106 return CORBA::string_dup( path ) ;
109 Engines::Container_var SALOME_LifeCycleCORBA::FindContainer(const char *containerName ) {
112 if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
114 string theContainer ;
115 cont = ContainerName( containerName , &theComputer , &theContainer ) ;
118 cont = containerName ;
124 CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
125 if( !CORBA::is_nil( obj ) ) {
126 return Engines::Container::_narrow( obj ) ;
129 catch (ServiceUnreachable&) {
130 INFOS("Caught exception: Naming Service Unreachable");
133 INFOS("Caught unknown exception.");
135 return Engines::Container::_nil();
138 Engines::Container_var SALOME_LifeCycleCORBA::FindOrStartContainer(
139 const string aComputerContainer ,
140 const string theComputer ,
141 const string theContainer ) {
142 Engines::Container_var aContainer = FindContainer( aComputerContainer.c_str() ) ;
143 Engines::Container_var aFactoryServer ;
144 SCRUTE( aComputerContainer ) ;
145 SCRUTE( theComputer ) ;
146 SCRUTE( theContainer ) ;
147 bool pyCont = false ;
148 int len = theContainer.length() ;
149 if ( !strcmp( &theContainer.c_str()[len-2] , "Py" ) ) {
152 if ( !CORBA::is_nil( aContainer ) ) {
156 string FactoryServer = theComputer ;
158 FactoryServer += "/FactoryServerPy" ;
161 FactoryServer += "/FactoryServer" ;
163 aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
164 if ( CORBA::is_nil( aFactoryServer ) ) {
165 // rsh -n ikkyo /export/home/rahuel/SALOME_ROOT/bin/runSession SALOME_Container -ORBInitRef NameService=corbaname::dm2s0017:1515 &
167 if ( theComputer!= GetHostname() ) {
172 string path = ComputerPath( theComputer.c_str() ) ;
174 if ( path[0] != '\0' ) {
178 rsh += "runSession " ;
180 rsh += "SALOME_ContainerPy.py " ;
181 rsh += "FactoryServerPy -" ;
184 rsh += "SALOME_Container " ;
185 rsh += "FactoryServer -" ;
187 string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
188 ifstream omniORBfile( omniORBcfg.c_str() ) ;
189 char ORBInitRef[12] ;
190 char nameservice[132] ;
191 omniORBfile >> ORBInitRef ;
194 omniORBfile >> nameservice ;
195 omniORBfile.close() ;
196 char * bsn = strchr( nameservice , '\n' ) ;
202 rsh += " > /tmp/FactoryServerPy_" ;
205 rsh += " > /tmp/FactoryServer_" ;
208 rsh += ".log 2>&1 &" ;
210 int status = system( rsh.c_str() ) ;
212 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status -1)") ;
214 else if (status == 217) {
215 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status 217)") ;
219 while ( CORBA::is_nil( aFactoryServer ) && count ) {
223 MESSAGE( count << ". Waiting for FactoryServer on " << theComputer)
224 aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
226 if ( CORBA::is_nil( aFactoryServer ) ) {
227 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ;
229 else if ( strcmp( theComputer.c_str() , GetHostname().c_str() ) ) {
230 _FactoryServer = aFactoryServer ;
234 if ( !CORBA::is_nil( aFactoryServer ) ) {
235 if ( strcmp( theContainer.c_str() , "FactoryServer" ) ||
236 strcmp( theContainer.c_str() , "FactoryServerPy" ) ) {
237 MESSAGE("Container not found ! trying to start " << aComputerContainer);
238 Engines::Container_var myContainer = aFactoryServer->start_impl( theContainer.c_str() ) ;
239 if ( !CORBA::is_nil( myContainer ) ) {
240 MESSAGE("Container " << aComputerContainer << " started");
244 MESSAGE("Container " << aComputerContainer << " NOT started");
248 MESSAGE("Container " << aComputerContainer << " started");
249 return aFactoryServer ;
253 return Engines::Container::_nil();
256 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
257 (const char *containerName,
258 const char *componentName,
259 const char *implementation)
261 BEGIN_OF("FindOrLoad_Component(1)");
264 string theContainer ;
265 string theComputerContainer = ContainerName( containerName ,
268 Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
271 // ASSERT(!CORBA::is_nil(cont));
273 string path( theComputerContainer );
275 path = path + componentName;
279 CORBA::Object_var obj = _NS->Resolve(path.c_str());
280 if (CORBA::is_nil(obj))
282 MESSAGE("Component not found ! trying to load " << path);
283 Engines::Component_var compo
284 = cont->load_impl(componentName, implementation);
285 // ASSERT(!CORBA::is_nil(compo));
286 MESSAGE("Component launched !" << path);
291 MESSAGE("Component found !" << path);
292 Engines::Component_var compo = Engines::Component::_narrow(obj);
293 // ASSERT(!CORBA::is_nil(compo));
298 catch (CORBA::COMM_FAILURE&)
300 INFOS("Caught CORBA::SystemException CommFailure. Engine "
301 << path << "does not respond" );
306 catch (ServiceUnreachable&)
308 INFOS("Caught exception: Naming Service Unreachable");
312 INFOS("Caught unknown exception.");
314 return Engines::Component::_nil();
317 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
318 (const char *containerName,
319 const char *componentName)
321 // BEGIN_OF("FindOrLoad_Component(2)");
324 string theContainer ;
325 string theComputerContainer = ContainerName( containerName ,
328 Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
332 if ( CORBA::is_nil( cont ) ) {
333 MESSAGE("Container not found ! " << theComputerContainer );
334 return Engines::Component::_nil();
337 // char * machine = cont->machineName() ;
338 const char * machine = theComputer.c_str() ;
340 string path( theComputerContainer );
342 path += componentName;
346 CORBA::Object_var obj = _NS->Resolve(path.c_str());
347 if ( CORBA::is_nil( obj ) ) {
348 MESSAGE("Component not found ! trying to load " << path);
349 CORBA::Object_var obj2 = _NS->Resolve("/Kernel/ModulCatalog");
350 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
351 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj2);
353 SALOME_ModuleCatalog::Acomponent_ptr compoInfo =
354 Catalog->GetComponent(componentName);
355 if (CORBA::is_nil (compoInfo))
357 INFOS("Catalog Error : Component not found in the catalog")
358 return Engines::Component::_nil();
365 path = compoInfo->GetPathPrefix( machine ) ;
368 catch (SALOME_ModuleCatalog::NotFound&)
370 MESSAGE("GetPathPrefix(" << machine << ") not found!"
371 << "trying localhost");
373 path = compoInfo->GetPathPrefix("localhost") ;
376 catch (SALOME_ModuleCatalog::NotFound&) {
377 MESSAGE("GetPathPrefix(localhost) not found!") ;
383 string implementation(path);
384 implementation += "lib";
385 implementation += componentName;
386 implementation += "Engine.so";
388 Engines::Component_var compo
389 = cont->load_impl(componentName, implementation.c_str());
391 // ASSERT(!CORBA::is_nil(compo));
392 // MESSAGE("Component launched !" << path);
397 MESSAGE("Component found !" << path);
398 Engines::Component_var compo = Engines::Component::_narrow(obj);
399 // ASSERT(!CORBA::is_nil(compo));
402 string instanceName = compo->instanceName();
404 catch (CORBA::COMM_FAILURE&)
406 INFOS("Caught CORBA::SystemException CommFailure. Engine "
407 << path << "does not respond" );
412 catch (ServiceUnreachable&)
414 INFOS("Caught exception: Naming Service Unreachable");
418 INFOS("Caught unknown exception.");
420 return Engines::Component::_nil();