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