]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/Test/echoSrv.cxx
Salome HOME
ee7423f3bf721cfcedd1ee71e0abf5b5e2b557d5
[modules/yacs.git] / src / runtime / Test / echoSrv.cxx
1 // Copyright (C) 2006-2024  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
22 #include <echo.hh>
23
24 #include <iostream>
25 using namespace std;
26
27 //#define _DEVDEBUG_
28 #include "YacsTrace.hxx"
29
30 #include <SALOMEconfig.h>
31 #include CORBA_CLIENT_HEADER(SALOME_Embedded_NamingService)
32
33 static CORBA::Boolean bindObjectToName(Engines::EmbeddedNamingService_ptr ns, 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   CORBA::Long echoLong(CORBA::Long i);
92   void echoDouble(CORBA::Double i,CORBA::Double& j) ;
93   void echoDoubleVec(const eo::DoubleVec& i,eo::DoubleVec_out j) ;
94   void echoDoubleVecVec(const eo::DoubleVecVec&, eo::DoubleVecVec_out);
95   void echoIntVec(const eo::IntVec&, eo::IntVec_out);
96   void echoStrVec(const eo::StrVec&, eo::StrVec_out);
97   void echoObjectVec(const eo::ObjectVec&, eo::ObjectVec_out);
98   void echoObjVec(const eo::ObjVec&, eo::ObjVec_out);
99   void echoObj2(eo::Obj_ptr , eo::Obj_out);
100   void echoD(eo::D_ptr , eo::D_out);
101   void echoC(eo::C_ptr , eo::C_out);
102   void echoObjectVecVec(const eo::ObjectVecVec&, eo::ObjectVecVec_out);
103
104   eo::Obj_ptr echoObj(CORBA::Long i, eo::Obj_ptr o, CORBA::Long j, eo::Obj_out oo);
105   void createObj(CORBA::Long i, eo::Obj_out oo);
106   void createC(eo::C_out oo);
107   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);
108   virtual PortableServer::POA_ptr _default_POA();
109 };
110
111 //Implementation Echo
112 PortableServer::POA_ptr Echo_i::_default_POA()
113 {
114   PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
115   try{
116     return PortableServer::POA::_duplicate(root_poa->find_POA("shortcut",0));
117   }
118   catch(...){
119     //return PortableServer::POA::_duplicate(root_poa);
120     return root_poa._retn();
121   }
122 }
123
124 char* Echo_i::echoString(const char* mesg)
125 {
126   DEBTRACE("Echo_i::echoString " << mesg);
127   return CORBA::string_dup(mesg);
128 }
129
130 void Echo_i::echoDouble(CORBA::Double i,CORBA::Double& j ) {
131   DEBTRACE("Echo_i::echoDouble " << i);
132   j=i+1;
133 }
134
135 void Echo_i::echoIntVec(const eo::IntVec& in, eo::IntVec_out out)
136 {
137   DEBTRACE("Echo_i::echoIntVec " << in.length());
138   for(int i=0;i<in.length(); i++){
139     DEBTRACE(in[i]);
140   };
141   out=new eo::IntVec(in);
142 }
143
144 void Echo_i::echoStrVec(const eo::StrVec& in, eo::StrVec_out out)
145 {
146   DEBTRACE("Echo_i::echoStrVec " << in.length());
147   for(int i=0;i<in.length(); i++){
148     DEBTRACE(in[i]);
149   }
150   out=new eo::StrVec(in);
151 }
152
153 void Echo_i::echoObjectVec(const eo::ObjectVec& in, eo::ObjectVec_out out)
154 {
155   DEBTRACE("Echo_i::echoObjectVec " << in.length());
156   for(int i=0;i<in.length(); i++){
157     DEBTRACE(in[i]->_PD_repoId);
158   };
159   out=new eo::ObjectVec(in);
160 }
161
162 void Echo_i::echoObjVec(const eo::ObjVec& in, eo::ObjVec_out out)
163   {
164     DEBTRACE("Echo_i::echoObjVec " << in.length());
165     for(int i=0;i<in.length(); i++)
166       {
167         DEBTRACE(in[i]->_mostDerivedRepoId());
168       };
169
170     out=new eo::ObjVec;
171     out->length(2);
172
173     Obj_i* obj0 = new Obj_i();
174     CORBA::Object_var ref0 = obj0->_this();
175     eo::Obj_var o0 = eo::Obj::_narrow(ref0);
176     (*out)[0]=o0;
177     obj0->_remove_ref();
178
179     C_i* obj1 = new C_i();
180     CORBA::Object_var ref1 = obj1->_this();
181     eo::Obj_var o1 = eo::Obj::_narrow(ref1);
182     (*out)[1]=o1;
183     obj1->_remove_ref();
184
185     DEBTRACE("ENDOF Echo_i::echoObjVec " );
186 }
187
188 void Echo_i::echoObjectVecVec(const eo::ObjectVecVec& in, eo::ObjectVecVec_out out)
189 {
190   DEBTRACE("Echo_i::echoObjectVecVec " << in.length());
191   for(int i=0;i< in.length(); i++){
192     for(int j=0;j< in[i].length(); j++){
193       DEBTRACE(in[i][j]->_PD_repoId);
194     };
195   };
196   out=new eo::ObjectVecVec(in);
197 }
198
199 void Echo_i::echoDoubleVec(const eo::DoubleVec& in,eo::DoubleVec_out out ) 
200 {
201   DEBTRACE("Echo_i::echoDoubleVec " << in.length());
202   for(int i=0;i<in.length(); i++){
203     DEBTRACE(in[i]);
204   };
205   out=new eo::DoubleVec(in);
206 }
207
208 void Echo_i::echoDoubleVecVec(const eo::DoubleVecVec& in, eo::DoubleVecVec_out out)
209 {
210   DEBTRACE("Echo_i::echoDoubleVecVec " << in.length());
211   for(int i=0;i< in.length(); i++){
212     for(int j=0;j< in[i].length(); j++){
213       DEBTRACE(in[i][j]);
214     };
215   };
216   out=new eo::DoubleVecVec(in);
217 }
218
219 CORBA::Long Echo_i::echoLong(CORBA::Long i )
220 {
221   DEBTRACE("Echo_i::echoLong " << i);
222   if(i < 0) {
223     eo::ExceptionStruct es;
224     es.type = eo::COMM;
225     es.text = "error Socket exception";
226     throw eo::SALOME_Exception(es);
227   }
228
229   CORBA::Long j=i+1;
230   return j;
231 }
232
233 void Echo_i::echoC(eo::C_ptr o,eo::C_out oo){
234   DEBTRACE("Echo_i::echoC ");
235   o->echoLong(10);
236   oo=eo::C::_duplicate(o); 
237 }
238
239 void Echo_i::echoD(eo::D_ptr o,eo::D_out oo){
240   DEBTRACE("Echo_i::echoD ");
241   o->echoLong2(10);
242   //oo=eo::D::_duplicate(o); 
243   D_i* myD = new D_i();
244   oo=myD->_this();
245   myD->_remove_ref();
246 }
247
248 void Echo_i::echoObj2(eo::Obj_ptr o,eo::Obj_out oo){
249   DEBTRACE("Echo_i::echoObj2 ");
250   o->echoLong(10);
251   oo=eo::Obj::_duplicate(o); 
252 }
253
254 eo::Obj_ptr Echo_i::echoObj(CORBA::Long i ,eo::Obj_ptr o,CORBA::Long j,eo::Obj_out oo){
255   DEBTRACE("Echo_i::echoObj " << i << "," << j );
256   oo=eo::Obj::_duplicate(o); 
257   return eo::Obj::_duplicate(o); 
258 }
259
260 void Echo_i::createObj(CORBA::Long i ,eo::Obj_out oo){
261   DEBTRACE("Echo_i::createObj " << i);
262   Obj_i* myobj = new Obj_i();
263   CORBA::Object_var myref = myobj->_this();
264   oo = eo::Obj::_narrow(myref);
265   myobj->_remove_ref();
266 }
267
268 void Echo_i::createC(eo::C_out oo){
269   DEBTRACE("Echo_i::createC ");
270   C_i* myobj = new C_i();
271   CORBA::Object_var myref = myobj->_this();
272   oo = eo::C::_narrow(myref);
273   myobj->_remove_ref();
274 }
275
276 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)
277 {
278   DEBTRACE("Echo_i::echoAll " << d << "," << l << "," << m);
279   dd=d;
280   ll=l;
281   s=CORBA::string_dup(m);
282   oo=eo::Obj::_duplicate(o); 
283 };
284
285 //Implementation Obj
286 CORBA::Long Obj_i::echoLong(CORBA::Long i ){
287   DEBTRACE("Obj_i::echoLong " << i );
288   CORBA::Long j=i+1;
289   return j;
290 }
291
292 //Implementation C
293 CORBA::Long C_i::echoLong(CORBA::Long i ){
294   DEBTRACE("C_i::echoLong " << i);
295   CORBA::Long j=i+5;
296   return j;
297 }
298
299 //Implementation D
300 CORBA::Long D_i::echoLong2(CORBA::Long i ){
301   DEBTRACE("D_i::echoLong " << i);
302   CORBA::Long j=i+10;
303   return j;
304 }
305 CORBA::Long D_i::echoLong(CORBA::Long i ){
306   DEBTRACE("D_i::echoLong " << i);
307   CORBA::Long j=i+1;
308   return j;
309 }
310
311 //Implementation E
312 CORBA::Long E_i::echoLong2(CORBA::Long i ){
313   DEBTRACE("E_i::echoLong " << i);
314   CORBA::Long j=i+20;
315   return j;
316 }
317 CORBA::Long E_i::echoLong(CORBA::Long i ){
318   DEBTRACE("E_i::echoLong " << i);
319   CORBA::Long j=i+15;
320   return j;
321 }
322
323 CORBA::ORB_ptr orb;
324 eo::Echo_var myechoref;
325
326 int main(int argc, char** argv)
327 {
328   try {
329     orb = CORBA::ORB_init(argc, argv);
330
331     std::string theIOR( argv[1] );
332     
333     CORBA::Object_var obj = orb->string_to_object(theIOR.c_str());
334     Engines::EmbeddedNamingService_var ns = Engines::EmbeddedNamingService::_narrow( obj );
335     if( CORBA::is_nil( ns ) )
336       return 1;
337     //std::cout << getpid() << std::endl;
338     //usleep(20000000);
339
340     {
341       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
342       PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
343       // POA manager
344       PortableServer::POAManager_var poa_man = root_poa->the_POAManager();
345       poa_man->activate();
346
347       // Create a new POA with the shortcut policy
348       CORBA::PolicyList pl2;
349       pl2.length(2);
350       CORBA::Any v;
351       v <<= omniPolicy::LOCAL_CALLS_SHORTCUT;
352       pl2[0] = orb->create_policy(omniPolicy::LOCAL_SHORTCUT_POLICY_TYPE, v);
353       pl2[1] = root_poa->create_implicit_activation_policy(PortableServer::IMPLICIT_ACTIVATION);
354       PortableServer::POA_var shortcut_poa = root_poa->create_POA("shortcut", poa_man, pl2);
355
356       // Create and activate servant
357       Echo_i* myecho = new Echo_i();
358       // Obtain a reference to the object, and print it out as a
359       // stringified IOR.
360       obj = myecho->_this();
361       CORBA::String_var sior(orb->object_to_string(obj));
362       DEBTRACE("'" << (char*)sior << "'");
363       myechoref = eo::Echo::_narrow(obj);
364
365       if( !bindObjectToName(ns, myechoref,"Echo") ) return 1;
366
367       // Decrement the reference count of the object implementation, so
368       // that it will be properly cleaned up when the POA has determined
369       // that it is no longer needed.
370       myecho->_remove_ref();
371
372       //create object C and register it in naming service
373       C_i* myC = new C_i();
374       obj=myC->_this();
375       eo::C_var myCref=eo::C::_narrow(obj);
376       myC->_remove_ref();
377       if( !bindObjectToName(ns, myCref,"C") ) return 1;
378
379       //create object D and register it in naming service
380       D_i* myD = new D_i();
381       obj=myD->_this();
382       eo::D_var myDref=eo::D::_narrow(obj);
383       myD->_remove_ref();
384       if( !bindObjectToName(ns, myDref,"D") ) return 1;
385
386       //create object Obj and register it in naming service
387       Obj_i* myObj = new Obj_i();
388       obj=myObj->_this();
389       eo::Obj_var myObjref=eo::Obj::_narrow(obj);
390       myObj->_remove_ref();
391       if( !bindObjectToName(ns, myObjref,"Obj") ) return 1;
392     }
393     orb->run();
394   }
395   catch(CORBA::SystemException&) {
396     DEBTRACE("Caught CORBA::SystemException.");
397   }
398   catch(CORBA::Exception& ex) {
399     DEBTRACE("Caught CORBA::Exception." << ex);
400   }
401   catch(omniORB::fatalException& fe) {
402     DEBTRACE("Caught omniORB::fatalException:");
403     DEBTRACE("  file: " << fe.file());
404     DEBTRACE("  line: " << fe.line());
405     DEBTRACE("  mesg: " << fe.errmsg());
406   }
407   catch(...) {
408     DEBTRACE("Caught unknown exception." );
409   }
410
411   return 0;
412 }
413
414
415 //////////////////////////////////////////////////////////////////////
416
417 static CORBA::Boolean
418 bindObjectToName(Engines::EmbeddedNamingService_ptr ns, CORBA::Object_ptr objref,const char *name)
419 {
420   CORBA::String_var iorNSUg = orb->object_to_string(objref);
421   std::string iorNS(iorNSUg);
422   Engines::IORType iorInput;
423   auto len = iorNS.length();
424   iorInput.length( len );
425   for(auto i = 0 ; i < len ; ++i)
426     iorInput[i] = iorNS[i];
427   ns->Register(iorInput,name);
428   return 1;
429 }
430