1 // Copyright (C) 2006-2021 CEA/DEN, EDF R&D
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
34 #include "YacsTrace.hxx"
39 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
41 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
45 CORBA::TypeCode_var tc = tmp.type();
46 const char *p = tc->name();
56 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
61 CORBA::Long echoLong(CORBA::Long i);
64 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
69 CORBA::Long echoLong(CORBA::Long i);
72 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
77 CORBA::Long echoLong(CORBA::Long i);
78 CORBA::Long echoLong2(CORBA::Long i);
81 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
86 CORBA::Long echoLong(CORBA::Long i);
87 CORBA::Long echoLong2(CORBA::Long i);
90 class Echo_i : public POA_eo::Echo,
91 public PortableServer::RefCountServantBase
96 virtual char* echoString(const char* mesg);
97 virtual CORBA::Boolean echoBoolean(CORBA::Boolean b);
98 CORBA::Long echoLong(CORBA::Long i);
99 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
100 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
101 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
102 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
103 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
104 void echoBoolVec(const eo::BoolVec&, eo::BoolVec_out);
105 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
106 void echoObj2(eo::Obj_ptr , eo::Obj_out);
107 void echoD(eo::D_ptr , eo::D_out);
108 void echoC(eo::C_ptr , eo::C_out);
109 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
111 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
112 void createObj(CORBA::Long i, eo::Obj_out oo);
113 void createC(eo::C_out oo);
114 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);
115 void sleepLong(CORBA::Double time1,CORBA::Double& time2) ;
116 virtual eo::S2* echoStruct(const eo::S2&);
117 virtual eo::S3* echoStruct2(const eo::S3&);
118 virtual void shutdown();
119 virtual PortableServer::POA_ptr _default_POA();
122 pthread_mutex_t _mutex;
125 //Implementation Echo
126 PortableServer::POA_ptr Echo_i::_default_POA()
129 pthread_mutex_init(&_mutex, NULL);
130 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
132 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
135 //return PortableServer::POA::_duplicate(root_poa);
136 return root_poa._retn();
141 void Echo_i::shutdown()
143 // Shutdown the ORB (but do not wait for completion). This also
144 // causes the main thread to unblock from CORBA::ORB::run().
148 char* Echo_i::echoString(const char* mesg)
150 DEBTRACE("Echo_i::echoString " << mesg);
151 return CORBA::string_dup(mesg);
154 CORBA::Boolean Echo_i::echoBoolean(CORBA::Boolean b )
156 DEBTRACE("Echo_i::echoBoolean " << b);
160 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
161 DEBTRACE("Echo_i::echoDouble " << i);
165 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
167 DEBTRACE("Echo_i::echoIntVec " << in.length());
168 for(int i=0;i<in.length(); i++){
171 out=new eo::IntVec(in);
174 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
176 DEBTRACE("Echo_i::echoStrVec " << in.length());
177 for(int i=0;i<in.length(); i++){
180 out=new eo::StrVec(in);
183 void Echo_i::echoBoolVec(const eo::BoolVec& in, eo::BoolVec_out out)
185 DEBTRACE("Echo_i::echoBoolVec " << in.length());
186 for(int i=0;i<in.length(); i++){
189 out=new eo::BoolVec(in);
192 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
194 DEBTRACE("Echo_i::echoObjectVec " << in.length());
195 for(int i=0;i<in.length(); i++){
196 DEBTRACE(in[i]->_PD_repoId);
198 out=new eo::ObjectVec(in);
201 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
203 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
204 for(int i=0;i< in.length(); i++){
205 for(int j=0;j< in[i].length(); j++){
206 DEBTRACE(in[i][j]->_PD_repoId);
209 out=new eo::ObjectVecVec(in);
212 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
214 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
215 for(int i=0;i<in.length(); i++){
218 out=new eo::DoubleVec(in);
221 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
223 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
224 for(int i=0;i< in.length(); i++){
225 for(int j=0;j< in[i].length(); j++){
229 out=new eo::DoubleVecVec(in);
232 CORBA::Long Echo_i::echoLong(CORBA::Long i )
234 DEBTRACE("Echo_i::echoLong " << i);
236 eo::ExceptionStruct es;
238 es.text = "error Socket exception";
239 throw eo::SALOME_Exception(es);
246 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
247 DEBTRACE("Echo_i::echoC ");
249 oo=eo::C::_duplicate(o);
252 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
253 DEBTRACE("Echo_i::echoD ");
255 //oo=eo::D::_duplicate(o);
256 D_i* myD = new D_i();
261 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
262 DEBTRACE("Echo_i::echoObj2 ");
264 oo=eo::Obj::_duplicate(o);
267 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
268 DEBTRACE("Echo_i::echoObj " << i << "," << j );
269 oo=eo::Obj::_duplicate(o);
270 return eo::Obj::_duplicate(o);
273 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
274 DEBTRACE("Echo_i::createObj " << i);
275 Obj_i* myobj = new Obj_i();
276 CORBA::Object_var myref = myobj->_this();
277 oo = eo::Obj::_narrow(myref);
278 myobj->_remove_ref();
281 void Echo_i::createC(eo::C_out oo){
282 DEBTRACE("Echo_i::createC ");
283 C_i* myobj = new C_i();
284 CORBA::Object_var myref = myobj->_this();
285 oo = eo::C::_narrow(myref);
286 myobj->_remove_ref();
289 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)
291 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
294 s=CORBA::string_dup(m);
295 oo=eo::Obj::_duplicate(o);
298 void Echo_i::sleepLong(CORBA::Double time1, CORBA::Double& time2)
300 DEBTRACE("Echo_i::sleepLong");
301 pthread_mutex_lock(&_mutex);
303 pthread_mutex_unlock(&_mutex);
304 DEBTRACE("Echo_i::sleepLong start " << num);
305 unsigned int t=(unsigned int) time1;
307 DEBTRACE("Echo_i::sleepLong stop " << num);
311 eo::S2* Echo_i::echoStruct(const eo::S2& s)
313 DEBTRACE("Echo_i::echoStruct " << s.s.x << " " << s.s.y);
317 eo::S2* s2=new eo::S2;
322 eo::S3* Echo_i::echoStruct2(const eo::S3& s)
324 DEBTRACE("Echo_i::echoStruct " << s.x << " " << s.y);
325 if( !CORBA::is_nil(s.ob) )
327 std::cerr << s.ob->echoLong(10) << std::endl;
329 eo::S3* s2=new eo::S3;
336 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
337 DEBTRACE("Obj_i::echoLong " << i );
343 CORBA::Long C_i::echoLong(CORBA::Long i ){
344 DEBTRACE("C_i::echoLong " << i);
350 CORBA::Long D_i::echoLong2(CORBA::Long i ){
351 DEBTRACE("D_i::echoLong " << i);
355 CORBA::Long D_i::echoLong(CORBA::Long i ){
356 DEBTRACE("D_i::echoLong " << i);
362 CORBA::Long E_i::echoLong2(CORBA::Long i ){
363 DEBTRACE("E_i::echoLong " << i);
367 CORBA::Long E_i::echoLong(CORBA::Long i ){
368 DEBTRACE("E_i::echoLong " << i);
373 eo::Echo_var myechoref;
375 int main(int argc, char** argv)
378 orb = CORBA::ORB_init(argc, argv);
381 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
382 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
384 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
387 // Create a new POA with the shortcut policy
389 CORBA::PolicyList pl2;
392 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
393 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
394 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
395 PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
398 // Create and activate servant
399 Echo_i* myecho = new Echo_i();
400 // Obtain a reference to the object
401 CORBA::Object_var obj2 = myecho->_this();
402 myechoref = eo::Echo::_narrow(obj2);
403 // Decrement the reference count of the object implementation, so
404 // that it will be properly cleaned up when the POA has determined
405 // that it is no longer needed.
406 myecho->_remove_ref();
408 // print the reference as a stringified IOR.
409 CORBA::String_var sior(orb->object_to_string(obj2));
410 DEBTRACE("'" << (char*)sior << "'");
412 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
414 //create object C and register it in naming service
415 C_i* myC = new C_i();
416 CORBA::Object_var obj3 =myC->_this();
417 eo::C_var myCref=eo::C::_narrow(obj3);
419 if( !bindObjectToName(orb, myCref,"C") ) return 1;
421 //create object D and register it in naming service
422 D_i* myD = new D_i();
423 CORBA::Object_var obj4=myD->_this();
424 eo::D_var myDref=eo::D::_narrow(obj4);
426 if( !bindObjectToName(orb, myDref,"D") ) return 1;
428 //create object Obj and register it in naming service
429 Obj_i* myObj = new Obj_i();
430 CORBA::Object_var obj5=myObj->_this();
431 eo::Obj_var myObjref=eo::Obj::_narrow(obj5);
432 myObj->_remove_ref();
433 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
436 std::cout << "Returned from orb->run()." << std::endl;
439 catch(CORBA::SystemException&) {
440 DEBTRACE("Caught CORBA::SystemException.");
442 catch(CORBA::Exception& ex) {
443 DEBTRACE("Caught CORBA::Exception." << ex);
445 catch(omniORB::fatalException& fe) {
446 DEBTRACE("Caught omniORB::fatalException:");
447 DEBTRACE(" file: " << fe.file());
448 DEBTRACE(" line: " << fe.line());
449 DEBTRACE(" mesg: " << fe.errmsg());
452 DEBTRACE("Caught unknown exception." );
459 //////////////////////////////////////////////////////////////////////
461 static CORBA::Boolean
462 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
464 CosNaming::NamingContext_var rootContext;
467 // Obtain a reference to the root context of the Name service:
468 CORBA::Object_var obj;
469 obj = orb->resolve_initial_references("NameService");
471 // Narrow the reference returned.
472 rootContext = CosNaming::NamingContext::_narrow(obj);
473 if( CORBA::is_nil(rootContext) ) {
474 DEBTRACE("Failed to narrow the root naming context.");
478 catch(CORBA::ORB::InvalidName& ex) {
479 // This should not happen!
480 DEBTRACE("Service required is invalid [does not exist]." );
485 // Bind a context called "test" to the root context:
487 CosNaming::Name contextName;
488 contextName.length(1);
489 contextName[0].id = (const char*) "test"; // string copied
490 contextName[0].kind = (const char*) "my_context"; // string copied
491 // Note on kind: The kind field is used to indicate the type
492 // of the object. This is to avoid conventions such as that used
493 // by files (name.type -- e.g. test.ps = postscript etc.)
495 CosNaming::NamingContext_var testContext;
497 // Bind the context to root.
498 testContext = rootContext->bind_new_context(contextName);
500 catch(CosNaming::NamingContext::AlreadyBound& ex) {
501 // If the context already exists, this exception will be raised.
502 // In this case, just resolve the name and assign testContext
503 // to the object returned:
504 CORBA::Object_var obj;
505 obj = rootContext->resolve(contextName);
506 testContext = CosNaming::NamingContext::_narrow(obj);
507 if( CORBA::is_nil(testContext) ) {
508 DEBTRACE("Failed to narrow naming context.");
513 // Bind objref with name Echo to the testContext:
514 CosNaming::Name objectName;
515 objectName.length(1);
516 objectName[0].id = name; // string copied
517 objectName[0].kind = (const char*) "Object"; // string copied
520 testContext->bind(objectName, objref);
522 catch(CosNaming::NamingContext::AlreadyBound& ex) {
523 testContext->rebind(objectName, objref);
525 // Note: Using rebind() will overwrite any Object previously bound
526 // to /test/Echo with obj.
527 // Alternatively, bind() can be used, which will raise a
528 // CosNaming::NamingContext::AlreadyBound exception if the name
529 // supplied is already bound to an object.
531 // Amendment: When using OrbixNames, it is necessary to first try bind
532 // and then rebind, as rebind on it's own will throw a NotFoundexception if
533 // the Name has not already been bound. [This is incorrect behaviour -
534 // it should just bind].
536 catch(CORBA::COMM_FAILURE& ex) {
537 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
538 << "naming service.");
541 catch(CORBA::SystemException&) {
542 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");