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