Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMESDS / SALOME_DataScopeServer_Common.cxx
1 // Copyright (C) 2021-2023  CEA, EDF, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
21 #include "SALOMESDS_DataScopeServer.hxx"
22 #include "SALOMESDS_DataServerManager.hxx"
23 #include "SALOMESDS_Exception.hxx"
24 #include "SALOME_CPythonHelper.hxx"
25 #include "SALOME_NamingService_Abstract.hxx"
26 #include "ArgvKeeper.hxx"
27 #include "OpUtil.hxx"
28
29 #include <string>
30 #include <sstream>
31 #include <functional>
32 #include <vector>
33
34 CORBA::ORB_var GetCustomORB()
35 {
36   CORBA::ORB_var orb;
37   {
38     // by disabling supportCurrent it make the POAManager::hold_requests work !
39     std::vector<std::string> args = {"DTC", "-ORBsupportCurrent", "0"};
40     SetArgcArgv(args);
41     orb=KERNEL::GetRefToORB();
42   }
43   return orb;
44 }
45
46 int CommonDataScopeServerMain(int argc, char *argv[], CORBA::ORB_var orb, const std::string& scopeName, int isTransac, std::function<SALOME_NamingService_Container_Abstract *(CORBA::ORB_ptr)> nsGenrator)
47 {
48   CORBA::Object_var obj(orb->resolve_initial_references("RootPOA"));
49   PortableServer::POA_var poa(PortableServer::POA::_narrow(obj));
50   PortableServer::POAManager_var mgr(poa->the_POAManager());
51   mgr->activate();
52   //
53   SALOMESDS::DataScopeKiller *killer(new SALOMESDS::DataScopeKiller(orb));
54   SALOME::DataScopeKiller_var killerObj(killer->_this());
55   //
56   SALOME_CPythonHelper cPyHelper;
57   cPyHelper.initializePython(argc,argv);
58   //
59   SALOMESDS::DataScopeServerBase *server(nullptr);
60   if(!isTransac)
61     server=new SALOMESDS::DataScopeServer(&cPyHelper,orb,killerObj,scopeName,nsGenrator(orb));
62   else
63     server=new SALOMESDS::DataScopeServerTransaction(&cPyHelper,orb,killerObj,scopeName,nsGenrator(orb));
64   //
65   CORBA::PolicyList policies;
66   policies.length(3);
67   PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
68   policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol);
69   policies[1]=poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
70   policies[2]=poa->create_id_uniqueness_policy(PortableServer::UNIQUE_ID);
71   PortableServer::POA_var poa2(poa->create_POA("SingleThPOA4SDS",mgr,policies));
72   threadPol->destroy();
73   server->registerToSalomePiDict();
74   //
75   server->setPOA(poa2);
76   obj=server->activate();
77   SALOME::DataScopeServerBase_var serverPtr(SALOME::DataScopeServerBase::_narrow(obj));
78   server->registerInNS(serverPtr);
79   //
80   orb->run();
81   delete killer;
82   Py_Finalize();
83   return 0;
84 }