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