Salome HOME
merge from BR_V51_AR 7 may 09
[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 #ifndef WIN32
74 typedef void (*sighandler_t)(int);
75 sighandler_t setsig(int sig, sighandler_t handler)
76 {
77   struct sigaction context, ocontext;
78   context.sa_handler = handler;
79   sigemptyset(&context.sa_mask);
80   context.sa_flags = 0;
81   if (sigaction(sig, &context, &ocontext) == -1)
82     return SIG_ERR;
83   return ocontext.sa_handler;
84 }
85 #endif //WIN32
86
87 void AttachDebugger()
88 {
89 #ifndef WIN32
90   if(getenv ("DEBUGGER"))
91     {
92       std::stringstream exec;
93       exec << "$DEBUGGER SALOME_Container " << getpid() << "&";
94       std::cerr << exec.str() << std::endl;
95       system(exec.str().c_str());
96       while(1);
97     }
98 #endif
99 }
100
101 void Handler(int theSigId)
102 {
103   std::cerr << "SIGSEGV: "  << std::endl;
104   AttachDebugger();
105   //to exit or not to exit
106   exit(1);
107 }
108
109 void terminateHandler(void)
110 {
111   std::cerr << "Terminate: not managed exception !"  << std::endl;
112   AttachDebugger();
113 }
114
115 void unexpectedHandler(void)
116 {
117   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
118   AttachDebugger();
119 }
120
121 int main(int argc, char* argv[])
122 {
123 #ifdef _MPI_SEQ_CONTAINER_
124   #ifdef HAVE_MPI2
125   MPI_Init(&argc,&argv);
126   #endif
127 #endif  
128
129 #ifndef WIN32
130   if(getenv ("DEBUGGER"))
131     {
132       setsig(SIGSEGV,&Handler);
133       set_terminate(&terminateHandler);
134       set_unexpected(&unexpectedHandler);
135     }
136 #endif
137
138   // Initialise the ORB.
139   //SRN: BugID: IPAL9541, it's necessary to set a size of one message to be at least 100Mb
140   //CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
141   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
142   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
143   CORBA::ORB_ptr orb = init(argc , argv ) ;
144
145   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
146   INFOS_COMPILATION;
147   BEGIN_OF(argv[0]);
148
149   ASSERT(argc > 1);
150   SCRUTE(argv[1]);
151   bool isSupervContainer = false;
152   if (strcmp(argv[1],"SuperVisionContainer") == 0) isSupervContainer = true;
153
154   if (!isSupervContainer)
155     {
156       // int _argc = 1;
157       // char* _argv[] = {""};
158       KERNEL_PYTHON::init_python(argc,argv);
159     }
160   else
161     {
162       Py_Initialize() ;
163       PySys_SetArgv( argc , argv ) ;
164     }
165     
166   char *containerName = (char *)"";
167   if(argc > 1)
168     {
169       containerName = argv[1] ;
170     }
171
172   try
173     {  
174       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
175       ASSERT(!CORBA::is_nil(obj));
176       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
177
178       PortableServer::POAManager_var pman = root_poa->the_POAManager();
179
180       // add new container to the kill list
181 #ifndef WIN32
182       stringstream aCommand ;
183       aCommand << "addToKillList.py " << getpid() << " SALOME_Container" << ends ;
184       system(aCommand.str().c_str());
185 #endif
186       
187       new Engines_Container_i(orb, root_poa, containerName , argc , argv );
188       
189       pman->activate();
190       
191 #ifdef CHECKTIME
192       Utils_Timer timer;
193       timer.Start();
194       timer.Stop();
195       timer.ShowAbsolute();
196 #endif
197
198       HandleServerSideSignals(orb);
199
200       if (!isSupervContainer)
201       {
202         PyGILState_Ensure();
203         //Delete python container that destroy orb from python (pyCont._orb.destroy())
204         Py_Finalize();
205       }
206       else
207       {
208         orb->destroy();
209       }
210     }
211   catch(CORBA::SystemException&)
212     {
213       INFOS("Caught CORBA::SystemException.");
214     }
215   catch(PortableServer::POA::ServantAlreadyActive&)
216     {
217       INFOS("Caught CORBA::ServantAlreadyActiveException");
218     }
219   catch(CORBA::Exception&)
220     {
221       INFOS("Caught CORBA::Exception.");
222     }
223   catch(std::exception& exc)
224     {
225       INFOS("Caught std::exception - "<<exc.what()); 
226     }
227   catch(...)
228     {
229       INFOS("Caught unknown exception.");
230     }
231
232 #ifdef _MPI_SEQ_CONTAINER_
233   #ifdef HAVE_MPI2
234   MPI_Finalize();
235   #endif
236 #endif  
237
238   //END_OF(argv[0]);
239   //LocalTraceBufferPool* bp1 = LocalTraceBufferPool::instance();
240   //bp1->deleteInstance(bp1);
241   return 0 ;
242 }
243