]> SALOME platform Git repositories - modules/yacs.git/blob - src/yacsloader/Test/echoSrv.cxx
Salome HOME
mergefrom branch BR_V511_PR tag mergeto_trunk_03feb09
[modules/yacs.git] / src / yacsloader / Test / echoSrv.cxx
1 //  Copyright (C) 2006-2008  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.
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 #include <time.h>
20 #include <pthread.h>
21 #include <unistd.h>
22
23 #include <echo.hh>
24
25 #include <iostream>
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 using namespace std;
31
32 CORBA::ORB_var orb;
33 static CORBA::Boolean bindObjectToName(CORBA::ORB_ptr, CORBA::Object_ptr,const char*);
34
35 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
36 {
37   CORBA::Any tmp;
38   tmp<<= e;
39   CORBA::TypeCode_var tc = tmp.type();
40   const char *p = tc->name();
41   if ( *p != '\0' ) {
42     os<<p;
43   }
44   else  {
45     os << tc->id();
46   }
47   return os;
48 }
49
50 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
51 {
52 public:
53   inline Obj_i() {}
54   virtual ~Obj_i() {}
55   CORBA::Long echoLong(CORBA::Long i);
56 };
57
58 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
59 {
60 public:
61   inline C_i() {}
62   virtual ~C_i() {}
63   CORBA::Long echoLong(CORBA::Long i);
64 };
65
66 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
67 {
68 public:
69   inline D_i() {}
70   virtual ~D_i() {}
71   CORBA::Long echoLong(CORBA::Long i);
72   CORBA::Long echoLong2(CORBA::Long i);
73 };
74
75 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
76 {
77 public:
78   inline E_i() {}
79   virtual ~E_i() {}
80   CORBA::Long echoLong(CORBA::Long i);
81   CORBA::Long echoLong2(CORBA::Long i);
82 };
83
84 class Echo_i : public POA_eo::Echo,
85                public PortableServer::RefCountServantBase
86 {
87 public:
88   inline Echo_i() {}
89   virtual ~Echo_i() {}
90   virtual char* echoString(const char* mesg);
91   virtual CORBA::Boolean echoBoolean(CORBA::Boolean b);
92   CORBA::Long echoLong(CORBA::Long i) throw(eo::SALOME_Exception);
93   void echoDouble(CORBA::Double i,CORBA::Double& j) ;
94   void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
95   void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
96   void echoIntVec(const eo::IntVec&, eo::IntVec_out);
97   void echoStrVec(const eo::StrVec&, eo::StrVec_out);
98   void echoBoolVec(const eo::BoolVec&, eo::BoolVec_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);
104
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   void sleepLong(CORBA::Double time1,CORBA::Double& time2) ;
110   virtual eo::S2* echoStruct(const eo::S2&);
111   virtual eo::S3* echoStruct2(const eo::S3&);
112   virtual void shutdown();
113   virtual PortableServer::POA_ptr _default_POA();
114 protected:
115   int _ctr;
116   pthread_mutex_t _mutex;
117 };
118
119 //Implementation Echo
120 PortableServer::POA_ptr Echo_i::_default_POA()
121 {
122   _ctr =0;
123   pthread_mutex_init(&_mutex, NULL);
124   PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
125   try{
126     return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
127   }
128   catch(...){
129     //return PortableServer::POA::_duplicate(root_poa);
130     return root_poa._retn();
131   }
132 }
133
134
135 void Echo_i::shutdown()
136 {
137   // Shutdown the ORB (but do not wait for completion).  This also
138   // causes the main thread to unblock from CORBA::ORB::run().
139   orb->shutdown(0);
140 }
141
142 char* Echo_i::echoString(const char* mesg)
143 {
144   DEBTRACE("Echo_i::echoString " << mesg);
145   return CORBA::string_dup(mesg);
146 }
147
148 CORBA::Boolean Echo_i::echoBoolean(CORBA::Boolean b ) 
149 {
150   DEBTRACE("Echo_i::echoBoolean " << b);
151   return b;
152 }
153
154 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
155   DEBTRACE("Echo_i::echoDouble " << i);
156   j=i+1;
157 }
158
159 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
160 {
161   DEBTRACE("Echo_i::echoIntVec " << in.length());
162   for(int i=0;i<in.length(); i++){
163     DEBTRACE(in[i]);
164   };
165   out=new eo::IntVec(in);
166 }
167
168 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
169 {
170   DEBTRACE("Echo_i::echoStrVec " << in.length());
171   for(int i=0;i<in.length(); i++){
172     DEBTRACE(in[i]);
173   }
174   out=new eo::StrVec(in);
175 }
176
177 void Echo_i::echoBoolVec(const eo::BoolVec& in, eo::BoolVec_out out)
178 {
179   DEBTRACE("Echo_i::echoBoolVec " << in.length());
180   for(int i=0;i<in.length(); i++){
181     DEBTRACE(in[i]);
182   };
183   out=new eo::BoolVec(in);
184 }
185
186 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
187 {
188   DEBTRACE("Echo_i::echoObjectVec " << in.length());
189   for(int i=0;i<in.length(); i++){
190     DEBTRACE(in[i]->_PD_repoId);
191   };
192   out=new eo::ObjectVec(in);
193 }
194
195 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
196 {
197   DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
198   for(int i=0;i< in.length(); i++){
199     for(int j=0;j< in[i].length(); j++){
200       DEBTRACE(in[i][j]->_PD_repoId);
201     };
202   };
203   out=new eo::ObjectVecVec(in);
204 }
205
206 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out ) 
207 {
208   DEBTRACE("Echo_i::echoDoubleVec " << in.length());
209   for(int i=0;i<in.length(); i++){
210     DEBTRACE(in[i]);
211   };
212   out=new eo::DoubleVec(in);
213 }
214
215 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
216 {
217   DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
218   for(int i=0;i< in.length(); i++){
219     for(int j=0;j< in[i].length(); j++){
220       DEBTRACE(in[i][j]);
221     };
222   };
223   out=new eo::DoubleVecVec(in);
224 }
225
226 CORBA::Long Echo_i::echoLong(CORBA::Long i ) throw(eo::SALOME_Exception) 
227 {
228   DEBTRACE("Echo_i::echoLong " << i);
229   if(i < 0) {
230     eo::ExceptionStruct es;
231     es.type = eo::COMM;
232     es.text = "error Socket exception";
233     throw eo::SALOME_Exception(es);
234   }
235
236   CORBA::Long j=i+1;
237   return j;
238 }
239
240 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
241   DEBTRACE("Echo_i::echoC ");
242   o->echoLong(10);
243   oo=eo::C::_duplicate(o); 
244 }
245
246 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
247   DEBTRACE("Echo_i::echoD ");
248   o->echoLong2(10);
249   //oo=eo::D::_duplicate(o); 
250   D_i* myD = new D_i();
251   oo=myD->_this();
252   myD->_remove_ref();
253 }
254
255 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
256   DEBTRACE("Echo_i::echoObj2 ");
257   o->echoLong(10);
258   oo=eo::Obj::_duplicate(o); 
259 }
260
261 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
262   DEBTRACE("Echo_i::echoObj " << i << "," << j );
263   oo=eo::Obj::_duplicate(o); 
264   return eo::Obj::_duplicate(o); 
265 }
266
267 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
268   DEBTRACE("Echo_i::createObj " << i);
269   Obj_i* myobj = new Obj_i();
270   CORBA::Object_var myref = myobj->_this();
271   oo = eo::Obj::_narrow(myref);
272   myobj->_remove_ref();
273 }
274
275 void Echo_i::createC(eo::C_out oo){
276   DEBTRACE("Echo_i::createC ");
277   C_i* myobj = new C_i();
278   CORBA::Object_var myref = myobj->_this();
279   oo = eo::C::_narrow(myref);
280   myobj->_remove_ref();
281 }
282
283 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)
284 {
285   DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
286   dd=d;
287   ll=l;
288   s=CORBA::string_dup(m);
289   oo=eo::Obj::_duplicate(o); 
290 };
291
292 void Echo_i::sleepLong(CORBA::Double time1, CORBA::Double& time2)
293 {
294   DEBTRACE("Echo_i::sleepLong");
295   pthread_mutex_lock(&_mutex);
296   int num = _ctr++;
297   pthread_mutex_unlock(&_mutex);
298   DEBTRACE("Echo_i::sleepLong start " << num);
299   unsigned int t=(unsigned int) time1;
300   sleep(t);
301   DEBTRACE("Echo_i::sleepLong stop  " << num);
302   time2 = time1;
303 }
304
305 eo::S2* Echo_i::echoStruct(const eo::S2& s)
306 {
307   DEBTRACE("Echo_i::echoStruct " << s.s.x << " " << s.s.y);
308   eo::S1 s1;
309   s1.x=10.;
310   s1.y=2;
311   eo::S2* s2=new eo::S2;
312   s2->s=s1;
313   return s2;
314 }
315
316 eo::S3* Echo_i::echoStruct2(const eo::S3& s)
317 {
318   DEBTRACE("Echo_i::echoStruct " << s.x << " " << s.y);
319   if( !CORBA::is_nil(s.ob) ) 
320     {
321       std::cerr << s.ob->echoLong(10) << std::endl;
322     }
323   eo::S3* s2=new eo::S3;
324   s2->x=10.;
325   s2->y=2;
326   return s2;
327 }
328
329 //Implementation Obj
330 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
331   DEBTRACE("Obj_i::echoLong " << i );
332   CORBA::Long j=i+1;
333   return j;
334 }
335
336 //Implementation C
337 CORBA::Long C_i::echoLong(CORBA::Long i ){
338   DEBTRACE("C_i::echoLong " << i);
339   CORBA::Long j=i+5;
340   return j;
341 }
342
343 //Implementation D
344 CORBA::Long D_i::echoLong2(CORBA::Long i ){
345   DEBTRACE("D_i::echoLong " << i);
346   CORBA::Long j=i+10;
347   return j;
348 }
349 CORBA::Long D_i::echoLong(CORBA::Long i ){
350   DEBTRACE("D_i::echoLong " << i);
351   CORBA::Long j=i+1;
352   return j;
353 }
354
355 //Implementation E
356 CORBA::Long E_i::echoLong2(CORBA::Long i ){
357   DEBTRACE("E_i::echoLong " << i);
358   CORBA::Long j=i+20;
359   return j;
360 }
361 CORBA::Long E_i::echoLong(CORBA::Long i ){
362   DEBTRACE("E_i::echoLong " << i);
363   CORBA::Long j=i+15;
364   return j;
365 }
366
367 eo::Echo_var myechoref;
368
369 int main(int argc, char** argv)
370 {
371   try {
372     orb = CORBA::ORB_init(argc, argv);
373
374     {
375       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
376       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
377       // POA manager
378       PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
379       poa_man->activate();
380
381       // Create a new POA with the shortcut policy
382       /*
383       CORBA::PolicyList pl2;
384       pl2.length(2);
385       CORBA::Any v;
386       v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
387       pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
388       pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
389       PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
390       */
391
392       // Create and activate servant
393       Echo_i* myecho = new Echo_i();
394       // Obtain a reference to the object
395       CORBA::Object_var obj2 = myecho->_this();
396       myechoref = eo::Echo::_narrow(obj2);
397       // Decrement the reference count of the object implementation, so
398       // that it will be properly cleaned up when the POA has determined
399       // that it is no longer needed.
400       myecho->_remove_ref();
401
402       // print the reference as a stringified IOR.
403       CORBA::String_var sior(orb->object_to_string(obj2));
404       DEBTRACE("'" << (char*)sior << "'");
405
406       if( !bindObjectToName(orb, myechoref,"Echo") ) return 1;
407
408       //create object C and register it in naming service
409       C_i* myC = new C_i();
410       CORBA::Object_var obj3 =myC->_this();
411       eo::C_var myCref=eo::C::_narrow(obj3);
412       myC->_remove_ref();
413       if( !bindObjectToName(orb, myCref,"C") ) return 1;
414
415       //create object D and register it in naming service
416       D_i* myD = new D_i();
417       CORBA::Object_var obj4=myD->_this();
418       eo::D_var myDref=eo::D::_narrow(obj4);
419       myD->_remove_ref();
420       if( !bindObjectToName(orb, myDref,"D") ) return 1;
421
422       //create object Obj and register it in naming service
423       Obj_i* myObj = new Obj_i();
424       CORBA::Object_var obj5=myObj->_this();
425       eo::Obj_var myObjref=eo::Obj::_narrow(obj5);
426       myObj->_remove_ref();
427       if( !bindObjectToName(orb, myObjref,"Obj") ) return 1;
428     }
429     orb->run();
430     std::cout << "Returned from orb->run()." << std::endl;
431     orb->destroy();
432   }
433   catch(CORBA::SystemException&) {
434     DEBTRACE("Caught CORBA::SystemException.");
435   }
436   catch(CORBA::Exception& ex) {
437     DEBTRACE("Caught CORBA::Exception." << ex);
438   }
439   catch(omniORB::fatalException& fe) {
440     DEBTRACE("Caught omniORB::fatalException:");
441     DEBTRACE("  file: " << fe.file());
442     DEBTRACE("  line: " << fe.line());
443     DEBTRACE("  mesg: " << fe.errmsg());
444   }
445   catch(...) {
446     DEBTRACE("Caught unknown exception." );
447   }
448
449   return 0;
450 }
451
452
453 //////////////////////////////////////////////////////////////////////
454
455 static CORBA::Boolean
456 bindObjectToName(CORBA::ORB_ptr orb, CORBA::Object_ptr objref,const char *name)
457 {
458   CosNaming::NamingContext_var rootContext;
459
460   try {
461     // Obtain a reference to the root context of the Name service:
462     CORBA::Object_var obj;
463     obj = orb->resolve_initial_references("NameService");
464
465     // Narrow the reference returned.
466     rootContext = CosNaming::NamingContext::_narrow(obj);
467     if( CORBA::is_nil(rootContext) ) {
468       DEBTRACE("Failed to narrow the root naming context.");
469       return 0;
470     }
471   }
472   catch(CORBA::ORB::InvalidName& ex) {
473     // This should not happen!
474     DEBTRACE("Service required is invalid [does not exist]." );
475     return 0;
476   }
477
478   try {
479     // Bind a context called "test" to the root context:
480
481     CosNaming::Name contextName;
482     contextName.length(1);
483     contextName[0].id   = (const char*) "test";       // string copied
484     contextName[0].kind = (const char*) "my_context"; // string copied
485     // Note on kind: The kind field is used to indicate the type
486     // of the object. This is to avoid conventions such as that used
487     // by files (name.type -- e.g. test.ps = postscript etc.)
488
489     CosNaming::NamingContext_var testContext;
490     try {
491       // Bind the context to root.
492       testContext = rootContext->bind_new_context(contextName);
493     }
494     catch(CosNaming::NamingContext::AlreadyBound& ex) {
495       // If the context already exists, this exception will be raised.
496       // In this case, just resolve the name and assign testContext
497       // to the object returned:
498       CORBA::Object_var obj;
499       obj = rootContext->resolve(contextName);
500       testContext = CosNaming::NamingContext::_narrow(obj);
501       if( CORBA::is_nil(testContext) ) {
502         DEBTRACE("Failed to narrow naming context.");
503         return 0;
504       }
505     }
506
507     // Bind objref with name Echo to the testContext:
508     CosNaming::Name objectName;
509     objectName.length(1);
510     objectName[0].id   = name;   // string copied
511     objectName[0].kind = (const char*) "Object"; // string copied
512
513     try {
514       testContext->bind(objectName, objref);
515     }
516     catch(CosNaming::NamingContext::AlreadyBound& ex) {
517       testContext->rebind(objectName, objref);
518     }
519     // Note: Using rebind() will overwrite any Object previously bound
520     //       to /test/Echo with obj.
521     //       Alternatively, bind() can be used, which will raise a
522     //       CosNaming::NamingContext::AlreadyBound exception if the name
523     //       supplied is already bound to an object.
524
525     // Amendment: When using OrbixNames, it is necessary to first try bind
526     // and then rebind, as rebind on it's own will throw a NotFoundexception if
527     // the Name has not already been bound. [This is incorrect behaviour -
528     // it should just bind].
529   }
530   catch(CORBA::COMM_FAILURE& ex) {
531     DEBTRACE("Caught system exception COMM_FAILURE -- unable to contact the "
532              << "naming service.");
533     return 0;
534   }
535   catch(CORBA::SystemException&) {
536     DEBTRACE("Caught a CORBA::SystemException while using the naming service.");
537     return 0;
538   }
539
540   return 1;
541 }
542