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