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