1 // Copyright (C) 2006-2023 CEA, EDF
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
28 #include "YacsTrace.hxx"
30 #include <SALOMEconfig.h>
31 #include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService)
33 static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr,const char*);
35 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
39 CORBA::TypeCode_var tc = tmp.type();
40 const char *p = tc->name();
50 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
55 CORBA::Long echoLong(CORBA::Long i);
58 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
63 CORBA::Long echoLong(CORBA::Long i);
66 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
71 CORBA::Long echoLong(CORBA::Long i);
72 CORBA::Long echoLong2(CORBA::Long i);
75 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
80 CORBA::Long echoLong(CORBA::Long i);
81 CORBA::Long echoLong2(CORBA::Long i);
84 class Echo_i : public POA_eo::Echo,
85 public PortableServer::RefCountServantBase
90 virtual char* echoString(const char* mesg);
91 CORBA::Long echoLong(CORBA::Long i);
92 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
93 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
94 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
95 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
96 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
97 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
98 void echoObjVec(const eo::ObjVec&, eo::ObjVec_out);
99 void echoObj2(eo::Obj_ptr , eo::Obj_out);
100 void echoD(eo::D_ptr , eo::D_out);
101 void echoC(eo::C_ptr , eo::C_out);
102 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
104 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
105 void createObj(CORBA::Long i, eo::Obj_out oo);
106 void createC(eo::C_out oo);
107 void echoAll(CORBA::Double d,CORBA::Long l,const char * m,eo::Obj_ptr o,CORBA::Double& dd,CORBA::Long& ll,CORBA::String_out s,eo::Obj_out oo);
108 virtual PortableServer::POA_ptr _default_POA();
111 //Implementation Echo
112 PortableServer::POA_ptr Echo_i::_default_POA()
114 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
116 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
119 //return PortableServer::POA::_duplicate(root_poa);
120 return root_poa._retn();
124 char* Echo_i::echoString(const char* mesg)
126 DEBTRACE("Echo_i::echoString " << mesg);
127 return CORBA::string_dup(mesg);
130 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
131 DEBTRACE("Echo_i::echoDouble " << i);
135 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
137 DEBTRACE("Echo_i::echoIntVec " << in.length());
138 for(int i=0;i<in.length(); i++){
141 out=new eo::IntVec(in);
144 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
146 DEBTRACE("Echo_i::echoStrVec " << in.length());
147 for(int i=0;i<in.length(); i++){
150 out=new eo::StrVec(in);
153 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
155 DEBTRACE("Echo_i::echoObjectVec " << in.length());
156 for(int i=0;i<in.length(); i++){
157 DEBTRACE(in[i]->_PD_repoId);
159 out=new eo::ObjectVec(in);
162 void Echo_i::echoObjVec(const eo::ObjVec& in, eo::ObjVec_out out)
164 DEBTRACE("Echo_i::echoObjVec " << in.length());
165 for(int i=0;i<in.length(); i++)
167 DEBTRACE(in[i]->_mostDerivedRepoId());
173 Obj_i* obj0 = new Obj_i();
174 CORBA::Object_var ref0 = obj0->_this();
175 eo::Obj_var o0 = eo::Obj::_narrow(ref0);
179 C_i* obj1 = new C_i();
180 CORBA::Object_var ref1 = obj1->_this();
181 eo::Obj_var o1 = eo::Obj::_narrow(ref1);
185 DEBTRACE("ENDOF Echo_i::echoObjVec " );
188 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
190 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
191 for(int i=0;i< in.length(); i++){
192 for(int j=0;j< in[i].length(); j++){
193 DEBTRACE(in[i][j]->_PD_repoId);
196 out=new eo::ObjectVecVec(in);
199 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
201 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
202 for(int i=0;i<in.length(); i++){
205 out=new eo::DoubleVec(in);
208 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
210 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
211 for(int i=0;i< in.length(); i++){
212 for(int j=0;j< in[i].length(); j++){
216 out=new eo::DoubleVecVec(in);
219 CORBA::Long Echo_i::echoLong(CORBA::Long i )
221 DEBTRACE("Echo_i::echoLong " << i);
223 eo::ExceptionStruct es;
225 es.text = "error Socket exception";
226 throw eo::SALOME_Exception(es);
233 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
234 DEBTRACE("Echo_i::echoC ");
236 oo=eo::C::_duplicate(o);
239 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
240 DEBTRACE("Echo_i::echoD ");
242 //oo=eo::D::_duplicate(o);
243 D_i* myD = new D_i();
248 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
249 DEBTRACE("Echo_i::echoObj2 ");
251 oo=eo::Obj::_duplicate(o);
254 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
255 DEBTRACE("Echo_i::echoObj " << i << "," << j );
256 oo=eo::Obj::_duplicate(o);
257 return eo::Obj::_duplicate(o);
260 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
261 DEBTRACE("Echo_i::createObj " << i);
262 Obj_i* myobj = new Obj_i();
263 CORBA::Object_var myref = myobj->_this();
264 oo = eo::Obj::_narrow(myref);
265 myobj->_remove_ref();
268 void Echo_i::createC(eo::C_out oo){
269 DEBTRACE("Echo_i::createC ");
270 C_i* myobj = new C_i();
271 CORBA::Object_var myref = myobj->_this();
272 oo = eo::C::_narrow(myref);
273 myobj->_remove_ref();
276 void Echo_i::echoAll(CORBA::Double d,CORBA::Long l,const char * m,eo::Obj_ptr o,CORBA::Double& dd,CORBA::Long& ll,CORBA::String_out s,eo::Obj_out oo)
278 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
281 s=CORBA::string_dup(m);
282 oo=eo::Obj::_duplicate(o);
286 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
287 DEBTRACE("Obj_i::echoLong " << i );
293 CORBA::Long C_i::echoLong(CORBA::Long i ){
294 DEBTRACE("C_i::echoLong " << i);
300 CORBA::Long D_i::echoLong2(CORBA::Long i ){
301 DEBTRACE("D_i::echoLong " << i);
305 CORBA::Long D_i::echoLong(CORBA::Long i ){
306 DEBTRACE("D_i::echoLong " << i);
312 CORBA::Long E_i::echoLong2(CORBA::Long i ){
313 DEBTRACE("E_i::echoLong " << i);
317 CORBA::Long E_i::echoLong(CORBA::Long i ){
318 DEBTRACE("E_i::echoLong " << i);
324 eo::Echo_var myechoref;
326 int main(int argc, char** argv)
329 orb = CORBA::ORB_init(argc, argv);
331 std::string theIOR( argv[1] );
333 CORBA::Object_var obj = orb->string_to_object(theIOR.c_str());
334 Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj );
335 if( CORBA::is_nil( ns ) )
337 //std::cout << getpid() << std::endl;
341 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
342 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
344 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
347 // Create a new POA with the shortcut policy
348 CORBA::PolicyList pl2;
351 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
352 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
353 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
354 PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
356 // Create and activate servant
357 Echo_i* myecho = new Echo_i();
358 // Obtain a reference to the object, and print it out as a
360 obj = myecho->_this();
361 CORBA::String_var sior(orb->object_to_string(obj));
362 DEBTRACE("'" << (char*)sior << "'");
363 myechoref = eo::Echo::_narrow(obj);
365 if( !bindObjectToName(ns, myechoref,"Echo") ) return 1;
367 // Decrement the reference count of the object implementation, so
368 // that it will be properly cleaned up when the POA has determined
369 // that it is no longer needed.
370 myecho->_remove_ref();
372 //create object C and register it in naming service
373 C_i* myC = new C_i();
375 eo::C_var myCref=eo::C::_narrow(obj);
377 if( !bindObjectToName(ns, myCref,"C") ) return 1;
379 //create object D and register it in naming service
380 D_i* myD = new D_i();
382 eo::D_var myDref=eo::D::_narrow(obj);
384 if( !bindObjectToName(ns, myDref,"D") ) return 1;
386 //create object Obj and register it in naming service
387 Obj_i* myObj = new Obj_i();
389 eo::Obj_var myObjref=eo::Obj::_narrow(obj);
390 myObj->_remove_ref();
391 if( !bindObjectToName(ns, myObjref,"Obj") ) return 1;
395 catch(CORBA::SystemException&) {
396 DEBTRACE("Caught CORBA::SystemException.");
398 catch(CORBA::Exception& ex) {
399 DEBTRACE("Caught CORBA::Exception." << ex);
401 catch(omniORB::fatalException& fe) {
402 DEBTRACE("Caught omniORB::fatalException:");
403 DEBTRACE(" file: " << fe.file());
404 DEBTRACE(" line: " << fe.line());
405 DEBTRACE(" mesg: " << fe.errmsg());
408 DEBTRACE("Caught unknown exception." );
415 //////////////////////////////////////////////////////////////////////
417 static CORBA::Boolean
418 bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name)
420 CORBA::String_var iorNSUg = orb->object_to_string(objref);
421 std::string iorNS(iorNSUg);
422 Engines::IORType iorInput;
423 auto len = iorNS.length();
424 iorInput.length( len );
425 for(auto i = 0 ; i < len ; ++i)
426 iorInput[i] = iorNS[i];
427 ns->Register(iorInput,name);