Salome HOME
Merge branch 'V9_9_BR'
[modules/yacs.git] / src / runtime / Test / echoSrv.cxx
1 // Copyright (C) 2006-2022  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <time.h>
21
22 #include <echo.hh>
23
24 #include <iostream>
25 using namespace std;
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
31
32 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
33 {
34   CORBA::Any tmp;
35   tmp<<= e;
36   CORBA::TypeCode_var tc = tmp.type();
37   const char *p = tc->name();
38   if ( *p != '\0' ) {
39     os<<p;
40   }
41   else  {
42     os << tc->id();
43   }
44   return os;
45 }
46
47 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
48 {
49 public:
50   inline Obj_i() {}
51   virtual ~Obj_i() {}
52   CORBA::Long echoLong(CORBA::Long i);
53 };
54
55 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
56 {
57 public:
58   inline C_i() {}
59   virtual ~C_i() {}
60   CORBA::Long echoLong(CORBA::Long i);
61 };
62
63 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
64 {
65 public:
66   inline D_i() {}
67   virtual ~D_i() {}
68   CORBA::Long echoLong(CORBA::Long i);
69   CORBA::Long echoLong2(CORBA::Long i);
70 };
71
72 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
73 {
74 public:
75   inline E_i() {}
76   virtual ~E_i() {}
77   CORBA::Long echoLong(CORBA::Long i);
78   CORBA::Long echoLong2(CORBA::Long i);
79 };
80
81 class Echo_i : public POA_eo::Echo,
82                public PortableServer::RefCountServantBase
83 {
84 public:
85   inline Echo_i() {}
86   virtual ~Echo_i() {}
87   virtual char* echoString(const char* mesg);
88   CORBA::Long echoLong(CORBA::Long i);
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);
100
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();
106 };
107
108 //Implementation Echo
109 PortableServer::POA_ptr Echo_i::_default_POA()
110 {
111   PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
112   try{
113     return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
114   }
115   catch(...){
116     //return PortableServer::POA::_duplicate(root_poa);
117     return root_poa._retn();
118   }
119 }
120
121 char* Echo_i::echoString(const char* mesg)
122 {
123   DEBTRACE("Echo_i::echoString " << mesg);
124   return CORBA::string_dup(mesg);
125 }
126
127 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
128   DEBTRACE("Echo_i::echoDouble " << i);
129   j=i+1;
130 }
131
132 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
133 {
134   DEBTRACE("Echo_i::echoIntVec " << in.length());
135   for(int i=0;i<in.length(); i++){
136     DEBTRACE(in[i]);
137   };
138   out=new eo::IntVec(in);
139 }
140
141 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
142 {
143   DEBTRACE("Echo_i::echoStrVec " << in.length());
144   for(int i=0;i<in.length(); i++){
145     DEBTRACE(in[i]);
146   }
147   out=new eo::StrVec(in);
148 }
149
150 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
151 {
152   DEBTRACE("Echo_i::echoObjectVec " << in.length());
153   for(int i=0;i<in.length(); i++){
154     DEBTRACE(in[i]->_PD_repoId);
155   };
156   out=new eo::ObjectVec(in);
157 }
158
159 void Echo_i::echoObjVec(const eo::ObjVec& in, eo::ObjVec_out out)
160   {
161     DEBTRACE("Echo_i::echoObjVec " << in.length());
162     for(int i=0;i<in.length(); i++)
163       {
164         DEBTRACE(in[i]->_mostDerivedRepoId());
165       };
166
167     out=new eo::ObjVec;
168     out->length(2);
169
170     Obj_i* obj0 = new Obj_i();
171     CORBA::Object_var ref0 = obj0->_this();
172     eo::Obj_var o0 = eo::Obj::_narrow(ref0);
173     (*out)[0]=o0;
174     obj0->_remove_ref();
175
176     C_i* obj1 = new C_i();
177     CORBA::Object_var ref1 = obj1->_this();
178     eo::Obj_var o1 = eo::Obj::_narrow(ref1);
179     (*out)[1]=o1;
180     obj1->_remove_ref();
181
182     DEBTRACE("ENDOF Echo_i::echoObjVec " );
183 }
184
185 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
186 {
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);
191     };
192   };
193   out=new eo::ObjectVecVec(in);
194 }
195
196 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out ) 
197 {
198   DEBTRACE("Echo_i::echoDoubleVec " << in.length());
199   for(int i=0;i<in.length(); i++){
200     DEBTRACE(in[i]);
201   };
202   out=new eo::DoubleVec(in);
203 }
204
205 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
206 {
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++){
210       DEBTRACE(in[i][j]);
211     };
212   };
213   out=new eo::DoubleVecVec(in);
214 }
215
216 CORBA::Long Echo_i::echoLong(CORBA::Long i )
217 {
218   DEBTRACE("Echo_i::echoLong " << i);
219   if(i < 0) {
220     eo::ExceptionStruct es;
221     es.type = eo::COMM;
222     es.text = "error Socket exception";
223     throw eo::SALOME_Exception(es);
224   }
225
226   CORBA::Long j=i+1;
227   return j;
228 }
229
230 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
231   DEBTRACE("Echo_i::echoC ");
232   o->echoLong(10);
233   oo=eo::C::_duplicate(o); 
234 }
235
236 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
237   DEBTRACE("Echo_i::echoD ");
238   o->echoLong2(10);
239   //oo=eo::D::_duplicate(o); 
240   D_i* myD = new D_i();
241   oo=myD->_this();
242   myD->_remove_ref();
243 }
244
245 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
246   DEBTRACE("Echo_i::echoObj2 ");
247   o->echoLong(10);
248   oo=eo::Obj::_duplicate(o); 
249 }
250
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); 
255 }
256
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();
263 }
264
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();
271 }
272
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)
274 {
275   DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
276   dd=d;
277   ll=l;
278   s=CORBA::string_dup(m);
279   oo=eo::Obj::_duplicate(o); 
280 };
281
282 //Implementation Obj
283 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
284   DEBTRACE("Obj_i::echoLong " << i );
285   CORBA::Long j=i+1;
286   return j;
287 }
288
289 //Implementation C
290 CORBA::Long C_i::echoLong(CORBA::Long i ){
291   DEBTRACE("C_i::echoLong " << i);
292   CORBA::Long j=i+5;
293   return j;
294 }
295
296 //Implementation D
297 CORBA::Long D_i::echoLong2(CORBA::Long i ){
298   DEBTRACE("D_i::echoLong " << i);
299   CORBA::Long j=i+10;
300   return j;
301 }
302 CORBA::Long D_i::echoLong(CORBA::Long i ){
303   DEBTRACE("D_i::echoLong " << i);
304   CORBA::Long j=i+1;
305   return j;
306 }
307
308 //Implementation E
309 CORBA::Long E_i::echoLong2(CORBA::Long i ){
310   DEBTRACE("E_i::echoLong " << i);
311   CORBA::Long j=i+20;
312   return j;
313 }
314 CORBA::Long E_i::echoLong(CORBA::Long i ){
315   DEBTRACE("E_i::echoLong " << i);
316   CORBA::Long j=i+15;
317   return j;
318 }
319
320 CORBA::ORB_ptr orb;
321 eo::Echo_var myechoref;
322
323 int main(int argc, char** argv)
324 {
325   try {
326     orb = CORBA::ORB_init(argc, argv);
327
328     {
329       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
330       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
331       // POA manager
332       PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
333       poa_man->activate();
334
335       // Create a new POA with the shortcut policy
336       CORBA::PolicyList pl2;
337       pl2.length(2);
338       CORBA::Any v;
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);
343
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
347       // stringified IOR.
348       obj = myecho->_this();
349       CORBA::String_var sior(orb->object_to_string(obj));
350       DEBTRACE("'" << (char*)sior << "'");
351       myechoref = eo::Echo::_narrow(obj);
352
353       if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
354
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();
359
360       //create object C and register it in naming service
361       C_i* myC = new C_i();
362       obj=myC->_this();
363       eo::C_var myCref=eo::C::_narrow(obj);
364       myC->_remove_ref();
365       if( !bindObjectToName(orb, myCref,"C") ) return 1;
366
367       //create object D and register it in naming service
368       D_i* myD = new D_i();
369       obj=myD->_this();
370       eo::D_var myDref=eo::D::_narrow(obj);
371       myD->_remove_ref();
372       if( !bindObjectToName(orb, myDref,"D") ) return 1;
373
374       //create object Obj and register it in naming service
375       Obj_i* myObj = new Obj_i();
376       obj=myObj->_this();
377       eo::Obj_var myObjref=eo::Obj::_narrow(obj);
378       myObj->_remove_ref();
379       if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
380     }
381     orb->run();
382   }
383   catch(CORBA::SystemException&) {
384     DEBTRACE("Caught CORBA::SystemException.");
385   }
386   catch(CORBA::Exception& ex) {
387     DEBTRACE("Caught CORBA::Exception." << ex);
388   }
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());
394   }
395   catch(...) {
396     DEBTRACE("Caught unknown exception." );
397   }
398
399   return 0;
400 }
401
402
403 //////////////////////////////////////////////////////////////////////
404
405 static CORBA::Boolean
406 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
407 {
408   CosNaming::NamingContext_var rootContext;
409
410   try {
411     // Obtain a reference to the root context of the Name service:
412     CORBA::Object_var obj;
413     obj = orb->resolve_initial_references("NameService");
414
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.");
419       return 0;
420     }
421   }
422   catch(CORBA::ORB::InvalidName& ex) {
423     // This should not happen!
424     DEBTRACE("Service required is invalid [does not exist]." );
425     return 0;
426   }
427
428   try {
429     // Bind a context called "test" to the root context:
430
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.)
438
439     CosNaming::NamingContext_var testContext;
440     try {
441       // Bind the context to root.
442       testContext = rootContext->bind_new_context(contextName);
443     }
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.");
453         return 0;
454       }
455     }
456
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
462
463     try {
464       testContext->bind(objectName, objref);
465     }
466     catch(CosNaming::NamingContext::AlreadyBound& ex) {
467       testContext->rebind(objectName, objref);
468     }
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.
474
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].
479   }
480   catch(CORBA::COMM_FAILURE& ex) {
481     DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
482              << "naming service.");
483     return 0;
484   }
485   catch(CORBA::SystemException&) {
486     DEBTRACE("Caught a CORBA::SystemException while using the naming service.");
487     return 0;
488   }
489
490   return 1;
491 }
492