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!");
105 // Modification provisoire B. Secher en attendant le gestionnaire de ressources
107 // Le KERNEL_ROOT_DIR sera a lire dans le catalogue de machines
108 // en attendant on suppose qu'il est identique au KERNEL_ROOT_DIR local
109 string path = string(getenv("KERNEL_ROOT_DIR")) + "/bin/salome/";
114 Engines::Container_var SALOME_LifeCycleCORBA::FindContainer(const char *containerName ) {
117 if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
119 string theContainer ;
120 cont = ContainerName( containerName , &theComputer , &theContainer ) ;
123 cont = containerName ;
129 CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
130 if( !CORBA::is_nil( obj ) ) {
131 return Engines::Container::_narrow( obj ) ;
134 catch (ServiceUnreachable&) {
135 INFOS("Caught exception: Naming Service Unreachable");
138 INFOS("Caught unknown exception.");
140 return Engines::Container::_nil();
143 Engines::Container_var SALOME_LifeCycleCORBA::FindOrStartContainer(
144 const string aComputerContainer ,
145 const string theComputer ,
146 const string theContainer ) {
147 Engines::Container_var aContainer = FindContainer( aComputerContainer.c_str() ) ;
148 Engines::Container_var aFactoryServer ;
149 SCRUTE( aComputerContainer ) ;
150 SCRUTE( theComputer ) ;
151 SCRUTE( theContainer ) ;
152 bool pyCont = false ;
153 int len = theContainer.length() ;
154 if ( !strcmp( &theContainer.c_str()[len-2] , "Py" ) ) {
157 if ( !CORBA::is_nil( aContainer ) ) {
161 string FactoryServer = theComputer ;
163 FactoryServer += "/FactoryServerPy" ;
166 FactoryServer += "/FactoryServer" ;
168 aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
169 if ( CORBA::is_nil( aFactoryServer ) ) {
170 // rsh -n machine_name ${KERNEL_ROOT_DIR}/bin/salome SALOME_Container -ORBInitRef NameService=corbaname::localhost:1515 &
172 if ( theComputer!= GetHostname() ) {
177 string path = ComputerPath( theComputer.c_str() ) ;
180 // rsh += "runSession " ;
182 rsh += "SALOME_ContainerPy.py " ;
183 rsh += "FactoryServerPy -" ;
186 rsh += "SALOME_Container " ;
187 rsh += "FactoryServer -" ;
189 string omniORBcfg( getenv( "OMNIORB_CONFIG" ) ) ;
190 ifstream omniORBfile( omniORBcfg.c_str() ) ;
191 char ORBInitRef[12] ;
192 char nameservice[132] ;
193 omniORBfile >> ORBInitRef ;
196 omniORBfile >> nameservice ;
197 omniORBfile.close() ;
198 char * bsn = strchr( nameservice , '\n' ) ;
204 rsh += " > /tmp/FactoryServerPy_" ;
207 rsh += " > /tmp/FactoryServer_" ;
210 rsh += ".log 2>&1 &" ;
212 int status = system( rsh.c_str() ) ;
214 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status -1)") ;
216 else if (status == 217) {
217 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status 217)") ;
221 while ( CORBA::is_nil( aFactoryServer ) && count ) {
225 MESSAGE( count << ". Waiting for FactoryServer on " << theComputer)
226 aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
228 if ( CORBA::is_nil( aFactoryServer ) ) {
229 INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ;
231 else if ( strcmp( theComputer.c_str() , GetHostname().c_str() ) ) {
232 _FactoryServer = aFactoryServer ;
236 if ( !CORBA::is_nil( aFactoryServer ) ) {
237 if ( strcmp( theContainer.c_str() , "FactoryServer" ) ||
238 strcmp( theContainer.c_str() , "FactoryServerPy" ) ) {
239 MESSAGE("Container not found ! trying to start " << aComputerContainer);
240 Engines::Container_var myContainer = aFactoryServer->start_impl( theContainer.c_str() ) ;
241 if ( !CORBA::is_nil( myContainer ) ) {
242 MESSAGE("Container " << aComputerContainer << " started");
246 MESSAGE("Container " << aComputerContainer << " NOT started");
250 MESSAGE("Container " << aComputerContainer << " started");
251 return aFactoryServer ;
255 return Engines::Container::_nil();
258 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
259 (const char *containerName,
260 const char *componentName,
261 const char *implementation)
263 BEGIN_OF("FindOrLoad_Component(1)");
266 string theContainer ;
267 string theComputerContainer = ContainerName( containerName ,
270 Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
273 // ASSERT(!CORBA::is_nil(cont));
275 string path( theComputerContainer );
277 path = path + componentName;
281 CORBA::Object_var obj = _NS->Resolve(path.c_str());
282 if (CORBA::is_nil(obj))
284 MESSAGE("Component not found ! trying to load " << path);
285 Engines::Component_var compo
286 = cont->load_impl(componentName, implementation);
287 // ASSERT(!CORBA::is_nil(compo));
288 MESSAGE("Component launched !" << path);
293 MESSAGE("Component found !" << path);
294 Engines::Component_var compo = Engines::Component::_narrow(obj);
295 // ASSERT(!CORBA::is_nil(compo));
300 catch (CORBA::COMM_FAILURE&)
302 INFOS("Caught CORBA::SystemException CommFailure. Engine "
303 << path << "does not respond" );
308 catch (ServiceUnreachable&)
310 INFOS("Caught exception: Naming Service Unreachable");
314 INFOS("Caught unknown exception.");
316 return Engines::Component::_nil();
319 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
320 (const char *containerName,
321 const char *componentName)
323 // BEGIN_OF("FindOrLoad_Component(2)");
326 string theContainer ;
327 string theComputerContainer = ContainerName( containerName ,
330 Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
334 if ( CORBA::is_nil( cont ) ) {
335 MESSAGE("Container not found ! " << theComputerContainer );
336 return Engines::Component::_nil();
339 // char * machine = cont->machineName() ;
340 const char * machine = theComputer.c_str() ;
342 string path( theComputerContainer );
344 path += componentName;
348 CORBA::Object_var obj = _NS->Resolve(path.c_str());
349 if ( CORBA::is_nil( obj ) ) {
350 MESSAGE("Component not found ! trying to load " << path);
351 CORBA::Object_var obj2 = _NS->Resolve("/Kernel/ModulCatalog");
352 SALOME_ModuleCatalog::ModuleCatalog_var Catalog =
353 SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj2);
355 SALOME_ModuleCatalog::Acomponent_ptr compoInfo =
356 Catalog->GetComponent(componentName);
357 if (CORBA::is_nil (compoInfo))
359 INFOS("Catalog Error : Component not found in the catalog")
360 return Engines::Component::_nil();
367 path = compoInfo->GetPathPrefix( machine ) ;
370 catch (SALOME_ModuleCatalog::NotFound&)
372 MESSAGE("GetPathPrefix(" << machine << ") not found!"
373 << "trying localhost");
375 path = compoInfo->GetPathPrefix("localhost") ;
378 catch (SALOME_ModuleCatalog::NotFound&) {
379 MESSAGE("GetPathPrefix(localhost) not found!") ;
385 string implementation(path);
386 implementation += "lib";
387 implementation += componentName;
388 implementation += "Engine.so";
390 Engines::Component_var compo
391 = cont->load_impl(componentName, implementation.c_str());
393 // ASSERT(!CORBA::is_nil(compo));
394 // MESSAGE("Component launched !" << path);
399 MESSAGE("Component found !" << path);
400 Engines::Component_var compo = Engines::Component::_narrow(obj);
401 // ASSERT(!CORBA::is_nil(compo));
404 string instanceName = compo->instanceName();
406 catch (CORBA::COMM_FAILURE&)
408 INFOS("Caught CORBA::SystemException CommFailure. Engine "
409 << path << "does not respond" );
414 catch (ServiceUnreachable&)
416 INFOS("Caught exception: Naming Service Unreachable");
420 INFOS("Caught unknown exception.");
422 return Engines::Component::_nil();