Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/yacs.git] / src / LifeCycleCORBA / SALOME_LifeCycleCORBA.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SALOME_LifeCycleCORBA.cxx
4 // Created   : jeu jui 12 14:55:50 CEST 2001
5 // Author    : Paul RASCLE, EDF
6 // Project   : SALOME
7 // Copyright : EDF 2001
8 // $Header$
9 //=============================================================================
10
11 #include <iostream>
12 #include <fstream>
13 #include <strstream>
14 #include <iomanip>
15
16 #include "OpUtil.hxx"
17 #include "utilities.h"
18
19 #include <ServiceUnreachable.hxx>
20
21 #include "SALOME_LifeCycleCORBA.hxx"
22 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
23 #include "SALOME_NamingService.hxx"
24
25 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA()
26 {
27   _NS = NULL;
28   _FactoryServer = NULL ;
29 }
30
31 SALOME_LifeCycleCORBA::SALOME_LifeCycleCORBA(SALOME_NamingService *ns)
32 {
33   _NS = ns;
34   _FactoryServer = NULL ;
35 }
36
37 SALOME_LifeCycleCORBA::~SALOME_LifeCycleCORBA()
38 {
39 }
40
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 , '/' ) ;
49   if ( !slash ) {
50     *theComputer = GetHostname() ;
51     theComputerContainer += *theComputer ;
52     theComputerContainer += "/" ;
53     *theContainer = ContainerName ;
54     theComputerContainer += *theContainer ;
55   }
56   else {
57     slash[ 0 ] = '\0' ;
58     slash += 1 ;
59     *theContainer = slash ;
60     if ( !strcmp( ContainerName , "localhost" ) ) {
61       *theComputer = GetHostname() ;
62     }
63     else {
64       *theComputer = ContainerName ;
65     }
66     theComputerContainer += *theComputer ;
67     theComputerContainer += "/" ;
68     theComputerContainer += *theContainer ;
69   }
70   return theComputerContainer ;
71 }
72
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) ;
79   try {
80     path = Catalog->GetPathPrefix( theComputer );
81   }
82   catch (SALOME_ModuleCatalog::NotFound&) {
83     MESSAGE("GetPathPrefix(" << theComputer << ") not found!");
84     path = "" ;
85   }
86   SCRUTE( path ) ;
87   return CORBA::string_dup( path ) ;
88 }
89
90 Engines::Container_var SALOME_LifeCycleCORBA::FindContainer(const char *containerName ) {
91   ASSERT(_NS != NULL);
92   string cont ;
93   if ( strncmp( containerName , "/Containers/" , 12 ) ) { // Compatibility ...
94     string theComputer ;
95     string theContainer ;
96     cont = ContainerName( containerName , &theComputer , &theContainer ) ;
97   }
98   else {
99     cont = containerName ;
100   }
101   try {
102
103     SCRUTE( cont );
104
105     CORBA::Object_var obj = _NS->Resolve( cont.c_str() );
106     if( !CORBA::is_nil( obj ) ) {
107       return Engines::Container::_narrow( obj ) ;
108     }
109   }
110   catch (ServiceUnreachable&) {
111     INFOS("Caught exception: Naming Service Unreachable");
112   }
113   catch (...) {
114     INFOS("Caught unknown exception.");
115   }
116   return Engines::Container::_nil();
117 }
118
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" ) ) {
131     pyCont = true ;
132   }
133   if ( !CORBA::is_nil( aContainer ) ) {
134     return aContainer ;
135   }
136   else {
137     string FactoryServer = theComputer ;
138     if ( pyCont ) {
139       FactoryServer += "/FactoryServerPy" ;
140     }
141     else {
142       FactoryServer += "/FactoryServer" ;
143     }
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 &
147       string rsh( "" ) ;
148       if ( theComputer!= GetHostname() ) {
149         rsh += "rsh -n " ;
150         rsh += theComputer ;
151         rsh += " " ;
152       }
153       string path = ComputerPath( theComputer.c_str() ) ;
154       SCRUTE( path ) ;
155       if ( path[0] != '\0' ) {
156         rsh += path ;
157         rsh += "/../bin/" ;
158       }
159       rsh += "runSession " ;
160       if ( pyCont ) {
161         rsh += "SALOME_ContainerPy.py " ;
162         rsh += "FactoryServerPy -" ;
163       }
164       else {
165         rsh += "SALOME_Container " ;
166         rsh += "FactoryServer -" ;
167       }
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 ;
174       rsh += ORBInitRef ;
175       rsh += " " ;
176       omniORBfile >> nameservice ;
177       omniORBfile.close() ;
178       char * bsn = strchr( nameservice , '\n' ) ;
179       if ( bsn ) {
180         bsn[ 0 ] = '\0' ;
181       }
182       rsh += nameservice ;
183       if ( pyCont ) {
184         rsh += " > /tmp/FactoryServerPy_" ;
185       }
186       else {
187         rsh += " > /tmp/FactoryServer_" ;
188       }
189       rsh += theComputer ;
190       rsh += ".log 2>&1 &" ;
191       SCRUTE( rsh );
192       int status = system( rsh.c_str() ) ;
193       if (status == -1) {
194         INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status -1)") ;
195       }
196       else if (status == 217) {
197         INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed (system command status 217)") ;
198       }
199       else {
200         int count = 21 ;
201         while ( CORBA::is_nil( aFactoryServer ) && count ) {
202           sleep( 1 ) ;
203           count-- ;
204           if ( count != 10 )
205             MESSAGE( count << ". Waiting for FactoryServer on " << theComputer)
206           aFactoryServer = FindContainer( FactoryServer.c_str() ) ;
207         }
208         if ( CORBA::is_nil( aFactoryServer ) ) {
209           INFOS("SALOME_LifeCycleCORBA::StartOrFindContainer rsh failed") ;
210         }
211         else if ( strcmp( theComputer.c_str() , GetHostname().c_str() ) ) {
212           _FactoryServer = aFactoryServer ;
213         }
214       }
215     }
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");
223           return myContainer ;
224         }
225         else {
226           MESSAGE("Container " << aComputerContainer << " NOT started");
227         }
228       }
229       else {
230         MESSAGE("Container " << aComputerContainer << " started");
231         return aFactoryServer ;
232       }
233     }
234   }
235   return Engines::Container::_nil();
236 }
237
238 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
239                                    (const char *containerName,
240                                     const char *componentName,
241                                     const char *implementation)
242 {
243   BEGIN_OF("FindOrLoad_Component(1)");
244   ASSERT(_NS != NULL);
245   string theComputer ;
246   string theContainer ;
247   string theComputerContainer = ContainerName( containerName ,
248                                                &theComputer ,
249                                                &theContainer ) ;
250   Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
251                                                       theComputer ,
252                                                       theContainer ) ;
253 //  ASSERT(!CORBA::is_nil(cont));
254
255   string path( theComputerContainer );
256   path = path + "/";
257   path = path + componentName;
258   SCRUTE(path);
259   try
260     {
261       CORBA::Object_var obj = _NS->Resolve(path.c_str());
262       if (CORBA::is_nil(obj))
263         {
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);
269           return compo;
270         }
271       else
272         {
273           MESSAGE("Component found !" << path);
274           Engines::Component_var compo = Engines::Component::_narrow(obj);
275 //        ASSERT(!CORBA::is_nil(compo));
276           try
277             {
278               compo->ping(); 
279             }
280           catch (CORBA::COMM_FAILURE&)
281             {
282               INFOS("Caught CORBA::SystemException CommFailure. Engine "
283                     << path << "does not respond" );
284             }
285           return compo;
286         }
287     }
288   catch (ServiceUnreachable&)
289     {
290       INFOS("Caught exception: Naming Service Unreachable");
291     }
292   catch (...)
293     {
294       INFOS("Caught unknown exception.");
295     }
296   return Engines::Component::_nil();
297 }
298
299 Engines::Component_var SALOME_LifeCycleCORBA::FindOrLoad_Component
300                                   (const char *containerName,
301                                    const char *componentName)
302 {
303 //  BEGIN_OF("FindOrLoad_Component(2)");
304   ASSERT(_NS != NULL);
305   string theComputer ;
306   string theContainer ;
307   string theComputerContainer = ContainerName( containerName ,
308                                                &theComputer ,
309                                                &theContainer ) ;
310   Engines::Container_var cont = FindOrStartContainer( theComputerContainer ,
311                                                       theComputer ,
312                                                       theContainer ) ;
313
314   if ( CORBA::is_nil( cont ) ) {
315     MESSAGE("Container not found ! " << theComputerContainer );
316     return Engines::Component::_nil();
317   }
318
319 //  char * machine = cont->machineName() ;
320   const char * machine = theComputer.c_str() ;
321
322   string path( theComputerContainer );
323   path += "/";
324   path += componentName;
325   SCRUTE(path);
326
327   try {
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);
334
335           SALOME_ModuleCatalog::Acomponent_ptr compoInfo = 
336             Catalog->GetComponent(componentName);
337           if (CORBA::is_nil (compoInfo)) 
338             {
339               INFOS("Catalog Error : Component not found in the catalog")
340               return Engines::Component::_nil();
341 //              exit (-1);
342             }
343           
344           string  path;
345           try
346             {
347               path = compoInfo->GetPathPrefix( machine ) ;
348               path += "/" ;
349             }
350           catch (SALOME_ModuleCatalog::NotFound&)
351             {
352               MESSAGE("GetPathPrefix(" << machine << ") not found!"
353                       << "trying localhost");
354               try {
355                 path = compoInfo->GetPathPrefix("localhost") ;
356                 path += "/" ;
357               }
358               catch (SALOME_ModuleCatalog::NotFound&) {
359                 MESSAGE("GetPathPrefix(localhost) not found!") ;
360                 path = "" ;
361               }
362             }
363
364           SCRUTE(path); 
365           string implementation(path);
366           implementation += "lib";
367           implementation += componentName;
368           implementation += "Engine.so";
369           
370           Engines::Component_var compo 
371             = cont->load_impl(componentName, implementation.c_str());
372
373 //        ASSERT(!CORBA::is_nil(compo));
374 //        MESSAGE("Component launched !" << path);
375           return compo;
376     }
377     else
378         {
379           MESSAGE("Component found !" << path);
380           Engines::Component_var compo = Engines::Component::_narrow(obj);
381 //        ASSERT(!CORBA::is_nil(compo));
382           try
383             {
384               string instanceName = compo->instanceName(); 
385             }
386           catch (CORBA::COMM_FAILURE&)
387             {
388               INFOS("Caught CORBA::SystemException CommFailure. Engine "
389                     << path << "does not respond" );
390             }
391           return compo;
392         }
393     }
394   catch (ServiceUnreachable&)
395     {
396       INFOS("Caught exception: Naming Service Unreachable");
397     }
398   catch (...)
399     {
400       INFOS("Caught unknown exception.");
401     }
402   return Engines::Component::_nil();
403 }