Back to index

salome-kernel  6.5.0
SALOME_Container.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Container : implementation of container and engine for Kernel
00024 //  File   : SALOME_Container.cxx
00025 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/SALOME_Container.cxx,v 1.20.2.3.14.3.12.1 2012-04-12 14:05:03 vsr Exp $
00028 //
00029 #ifdef _MPI_SEQ_CONTAINER_
00030   #ifdef HAVE_MPI2
00031 #include <mpi.h>
00032   #endif
00033 #endif
00034 
00035 #include <iostream>
00036 #include <sstream>
00037 #include <string>
00038 #include <stdio.h>
00039 #include <time.h>
00040 #ifndef WIN32
00041 # include <sys/time.h>
00042 # include <dlfcn.h>
00043 #endif
00044 
00045 
00046 #ifndef WIN32
00047 #include <unistd.h>
00048 #else
00049 #include <process.h>
00050 #endif
00051 #include "SALOME_Container_i.hxx"
00052 #include "utilities.h"
00053 #include "Utils_ORB_INIT.hxx"
00054 #include "Utils_SINGLETON.hxx"
00055 #include "OpUtil.hxx"
00056 
00057 #ifdef CHECKTIME
00058 #include <Utils_Timer.hxx>
00059 #endif
00060 
00061 #include "Container_init_python.hxx"
00062 
00063 extern "C" void HandleServerSideSignals(CORBA::ORB_ptr theORB);
00064 
00065 #include <stdexcept>
00066 #include <signal.h>
00067 #include <sys/types.h>
00068 #ifndef WIN32
00069 # include <sys/wait.h>
00070 #endif
00071 
00072 void AttachDebugger();
00073 void Handler(int);
00074 void terminateHandler();
00075 void unexpectedHandler();
00076 
00077 #ifndef WIN32
00078 void (* setsig(int, void (*)(int)))(int);
00079 
00080 typedef void (*sighandler_t)(int);
00081 sighandler_t setsig(int sig, sighandler_t handler)
00082 {
00083   struct sigaction context, ocontext;
00084   context.sa_handler = handler;
00085   sigemptyset(&context.sa_mask);
00086   context.sa_flags = 0;
00087   if (sigaction(sig, &context, &ocontext) == -1)
00088     return SIG_ERR;
00089   return ocontext.sa_handler;
00090 }
00091 #endif //WIN32
00092 
00093 void AttachDebugger()
00094 {
00095 #ifndef WIN32
00096   if(getenv ("DEBUGGER"))
00097     {
00098       std::stringstream exec;
00099       exec << "$DEBUGGER SALOME_Container " << getpid() << "&";
00100       std::cerr << exec.str() << std::endl;
00101       system(exec.str().c_str());
00102       while(1);
00103     }
00104 #endif
00105 }
00106 
00107 void Handler(int theSigId)
00108 {
00109   std::cerr << "Signal= "<< theSigId  << std::endl;
00110   AttachDebugger();
00111   //to exit or not to exit
00112   _exit(1);
00113 }
00114 
00115 void terminateHandler(void)
00116 {
00117   std::cerr << "Terminate: not managed exception !"  << std::endl;
00118   AttachDebugger();
00119 }
00120 
00121 void unexpectedHandler(void)
00122 {
00123   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
00124   AttachDebugger();
00125 }
00126 
00127 int main(int argc, char* argv[])
00128 {
00129 #ifdef _MPI_SEQ_CONTAINER_
00130   #ifdef HAVE_MPI2
00131   MPI_Init(&argc,&argv);
00132   #endif
00133 #endif  
00134 
00135 #ifndef WIN32
00136   if(getenv ("DEBUGGER"))
00137     {
00138       setsig(SIGSEGV,&Handler);
00139       setsig(SIGFPE,&Handler);
00140       std::set_terminate(&terminateHandler);
00141       std::set_unexpected(&unexpectedHandler);
00142     }
00143 #endif
00144 
00145   // Initialise the ORB.
00146   //SRN: BugID: IPAL9541, it's necessary to set a size of one message to be at least 100Mb
00147   //CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
00148   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
00149   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
00150   CORBA::ORB_ptr orb = init(argc , argv ) ;
00151 
00152   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
00153   INFOS_COMPILATION;
00154   BEGIN_OF(argv[0]);
00155 
00156   ASSERT(argc > 1);
00157   SCRUTE(argv[1]);
00158 
00159   KERNEL_PYTHON::init_python(argc,argv);
00160     
00161   char *containerName = (char *)"";
00162   if(argc > 1)
00163     {
00164       containerName = argv[1] ;
00165     }
00166 
00167   try
00168     {  
00169       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
00170       ASSERT(!CORBA::is_nil(obj));
00171       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
00172 
00173       PortableServer::POAManager_var pman = root_poa->the_POAManager();
00174 
00175       // add new container to the kill list
00176 #ifndef WIN32
00177       std::stringstream aCommand ;
00178       aCommand << "addToKillList.py " << getpid() << " SALOME_Container" << std::ends ;
00179       system(aCommand.str().c_str());
00180 #endif
00181       
00182       new Engines_Container_i(orb, root_poa, containerName , argc , argv );
00183       
00184       pman->activate();
00185       
00186 #ifdef CHECKTIME
00187       Utils_Timer timer;
00188       timer.Start();
00189       timer.Stop();
00190       timer.ShowAbsolute();
00191 #endif
00192 
00193       HandleServerSideSignals(orb);
00194 
00195 //#define MEMORYLEAKS
00196 #ifdef MEMORYLEAKS
00197         PyGILState_Ensure();
00198         //Destroy orb from python (for chasing memory leaks)
00199         PyRun_SimpleString("from omniORB import CORBA");
00200         PyRun_SimpleString("orb=CORBA.ORB_init([''], CORBA.ORB_ID)");
00201         PyRun_SimpleString("orb.destroy()");
00202         Py_Finalize();
00203 #endif
00204     }
00205   catch(CORBA::SystemException&)
00206     {
00207       INFOS("Caught CORBA::SystemException.");
00208     }
00209   catch(PortableServer::POA::ServantAlreadyActive&)
00210     {
00211       INFOS("Caught CORBA::ServantAlreadyActiveException");
00212     }
00213   catch(CORBA::Exception&)
00214     {
00215       INFOS("Caught CORBA::Exception.");
00216     }
00217   catch(std::exception& exc)
00218     {
00219       INFOS("Caught std::exception - "<<exc.what()); 
00220     }
00221   catch(...)
00222     {
00223       INFOS("Caught unknown exception.");
00224     }
00225 
00226 #ifdef _MPI_SEQ_CONTAINER_
00227   #ifdef HAVE_MPI2
00228   MPI_Finalize();
00229   #endif
00230 #endif  
00231 
00232   return 0 ;
00233 }
00234