1 // Copyright (C) 2006-2008 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.
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"
33 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, 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 virtual CORBA::Boolean echoBoolean(CORBA::Boolean b);
92 CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
93 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
94 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
95 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
96 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
97 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
98 void echoBoolVec(const eo::BoolVec&, eo::BoolVec_out);
99 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
100 void echoObj2(eo::Obj_ptr , eo::Obj_out);
101 void echoD(eo::D_ptr , eo::D_out);
102 void echoC(eo::C_ptr , eo::C_out);
103 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
105 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
106 void createObj(CORBA::Long i, eo::Obj_out oo);
107 void createC(eo::C_out oo);
108 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);
109 void sleepLong(CORBA::Double time1,CORBA::Double& time2) ;
110 virtual eo::S2* echoStruct(const eo::S2&);
111 virtual eo::S3* echoStruct2(const eo::S3&);
112 virtual void shutdown();
113 virtual PortableServer::POA_ptr _default_POA();
116 pthread_mutex_t _mutex;
119 //Implementation Echo
120 PortableServer::POA_ptr Echo_i::_default_POA()
123 pthread_mutex_init(&_mutex, NULL);
124 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
126 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
129 //return PortableServer::POA::_duplicate(root_poa);
130 return root_poa._retn();
135 void Echo_i::shutdown()
137 // Shutdown the ORB (but do not wait for completion). This also
138 // causes the main thread to unblock from CORBA::ORB::run().
142 char* Echo_i::echoString(const char* mesg)
144 DEBTRACE("Echo_i::echoString " << mesg);
145 return CORBA::string_dup(mesg);
148 CORBA::Boolean Echo_i::echoBoolean(CORBA::Boolean b )
150 DEBTRACE("Echo_i::echoBoolean " << b);
154 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
155 DEBTRACE("Echo_i::echoDouble " << i);
159 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
161 DEBTRACE("Echo_i::echoIntVec " << in.length());
162 for(int i=0;i<in.length(); i++){
165 out=new eo::IntVec(in);
168 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
170 DEBTRACE("Echo_i::echoStrVec " << in.length());
171 for(int i=0;i<in.length(); i++){
174 out=new eo::StrVec(in);
177 void Echo_i::echoBoolVec(const eo::BoolVec& in, eo::BoolVec_out out)
179 DEBTRACE("Echo_i::echoBoolVec " << in.length());
180 for(int i=0;i<in.length(); i++){
183 out=new eo::BoolVec(in);
186 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
188 DEBTRACE("Echo_i::echoObjectVec " << in.length());
189 for(int i=0;i<in.length(); i++){
190 DEBTRACE(in[i]->_PD_repoId);
192 out=new eo::ObjectVec(in);
195 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
197 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
198 for(int i=0;i< in.length(); i++){
199 for(int j=0;j< in[i].length(); j++){
200 DEBTRACE(in[i][j]->_PD_repoId);
203 out=new eo::ObjectVecVec(in);
206 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
208 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
209 for(int i=0;i<in.length(); i++){
212 out=new eo::DoubleVec(in);
215 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
217 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
218 for(int i=0;i< in.length(); i++){
219 for(int j=0;j< in[i].length(); j++){
223 out=new eo::DoubleVecVec(in);
226 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception)
228 DEBTRACE("Echo_i::echoLong " << i);
230 eo::ExceptionStruct es;
232 es.text = "error Socket exception";
233 throw eo::SALOME_Exception(es);
240 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
241 DEBTRACE("Echo_i::echoC ");
243 oo=eo::C::_duplicate(o);
246 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
247 DEBTRACE("Echo_i::echoD ");
249 //oo=eo::D::_duplicate(o);
250 D_i* myD = new D_i();
255 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
256 DEBTRACE("Echo_i::echoObj2 ");
258 oo=eo::Obj::_duplicate(o);
261 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
262 DEBTRACE("Echo_i::echoObj " << i << "," << j );
263 oo=eo::Obj::_duplicate(o);
264 return eo::Obj::_duplicate(o);
267 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
268 DEBTRACE("Echo_i::createObj " << i);
269 Obj_i* myobj = new Obj_i();
270 CORBA::Object_var myref = myobj->_this();
271 oo = eo::Obj::_narrow(myref);
272 myobj->_remove_ref();
275 void Echo_i::createC(eo::C_out oo){
276 DEBTRACE("Echo_i::createC ");
277 C_i* myobj = new C_i();
278 CORBA::Object_var myref = myobj->_this();
279 oo = eo::C::_narrow(myref);
280 myobj->_remove_ref();
283 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)
285 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
288 s=CORBA::string_dup(m);
289 oo=eo::Obj::_duplicate(o);
292 void Echo_i::sleepLong(CORBA::Double time1, CORBA::Double& time2)
294 DEBTRACE("Echo_i::sleepLong");
295 pthread_mutex_lock(&_mutex);
297 pthread_mutex_unlock(&_mutex);
298 DEBTRACE("Echo_i::sleepLong start " << num);
299 unsigned int t=(unsigned int) time1;
301 DEBTRACE("Echo_i::sleepLong stop " << num);
305 eo::S2* Echo_i::echoStruct(const eo::S2& s)
307 DEBTRACE("Echo_i::echoStruct " << s.s.x << " " << s.s.y);
311 eo::S2* s2=new eo::S2;
316 eo::S3* Echo_i::echoStruct2(const eo::S3& s)
318 DEBTRACE("Echo_i::echoStruct " << s.x << " " << s.y);
319 if( !CORBA::is_nil(s.ob) )
321 std::cerr << s.ob->echoLong(10) << std::endl;
323 eo::S3* s2=new eo::S3;
330 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
331 DEBTRACE("Obj_i::echoLong " << i );
337 CORBA::Long C_i::echoLong(CORBA::Long i ){
338 DEBTRACE("C_i::echoLong " << i);
344 CORBA::Long D_i::echoLong2(CORBA::Long i ){
345 DEBTRACE("D_i::echoLong " << i);
349 CORBA::Long D_i::echoLong(CORBA::Long i ){
350 DEBTRACE("D_i::echoLong " << i);
356 CORBA::Long E_i::echoLong2(CORBA::Long i ){
357 DEBTRACE("E_i::echoLong " << i);
361 CORBA::Long E_i::echoLong(CORBA::Long i ){
362 DEBTRACE("E_i::echoLong " << i);
367 eo::Echo_var myechoref;
369 int main(int argc, char** argv)
372 orb = CORBA::ORB_init(argc, argv);
375 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
376 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
378 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
381 // Create a new POA with the shortcut policy
383 CORBA::PolicyList pl2;
386 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
387 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
388 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
389 PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
392 // Create and activate servant
393 Echo_i* myecho = new Echo_i();
394 // Obtain a reference to the object
395 CORBA::Object_var obj2 = myecho->_this();
396 myechoref = eo::Echo::_narrow(obj2);
397 // Decrement the reference count of the object implementation, so
398 // that it will be properly cleaned up when the POA has determined
399 // that it is no longer needed.
400 myecho->_remove_ref();
402 // print the reference as a stringified IOR.
403 CORBA::String_var sior(orb->object_to_string(obj2));
404 DEBTRACE("'" << (char*)sior << "'");
406 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
408 //create object C and register it in naming service
409 C_i* myC = new C_i();
410 CORBA::Object_var obj3 =myC->_this();
411 eo::C_var myCref=eo::C::_narrow(obj3);
413 if( !bindObjectToName(orb, myCref,"C") ) return 1;
415 //create object D and register it in naming service
416 D_i* myD = new D_i();
417 CORBA::Object_var obj4=myD->_this();
418 eo::D_var myDref=eo::D::_narrow(obj4);
420 if( !bindObjectToName(orb, myDref,"D") ) return 1;
422 //create object Obj and register it in naming service
423 Obj_i* myObj = new Obj_i();
424 CORBA::Object_var obj5=myObj->_this();
425 eo::Obj_var myObjref=eo::Obj::_narrow(obj5);
426 myObj->_remove_ref();
427 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
430 std::cout << "Returned from orb->run()." << std::endl;
433 catch(CORBA::SystemException&) {
434 DEBTRACE("Caught CORBA::SystemException.");
436 catch(CORBA::Exception& ex) {
437 DEBTRACE("Caught CORBA::Exception." << ex);
439 catch(omniORB::fatalException& fe) {
440 DEBTRACE("Caught omniORB::fatalException:");
441 DEBTRACE(" file: " << fe.file());
442 DEBTRACE(" line: " << fe.line());
443 DEBTRACE(" mesg: " << fe.errmsg());
446 DEBTRACE("Caught unknown exception." );
453 //////////////////////////////////////////////////////////////////////
455 static CORBA::Boolean
456 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
458 CosNaming::NamingContext_var rootContext;
461 // Obtain a reference to the root context of the Name service:
462 CORBA::Object_var obj;
463 obj = orb->resolve_initial_references("NameService");
465 // Narrow the reference returned.
466 rootContext = CosNaming::NamingContext::_narrow(obj);
467 if( CORBA::is_nil(rootContext) ) {
468 DEBTRACE("Failed to narrow the root naming context.");
472 catch(CORBA::ORB::InvalidName& ex) {
473 // This should not happen!
474 DEBTRACE("Service required is invalid [does not exist]." );
479 // Bind a context called "test" to the root context:
481 CosNaming::Name contextName;
482 contextName.length(1);
483 contextName[0].id = (const char*) "test"; // string copied
484 contextName[0].kind = (const char*) "my_context"; // string copied
485 // Note on kind: The kind field is used to indicate the type
486 // of the object. This is to avoid conventions such as that used
487 // by files (name.type -- e.g. test.ps = postscript etc.)
489 CosNaming::NamingContext_var testContext;
491 // Bind the context to root.
492 testContext = rootContext->bind_new_context(contextName);
494 catch(CosNaming::NamingContext::AlreadyBound& ex) {
495 // If the context already exists, this exception will be raised.
496 // In this case, just resolve the name and assign testContext
497 // to the object returned:
498 CORBA::Object_var obj;
499 obj = rootContext->resolve(contextName);
500 testContext = CosNaming::NamingContext::_narrow(obj);
501 if( CORBA::is_nil(testContext) ) {
502 DEBTRACE("Failed to narrow naming context.");
507 // Bind objref with name Echo to the testContext:
508 CosNaming::Name objectName;
509 objectName.length(1);
510 objectName[0].id = name; // string copied
511 objectName[0].kind = (const char*) "Object"; // string copied
514 testContext->bind(objectName, objref);
516 catch(CosNaming::NamingContext::AlreadyBound& ex) {
517 testContext->rebind(objectName, objref);
519 // Note: Using rebind() will overwrite any Object previously bound
520 // to /test/Echo with obj.
521 // Alternatively, bind() can be used, which will raise a
522 // CosNaming::NamingContext::AlreadyBound exception if the name
523 // supplied is already bound to an object.
525 // Amendment: When using OrbixNames, it is necessary to first try bind
526 // and then rebind, as rebind on it's own will throw a NotFoundexception if
527 // the Name has not already been bound. [This is incorrect behaviour -
528 // it should just bind].
530 catch(CORBA::COMM_FAILURE& ex) {
531 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
532 << "naming service.");
535 catch(CORBA::SystemException&) {
536 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");