Salome HOME
Fix for IPAL9408 : 3.0.0(current3106): CRASH after trying to "Close" saved document...
[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 #include "OpUtil.hxx"
41
42 #ifdef CHECKTIME
43 #include <Utils_Timer.hxx>
44 #endif
45
46 #ifdef HAVE_MPI2
47 #include <mpi.h>
48 #endif
49
50 #include "Container_init_python.hxx"
51
52 using namespace std;
53
54 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
55
56 int main(int argc, char* argv[])
57 {
58 #ifdef HAVE_MPI2
59   MPI_Init(&argc,&argv);
60 #endif
61   // Initialise the ORB.
62   CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
63   SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
64   INFOS_COMPILATION;
65   BEGIN_OF(argv[0]);
66
67   ASSERT(argc > 1);
68   SCRUTE(argv[1]);
69   bool isSupervContainer = false;
70   if (strcmp(argv[1],"SuperVisionContainer") == 0) isSupervContainer = true;
71
72   if (!isSupervContainer)
73     {
74       int _argc = 1;
75       char* _argv[] = {""};
76       KERNEL_PYTHON::init_python(argc,argv);
77     }
78   else
79     {
80       Py_Initialize() ;
81       PySys_SetArgv( argc , argv ) ;
82     }
83
84   try{
85     // Obtain a reference to the root POA.
86     // obtain the root poa manager
87     //
88     long TIMESleep = 500000000;
89     int NumberOfTries = 40;
90     int a;
91     timespec ts_req;
92     ts_req.tv_nsec=TIMESleep;
93     ts_req.tv_sec=0;
94     timespec ts_rem;
95     ts_rem.tv_nsec=0;
96     ts_rem.tv_sec=0;
97     CosNaming::NamingContext_var inc;
98     PortableServer::POA_var root_poa;
99     CORBA::Object_var theObj;
100     CORBA::Object_var obj;
101     CORBA::Object_var object;
102     //SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
103     int CONTAINER=0;
104     const char * Env = getenv("USE_LOGGER");
105     int EnvL =0;
106     if(Env != NULL && strlen(Env))
107       EnvL=1;
108     
109     CosNaming::Name name;
110     name.length(1);
111     name[0].id=CORBA::string_dup("Logger");    
112     PortableServer::POAManager_var pman; 
113     for(int i = 1; i <= NumberOfTries; i++){
114       if(i != 1) 
115         a=nanosleep(&ts_req,&ts_rem);
116       try{ 
117         obj = orb->resolve_initial_references("RootPOA");
118         if(!CORBA::is_nil(obj))
119           root_poa = PortableServer::POA::_narrow(obj);
120         if(!CORBA::is_nil(root_poa))
121           pman = root_poa->the_POAManager();
122         if(!CORBA::is_nil(orb)) 
123           theObj = orb->resolve_initial_references("NameService");
124         if (!CORBA::is_nil(theObj))
125           inc = CosNaming::NamingContext::_narrow(theObj);
126       }catch(CORBA::SystemException&){
127         MESSAGE( "Container: CORBA::SystemException: Unable to contact the Naming Service" );
128       }
129       if(!CORBA::is_nil(inc)){
130         MESSAGE( "Container: Naming Service was found" );
131         if(EnvL == 1){
132           for(int j = 1; j <= NumberOfTries; j++){
133             if(j != 1) 
134               a=nanosleep(&ts_req, &ts_rem);
135             try{
136               object = inc->resolve(name);
137             }catch(CosNaming::NamingContext::NotFound){
138               MESSAGE( "Container: Logger Server wasn't found" );
139             }catch(...){
140               MESSAGE( "Container: Unknown exception" );
141             }
142             if(!CORBA::is_nil(object)){
143               MESSAGE( "Container: Logger Server was found" );
144               CONTAINER = 1;
145               break;
146             }
147           }
148         }
149       }
150       if(CONTAINER == 1 || (EnvL == 0 && !CORBA::is_nil(inc)))
151         break;
152     }
153     
154     char *containerName = "";
155     if(argc > 1){
156       containerName = argv[1] ;
157     }
158
159     // add new container to the kill list
160     char aCommand[40];
161     sprintf(aCommand, "addToKillList.py %d SALOME_Container", getpid());
162     system(aCommand);
163     
164     Engines_Container_i * myContainer 
165       = new Engines_Container_i(orb, root_poa, containerName , argc , argv );
166
167     pman->activate();
168     
169 #ifdef CHECKTIME
170     Utils_Timer timer;
171     timer.Start();
172     timer.Stop();
173     MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
174     timer.ShowAbsolute();
175 #endif
176     
177     HandleServerSideSignals(orb);
178   }catch(CORBA::SystemException&){
179     INFOS("Caught CORBA::SystemException.");
180   }catch(PortableServer::POA::WrongPolicy&){
181     INFOS("Caught CORBA::WrongPolicyException.");
182   }catch(PortableServer::POA::ServantAlreadyActive&){
183     INFOS("Caught CORBA::ServantAlreadyActiveException");
184   }catch(CORBA::Exception&){
185     INFOS("Caught CORBA::Exception.");
186   }catch(std::exception& exc){
187     INFOS("Caught std::exception - "<<exc.what()); 
188   }catch(...){
189     INFOS("Caught unknown exception.");
190   }
191 #ifdef HAVE_MPI2
192   MPI_Finalize();
193 #endif
194   END_OF(argv[0]);
195   delete myThreadTrace;
196   return 0 ;
197 }
198