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