Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / Launcher / SALOME_LauncherServer.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 #include "SALOME_Launcher.hxx"
23 #include "utilities.h"
24 #include <sstream>
25 #include <iostream>
26 #include <stdexcept>
27 #include <libxml/parser.h>
28
29 using namespace std;
30
31 void AttachDebugger()
32 {
33 #ifndef WIN32
34   if(getenv ("DEBUGGER"))
35     {
36       std::stringstream exec;
37       exec << "$DEBUGGER SALOME_LauncherServer " << getpid() << "&";
38       MESSAGE ( exec.str() );
39       system(exec.str().c_str());
40       while(1);
41     }
42 #endif
43 }
44
45 void terminateHandler(void)
46 {
47   MESSAGE ( "Terminate: not managed exception !"  );
48   AttachDebugger();
49 }
50
51 void unexpectedHandler(void)
52 {
53   MESSAGE ( "Unexpected: unexpected exception !"  );
54   AttachDebugger();
55 }
56
57
58 int main(int argc, char* argv[])
59 {
60   if(getenv ("DEBUGGER"))
61     {
62 //       setsig(SIGSEGV,&Handler);
63       set_terminate(&terminateHandler);
64       set_unexpected(&unexpectedHandler);
65     }
66   /* Init libxml 
67    * To avoid memory leak, need to call xmlInitParser in the main thread
68    * and not call xmlCleanupParser later (cause implicit reinitialization in thread)
69    */
70   xmlInitParser();
71
72   PortableServer::POA_var root_poa;
73   PortableServer::POAManager_var pman;
74   CORBA::Object_var obj;
75
76   CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
77   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
78   INFOS_COMPILATION;
79   BEGIN_OF(argv[0]);
80   try{ 
81     obj = orb->resolve_initial_references("RootPOA");
82     if(!CORBA::is_nil(obj))
83       root_poa = PortableServer::POA::_narrow(obj);
84     if(!CORBA::is_nil(root_poa))
85       pman = root_poa->the_POAManager();
86   }
87   catch(CORBA::COMM_FAILURE&){
88     MESSAGE( "Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
89   }
90   try{
91     SALOME_Launcher *lServ=new SALOME_Launcher(orb,root_poa);
92     pman->activate();
93     lServ->_remove_ref();
94     orb->run();
95     orb->destroy();
96   }catch(CORBA::SystemException&){
97     MESSAGE("Caught CORBA::SystemException.");
98   }catch(PortableServer::POA::WrongPolicy&){
99     MESSAGE("Caught CORBA::WrongPolicyException.");
100   }catch(PortableServer::POA::ServantAlreadyActive&){
101     MESSAGE("Caught CORBA::ServantAlreadyActiveException");
102   }catch(CORBA::Exception&){
103     MESSAGE("Caught CORBA::Exception.");
104   }catch(std::exception& exc){
105     MESSAGE("Caught std::exception - "<<exc.what()); 
106   }catch(...){
107     MESSAGE("Caught unknown exception.");
108   }
109   END_OF(argv[0]);
110   //  delete myThreadTrace;
111 }
112