9 #include "YacsTrace.hxx"
11 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
13 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
17 CORBA::TypeCode_var tc = tmp.type();
18 const char *p = tc->name();
28 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
33 CORBA::Long echoLong(CORBA::Long i);
36 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
41 CORBA::Long echoLong(CORBA::Long i);
44 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
49 CORBA::Long echoLong(CORBA::Long i);
50 CORBA::Long echoLong2(CORBA::Long i);
53 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
58 CORBA::Long echoLong(CORBA::Long i);
59 CORBA::Long echoLong2(CORBA::Long i);
62 class Echo_i : public POA_eo::Echo,
63 public PortableServer::RefCountServantBase
68 virtual char* echoString(const char* mesg);
69 CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
70 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
71 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
72 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
73 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
74 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
75 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
76 void echoObjVec(const eo::ObjVec&, eo::ObjVec_out);
77 void echoObj2(eo::Obj_ptr , eo::Obj_out);
78 void echoD(eo::D_ptr , eo::D_out);
79 void echoC(eo::C_ptr , eo::C_out);
80 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
82 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
83 void createObj(CORBA::Long i, eo::Obj_out oo);
84 void createC(eo::C_out oo);
85 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);
86 virtual PortableServer::POA_ptr _default_POA();
90 PortableServer::POA_ptr Echo_i::_default_POA()
92 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
94 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
97 //return PortableServer::POA::_duplicate(root_poa);
98 return root_poa._retn();
102 char* Echo_i::echoString(const char* mesg)
104 DEBTRACE("Echo_i::echoString " << mesg);
105 return CORBA::string_dup(mesg);
108 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
109 DEBTRACE("Echo_i::echoDouble " << i);
113 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
115 DEBTRACE("Echo_i::echoIntVec " << in.length());
116 for(int i=0;i<in.length(); i++){
119 out=new eo::IntVec(in);
122 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
124 DEBTRACE("Echo_i::echoStrVec " << in.length());
125 for(int i=0;i<in.length(); i++){
128 out=new eo::StrVec(in);
131 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
133 DEBTRACE("Echo_i::echoObjectVec " << in.length());
134 for(int i=0;i<in.length(); i++){
135 DEBTRACE(in[i]->_PD_repoId);
137 out=new eo::ObjectVec(in);
140 void Echo_i::echoObjVec(const eo::ObjVec& in, eo::ObjVec_out out)
142 DEBTRACE("Echo_i::echoObjVec " << in.length());
143 for(int i=0;i<in.length(); i++)
145 DEBTRACE(in[i]->_mostDerivedRepoId());
151 Obj_i* obj0 = new Obj_i();
152 CORBA::Object_var ref0 = obj0->_this();
153 eo::Obj_var o0 = eo::Obj::_narrow(ref0);
157 C_i* obj1 = new C_i();
158 CORBA::Object_var ref1 = obj1->_this();
159 eo::Obj_var o1 = eo::Obj::_narrow(ref1);
163 DEBTRACE("ENDOF Echo_i::echoObjVec " );
166 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
168 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
169 for(int i=0;i< in.length(); i++){
170 for(int j=0;j< in[i].length(); j++){
171 DEBTRACE(in[i][j]->_PD_repoId);
174 out=new eo::ObjectVecVec(in);
177 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
179 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
180 for(int i=0;i<in.length(); i++){
183 out=new eo::DoubleVec(in);
186 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
188 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
189 for(int i=0;i< in.length(); i++){
190 for(int j=0;j< in[i].length(); j++){
194 out=new eo::DoubleVecVec(in);
197 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception)
199 DEBTRACE("Echo_i::echoLong " << i);
201 eo::ExceptionStruct es;
203 es.text = "error Socket exception";
204 throw eo::SALOME_Exception(es);
211 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
212 DEBTRACE("Echo_i::echoC ");
214 oo=eo::C::_duplicate(o);
217 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
218 DEBTRACE("Echo_i::echoD ");
220 //oo=eo::D::_duplicate(o);
221 D_i* myD = new D_i();
226 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
227 DEBTRACE("Echo_i::echoObj2 ");
229 oo=eo::Obj::_duplicate(o);
232 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
233 DEBTRACE("Echo_i::echoObj " << i << "," << j );
234 oo=eo::Obj::_duplicate(o);
235 return eo::Obj::_duplicate(o);
238 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
239 DEBTRACE("Echo_i::createObj " << i);
240 Obj_i* myobj = new Obj_i();
241 CORBA::Object_var myref = myobj->_this();
242 oo = eo::Obj::_narrow(myref);
243 myobj->_remove_ref();
246 void Echo_i::createC(eo::C_out oo){
247 DEBTRACE("Echo_i::createC ");
248 C_i* myobj = new C_i();
249 CORBA::Object_var myref = myobj->_this();
250 oo = eo::C::_narrow(myref);
251 myobj->_remove_ref();
254 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)
256 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
259 s=CORBA::string_dup(m);
260 oo=eo::Obj::_duplicate(o);
264 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
265 DEBTRACE("Obj_i::echoLong " << i );
271 CORBA::Long C_i::echoLong(CORBA::Long i ){
272 DEBTRACE("C_i::echoLong " << i);
278 CORBA::Long D_i::echoLong2(CORBA::Long i ){
279 DEBTRACE("D_i::echoLong " << i);
283 CORBA::Long D_i::echoLong(CORBA::Long i ){
284 DEBTRACE("D_i::echoLong " << i);
290 CORBA::Long E_i::echoLong2(CORBA::Long i ){
291 DEBTRACE("E_i::echoLong " << i);
295 CORBA::Long E_i::echoLong(CORBA::Long i ){
296 DEBTRACE("E_i::echoLong " << i);
302 eo::Echo_var myechoref;
304 int main(int argc, char** argv)
307 orb = CORBA::ORB_init(argc, argv);
310 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
311 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
313 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
316 // Create a new POA with the shortcut policy
317 CORBA::PolicyList pl2;
320 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
321 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
322 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
323 PortableServer::POA_ptr shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
325 // Create and activate servant
326 Echo_i* myecho = new Echo_i();
327 // Obtain a reference to the object, and print it out as a
329 obj = myecho->_this();
330 CORBA::String_var sior(orb->object_to_string(obj));
331 DEBTRACE("'" << (char*)sior << "'");
332 myechoref = eo::Echo::_narrow(obj);
334 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
336 // Decrement the reference count of the object implementation, so
337 // that it will be properly cleaned up when the POA has determined
338 // that it is no longer needed.
339 myecho->_remove_ref();
341 //create object C and register it in naming service
342 C_i* myC = new C_i();
344 eo::C_var myCref=eo::C::_narrow(obj);
346 if( !bindObjectToName(orb, myCref,"C") ) return 1;
348 //create object D and register it in naming service
349 D_i* myD = new D_i();
351 eo::D_var myDref=eo::D::_narrow(obj);
353 if( !bindObjectToName(orb, myDref,"D") ) return 1;
355 //create object Obj and register it in naming service
356 Obj_i* myObj = new Obj_i();
358 eo::Obj_var myObjref=eo::Obj::_narrow(obj);
359 myObj->_remove_ref();
360 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
364 catch(CORBA::SystemException&) {
365 DEBTRACE("Caught CORBA::SystemException.");
367 catch(CORBA::Exception& ex) {
368 DEBTRACE("Caught CORBA::Exception." << ex);
370 catch(omniORB::fatalException& fe) {
371 DEBTRACE("Caught omniORB::fatalException:");
372 DEBTRACE(" file: " << fe.file());
373 DEBTRACE(" line: " << fe.line());
374 DEBTRACE(" mesg: " << fe.errmsg());
377 DEBTRACE("Caught unknown exception." );
384 //////////////////////////////////////////////////////////////////////
386 static CORBA::Boolean
387 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
389 CosNaming::NamingContext_var rootContext;
392 // Obtain a reference to the root context of the Name service:
393 CORBA::Object_var obj;
394 obj = orb->resolve_initial_references("NameService");
396 // Narrow the reference returned.
397 rootContext = CosNaming::NamingContext::_narrow(obj);
398 if( CORBA::is_nil(rootContext) ) {
399 DEBTRACE("Failed to narrow the root naming context.");
403 catch(CORBA::ORB::InvalidName& ex) {
404 // This should not happen!
405 DEBTRACE("Service required is invalid [does not exist]." );
410 // Bind a context called "test" to the root context:
412 CosNaming::Name contextName;
413 contextName.length(1);
414 contextName[0].id = (const char*) "test"; // string copied
415 contextName[0].kind = (const char*) "my_context"; // string copied
416 // Note on kind: The kind field is used to indicate the type
417 // of the object. This is to avoid conventions such as that used
418 // by files (name.type -- e.g. test.ps = postscript etc.)
420 CosNaming::NamingContext_var testContext;
422 // Bind the context to root.
423 testContext = rootContext->bind_new_context(contextName);
425 catch(CosNaming::NamingContext::AlreadyBound& ex) {
426 // If the context already exists, this exception will be raised.
427 // In this case, just resolve the name and assign testContext
428 // to the object returned:
429 CORBA::Object_var obj;
430 obj = rootContext->resolve(contextName);
431 testContext = CosNaming::NamingContext::_narrow(obj);
432 if( CORBA::is_nil(testContext) ) {
433 DEBTRACE("Failed to narrow naming context.");
438 // Bind objref with name Echo to the testContext:
439 CosNaming::Name objectName;
440 objectName.length(1);
441 objectName[0].id = name; // string copied
442 objectName[0].kind = (const char*) "Object"; // string copied
445 testContext->bind(objectName, objref);
447 catch(CosNaming::NamingContext::AlreadyBound& ex) {
448 testContext->rebind(objectName, objref);
450 // Note: Using rebind() will overwrite any Object previously bound
451 // to /test/Echo with obj.
452 // Alternatively, bind() can be used, which will raise a
453 // CosNaming::NamingContext::AlreadyBound exception if the name
454 // supplied is already bound to an object.
456 // Amendment: When using OrbixNames, it is necessary to first try bind
457 // and then rebind, as rebind on it's own will throw a NotFoundexception if
458 // the Name has not already been bound. [This is incorrect behaviour -
459 // it should just bind].
461 catch(CORBA::COMM_FAILURE& ex) {
462 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
463 << "naming service.");
466 catch(CORBA::SystemException&) {
467 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");