Salome HOME
PR: relax start timers everywhere to 40x500ms = 20s
[modules/kernel.git] / src / Container / SALOME_Container.cxx
1 //  SALOME Container : implementation of container and engine for Kernel
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Container.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28
29 #include <iostream>
30 #include <string>
31 #include <stdio.h>
32
33 //#include "Utils_ORB_INIT.hxx"
34 //#include "Utils_SINGLETON.hxx"
35 #include <unistd.h>
36 #include "SALOME_NamingService.hxx"
37 #include "SALOME_Container_i.hxx"
38 #include "utilities.h"
39 #include "SALOMETraceCollector.hxx"
40
41 #ifdef CHECKTIME
42 #include <Utils_Timer.hxx>
43 #endif
44
45 #ifdef HAVE_MPI2
46 #include <mpi.h>
47 #endif
48
49 #include <Python.h>
50
51 using namespace std;
52
53 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
54
55 static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }};
56
57 int main(int argc, char* argv[])
58 {
59 #ifdef HAVE_MPI2
60   MPI_Init(&argc,&argv);
61 #endif
62   // Initialise the ORB.
63   //ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
64   CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
65   SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
66   INFOS_COMPILATION;
67   BEGIN_OF(argv[0]);
68     
69   Py_Initialize() ;
70   PySys_SetArgv( argc , argv ) ;
71   Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ;
72   
73   try{
74     // Obtain a reference to the root POA.
75     // obtain the root poa manager
76     //
77     long TIMESleep = 500000000;
78     int NumberOfTries = 40;
79     int a;
80     timespec ts_req;
81     ts_req.tv_nsec=TIMESleep;
82     ts_req.tv_sec=0;
83     timespec ts_rem;
84     ts_rem.tv_nsec=0;
85     ts_rem.tv_sec=0;
86     CosNaming::NamingContext_var inc;
87     PortableServer::POA_var root_poa;
88     CORBA::Object_var theObj;
89     CORBA::Object_var obj;
90     CORBA::Object_var object;
91     //SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
92     int CONTAINER=0;
93     const char * Env = getenv("USE_LOGGER");
94     int EnvL =0;
95     if(Env != NULL && strlen(Env))
96       EnvL=1;
97     
98     CosNaming::Name name;
99     name.length(1);
100     name[0].id=CORBA::string_dup("Logger");    
101     PortableServer::POAManager_var pman; 
102     for(int i = 1; i <= NumberOfTries; i++){
103       if(i != 1) 
104         a=nanosleep(&ts_req,&ts_rem);
105       try{ 
106         obj = orb->resolve_initial_references("RootPOA");
107         if(!CORBA::is_nil(obj))
108           root_poa = PortableServer::POA::_narrow(obj);
109         if(!CORBA::is_nil(root_poa))
110           pman = root_poa->the_POAManager();
111         if(!CORBA::is_nil(orb)) 
112           theObj = orb->resolve_initial_references("NameService");
113         if (!CORBA::is_nil(theObj))
114           inc = CosNaming::NamingContext::_narrow(theObj);
115       }catch(CORBA::COMM_FAILURE&){
116         MESSAGE( "Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
117       }
118       if(!CORBA::is_nil(inc)){
119         MESSAGE( "Container: Naming Service was found" );
120         if(EnvL == 1){
121           for(int j = 1; j <= NumberOfTries; j++){
122             if(j != 1) 
123               a=nanosleep(&ts_req, &ts_rem);
124             try{
125               object = inc->resolve(name);
126             }catch(CosNaming::NamingContext::NotFound){
127               MESSAGE( "Container: Logger Server wasn't found" );
128             }catch(...){
129               MESSAGE( "Container: Unknown exception" );
130             }
131             if(!CORBA::is_nil(object)){
132               MESSAGE( "Container: Logger Server was found" );
133               CONTAINER = 1;
134               break;
135             }
136           }
137         }
138       }
139       if(CONTAINER == 1 || (EnvL == 0 && !CORBA::is_nil(inc)))
140         break;
141     }
142     
143     char *containerName = "";
144     if(argc > 1){
145       containerName = argv[1] ;
146     }
147     
148     Engines_Container_i * myContainer 
149       = new Engines_Container_i(orb, root_poa, containerName , argc , argv );
150
151     pman->activate();
152     
153 #ifdef CHECKTIME
154     Utils_Timer timer;
155     timer.Start();
156     timer.Stop();
157     MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
158     timer.ShowAbsolute();
159 #endif
160     
161     HandleServerSideSignals(orb);
162   }catch(CORBA::SystemException&){
163     INFOS("Caught CORBA::SystemException.");
164   }catch(PortableServer::POA::WrongPolicy&){
165     INFOS("Caught CORBA::WrongPolicyException.");
166   }catch(PortableServer::POA::ServantAlreadyActive&){
167     INFOS("Caught CORBA::ServantAlreadyActiveException");
168   }catch(CORBA::Exception&){
169     INFOS("Caught CORBA::Exception.");
170   }catch(std::exception& exc){
171     INFOS("Caught std::exception - "<<exc.what()); 
172   }catch(...){
173     INFOS("Caught unknown exception.");
174   }
175 #ifdef HAVE_MPI2
176   MPI_Finalize();
177 #endif
178   END_OF(argv[0]);
179   delete myThreadTrace;
180   return 0 ;
181 }
182