Salome HOME
[EDF27816] : Get rid of SALOME session for 3 last tests (YacsLoader, YacsLoader_Swig...
[modules/yacs.git] / src / yacsloader / Test / echoSrv.cxx
1 // Copyright (C) 2006-2023  CEA, EDF
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 #include <pthread.h>
22 #ifdef WIN32
23 #include <windows.h>
24 #define sleep _sleep
25 #else
26 #include <unistd.h>
27 #endif
28
29 #include <echo.hh>
30
31 #include <iostream>
32 #include <memory>
33
34 //#define _DEVDEBUG_
35 #include "YacsTrace.hxx"
36
37 #include <SALOMEconfig.h>
38 #include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService)
39
40 using namespace std;
41
42 CORBA::ORB_var orb;
43 static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr,const char*);
44
45 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
46 {
47   CORBA::Any tmp;
48   tmp<<= e;
49   CORBA::TypeCode_var tc = tmp.type();
50   const char *p = tc->name();
51   if ( *p != '\0' ) {
52     os<<p;
53   }
54   else  {
55     os << tc->id();
56   }
57   return os;
58 }
59
60 class Obj_i : public POA_eo::Obj, public PortableServer::RefCountServantBase
61 {
62 public:
63   inline Obj_i() {}
64   virtual ~Obj_i() {}
65   CORBA::Long echoLong(CORBA::Long i);
66 };
67
68 class C_i : public POA_eo::C, public PortableServer::RefCountServantBase
69 {
70 public:
71   inline C_i() {}
72   virtual ~C_i() {}
73   CORBA::Long echoLong(CORBA::Long i);
74 };
75
76 class D_i : public POA_eo::D, public PortableServer::RefCountServantBase
77 {
78 public:
79   inline D_i() {}
80   virtual ~D_i() {}
81   CORBA::Long echoLong(CORBA::Long i);
82   CORBA::Long echoLong2(CORBA::Long i);
83 };
84
85 class E_i : public POA_eo::E, public PortableServer::RefCountServantBase
86 {
87 public:
88   inline E_i() {}
89   virtual ~E_i() {}
90   CORBA::Long echoLong(CORBA::Long i);
91   CORBA::Long echoLong2(CORBA::Long i);
92 };
93
94 class Echo_i : public POA_eo::Echo,
95                public PortableServer::RefCountServantBase
96 {
97 public:
98   inline Echo_i() {}
99   virtual ~Echo_i() {}
100   virtual char* echoString(const char* mesg);
101   virtual CORBA::Boolean echoBoolean(CORBA::Boolean b);
102   CORBA::Long echoLong(CORBA::Long i);
103   void echoDouble(CORBA::Double i,CORBA::Double& j) ;
104   void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
105   void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
106   void echoIntVec(const eo::IntVec&, eo::IntVec_out);
107   void echoStrVec(const eo::StrVec&, eo::StrVec_out);
108   void echoBoolVec(const eo::BoolVec&, eo::BoolVec_out);
109   void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
110   void echoObj2(eo::Obj_ptr , eo::Obj_out);
111   void echoD(eo::D_ptr , eo::D_out);
112   void echoC(eo::C_ptr , eo::C_out);
113   void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
114
115   eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
116   void createObj(CORBA::Long i, eo::Obj_out oo);
117   void createC(eo::C_out oo);
118   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);
119   void sleepLong(CORBA::Double time1,CORBA::Double& time2) ;
120   virtual eo::S2* echoStruct(const eo::S2&);
121   virtual eo::S3* echoStruct2(const eo::S3&);
122   virtual void shutdown();
123   virtual PortableServer::POA_ptr _default_POA();
124 protected:
125   int _ctr;
126   pthread_mutex_t _mutex;
127 };
128
129 //Implementation Echo
130 PortableServer::POA_ptr Echo_i::_default_POA()
131 {
132   _ctr =0;
133   pthread_mutex_init(&_mutex, NULL);
134   PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
135   try{
136     return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
137   }
138   catch(...){
139     //return PortableServer::POA::_duplicate(root_poa);
140     return root_poa._retn();
141   }
142 }
143
144
145 void Echo_i::shutdown()
146 {
147   // Shutdown the ORB (but do not wait for completion).  This also
148   // causes the main thread to unblock from CORBA::ORB::run().
149   orb->shutdown(0);
150 }
151
152 char* Echo_i::echoString(const char* mesg)
153 {
154   DEBTRACE("Echo_i::echoString " << mesg);
155   return CORBA::string_dup(mesg);
156 }
157
158 CORBA::Boolean Echo_i::echoBoolean(CORBA::Boolean b ) 
159 {
160   DEBTRACE("Echo_i::echoBoolean " << b);
161   return b;
162 }
163
164 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
165   DEBTRACE("Echo_i::echoDouble " << i);
166   j=i+1;
167 }
168
169 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
170 {
171   DEBTRACE("Echo_i::echoIntVec " << in.length());
172   for(int i=0;i<in.length(); i++){
173     DEBTRACE(in[i]);
174   };
175   out=new eo::IntVec(in);
176 }
177
178 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
179 {
180   DEBTRACE("Echo_i::echoStrVec " << in.length());
181   for(int i=0;i<in.length(); i++){
182     DEBTRACE(in[i]);
183   }
184   out=new eo::StrVec(in);
185 }
186
187 void Echo_i::echoBoolVec(const eo::BoolVec& in, eo::BoolVec_out out)
188 {
189   DEBTRACE("Echo_i::echoBoolVec " << in.length());
190   for(int i=0;i<in.length(); i++){
191     DEBTRACE(in[i]);
192   };
193   out=new eo::BoolVec(in);
194 }
195
196 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
197 {
198   DEBTRACE("Echo_i::echoObjectVec " << in.length());
199   for(int i=0;i<in.length(); i++){
200     DEBTRACE(in[i]->_PD_repoId);
201   };
202   out=new eo::ObjectVec(in);
203 }
204
205 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
206 {
207   DEBTRACE("Echo_i::echoObjectVecVec " << 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]->_PD_repoId);
211     };
212   };
213   out=new eo::ObjectVecVec(in);
214 }
215
216 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out ) 
217 {
218   DEBTRACE("Echo_i::echoDoubleVec " << in.length());
219   for(int i=0;i<in.length(); i++){
220     DEBTRACE(in[i]);
221   };
222   out=new eo::DoubleVec(in);
223 }
224
225 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
226 {
227   DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
228   for(int i=0;i< in.length(); i++){
229     for(int j=0;j< in[i].length(); j++){
230       DEBTRACE(in[i][j]);
231     };
232   };
233   out=new eo::DoubleVecVec(in);
234 }
235
236 CORBA::Long Echo_i::echoLong(CORBA::Long i )
237 {
238   DEBTRACE("Echo_i::echoLong " << i);
239   if(i < 0) {
240     eo::ExceptionStruct es;
241     es.type = eo::COMM;
242     es.text = "error Socket exception";
243     throw eo::SALOME_Exception(es);
244   }
245
246   CORBA::Long j=i+1;
247   return j;
248 }
249
250 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
251   DEBTRACE("Echo_i::echoC ");
252   o->echoLong(10);
253   oo=eo::C::_duplicate(o); 
254 }
255
256 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
257   DEBTRACE("Echo_i::echoD ");
258   o->echoLong2(10);
259   //oo=eo::D::_duplicate(o); 
260   D_i* myD = new D_i();
261   oo=myD->_this();
262   myD->_remove_ref();
263 }
264
265 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
266   DEBTRACE("Echo_i::echoObj2 ");
267   o->echoLong(10);
268   oo=eo::Obj::_duplicate(o); 
269 }
270
271 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
272   DEBTRACE("Echo_i::echoObj " << i << "," << j );
273   oo=eo::Obj::_duplicate(o); 
274   return eo::Obj::_duplicate(o); 
275 }
276
277 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
278   DEBTRACE("Echo_i::createObj " << i);
279   Obj_i* myobj = new Obj_i();
280   CORBA::Object_var myref = myobj->_this();
281   oo = eo::Obj::_narrow(myref);
282   myobj->_remove_ref();
283 }
284
285 void Echo_i::createC(eo::C_out oo){
286   DEBTRACE("Echo_i::createC ");
287   C_i* myobj = new C_i();
288   CORBA::Object_var myref = myobj->_this();
289   oo = eo::C::_narrow(myref);
290   myobj->_remove_ref();
291 }
292
293 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)
294 {
295   DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
296   dd=d;
297   ll=l;
298   s=CORBA::string_dup(m);
299   oo=eo::Obj::_duplicate(o); 
300 };
301
302 void Echo_i::sleepLong(CORBA::Double time1, CORBA::Double& time2)
303 {
304   DEBTRACE("Echo_i::sleepLong");
305   pthread_mutex_lock(&_mutex);
306   int num = _ctr++;
307   pthread_mutex_unlock(&_mutex);
308   DEBTRACE("Echo_i::sleepLong start " << num);
309   unsigned int t=(unsigned int) time1;
310   sleep(t);
311   DEBTRACE("Echo_i::sleepLong stop  " << num);
312   time2 = time1;
313 }
314
315 eo::S2* Echo_i::echoStruct(const eo::S2& s)
316 {
317   DEBTRACE("Echo_i::echoStruct " << s.s.x << " " << s.s.y);
318   eo::S1 s1;
319   s1.x=10.;
320   s1.y=2;
321   eo::S2* s2=new eo::S2;
322   s2->s=s1;
323   return s2;
324 }
325
326 eo::S3* Echo_i::echoStruct2(const eo::S3& s)
327 {
328   DEBTRACE("Echo_i::echoStruct " << s.x << " " << s.y);
329   if( !CORBA::is_nil(s.ob) ) 
330     {
331       std::cerr << s.ob->echoLong(10) << std::endl;
332     }
333   eo::S3* s2=new eo::S3;
334   s2->x=10.;
335   s2->y=2;
336   return s2;
337 }
338
339 //Implementation Obj
340 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
341   DEBTRACE("Obj_i::echoLong " << i );
342   CORBA::Long j=i+1;
343   return j;
344 }
345
346 //Implementation C
347 CORBA::Long C_i::echoLong(CORBA::Long i ){
348   DEBTRACE("C_i::echoLong " << i);
349   CORBA::Long j=i+5;
350   return j;
351 }
352
353 //Implementation D
354 CORBA::Long D_i::echoLong2(CORBA::Long i ){
355   DEBTRACE("D_i::echoLong " << i);
356   CORBA::Long j=i+10;
357   return j;
358 }
359 CORBA::Long D_i::echoLong(CORBA::Long i ){
360   DEBTRACE("D_i::echoLong " << i);
361   CORBA::Long j=i+1;
362   return j;
363 }
364
365 //Implementation E
366 CORBA::Long E_i::echoLong2(CORBA::Long i ){
367   DEBTRACE("E_i::echoLong " << i);
368   CORBA::Long j=i+20;
369   return j;
370 }
371 CORBA::Long E_i::echoLong(CORBA::Long i ){
372   DEBTRACE("E_i::echoLong " << i);
373   CORBA::Long j=i+15;
374   return j;
375 }
376
377 eo::Echo_var myechoref;
378
379 int main(int argc, char** argv)
380 {
381   try {
382     orb = CORBA::ORB_init(argc, argv);
383
384     std::string theIOR( argv[1] );
385     
386     CORBA::Object_var obj = orb->string_to_object(theIOR.c_str());
387     Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj );
388     if( CORBA::is_nil( ns ) )
389       return 1;
390     //std::cout << getpid() << std::endl;
391     //usleep(20000000);
392
393     {
394       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
395       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
396       // POA manager
397       PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
398       poa_man->activate();
399
400       // Create a new POA with the shortcut policy
401       /*
402       CORBA::PolicyList pl2;
403       pl2.length(2);
404       CORBA::Any v;
405       v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
406       pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
407       pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
408       PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
409       */
410
411       // Create and activate servant
412       Echo_i* myecho = new Echo_i();
413       // Obtain a reference to the object
414       CORBA::Object_var obj2 = myecho->_this();
415       myechoref = eo::Echo::_narrow(obj2);
416       // Decrement the reference count of the object implementation, so
417       // that it will be properly cleaned up when the POA has determined
418       // that it is no longer needed.
419       myecho->_remove_ref();
420
421       // print the reference as a stringified IOR.
422       CORBA::String_var sior(orb->object_to_string(obj2));
423       DEBTRACE("'" << (char*)sior << "'");
424       if( !bindObjectToName(ns, myechoref,"Echo") ) return 1;
425
426       //create object C and register it in naming service
427       C_i* myC = new C_i();
428       CORBA::Object_var obj3 =myC->_this();
429       eo::C_var myCref=eo::C::_narrow(obj3);
430       myC->_remove_ref();
431       if( !bindObjectToName(ns, myCref,"C") ) return 1;
432
433       //create object D and register it in naming service
434       D_i* myD = new D_i();
435       CORBA::Object_var obj4=myD->_this();
436       eo::D_var myDref=eo::D::_narrow(obj4);
437       myD->_remove_ref();
438       if( !bindObjectToName(ns, myDref,"D") ) return 1;
439
440       //create object Obj and register it in naming service
441       Obj_i* myObj = new Obj_i();
442       CORBA::Object_var obj5=myObj->_this();
443       eo::Obj_var myObjref=eo::Obj::_narrow(obj5);
444       myObj->_remove_ref();
445       if( !bindObjectToName(ns, myObjref,"Obj") ) return 1;
446     }
447     orb->run();
448     std::cout << "Returned from orb->run()." << std::endl;
449     orb->destroy();
450   }
451   catch(CORBA::SystemException&) {
452     DEBTRACE("Caught CORBA::SystemException.");
453   }
454   catch(CORBA::Exception& ex) {
455     DEBTRACE("Caught CORBA::Exception." << ex);
456   }
457   catch(omniORB::fatalException& fe) {
458     DEBTRACE("Caught omniORB::fatalException:");
459     DEBTRACE("  file: " << fe.file());
460     DEBTRACE("  line: " << fe.line());
461     DEBTRACE("  mesg: " << fe.errmsg());
462   }
463   catch(...) {
464     DEBTRACE("Caught unknown exception." );
465   }
466
467   return 0;
468 }
469
470
471 //////////////////////////////////////////////////////////////////////
472
473 static CORBA::Boolean
474 bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name)
475 {
476   CORBA::String_var iorNSUg = orb->object_to_string(objref);
477   std::string iorNS(iorNSUg);
478   Engines::IORType iorInput;
479   auto len = iorNS.length();
480   iorInput.length( len );
481   for(auto i = 0 ; i < len ; ++i)
482     iorInput[i] = iorNS[i];
483   ns->Register(iorInput,name);
484   return 1;
485 }
486