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 #define MYDEBTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
29 #define DEBTRACE(msg) {MYDEBTRACE; std::cerr<<msg<<std::endl<<std::flush;}
35 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
37 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
41 CORBA::TypeCode_var tc = tmp.type();
42 const char *p = tc->name();
52 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
57 CORBA::Long echoLong(CORBA::Long i);
60 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
65 CORBA::Long echoLong(CORBA::Long i);
68 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
73 CORBA::Long echoLong(CORBA::Long i);
74 CORBA::Long echoLong2(CORBA::Long i);
77 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
82 CORBA::Long echoLong(CORBA::Long i);
83 CORBA::Long echoLong2(CORBA::Long i);
86 class Echo_i : public POA_eo::Echo,
87 public PortableServer::RefCountServantBase
92 virtual char* echoString(const char* mesg);
93 CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
94 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
95 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
96 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
97 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
98 void echoStrVec(const eo::StrVec&, eo::StrVec_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 virtual PortableServer::POA_ptr _default_POA();
112 //Implementation Echo
113 PortableServer::POA_ptr Echo_i::_default_POA()
115 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
117 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
120 //return PortableServer::POA::_duplicate(root_poa);
121 return root_poa._retn();
125 char* Echo_i::echoString(const char* mesg)
127 DEBTRACE("Echo_i::echoString " << mesg);
128 return CORBA::string_dup(mesg);
131 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
132 DEBTRACE("Echo_i::echoDouble " << i);
136 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
138 DEBTRACE("Echo_i::echoIntVec " << in.length());
139 for(int i=0;i<in.length(); i++){
142 out=new eo::IntVec(in);
145 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
147 DEBTRACE("Echo_i::echoStrVec " << in.length());
148 for(int i=0;i<in.length(); i++){
151 out=new eo::StrVec(in);
154 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
156 DEBTRACE("Echo_i::echoObjectVec " << in.length());
157 for(int i=0;i<in.length(); i++){
158 DEBTRACE(in[i]->_PD_repoId);
160 out=new eo::ObjectVec(in);
163 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
165 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
166 for(int i=0;i< in.length(); i++){
167 for(int j=0;j< in[i].length(); j++){
168 DEBTRACE(in[i][j]->_PD_repoId);
171 out=new eo::ObjectVecVec(in);
174 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
176 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
177 for(int i=0;i<in.length(); i++){
180 out=new eo::DoubleVec(in);
183 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
185 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
186 for(int i=0;i< in.length(); i++){
187 for(int j=0;j< in[i].length(); j++){
191 out=new eo::DoubleVecVec(in);
194 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception)
196 DEBTRACE("Echo_i::echoLong " << i);
201 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
202 DEBTRACE("Echo_i::echoC ");
204 oo=eo::C::_duplicate(o);
207 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
208 DEBTRACE("Echo_i::echoD ");
210 //oo=eo::D::_duplicate(o);
211 D_i* myD = new D_i();
216 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
217 DEBTRACE("Echo_i::echoObj2 ");
219 oo=eo::Obj::_duplicate(o);
222 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
223 DEBTRACE("Echo_i::echoObj " << i << "," << j );
224 oo=eo::Obj::_duplicate(o);
225 return eo::Obj::_duplicate(o);
228 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
229 DEBTRACE("Echo_i::createObj " << i);
230 Obj_i* myobj = new Obj_i();
231 CORBA::Object_var myref = myobj->_this();
232 oo = eo::Obj::_narrow(myref);
233 myobj->_remove_ref();
236 void Echo_i::createC(eo::C_out oo){
237 DEBTRACE("Echo_i::createC ");
238 C_i* myobj = new C_i();
239 CORBA::Object_var myref = myobj->_this();
240 oo = eo::C::_narrow(myref);
241 myobj->_remove_ref();
244 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)
246 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
249 s=CORBA::string_dup(m);
250 oo=eo::Obj::_duplicate(o);
254 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
255 DEBTRACE("Obj_i::echoLong " << i );
261 CORBA::Long C_i::echoLong(CORBA::Long i ){
262 DEBTRACE("C_i::echoLong " << i);
268 CORBA::Long D_i::echoLong2(CORBA::Long i ){
269 DEBTRACE("D_i::echoLong " << i);
273 CORBA::Long D_i::echoLong(CORBA::Long i ){
274 DEBTRACE("D_i::echoLong " << i);
280 CORBA::Long E_i::echoLong2(CORBA::Long i ){
281 DEBTRACE("E_i::echoLong " << i);
285 CORBA::Long E_i::echoLong(CORBA::Long i ){
286 DEBTRACE("E_i::echoLong " << i);
292 eo::Echo_var myechoref;
294 int main(int argc, char** argv)
297 orb = CORBA::ORB_init(argc, argv);
300 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
301 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
303 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
306 // Create a new POA with the shortcut policy
307 CORBA::PolicyList pl2;
310 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
311 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
312 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
313 PortableServer::POA_ptr shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
315 // Create and activate servant
316 Echo_i* myecho = new Echo_i();
317 // Obtain a reference to the object, and print it out as a
319 obj = myecho->_this();
320 CORBA::String_var sior(orb->object_to_string(obj));
321 DEBTRACE("'" << (char*)sior << "'");
322 myechoref = eo::Echo::_narrow(obj);
324 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
326 // Decrement the reference count of the object implementation, so
327 // that it will be properly cleaned up when the POA has determined
328 // that it is no longer needed.
329 myecho->_remove_ref();
331 //create object C and register it in naming service
332 C_i* myC = new C_i();
334 eo::C_var myCref=eo::C::_narrow(obj);
336 if( !bindObjectToName(orb, myCref,"C") ) return 1;
338 //create object D and register it in naming service
339 D_i* myD = new D_i();
341 eo::D_var myDref=eo::D::_narrow(obj);
343 if( !bindObjectToName(orb, myDref,"D") ) return 1;
345 //create object Obj and register it in naming service
346 Obj_i* myObj = new Obj_i();
348 eo::Obj_var myObjref=eo::Obj::_narrow(obj);
349 myObj->_remove_ref();
350 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
354 catch(CORBA::SystemException&) {
355 DEBTRACE("Caught CORBA::SystemException.");
357 catch(CORBA::Exception& ex) {
358 DEBTRACE("Caught CORBA::Exception." << ex);
360 catch(omniORB::fatalException& fe) {
361 DEBTRACE("Caught omniORB::fatalException:");
362 DEBTRACE(" file: " << fe.file());
363 DEBTRACE(" line: " << fe.line());
364 DEBTRACE(" mesg: " << fe.errmsg());
367 DEBTRACE("Caught unknown exception." );
374 //////////////////////////////////////////////////////////////////////
376 static CORBA::Boolean
377 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
379 CosNaming::NamingContext_var rootContext;
382 // Obtain a reference to the root context of the Name service:
383 CORBA::Object_var obj;
384 obj = orb->resolve_initial_references("NameService");
386 // Narrow the reference returned.
387 rootContext = CosNaming::NamingContext::_narrow(obj);
388 if( CORBA::is_nil(rootContext) ) {
389 DEBTRACE("Failed to narrow the root naming context.");
393 catch(CORBA::ORB::InvalidName& ex) {
394 // This should not happen!
395 DEBTRACE("Service required is invalid [does not exist]." );
400 // Bind a context called "test" to the root context:
402 CosNaming::Name contextName;
403 contextName.length(1);
404 contextName[0].id = (const char*) "test"; // string copied
405 contextName[0].kind = (const char*) "my_context"; // string copied
406 // Note on kind: The kind field is used to indicate the type
407 // of the object. This is to avoid conventions such as that used
408 // by files (name.type -- e.g. test.ps = postscript etc.)
410 CosNaming::NamingContext_var testContext;
412 // Bind the context to root.
413 testContext = rootContext->bind_new_context(contextName);
415 catch(CosNaming::NamingContext::AlreadyBound& ex) {
416 // If the context already exists, this exception will be raised.
417 // In this case, just resolve the name and assign testContext
418 // to the object returned:
419 CORBA::Object_var obj;
420 obj = rootContext->resolve(contextName);
421 testContext = CosNaming::NamingContext::_narrow(obj);
422 if( CORBA::is_nil(testContext) ) {
423 DEBTRACE("Failed to narrow naming context.");
428 // Bind objref with name Echo to the testContext:
429 CosNaming::Name objectName;
430 objectName.length(1);
431 objectName[0].id = name; // string copied
432 objectName[0].kind = (const char*) "Object"; // string copied
435 testContext->bind(objectName, objref);
437 catch(CosNaming::NamingContext::AlreadyBound& ex) {
438 testContext->rebind(objectName, objref);
440 // Note: Using rebind() will overwrite any Object previously bound
441 // to /test/Echo with obj.
442 // Alternatively, bind() can be used, which will raise a
443 // CosNaming::NamingContext::AlreadyBound exception if the name
444 // supplied is already bound to an object.
446 // Amendment: When using OrbixNames, it is necessary to first try bind
447 // and then rebind, as rebind on it's own will throw a NotFoundexception if
448 // the Name has not already been bound. [This is incorrect behaviour -
449 // it should just bind].
451 catch(CORBA::COMM_FAILURE& ex) {
452 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
453 << "naming service.");
456 catch(CORBA::SystemException&) {
457 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");