Salome HOME
updated copyright message
[modules/gui.git] / src / Session / Session_NS_wrapper.cxx
1 // Copyright (C) 2021-2023  CEA/DEN, EDF R&D, 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
20 #include "Session_NS_wrapper.hxx"
21
22 #include "ArgvKeeper.hxx"
23 #include "OpUtil.hxx"
24 #include "SALOME_Fake_NamingService.hxx"
25 #include "SALOME_Container_i.hxx"
26 #include "SALOME_Launcher.hxx"
27 #include "SALOMEDSClient_ClientFactory.hxx"
28
29 #include "Session_Session_i.hxx"
30 #include "Session_Promises.hxx"
31 #include "utilities.h"
32
33 const char OldStyleNS::LibName[]="SalomeApp";
34 const char NewStyleNS::LibName[]="SalomeAppSL";
35
36 void CommonActivateSession(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex *GUIMutex, QWaitCondition *GUILauncher, SALOME_NamingService_Abstract *ns, int argc, char ** argv)
37 {
38   try {
39     MESSAGE("Session thread started");
40     SALOME_Session_i * mySALOME_Session = nullptr;
41     if(!ns)
42       mySALOME_Session = new SALOME_Session_i(argc, argv, orb, poa, GUIMutex, GUILauncher);
43     else
44       mySALOME_Session = new SALOME_Session_i(argc, argv, orb, poa, GUIMutex, GUILauncher,ns);
45     PortableServer::ObjectId_var mySALOME_Sessionid = poa->activate_object(mySALOME_Session);
46     MESSAGE("poa->activate_object(mySALOME_Session)");
47     
48     CORBA::Object_var obj = mySALOME_Session->_this();
49     SALOME::Session_var objC = SALOME::Session::_narrow(obj);
50     GetSessionRefSingleton()->set_value(objC);
51     CORBA::String_var sior(orb->object_to_string(obj));
52     mySALOME_Session->_remove_ref();
53     
54     mySALOME_Session->NSregister();
55   }
56   catch (CORBA::SystemException&) {
57     INFOS("Caught CORBA::SystemException.");
58   }
59   catch (CORBA::Exception&) {
60     INFOS("Caught CORBA::Exception.");
61   }
62   catch (...) {
63     INFOS("Caught unknown exception.");
64   }
65 }
66
67 Abstract_Engines_Container_i *OldStyleNS::activateContainer(CORBA::ORB_var orb, PortableServer::POA_var poa, int argc, char **argv)
68 {
69   Engines_Container_i *_container = nullptr;
70   try
71   {
72     MESSAGE("Container thread started");
73
74     // get or create the child POA
75
76     PortableServer::POA_var factory_poa;
77     try
78     {
79       factory_poa = poa->find_POA("factory_poa", 0);
80       // 0 = no activation (already done if exists)
81     }
82     catch (PortableServer::POA::AdapterNonExistent &)
83     {
84       MESSAGE("factory_poa does not exists, create...");
85       // define policy objects
86       PortableServer::ImplicitActivationPolicy_var implicitActivation =
87           poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
88       // default = NO_IMPLICIT_ACTIVATION
89       PortableServer::ThreadPolicy_var threadPolicy =
90           poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
91       // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
92
93       // create policy list
94       CORBA::PolicyList policyList;
95       policyList.length(2);
96       policyList[0] = PortableServer::ImplicitActivationPolicy::
97           _duplicate(implicitActivation);
98       policyList[1] = PortableServer::ThreadPolicy::
99           _duplicate(threadPolicy);
100
101       PortableServer::POAManager_var nil_mgr = PortableServer::POAManager::_nil();
102       factory_poa = poa->create_POA("factory_poa",
103                                     nil_mgr,
104                                     policyList);
105       //with nil_mgr instead of pman,
106       //a new POA manager is created with the new POA
107
108       // destroy policy objects
109       implicitActivation->destroy();
110       threadPolicy->destroy();
111
112       // obtain the factory poa manager
113       PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
114       pmanfac->activate();
115       MESSAGE("pmanfac->activate()");
116     }
117
118     char *containerName = (char *)"";
119     if (argc > 1)
120     {
121       containerName = argv[1];
122     }
123     _container = new Engines_Container_i(orb, poa, containerName, argc, argv, nullptr, false);
124   }
125   catch (CORBA::SystemException &)
126   {
127     INFOS("Caught CORBA::SystemException.");
128   }
129   catch (PortableServer::POA::WrongPolicy &)
130   {
131     INFOS("Caught CORBA::WrongPolicyException.");
132   }
133   catch (PortableServer::POA::ServantAlreadyActive &)
134   {
135     INFOS("Caught CORBA::ServantAlreadyActiveException");
136   }
137   catch (CORBA::Exception &)
138   {
139     INFOS("Caught CORBA::Exception.");
140   }
141   catch (...)
142   {
143     INFOS("Caught unknown exception.");
144   }
145   return _container;
146 }
147
148 void OldStyleNS::activateContainerManager(CORBA::ORB_var orb)
149 {
150   try {
151     PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
152     std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
153     new SALOME_Launcher(orb,root_poa);
154   }
155   catch(CORBA::SystemException&) {
156     INFOS("Caught CORBA::SystemException.");
157   }
158   catch(PortableServer::POA::WrongPolicy&) {
159     INFOS("Caught CORBA::WrongPolicyException.");
160   }
161   catch(PortableServer::POA::ServantAlreadyActive&) {
162     INFOS("Caught CORBA::ServantAlreadyActiveException");
163   }
164   catch(CORBA::Exception&) {
165     INFOS("Caught CORBA::Exception.");
166   }
167   catch(...) {
168     INFOS("Caught unknown exception.");
169   }
170 }
171
172 void OldStyleNS::activateSession(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex *GUIMutex, QWaitCondition *GUILauncher, int argc, char ** argv)
173 {
174   CommonActivateSession(orb,poa,GUIMutex,GUILauncher,nullptr,argc,argv);
175 }
176
177 void OldStyleNS::activateSALOMEDS(CORBA::ORB_var orb, PortableServer::POA_var poa)
178 {
179   try {
180     MESSAGE("SALOMEDS thread started");
181     // We allocate the objects on the heap.  Since these are reference
182     // counted objects, they will be deleted by the POA when they are no
183     // longer needed.    
184     
185     ClientFactory::createStudy(orb,poa);
186   }
187   catch(CORBA::SystemException&) {
188     INFOS( "Caught CORBA::SystemException." );
189   }
190   catch(CORBA::Exception&) {
191     INFOS( "Caught CORBA::Exception." );
192   }
193   catch(omniORB::fatalException& fe) {
194     INFOS( "Caught omniORB::fatalException:" );
195     INFOS( "  file: " << fe.file() );
196     INFOS( "  line: " << fe.line() );
197     INFOS( "  mesg: " << fe.errmsg() );
198   }
199   catch(...) {
200     INFOS( "Caught unknown exception." );
201   }
202 }
203
204 #include "Utils_SINGLETON.hxx"
205
206 CORBA::Object_var OldStyleNS::forServerChecker(const char *NSName, int argc, char **argv)
207 {
208   SetArgcArgv( argc, argv );
209   CORBA::ORB_var orb = KERNEL::GetRefToORB();
210   SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
211   ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
212   NS.init_orb( orb );
213   CORBA::Object_var obj = NS.Resolve( NSName );
214   return obj;
215 }
216
217 CosNaming::NamingContext_var OldStyleNS::checkTrueNamingServiceIfExpected(int argc, char **argv, bool& forceOK)
218 {
219   forceOK = false;//tell checker : do as before
220   SetArgcArgv( argc, argv );
221   CORBA::ORB_var orb = KERNEL::GetRefToORB();
222   CORBA::Object_var obj = orb->resolve_initial_references( "NameService" );
223   CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow( obj );
224   return _root_context;
225 }
226
227 #include "SALOME_KernelServices.hxx"
228
229 void NewStyleNS::defineDefaultSALOMEKERNELNamingService()
230 {
231   KERNEL::assignNamingServiceSL();
232 }
233
234 Abstract_Engines_Container_i *NewStyleNS::activateContainer(CORBA::ORB_var orb, PortableServer::POA_var poa, int argc, char **argv)
235 {
236   return KERNEL::getContainerSA();
237 }
238
239 void NewStyleNS::activateContainerManager(CORBA::ORB_var orb)
240 {
241   KERNEL::getLauncherSA();
242 }
243
244 void NewStyleNS::activateSession(CORBA::ORB_var orb, PortableServer::POA_var poa, QMutex *GUIMutex, QWaitCondition *GUILauncher, int argc, char ** argv)
245 {
246   SALOME_Fake_NamingService *ns=new SALOME_Fake_NamingService;
247   CommonActivateSession(orb,poa,GUIMutex,GUILauncher,ns,argc,argv);
248 }
249
250 void NewStyleNS::activateSALOMEDS(CORBA::ORB_var orb, PortableServer::POA_var poa)
251 {
252   ClientFactory::createStudyWithoutNS(orb,poa);
253 }
254
255 CORBA::Object_var NewStyleNS::forServerChecker(const char *NSName, int argc, char **argv)
256 {
257   SALOME_Fake_NamingService ns;
258   return ns.Resolve(NSName);
259 }
260
261 CosNaming::NamingContext_var NewStyleNS::checkTrueNamingServiceIfExpected(int argc, char **argv, bool& forceOK)
262 {
263   forceOK = true;//tell checker : forget it's always OK
264   return CosNaming::NamingContext::_nil();
265 }