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