10 #define MYDEBTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
11 #define DEBTRACE(msg) {MYDEBTRACE; std::cerr<<msg<<std::endl<<std::flush;}
17 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
19 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
23 CORBA::TypeCode_var tc = tmp.type();
24 const char *p = tc->name();
34 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
39 CORBA::Long echoLong(CORBA::Long i);
42 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
47 CORBA::Long echoLong(CORBA::Long i);
50 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
55 CORBA::Long echoLong(CORBA::Long i);
56 CORBA::Long echoLong2(CORBA::Long i);
59 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
64 CORBA::Long echoLong(CORBA::Long i);
65 CORBA::Long echoLong2(CORBA::Long i);
68 class Echo_i : public POA_eo::Echo,
69 public PortableServer::RefCountServantBase
74 virtual char* echoString(const char* mesg);
75 CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
76 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
77 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
78 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
79 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
80 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
81 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
82 void echoObj2(eo::Obj_ptr , eo::Obj_out);
83 void echoD(eo::D_ptr , eo::D_out);
84 void echoC(eo::C_ptr , eo::C_out);
85 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
87 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
88 void createObj(CORBA::Long i, eo::Obj_out oo);
89 void createC(eo::C_out oo);
90 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);
91 virtual PortableServer::POA_ptr _default_POA();
95 PortableServer::POA_ptr Echo_i::_default_POA()
97 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
99 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
102 //return PortableServer::POA::_duplicate(root_poa);
103 return root_poa._retn();
107 char* Echo_i::echoString(const char* mesg)
109 DEBTRACE("Echo_i::echoString " << mesg);
110 return CORBA::string_dup(mesg);
113 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
114 DEBTRACE("Echo_i::echoDouble " << i);
118 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
120 DEBTRACE("Echo_i::echoIntVec " << in.length());
121 for(int i=0;i<in.length(); i++){
124 out=new eo::IntVec(in);
127 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
129 DEBTRACE("Echo_i::echoStrVec " << in.length());
130 for(int i=0;i<in.length(); i++){
133 out=new eo::StrVec(in);
136 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
138 DEBTRACE("Echo_i::echoObjectVec " << in.length());
139 for(int i=0;i<in.length(); i++){
140 DEBTRACE(in[i]->_PD_repoId);
142 out=new eo::ObjectVec(in);
145 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
147 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
148 for(int i=0;i< in.length(); i++){
149 for(int j=0;j< in[i].length(); j++){
150 DEBTRACE(in[i][j]->_PD_repoId);
153 out=new eo::ObjectVecVec(in);
156 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
158 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
159 for(int i=0;i<in.length(); i++){
162 out=new eo::DoubleVec(in);
165 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
167 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
168 for(int i=0;i< in.length(); i++){
169 for(int j=0;j< in[i].length(); j++){
173 out=new eo::DoubleVecVec(in);
176 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception)
178 DEBTRACE("Echo_i::echoLong " << i);
183 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
184 DEBTRACE("Echo_i::echoC ");
186 oo=eo::C::_duplicate(o);
189 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
190 DEBTRACE("Echo_i::echoD ");
192 //oo=eo::D::_duplicate(o);
193 D_i* myD = new D_i();
198 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
199 DEBTRACE("Echo_i::echoObj2 ");
201 oo=eo::Obj::_duplicate(o);
204 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
205 DEBTRACE("Echo_i::echoObj " << i << "," << j );
206 oo=eo::Obj::_duplicate(o);
207 return eo::Obj::_duplicate(o);
210 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
211 DEBTRACE("Echo_i::createObj " << i);
212 Obj_i* myobj = new Obj_i();
213 CORBA::Object_var myref = myobj->_this();
214 oo = eo::Obj::_narrow(myref);
215 myobj->_remove_ref();
218 void Echo_i::createC(eo::C_out oo){
219 DEBTRACE("Echo_i::createC ");
220 C_i* myobj = new C_i();
221 CORBA::Object_var myref = myobj->_this();
222 oo = eo::C::_narrow(myref);
223 myobj->_remove_ref();
226 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)
228 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
231 s=CORBA::string_dup(m);
232 oo=eo::Obj::_duplicate(o);
236 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
237 DEBTRACE("Obj_i::echoLong " << i );
243 CORBA::Long C_i::echoLong(CORBA::Long i ){
244 DEBTRACE("C_i::echoLong " << i);
250 CORBA::Long D_i::echoLong2(CORBA::Long i ){
251 DEBTRACE("D_i::echoLong " << i);
255 CORBA::Long D_i::echoLong(CORBA::Long i ){
256 DEBTRACE("D_i::echoLong " << i);
262 CORBA::Long E_i::echoLong2(CORBA::Long i ){
263 DEBTRACE("E_i::echoLong " << i);
267 CORBA::Long E_i::echoLong(CORBA::Long i ){
268 DEBTRACE("E_i::echoLong " << i);
274 eo::Echo_var myechoref;
276 int main(int argc, char** argv)
279 orb = CORBA::ORB_init(argc, argv);
282 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
283 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
285 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
288 // Create a new POA with the shortcut policy
289 CORBA::PolicyList pl2;
292 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
293 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
294 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
295 PortableServer::POA_ptr shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
297 // Create and activate servant
298 Echo_i* myecho = new Echo_i();
299 // Obtain a reference to the object, and print it out as a
301 obj = myecho->_this();
302 CORBA::String_var sior(orb->object_to_string(obj));
303 DEBTRACE("'" << (char*)sior << "'");
304 myechoref = eo::Echo::_narrow(obj);
306 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
308 // Decrement the reference count of the object implementation, so
309 // that it will be properly cleaned up when the POA has determined
310 // that it is no longer needed.
311 myecho->_remove_ref();
313 //create object C and register it in naming service
314 C_i* myC = new C_i();
316 eo::C_var myCref=eo::C::_narrow(obj);
318 if( !bindObjectToName(orb, myCref,"C") ) return 1;
320 //create object D and register it in naming service
321 D_i* myD = new D_i();
323 eo::D_var myDref=eo::D::_narrow(obj);
325 if( !bindObjectToName(orb, myDref,"D") ) return 1;
327 //create object Obj and register it in naming service
328 Obj_i* myObj = new Obj_i();
330 eo::Obj_var myObjref=eo::Obj::_narrow(obj);
331 myObj->_remove_ref();
332 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
336 catch(CORBA::SystemException&) {
337 DEBTRACE("Caught CORBA::SystemException.");
339 catch(CORBA::Exception& ex) {
340 DEBTRACE("Caught CORBA::Exception." << ex);
342 catch(omniORB::fatalException& fe) {
343 DEBTRACE("Caught omniORB::fatalException:");
344 DEBTRACE(" file: " << fe.file());
345 DEBTRACE(" line: " << fe.line());
346 DEBTRACE(" mesg: " << fe.errmsg());
349 DEBTRACE("Caught unknown exception." );
356 //////////////////////////////////////////////////////////////////////
358 static CORBA::Boolean
359 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
361 CosNaming::NamingContext_var rootContext;
364 // Obtain a reference to the root context of the Name service:
365 CORBA::Object_var obj;
366 obj = orb->resolve_initial_references("NameService");
368 // Narrow the reference returned.
369 rootContext = CosNaming::NamingContext::_narrow(obj);
370 if( CORBA::is_nil(rootContext) ) {
371 DEBTRACE("Failed to narrow the root naming context.");
375 catch(CORBA::ORB::InvalidName& ex) {
376 // This should not happen!
377 DEBTRACE("Service required is invalid [does not exist]." );
382 // Bind a context called "test" to the root context:
384 CosNaming::Name contextName;
385 contextName.length(1);
386 contextName[0].id = (const char*) "test"; // string copied
387 contextName[0].kind = (const char*) "my_context"; // string copied
388 // Note on kind: The kind field is used to indicate the type
389 // of the object. This is to avoid conventions such as that used
390 // by files (name.type -- e.g. test.ps = postscript etc.)
392 CosNaming::NamingContext_var testContext;
394 // Bind the context to root.
395 testContext = rootContext->bind_new_context(contextName);
397 catch(CosNaming::NamingContext::AlreadyBound& ex) {
398 // If the context already exists, this exception will be raised.
399 // In this case, just resolve the name and assign testContext
400 // to the object returned:
401 CORBA::Object_var obj;
402 obj = rootContext->resolve(contextName);
403 testContext = CosNaming::NamingContext::_narrow(obj);
404 if( CORBA::is_nil(testContext) ) {
405 DEBTRACE("Failed to narrow naming context.");
410 // Bind objref with name Echo to the testContext:
411 CosNaming::Name objectName;
412 objectName.length(1);
413 objectName[0].id = name; // string copied
414 objectName[0].kind = (const char*) "Object"; // string copied
417 testContext->bind(objectName, objref);
419 catch(CosNaming::NamingContext::AlreadyBound& ex) {
420 testContext->rebind(objectName, objref);
422 // Note: Using rebind() will overwrite any Object previously bound
423 // to /test/Echo with obj.
424 // Alternatively, bind() can be used, which will raise a
425 // CosNaming::NamingContext::AlreadyBound exception if the name
426 // supplied is already bound to an object.
428 // Amendment: When using OrbixNames, it is necessary to first try bind
429 // and then rebind, as rebind on it's own will throw a NotFoundexception if
430 // the Name has not already been bound. [This is incorrect behaviour -
431 // it should just bind].
433 catch(CORBA::COMM_FAILURE& ex) {
434 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
435 << "naming service.");
438 catch(CORBA::SystemException&) {
439 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");