1 // Copyright (C) 2006-2024 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
35 #include "YacsTrace.hxx"
37 #include <SALOMEconfig.h>
38 #include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService)
43 static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr,const char*);
45 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
49 CORBA::TypeCode_var tc = tmp.type();
50 const char *p = tc->name();
60 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
65 CORBA::Long echoLong(CORBA::Long i);
68 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
73 CORBA::Long echoLong(CORBA::Long i);
76 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
81 CORBA::Long echoLong(CORBA::Long i);
82 CORBA::Long echoLong2(CORBA::Long i);
85 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
90 CORBA::Long echoLong(CORBA::Long i);
91 CORBA::Long echoLong2(CORBA::Long i);
94 class Echo_i : public POA_eo::Echo,
95 public PortableServer::RefCountServantBase
100 virtual char* echoString(const char* mesg);
101 virtual CORBA::Boolean echoBoolean(CORBA::Boolean b);
102 CORBA::Long echoLong(CORBA::Long i);
103 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
104 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
105 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
106 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
107 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
108 void echoBoolVec(const eo::BoolVec&, eo::BoolVec_out);
109 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
110 void echoObj2(eo::Obj_ptr , eo::Obj_out);
111 void echoD(eo::D_ptr , eo::D_out);
112 void echoC(eo::C_ptr , eo::C_out);
113 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
115 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
116 void createObj(CORBA::Long i, eo::Obj_out oo);
117 void createC(eo::C_out oo);
118 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);
119 void sleepLong(CORBA::Double time1,CORBA::Double& time2) ;
120 virtual eo::S2* echoStruct(const eo::S2&);
121 virtual eo::S3* echoStruct2(const eo::S3&);
122 virtual void shutdown();
123 virtual PortableServer::POA_ptr _default_POA();
126 pthread_mutex_t _mutex;
129 //Implementation Echo
130 PortableServer::POA_ptr Echo_i::_default_POA()
133 pthread_mutex_init(&_mutex, NULL);
134 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
136 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
139 //return PortableServer::POA::_duplicate(root_poa);
140 return root_poa._retn();
145 void Echo_i::shutdown()
147 // Shutdown the ORB (but do not wait for completion). This also
148 // causes the main thread to unblock from CORBA::ORB::run().
152 char* Echo_i::echoString(const char* mesg)
154 DEBTRACE("Echo_i::echoString " << mesg);
155 return CORBA::string_dup(mesg);
158 CORBA::Boolean Echo_i::echoBoolean(CORBA::Boolean b )
160 DEBTRACE("Echo_i::echoBoolean " << b);
164 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
165 DEBTRACE("Echo_i::echoDouble " << i);
169 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
171 DEBTRACE("Echo_i::echoIntVec " << in.length());
172 for(int i=0;i<in.length(); i++){
175 out=new eo::IntVec(in);
178 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
180 DEBTRACE("Echo_i::echoStrVec " << in.length());
181 for(int i=0;i<in.length(); i++){
184 out=new eo::StrVec(in);
187 void Echo_i::echoBoolVec(const eo::BoolVec& in, eo::BoolVec_out out)
189 DEBTRACE("Echo_i::echoBoolVec " << in.length());
190 for(int i=0;i<in.length(); i++){
193 out=new eo::BoolVec(in);
196 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
198 DEBTRACE("Echo_i::echoObjectVec " << in.length());
199 for(int i=0;i<in.length(); i++){
200 DEBTRACE(in[i]->_PD_repoId);
202 out=new eo::ObjectVec(in);
205 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
207 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
208 for(int i=0;i< in.length(); i++){
209 for(int j=0;j< in[i].length(); j++){
210 DEBTRACE(in[i][j]->_PD_repoId);
213 out=new eo::ObjectVecVec(in);
216 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
218 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
219 for(int i=0;i<in.length(); i++){
222 out=new eo::DoubleVec(in);
225 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
227 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
228 for(int i=0;i< in.length(); i++){
229 for(int j=0;j< in[i].length(); j++){
233 out=new eo::DoubleVecVec(in);
236 CORBA::Long Echo_i::echoLong(CORBA::Long i )
238 DEBTRACE("Echo_i::echoLong " << i);
240 eo::ExceptionStruct es;
242 es.text = "error Socket exception";
243 throw eo::SALOME_Exception(es);
250 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
251 DEBTRACE("Echo_i::echoC ");
253 oo=eo::C::_duplicate(o);
256 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
257 DEBTRACE("Echo_i::echoD ");
259 //oo=eo::D::_duplicate(o);
260 D_i* myD = new D_i();
265 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
266 DEBTRACE("Echo_i::echoObj2 ");
268 oo=eo::Obj::_duplicate(o);
271 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
272 DEBTRACE("Echo_i::echoObj " << i << "," << j );
273 oo=eo::Obj::_duplicate(o);
274 return eo::Obj::_duplicate(o);
277 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
278 DEBTRACE("Echo_i::createObj " << i);
279 Obj_i* myobj = new Obj_i();
280 CORBA::Object_var myref = myobj->_this();
281 oo = eo::Obj::_narrow(myref);
282 myobj->_remove_ref();
285 void Echo_i::createC(eo::C_out oo){
286 DEBTRACE("Echo_i::createC ");
287 C_i* myobj = new C_i();
288 CORBA::Object_var myref = myobj->_this();
289 oo = eo::C::_narrow(myref);
290 myobj->_remove_ref();
293 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)
295 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
298 s=CORBA::string_dup(m);
299 oo=eo::Obj::_duplicate(o);
302 void Echo_i::sleepLong(CORBA::Double time1, CORBA::Double& time2)
304 DEBTRACE("Echo_i::sleepLong");
305 pthread_mutex_lock(&_mutex);
307 pthread_mutex_unlock(&_mutex);
308 DEBTRACE("Echo_i::sleepLong start " << num);
309 unsigned int t=(unsigned int) time1;
311 DEBTRACE("Echo_i::sleepLong stop " << num);
315 eo::S2* Echo_i::echoStruct(const eo::S2& s)
317 DEBTRACE("Echo_i::echoStruct " << s.s.x << " " << s.s.y);
321 eo::S2* s2=new eo::S2;
326 eo::S3* Echo_i::echoStruct2(const eo::S3& s)
328 DEBTRACE("Echo_i::echoStruct " << s.x << " " << s.y);
329 if( !CORBA::is_nil(s.ob) )
331 std::cerr << s.ob->echoLong(10) << std::endl;
333 eo::S3* s2=new eo::S3;
340 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
341 DEBTRACE("Obj_i::echoLong " << i );
347 CORBA::Long C_i::echoLong(CORBA::Long i ){
348 DEBTRACE("C_i::echoLong " << i);
354 CORBA::Long D_i::echoLong2(CORBA::Long i ){
355 DEBTRACE("D_i::echoLong " << i);
359 CORBA::Long D_i::echoLong(CORBA::Long i ){
360 DEBTRACE("D_i::echoLong " << i);
366 CORBA::Long E_i::echoLong2(CORBA::Long i ){
367 DEBTRACE("E_i::echoLong " << i);
371 CORBA::Long E_i::echoLong(CORBA::Long i ){
372 DEBTRACE("E_i::echoLong " << i);
377 eo::Echo_var myechoref;
379 int main(int argc, char** argv)
382 orb = CORBA::ORB_init(argc, argv);
384 std::string theIOR( argv[1] );
386 CORBA::Object_var obj = orb->string_to_object(theIOR.c_str());
387 Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj );
388 if( CORBA::is_nil( ns ) )
390 //std::cout << getpid() << std::endl;
394 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
395 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
397 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
400 // Create a new POA with the shortcut policy
402 CORBA::PolicyList pl2;
405 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
406 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
407 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
408 PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
411 // Create and activate servant
412 Echo_i* myecho = new Echo_i();
413 // Obtain a reference to the object
414 CORBA::Object_var obj2 = myecho->_this();
415 myechoref = eo::Echo::_narrow(obj2);
416 // Decrement the reference count of the object implementation, so
417 // that it will be properly cleaned up when the POA has determined
418 // that it is no longer needed.
419 myecho->_remove_ref();
421 // print the reference as a stringified IOR.
422 CORBA::String_var sior(orb->object_to_string(obj2));
423 DEBTRACE("'" << (char*)sior << "'");
424 if( !bindObjectToName(ns, myechoref,"Echo") ) return 1;
426 //create object C and register it in naming service
427 C_i* myC = new C_i();
428 CORBA::Object_var obj3 =myC->_this();
429 eo::C_var myCref=eo::C::_narrow(obj3);
431 if( !bindObjectToName(ns, myCref,"C") ) return 1;
433 //create object D and register it in naming service
434 D_i* myD = new D_i();
435 CORBA::Object_var obj4=myD->_this();
436 eo::D_var myDref=eo::D::_narrow(obj4);
438 if( !bindObjectToName(ns, myDref,"D") ) return 1;
440 //create object Obj and register it in naming service
441 Obj_i* myObj = new Obj_i();
442 CORBA::Object_var obj5=myObj->_this();
443 eo::Obj_var myObjref=eo::Obj::_narrow(obj5);
444 myObj->_remove_ref();
445 if( !bindObjectToName(ns, myObjref,"Obj") ) return 1;
448 std::cout << "Returned from orb->run()." << std::endl;
451 catch(CORBA::SystemException&) {
452 DEBTRACE("Caught CORBA::SystemException.");
454 catch(CORBA::Exception& ex) {
455 DEBTRACE("Caught CORBA::Exception." << ex);
457 catch(omniORB::fatalException& fe) {
458 DEBTRACE("Caught omniORB::fatalException:");
459 DEBTRACE(" file: " << fe.file());
460 DEBTRACE(" line: " << fe.line());
461 DEBTRACE(" mesg: " << fe.errmsg());
464 DEBTRACE("Caught unknown exception." );
471 //////////////////////////////////////////////////////////////////////
473 static CORBA::Boolean
474 bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name)
476 CORBA::String_var iorNSUg = orb->object_to_string(objref);
477 std::string iorNS(iorNSUg);
478 Engines::IORType iorInput;
479 auto len = iorNS.length();
480 iorInput.length( len );
481 for(auto i = 0 ; i < len ; ++i)
482 iorInput[i] = iorNS[i];
483 ns->Register(iorInput,name);