1 // Copyright (C) 2006-2020 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
29 #define MYDEBTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
30 #define DEBTRACE(msg) {MYDEBTRACE; std::cerr<<msg<<std::endl<<std::flush;}
36 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
38 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
42 CORBA::TypeCode_var tc = tmp.type();
43 const char *p = tc->name();
53 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
58 CORBA::Long echoLong(CORBA::Long i);
61 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
66 CORBA::Long echoLong(CORBA::Long i);
69 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
74 CORBA::Long echoLong(CORBA::Long i);
75 CORBA::Long echoLong2(CORBA::Long i);
78 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
83 CORBA::Long echoLong(CORBA::Long i);
84 CORBA::Long echoLong2(CORBA::Long i);
87 class Echo_i : public POA_eo::Echo,
88 public PortableServer::RefCountServantBase
93 virtual char* echoString(const char* mesg);
94 CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
95 void echoDouble(CORBA::Double i,CORBA::Double& j) ;
96 void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
97 void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
98 void echoIntVec(const eo::IntVec&, eo::IntVec_out);
99 void echoStrVec(const eo::StrVec&, eo::StrVec_out);
100 void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
101 void echoObj2(eo::Obj_ptr , eo::Obj_out);
102 void echoD(eo::D_ptr , eo::D_out);
103 void echoC(eo::C_ptr , eo::C_out);
104 void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
106 eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
107 void createObj(CORBA::Long i, eo::Obj_out oo);
108 void createC(eo::C_out oo);
109 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);
110 virtual PortableServer::POA_ptr _default_POA();
113 //Implementation Echo
114 PortableServer::POA_ptr Echo_i::_default_POA()
116 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
118 return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
121 //return PortableServer::POA::_duplicate(root_poa);
122 return root_poa._retn();
126 char* Echo_i::echoString(const char* mesg)
128 DEBTRACE("Echo_i::echoString " << mesg);
129 return CORBA::string_dup(mesg);
132 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
133 DEBTRACE("Echo_i::echoDouble " << i);
137 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
139 DEBTRACE("Echo_i::echoIntVec " << in.length());
140 for(int i=0;i<in.length(); i++){
143 out=new eo::IntVec(in);
146 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
148 DEBTRACE("Echo_i::echoStrVec " << in.length());
149 for(int i=0;i<in.length(); i++){
152 out=new eo::StrVec(in);
155 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
157 DEBTRACE("Echo_i::echoObjectVec " << in.length());
158 for(int i=0;i<in.length(); i++){
159 DEBTRACE(in[i]->_PD_repoId);
161 out=new eo::ObjectVec(in);
164 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
166 DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
167 for(int i=0;i< in.length(); i++){
168 for(int j=0;j< in[i].length(); j++){
169 DEBTRACE(in[i][j]->_PD_repoId);
172 out=new eo::ObjectVecVec(in);
175 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out )
177 DEBTRACE("Echo_i::echoDoubleVec " << in.length());
178 for(int i=0;i<in.length(); i++){
181 out=new eo::DoubleVec(in);
184 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
186 DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
187 for(int i=0;i< in.length(); i++){
188 for(int j=0;j< in[i].length(); j++){
192 out=new eo::DoubleVecVec(in);
195 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception)
197 DEBTRACE("Echo_i::echoLong " << i);
202 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
203 DEBTRACE("Echo_i::echoC ");
205 oo=eo::C::_duplicate(o);
208 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
209 DEBTRACE("Echo_i::echoD ");
211 //oo=eo::D::_duplicate(o);
212 D_i* myD = new D_i();
217 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
218 DEBTRACE("Echo_i::echoObj2 ");
220 oo=eo::Obj::_duplicate(o);
223 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
224 DEBTRACE("Echo_i::echoObj " << i << "," << j );
225 oo=eo::Obj::_duplicate(o);
226 return eo::Obj::_duplicate(o);
229 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
230 DEBTRACE("Echo_i::createObj " << i);
231 Obj_i* myobj = new Obj_i();
232 CORBA::Object_var myref = myobj->_this();
233 oo = eo::Obj::_narrow(myref);
234 myobj->_remove_ref();
237 void Echo_i::createC(eo::C_out oo){
238 DEBTRACE("Echo_i::createC ");
239 C_i* myobj = new C_i();
240 CORBA::Object_var myref = myobj->_this();
241 oo = eo::C::_narrow(myref);
242 myobj->_remove_ref();
245 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)
247 DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
250 s=CORBA::string_dup(m);
251 oo=eo::Obj::_duplicate(o);
255 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
256 DEBTRACE("Obj_i::echoLong " << i );
262 CORBA::Long C_i::echoLong(CORBA::Long i ){
263 DEBTRACE("C_i::echoLong " << i);
269 CORBA::Long D_i::echoLong2(CORBA::Long i ){
270 DEBTRACE("D_i::echoLong " << i);
274 CORBA::Long D_i::echoLong(CORBA::Long i ){
275 DEBTRACE("D_i::echoLong " << i);
281 CORBA::Long E_i::echoLong2(CORBA::Long i ){
282 DEBTRACE("E_i::echoLong " << i);
286 CORBA::Long E_i::echoLong(CORBA::Long i ){
287 DEBTRACE("E_i::echoLong " << i);
293 eo::Echo_var myechoref;
295 int main(int argc, char** argv)
298 orb = CORBA::ORB_init(argc, argv);
301 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
302 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
304 PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
307 // Create a new POA with the shortcut policy
308 CORBA::PolicyList pl2;
311 v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
312 pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
313 pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
314 PortableServer::POA_ptr shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
316 // Create and activate servant
317 Echo_i* myecho = new Echo_i();
318 // Obtain a reference to the object, and print it out as a
320 obj = myecho->_this();
321 CORBA::String_var sior(orb->object_to_string(obj));
322 DEBTRACE("'" << (char*)sior << "'");
323 myechoref = eo::Echo::_narrow(obj);
325 if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
327 // Decrement the reference count of the object implementation, so
328 // that it will be properly cleaned up when the POA has determined
329 // that it is no longer needed.
330 myecho->_remove_ref();
332 //create object C and register it in naming service
333 C_i* myC = new C_i();
335 eo::C_var myCref=eo::C::_narrow(obj);
337 if( !bindObjectToName(orb, myCref,"C") ) return 1;
339 //create object D and register it in naming service
340 D_i* myD = new D_i();
342 eo::D_var myDref=eo::D::_narrow(obj);
344 if( !bindObjectToName(orb, myDref,"D") ) return 1;
346 //create object Obj and register it in naming service
347 Obj_i* myObj = new Obj_i();
349 eo::Obj_var myObjref=eo::Obj::_narrow(obj);
350 myObj->_remove_ref();
351 if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
355 catch(CORBA::SystemException&) {
356 DEBTRACE("Caught CORBA::SystemException.");
358 catch(CORBA::Exception& ex) {
359 DEBTRACE("Caught CORBA::Exception." << ex);
361 catch(omniORB::fatalException& fe) {
362 DEBTRACE("Caught omniORB::fatalException:");
363 DEBTRACE(" file: " << fe.file());
364 DEBTRACE(" line: " << fe.line());
365 DEBTRACE(" mesg: " << fe.errmsg());
368 DEBTRACE("Caught unknown exception." );
375 //////////////////////////////////////////////////////////////////////
377 static CORBA::Boolean
378 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
380 CosNaming::NamingContext_var rootContext;
383 // Obtain a reference to the root context of the Name service:
384 CORBA::Object_var obj;
385 obj = orb->resolve_initial_references("NameService");
387 // Narrow the reference returned.
388 rootContext = CosNaming::NamingContext::_narrow(obj);
389 if( CORBA::is_nil(rootContext) ) {
390 DEBTRACE("Failed to narrow the root naming context.");
394 catch(CORBA::ORB::InvalidName& ex) {
395 // This should not happen!
396 DEBTRACE("Service required is invalid [does not exist]." );
401 // Bind a context called "test" to the root context:
403 CosNaming::Name contextName;
404 contextName.length(1);
405 contextName[0].id = (const char*) "test"; // string copied
406 contextName[0].kind = (const char*) "my_context"; // string copied
407 // Note on kind: The kind field is used to indicate the type
408 // of the object. This is to avoid conventions such as that used
409 // by files (name.type -- e.g. test.ps = postscript etc.)
411 CosNaming::NamingContext_var testContext;
413 // Bind the context to root.
414 testContext = rootContext->bind_new_context(contextName);
416 catch(CosNaming::NamingContext::AlreadyBound& ex) {
417 // If the context already exists, this exception will be raised.
418 // In this case, just resolve the name and assign testContext
419 // to the object returned:
420 CORBA::Object_var obj;
421 obj = rootContext->resolve(contextName);
422 testContext = CosNaming::NamingContext::_narrow(obj);
423 if( CORBA::is_nil(testContext) ) {
424 DEBTRACE("Failed to narrow naming context.");
429 // Bind objref with name Echo to the testContext:
430 CosNaming::Name objectName;
431 objectName.length(1);
432 objectName[0].id = name; // string copied
433 objectName[0].kind = (const char*) "Object"; // string copied
436 testContext->bind(objectName, objref);
438 catch(CosNaming::NamingContext::AlreadyBound& ex) {
439 testContext->rebind(objectName, objref);
441 // Note: Using rebind() will overwrite any Object previously bound
442 // to /test/Echo with obj.
443 // Alternatively, bind() can be used, which will raise a
444 // CosNaming::NamingContext::AlreadyBound exception if the name
445 // supplied is already bound to an object.
447 // Amendment: When using OrbixNames, it is necessary to first try bind
448 // and then rebind, as rebind on it's own will throw a NotFoundexception if
449 // the Name has not already been bound. [This is incorrect behaviour -
450 // it should just bind].
452 catch(CORBA::COMM_FAILURE& ex) {
453 DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
454 << "naming service.");
457 catch(CORBA::SystemException&) {
458 DEBTRACE("Caught a CORBA::SystemException while using the naming service.");