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