]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/SALOME_Container.cxx
Salome HOME
Merge from V5_1_main 14/05/2010
[modules/kernel.git] / src / Container / SALOME_Container.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME Container : implementation of container and engine for Kernel
24 //  File   : SALOME_Container.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28 //
29 #ifdef _MPI_SEQ_CONTAINER_
30   #ifdef HAVE_MPI2
31 #include <mpi.h>
32   #endif
33 #endif
34
35 #include <iostream>
36 #include <sstream>
37 #include <string>
38 #include <stdio.h>
39 #include <time.h>
40 #ifndef WIN32
41 # include <sys/time.h>
42 # include <dlfcn.h>
43 #endif
44
45
46 #ifndef WIN32
47 #include <unistd.h>
48 #else
49 #include <process.h>
50 #endif
51 #include "SALOME_Container_i.hxx"
52 #include "utilities.h"
53 #include "Utils_ORB_INIT.hxx"
54 #include "Utils_SINGLETON.hxx"
55 #include "OpUtil.hxx"
56
57 #ifdef CHECKTIME
58 #include <Utils_Timer.hxx>
59 #endif
60
61 #include "Container_init_python.hxx"
62
63 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
64
65 #include <stdexcept>
66 #include <signal.h>
67 #include <sys/types.h>
68 #ifndef WIN32
69 # include <sys/wait.h>
70 #endif
71
72 void AttachDebugger();
73 void Handler(int);
74 void terminateHandler();
75 void unexpectedHandler();
76
77 #ifndef WIN32
78 void (* setsig(int, void (*)(int)))(int);
79
80 typedef void (*sighandler_t)(int);
81 sighandler_t setsig(int sig, sighandler_t handler)
82 {
83   struct sigaction context, ocontext;
84   context.sa_handler = handler;
85   sigemptyset(&context.sa_mask);
86   context.sa_flags = 0;
87   if (sigaction(sig, &context, &ocontext) == -1)
88     return SIG_ERR;
89   return ocontext.sa_handler;
90 }
91 #endif //WIN32
92
93 void AttachDebugger()
94 {
95 #ifndef WIN32
96   if(getenv ("DEBUGGER"))
97     {
98       std::stringstream exec;
99       exec << "$DEBUGGER SALOME_Container " << getpid() << "&";
100       std::cerr << exec.str() << std::endl;
101       system(exec.str().c_str());
102       while(1);
103     }
104 #endif
105 }
106
107 void Handler(int theSigId)
108 {
109   std::cerr << "SIGSEGV: "  << std::endl;
110   AttachDebugger();
111   //to exit or not to exit
112   _exit(1);
113 }
114
115 void terminateHandler(void)
116 {
117   std::cerr << "Terminate: not managed exception !"  << std::endl;
118   AttachDebugger();
119 }
120
121 void unexpectedHandler(void)
122 {
123   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
124   AttachDebugger();
125 }
126
127 int main(int argc, char* argv[])
128 {
129 #ifdef _MPI_SEQ_CONTAINER_
130   #ifdef HAVE_MPI2
131   MPI_Init(&argc,&argv);
132   #endif
133 #endif  
134
135 #ifndef WIN32
136   if(getenv ("DEBUGGER"))
137     {
138       setsig(SIGSEGV,&Handler);
139       std::set_terminate(&terminateHandler);
140       std::set_unexpected(&unexpectedHandler);
141     }
142 #endif
143
144   // Initialise the ORB.
145   //SRN: BugID: IPAL9541, it's necessary to set a size of one message to be at least 100Mb
146   //CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
147   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
148   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
149   CORBA::ORB_ptr orb = init(argc , argv ) ;
150
151   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
152   INFOS_COMPILATION;
153   BEGIN_OF(argv[0]);
154
155   ASSERT(argc > 1);
156   SCRUTE(argv[1]);
157
158   KERNEL_PYTHON::init_python(argc,argv);
159     
160   char *containerName = (char *)"";
161   if(argc > 1)
162     {
163       containerName = argv[1] ;
164     }
165
166   try
167     {  
168       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
169       ASSERT(!CORBA::is_nil(obj));
170       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
171
172       PortableServer::POAManager_var pman = root_poa->the_POAManager();
173
174       // add new container to the kill list
175 #ifndef WIN32
176       std::stringstream aCommand ;
177       aCommand << "addToKillList.py " << getpid() << " SALOME_Container" << std::ends ;
178       system(aCommand.str().c_str());
179 #endif
180       
181       new Engines_Container_i(orb, root_poa, containerName , argc , argv );
182       
183       pman->activate();
184       
185 #ifdef CHECKTIME
186       Utils_Timer timer;
187       timer.Start();
188       timer.Stop();
189       timer.ShowAbsolute();
190 #endif
191
192       HandleServerSideSignals(orb);
193
194 //#define MEMORYLEAKS
195 #ifdef MEMORYLEAKS
196         PyGILState_Ensure();
197         //Destroy orb from python (for chasing memory leaks)
198         PyRun_SimpleString("from omniORB import CORBA");
199         PyRun_SimpleString("orb=CORBA.ORB_init([''], CORBA.ORB_ID)");
200         PyRun_SimpleString("orb.destroy()");
201         Py_Finalize();
202 #endif
203     }
204   catch(CORBA::SystemException&)
205     {
206       INFOS("Caught CORBA::SystemException.");
207     }
208   catch(PortableServer::POA::ServantAlreadyActive&)
209     {
210       INFOS("Caught CORBA::ServantAlreadyActiveException");
211     }
212   catch(CORBA::Exception&)
213     {
214       INFOS("Caught CORBA::Exception.");
215     }
216   catch(std::exception& exc)
217     {
218       INFOS("Caught std::exception - "<<exc.what()); 
219     }
220   catch(...)
221     {
222       INFOS("Caught unknown exception.");
223     }
224
225 #ifdef _MPI_SEQ_CONTAINER_
226   #ifdef HAVE_MPI2
227   MPI_Finalize();
228   #endif
229 #endif  
230
231   return 0 ;
232 }
233