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