Salome HOME
#18963 Minimize compiler warnings
[modules/kernel.git] / src / Launcher / SALOME_LauncherServer.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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 #include "SALOME_Launcher.hxx"
24 #include "SALOMESDS_DataServerManager.hxx"
25 #include "SALOME_ExternalServerLauncher.hxx"
26 #include "SALOME_CPythonHelper.hxx"
27 #include "utilities.h"
28 #include <sstream>
29 #include <iostream>
30 #include <stdexcept>
31 #include <libxml/parser.h>
32
33 void AttachDebugger();
34 void terminateHandler(void);
35 void unexpectedHandler(void);
36
37 void AttachDebugger()
38 {
39 #ifndef WIN32
40   if(getenv ("DEBUGGER"))
41     {
42       std::stringstream exec;
43       exec << "$DEBUGGER SALOME_LauncherServer " << getpid() << "&";
44       MESSAGE ( exec.str() );
45       system(exec.str().c_str());
46       while(1);
47     }
48 #endif
49 }
50
51 void terminateHandler(void)
52 {
53   MESSAGE ( "Terminate: not managed exception !"  );
54   AttachDebugger();
55 }
56
57 void unexpectedHandler(void)
58 {
59   MESSAGE ( "Unexpected: unexpected exception !"  );
60   AttachDebugger();
61 }
62
63
64 int main(int argc, char* argv[])
65 {
66   if(getenv ("DEBUGGER"))
67     {
68 //       setsig(SIGSEGV,&Handler);
69       std::set_terminate(&terminateHandler);
70       //std::set_unexpected(&unexpectedHandler);
71     }
72   /* Init libxml 
73    * To avoid memory leak, need to call xmlInitParser in the main thread
74    * and not call xmlCleanupParser later (cause implicit reinitialization in thread)
75    */
76   xmlInitParser();
77
78   PortableServer::POA_var root_poa;
79   PortableServer::POAManager_var pman;
80   CORBA::Object_var obj;
81   CORBA::ORB_var orb;
82   {
83     int myArgc(argc+2);
84     char **myArgv(new char *[myArgc]);
85     for(int i=0;i<argc;i++)
86       myArgv[i]=strdup(argv[i]);
87     myArgv[argc+0]=strdup("-ORBsupportCurrent");
88     myArgv[argc+1]=strdup("0");
89     orb = CORBA::ORB_init( myArgc , myArgv ) ;
90     for(int i=0;i<myArgc;i++)
91       free(myArgv[i]);
92     delete [] myArgv;
93   }
94   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
95   INFOS_COMPILATION;
96   BEGIN_OF(argv[0]);
97   try{ 
98     obj = orb->resolve_initial_references("RootPOA");
99     if(!CORBA::is_nil(obj))
100       root_poa = PortableServer::POA::_narrow(obj);
101     if(!CORBA::is_nil(root_poa))
102       {
103         pman = root_poa->the_POAManager();
104         pman->activate();
105       }
106   }
107   catch(CORBA::COMM_FAILURE&){
108     MESSAGE( "Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
109   }
110   try
111     {
112       CORBA::PolicyList policies;
113       policies.length(1);
114       PortableServer::ThreadPolicy_var threadPol(root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
115       policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
116       PortableServer::POA_var safePOA = root_poa->create_POA("SingleThreadPOA",pman,policies);
117       threadPol->destroy();
118       SALOME_CPythonHelper cPyh;
119       cPyh.initializePython(argc,argv);
120       SALOME_Launcher *lServ(new SALOME_Launcher(orb,safePOA));
121       lServ->_remove_ref();
122       //
123       SALOMESDS::DataServerManager *dsm(new SALOMESDS::DataServerManager(&cPyh,orb,root_poa));
124       dsm->_remove_ref();
125       //
126       SALOME_ExternalServerLauncher *esm(new SALOME_ExternalServerLauncher(&cPyh,orb,safePOA));
127       esm->_remove_ref();
128       //
129       orb->run();
130       orb->destroy();
131     }
132   catch(CORBA::SystemException&){
133     MESSAGE("Caught CORBA::SystemException.");
134   }catch(PortableServer::POA::WrongPolicy&){
135     MESSAGE("Caught CORBA::WrongPolicyException.");
136   }catch(PortableServer::POA::ServantAlreadyActive&){
137     MESSAGE("Caught CORBA::ServantAlreadyActiveException");
138   }catch(CORBA::Exception&){
139     MESSAGE("Caught CORBA::Exception.");
140   }catch(std::exception& exc){
141     MESSAGE("Caught std::exception - "<<exc.what()); 
142   }catch(...){
143     MESSAGE("Caught unknown exception.");
144   }
145   END_OF(argv[0]);
146   //  delete myThreadTrace;
147 }
148