1 // Copyright (C) 2006-2019 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
28 #include "YacsTrace.hxx"
30 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
32 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
36 CORBA::TypeCode_var tc = tmp.type();
37 const char *p = tc->name();
47 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
52 CORBA::Long echoLong(CORBA::Long i);
55 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
60 CORBA::Long echoLong(CORBA::Long i);
63 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
68 CORBA::Long echoLong(CORBA::Long i);
69 CORBA::Long echoLong2(CORBA::Long i);
72 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
77 CORBA::Long echoLong(CORBA::Long i);
78 CORBA::Long echoLong2(CORBA::Long i);
81 class Echo_i : public POA_eo::Echo,
82 public PortableServer::RefCountServantBase
87 virtual char* echoString(const char* mesg);
88 CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
89 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
90 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
91 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
92 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
93 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
94 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
95 void echoObjVec(const eo::ObjVec&, eo::ObjVec_out);
96 void echoObj2(eo::Obj_ptr , eo::Obj_out);
97 void echoD(eo::D_ptr , eo::D_out);
98 void echoC(eo::C_ptr , eo::C_out);
99 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
101 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
102 void createObj(CORBA::Long i, eo::Obj_out oo);
103 void createC(eo::C_out oo);
104 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);
105 virtual PortableServer::POA_ptr _default_POA();
108 //Implementation Echo
109 PortableServer::POA_ptr Echo_i::_default_POA()
111 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
113 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
116 //return PortableServer::POA::_duplicate(root_poa);
117 return root_poa._retn();
121 char* Echo_i::echoString(const char* mesg)
123 DEBTRACE("Echo_i::echoString " << mesg);
124 return CORBA::string_dup(mesg);
127 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
128 DEBTRACE("Echo_i::echoDouble " << i);
132 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
134 DEBTRACE("Echo_i::echoIntVec " << in.length());
135 for(int i=0;i<in.length(); i++){
138 out=new eo::IntVec(in);
141 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
143 DEBTRACE("Echo_i::echoStrVec " << in.length());
144 for(int i=0;i<in.length(); i++){
147 out=new eo::StrVec(in);
150 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
152 DEBTRACE("Echo_i::echoObjectVec " << in.length());
153 for(int i=0;i<in.length(); i++){
154 DEBTRACE(in[i]->_PD_repoId);
156 out=new eo::ObjectVec(in);
159 void Echo_i::echoObjVec(const eo::ObjVec& in, eo::ObjVec_out out)
161 DEBTRACE("Echo_i::echoObjVec " << in.length());
162 for(int i=0;i<in.length(); i++)
164 DEBTRACE(in[i]->_mostDerivedRepoId());
170 Obj_i* obj0 = new Obj_i();
171 CORBA::Object_var ref0 = obj0->_this();
172 eo::Obj_var o0 = eo::Obj::_narrow(ref0);
176 C_i* obj1 = new C_i();
177 CORBA::Object_var ref1 = obj1->_this();
178 eo::Obj_var o1 = eo::Obj::_narrow(ref1);
182 DEBTRACE("ENDOF Echo_i::echoObjVec " );
185 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
187 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
188 for(int i=0;i< in.length(); i++){
189 for(int j=0;j< in[i].length(); j++){
190 DEBTRACE(in[i][j]->_PD_repoId);
193 out=new eo::ObjectVecVec(in);
196 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
198 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
199 for(int i=0;i<in.length(); i++){
202 out=new eo::DoubleVec(in);
205 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
207 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
208 for(int i=0;i< in.length(); i++){
209 for(int j=0;j< in[i].length(); j++){
213 out=new eo::DoubleVecVec(in);
216 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception)
218 DEBTRACE("Echo_i::echoLong " << i);
220 eo::ExceptionStruct es;
222 es.text = "error Socket exception";
223 throw eo::SALOME_Exception(es);
230 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
231 DEBTRACE("Echo_i::echoC ");
233 oo=eo::C::_duplicate(o);
236 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
237 DEBTRACE("Echo_i::echoD ");
239 //oo=eo::D::_duplicate(o);
240 D_i* myD = new D_i();
245 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
246 DEBTRACE("Echo_i::echoObj2 ");
248 oo=eo::Obj::_duplicate(o);
251 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
252 DEBTRACE("Echo_i::echoObj " << i << "," << j );
253 oo=eo::Obj::_duplicate(o);
254 return eo::Obj::_duplicate(o);
257 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
258 DEBTRACE("Echo_i::createObj " << i);
259 Obj_i* myobj = new Obj_i();
260 CORBA::Object_var myref = myobj->_this();
261 oo = eo::Obj::_narrow(myref);
262 myobj->_remove_ref();
265 void Echo_i::createC(eo::C_out oo){
266 DEBTRACE("Echo_i::createC ");
267 C_i* myobj = new C_i();
268 CORBA::Object_var myref = myobj->_this();
269 oo = eo::C::_narrow(myref);
270 myobj->_remove_ref();
273 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)
275 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
278 s=CORBA::string_dup(m);
279 oo=eo::Obj::_duplicate(o);
283 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
284 DEBTRACE("Obj_i::echoLong " << i );
290 CORBA::Long C_i::echoLong(CORBA::Long i ){
291 DEBTRACE("C_i::echoLong " << i);
297 CORBA::Long D_i::echoLong2(CORBA::Long i ){
298 DEBTRACE("D_i::echoLong " << i);
302 CORBA::Long D_i::echoLong(CORBA::Long i ){
303 DEBTRACE("D_i::echoLong " << i);
309 CORBA::Long E_i::echoLong2(CORBA::Long i ){
310 DEBTRACE("E_i::echoLong " << i);
314 CORBA::Long E_i::echoLong(CORBA::Long i ){
315 DEBTRACE("E_i::echoLong " << i);
321 eo::Echo_var myechoref;
323 int main(int argc, char** argv)
326 orb = CORBA::ORB_init(argc, argv);
329 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
330 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
332 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
335 // Create a new POA with the shortcut policy
336 CORBA::PolicyList pl2;
339 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
340 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
341 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
342 PortableServer::POA_ptr shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
344 // Create and activate servant
345 Echo_i* myecho = new Echo_i();
346 // Obtain a reference to the object, and print it out as a
348 obj = myecho->_this();
349 CORBA::String_var sior(orb->object_to_string(obj));
350 DEBTRACE("'" << (char*)sior << "'");
351 myechoref = eo::Echo::_narrow(obj);
353 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
355 // Decrement the reference count of the object implementation, so
356 // that it will be properly cleaned up when the POA has determined
357 // that it is no longer needed.
358 myecho->_remove_ref();
360 //create object C and register it in naming service
361 C_i* myC = new C_i();
363 eo::C_var myCref=eo::C::_narrow(obj);
365 if( !bindObjectToName(orb, myCref,"C") ) return 1;
367 //create object D and register it in naming service
368 D_i* myD = new D_i();
370 eo::D_var myDref=eo::D::_narrow(obj);
372 if( !bindObjectToName(orb, myDref,"D") ) return 1;
374 //create object Obj and register it in naming service
375 Obj_i* myObj = new Obj_i();
377 eo::Obj_var myObjref=eo::Obj::_narrow(obj);
378 myObj->_remove_ref();
379 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
383 catch(CORBA::SystemException&) {
384 DEBTRACE("Caught CORBA::SystemException.");
386 catch(CORBA::Exception& ex) {
387 DEBTRACE("Caught CORBA::Exception." << ex);
389 catch(omniORB::fatalException& fe) {
390 DEBTRACE("Caught omniORB::fatalException:");
391 DEBTRACE(" file: " << fe.file());
392 DEBTRACE(" line: " << fe.line());
393 DEBTRACE(" mesg: " << fe.errmsg());
396 DEBTRACE("Caught unknown exception." );
403 //////////////////////////////////////////////////////////////////////
405 static CORBA::Boolean
406 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
408 CosNaming::NamingContext_var rootContext;
411 // Obtain a reference to the root context of the Name service:
412 CORBA::Object_var obj;
413 obj = orb->resolve_initial_references("NameService");
415 // Narrow the reference returned.
416 rootContext = CosNaming::NamingContext::_narrow(obj);
417 if( CORBA::is_nil(rootContext) ) {
418 DEBTRACE("Failed to narrow the root naming context.");
422 catch(CORBA::ORB::InvalidName& ex) {
423 // This should not happen!
424 DEBTRACE("Service required is invalid [does not exist]." );
429 // Bind a context called "test" to the root context:
431 CosNaming::Name contextName;
432 contextName.length(1);
433 contextName[0].id = (const char*) "test"; // string copied
434 contextName[0].kind = (const char*) "my_context"; // string copied
435 // Note on kind: The kind field is used to indicate the type
436 // of the object. This is to avoid conventions such as that used
437 // by files (name.type -- e.g. test.ps = postscript etc.)
439 CosNaming::NamingContext_var testContext;
441 // Bind the context to root.
442 testContext = rootContext->bind_new_context(contextName);
444 catch(CosNaming::NamingContext::AlreadyBound& ex) {
445 // If the context already exists, this exception will be raised.
446 // In this case, just resolve the name and assign testContext
447 // to the object returned:
448 CORBA::Object_var obj;
449 obj = rootContext->resolve(contextName);
450 testContext = CosNaming::NamingContext::_narrow(obj);
451 if( CORBA::is_nil(testContext) ) {
452 DEBTRACE("Failed to narrow naming context.");
457 // Bind objref with name Echo to the testContext:
458 CosNaming::Name objectName;
459 objectName.length(1);
460 objectName[0].id = name; // string copied
461 objectName[0].kind = (const char*) "Object"; // string copied
464 testContext->bind(objectName, objref);
466 catch(CosNaming::NamingContext::AlreadyBound& ex) {
467 testContext->rebind(objectName, objref);
469 // Note: Using rebind() will overwrite any Object previously bound
470 // to /test/Echo with obj.
471 // Alternatively, bind() can be used, which will raise a
472 // CosNaming::NamingContext::AlreadyBound exception if the name
473 // supplied is already bound to an object.
475 // Amendment: When using OrbixNames, it is necessary to first try bind
476 // and then rebind, as rebind on it's own will throw a NotFoundexception if
477 // the Name has not already been bound. [This is incorrect behaviour -
478 // it should just bind].
480 catch(CORBA::COMM_FAILURE& ex) {
481 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
482 << "naming service.");
485 catch(CORBA::SystemException&) {
486 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");