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