Salome HOME
Merge from V6_main 01/04/2013
[samples/component.git] / src / SyrComponent / SyrComponent_Impl.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SuperVisionTest SyrComponent : example of component performing some mathinatical operations
24 //  File   : SyrComponent_Impl.cxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SuperVisionTest
27 //
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fstream>
31 #include <sstream>
32 #include <string>
33
34 #include "COMPONENT_version.h"
35 #include "SyrComponent_Impl.hxx"
36 #include "Adder_Impl.hxx"
37
38 using namespace std;
39
40 SyrComponent_Impl::SyrComponent_Impl( CORBA::ORB_ptr orb ,
41                                       PortableServer::POA_ptr poa ,
42                                       PortableServer::ObjectId * contId , 
43                                       const char *instanceName ,
44                                       const char *interfaceName ,
45                                       const bool kactivate ) :
46   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
47   MESSAGE("SyrComponent_Impl::SyrComponent_Impl this " << hex << this << dec
48           << "activate object instanceName("
49           << instanceName << ") interfaceName(" << interfaceName << ")" )
50   if ( kactivate ) {
51     _thisObj = this ;
52     _id = _poa->activate_object(_thisObj);
53   }
54 }
55
56 SyrComponent_Impl::SyrComponent_Impl() {
57 }
58
59 SyrComponent_Impl::~SyrComponent_Impl() {
60 }
61
62 char* SyrComponent_Impl::getVersion()
63 {
64 #if COMPONENT_DEVELOPMENT
65   return CORBA::string_dup(COMPONENT_VERSION_STR"dev");
66 #else
67   return CORBA::string_dup(COMPONENT_VERSION_STR);
68 #endif
69 }
70
71 CORBA::Long SyrComponent_Impl::C_ISEVEN( CORBA::Long anInteger ) {
72   bool RetVal ;
73   beginService( " SyrComponent_Impl::C_ISEVEN" );
74   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_ISEVEN is Computing");
75 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
76   int S = 1 ;
77   while ( S ) {
78     S = sleep(S);
79   }
80   RetVal = ( anInteger & 1 ) == 0 ;
81   endService( " SyrComponent_Impl::C_ISEVEN"  );
82   return RetVal ;
83 }
84
85 CORBA::Long SyrComponent_Impl::C_ISONE( CORBA::Long anOddInteger ) {
86   bool RetVal ;
87   beginService( " SyrComponent_Impl::C_ISONE" );
88   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_ISONE is Computing");
89 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
90   int S = 1 ;
91   while ( S ) {
92     S = sleep(S);
93   }
94   RetVal = ( anOddInteger == 1 ) ;
95   endService( " SyrComponent_Impl::C_ISONE"  );
96   return RetVal ;
97 }
98
99 CORBA::Long SyrComponent_Impl::C_M3( CORBA::Long anOddInteger ) {
100   beginService( " SyrComponent_Impl::C_M3" );
101   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_M3 is Computing");
102 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
103   int S = 1 ;
104   while ( S ) {
105     S = sleep(S);
106   }
107   endService( " SyrComponent_Impl::C_M3"  );
108   if ( C_ISEVEN( anOddInteger ) )
109     return 0 ;
110   return ( 3*anOddInteger ) ;
111 }
112
113 CORBA::Long SyrComponent_Impl::C_M3P1( CORBA::Long anOddInteger ) {
114   beginService( " SyrComponent_Impl::C_M3P1" );
115   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_M3P1 is Computing");
116 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
117   int S = 1 ;
118   while ( S ) {
119     S = sleep(S);
120   }
121   endService( " SyrComponent_Impl::C_M3P1"  );
122   if ( C_ISEVEN( anOddInteger ) )
123     return 0 ;
124   return ( 3*anOddInteger + 1 ) ;
125 }
126
127 CORBA::Long SyrComponent_Impl::C_DIV2( CORBA::Long anEvenInteger ) {
128   beginService( " SyrComponent_Impl::C_DIV2" );
129   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_DIV2 is Computing");
130 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
131   int S = 1 ;
132   while ( S ) {
133     S = sleep(S);
134   }
135   endService( " SyrComponent_Impl::C_DIV2"  );
136   if ( !C_ISEVEN( anEvenInteger ) )
137     return 0 ;
138   return ( anEvenInteger >> 1 ) ;
139 }
140
141 CORBA::Long SyrComponent_Impl::C_INCR( CORBA::Long aCount ) {
142   beginService( " SyrComponent_Impl::C_INCR" );
143   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_INCR is Computing");
144 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
145   int S = 1 ;
146   while ( S ) {
147     S = sleep(S);
148   }
149   endService( " SyrComponent_Impl::C_INCR"  );
150   return ( aCount + 1 ) ;
151 }
152
153 void SyrComponent_Impl::CPP_SETLONG( CORBA::Long aCount ) {
154   beginService( " SyrComponent_Impl::CPP_SETLONG" );
155   sendMessage(NOTIF_STEP, "SyrComponent_Impl::CPP_SETLONG is Computing");
156 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
157   int S = 1 ;
158   while ( S ) {
159     S = sleep(S);
160   }
161   _Count = aCount ;
162   endService( " SyrComponent_Impl::CPP_SETLONG"  );
163   return ;
164 }
165
166 CORBA::Long SyrComponent_Impl::CPP_ADDTOLONG( CORBA::Long anIncr ) {
167   beginService( " SyrComponent_Impl::CPP_ADDTOLONG" );
168   sendMessage(NOTIF_STEP, "SyrComponent_Impl::CPP_ADDTOLONG is Computing");
169 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
170   int S = 1 ;
171   while ( S ) {
172     S = sleep(S);
173   }
174   endService( " SyrComponent_Impl::CPP_ADDTOLONG"  );
175   return ( _Count + anIncr ) ;
176 }
177
178 CORBA::Long SyrComponent_Impl::C_MIN( CORBA::Long aMinVal , CORBA::Long anInteger ) {
179   beginService( " SyrComponent_Impl::C_MIN" );
180   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_MIN is Computing");
181 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
182   int S = 1 ;
183   while ( S ) {
184     S = sleep(S);
185   }
186   int min = aMinVal ;
187   if ( anInteger < min || aMinVal == 0 ) {
188     min = anInteger ;
189   }
190   string trace = "SyrComponent_Impl::C_MIN : " ;
191   char cmin[ 30 ] ;
192   sprintf( cmin , "%d" , min ) ;
193   trace += cmin ;
194   sendMessage(NOTIF_TRACE, trace.c_str() );
195   endService( " SyrComponent_Impl::C_MIN"  );
196   return min ;
197 }
198
199 CORBA::Long SyrComponent_Impl::C_MAX( CORBA::Long aMaxVal , CORBA::Long anInteger ) {
200   beginService( " SyrComponent_Impl::C_MAX" );
201   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_MAX is Computing");
202 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
203   int S = 1 ;
204   while ( S ) {
205     S = sleep(S);
206   }
207   int max = aMaxVal ;
208   if ( anInteger > max ) {
209     max = anInteger ;
210   }
211   string trace = "SyrComponent_Impl::C_MAX : " ;
212   char cmax[ 30 ] ;
213   sprintf( cmax , "%d" , max ) ;
214   trace += cmax ;
215   sendMessage(NOTIF_TRACE, trace.c_str() );
216   endService( " SyrComponent_Impl::C_MAX"  );
217   return max ;
218 }
219
220 SuperVisionTest::ListOfSyr_ptr SyrComponent_Impl::C_LISTOFSYR() {
221   beginService( " SyrComponent_Impl::C_LISTOFSYR" );
222   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_LISTOFSYR is Computing");
223 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
224   int S = 1 ;
225   while ( S ) {
226     S = sleep(S);
227   }
228   ListOfSyr_Impl * aNewListOfSyr = new ListOfSyr_Impl( _orb , _poa, _contId,
229                         instanceName() , interfaceName() ,
230                         graphName() , nodeName() ) ;
231   SuperVisionTest::ListOfSyr_var iobject = (SuperVisionTest::ListOfSyr_var ) NULL ;
232   PortableServer::ObjectId * id = aNewListOfSyr->getId() ;
233   CORBA::Object_var obj = _poa->id_to_reference(*id);
234   iobject = SuperVisionTest::ListOfSyr::_narrow(obj) ;
235   endService( " SyrComponent_Impl::C_LISTOFSYR"  );
236   return SuperVisionTest::ListOfSyr::_duplicate( iobject ) ;
237 //  return ( aNewListOfSyr._retn() ) ;
238 }
239
240 SuperVisionTest::ListOfSyr_ptr SyrComponent_Impl::C_AVERAGE(
241                        SuperVisionTest::ListOfSyr_ptr aListOfSyr ,
242                        CORBA::Long anInteger ,
243                        CORBA::Long aCount ,
244                        CORBA::Double & anAverage ) {
245   beginService( " SyrComponent_Impl::C_AVERAGE" );
246   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_AVERAGE is Computing");
247 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
248   int S = 1 ;
249   while ( S ) {
250     S = sleep(S);
251   }
252   SuperVisionTest::SeqOfSyr * aSeqOfSyr = aListOfSyr->GetSeqOfSyr() ;
253   int len = aSeqOfSyr->length() ;
254   aSeqOfSyr->length( len + 1 ) ;
255   (*aSeqOfSyr)[ len ] = anInteger ;
256   aListOfSyr->SetSeqOfSyr( *aSeqOfSyr ) ;
257   anAverage = 0 ;
258   int i ;
259   for ( i = 0 ; i < len + 1 ; i++ ) {
260     anAverage += (*aSeqOfSyr)[ i ] ;
261   }
262   anAverage = anAverage / ( len + 1 ) ;
263   string end = " SyrComponent_Impl::C_AVERAGE " ;
264   char caverage[ 30 ] ;
265   sprintf( caverage , "%fd" , anAverage ) ;
266   end += caverage ;
267   endService( end.c_str() );
268   return SuperVisionTest::ListOfSyr::_duplicate( aListOfSyr ) ;
269 }
270
271 SuperVisionTest::Syr_ptr SyrComponent_Impl::Init( CORBA::Long anOddInteger ) {
272   beginService( "SyrComponent_Impl::Init" );
273   sendMessage(NOTIF_STEP, "SyrComponent_Impl creates Syr_Impl");
274 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
275   int S = 1 ;
276   while ( S ) {
277     S = sleep(S);
278   }
279   Syr_Impl * mySyr ;
280   mySyr = new Syr_Impl( _orb , _poa, _contId,
281                         instanceName() , interfaceName() ,
282                         graphName() , nodeName() , anOddInteger ) ;
283   SuperVisionTest::Syr_var iobject = (SuperVisionTest::Syr_var ) NULL ;
284   PortableServer::ObjectId * id = mySyr->getId() ;
285   CORBA::Object_var obj = _poa->id_to_reference(*id);
286   iobject = SuperVisionTest::Syr::_narrow(obj) ;
287   endService( "SyrComponent_Impl::Init" );
288   return SuperVisionTest::Syr::_duplicate(iobject) ;
289 }
290
291 ListOfSyr_Impl::ListOfSyr_Impl( CORBA::ORB_ptr orb ,
292                     PortableServer::POA_ptr poa ,
293                     PortableServer::ObjectId * contId , 
294                     const char * instanceName ,
295                     const char * interfaceName , 
296                     const char * graphName ,
297                     const char * nodeName ) :
298   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
299   Names( graphName , nodeName ) ;
300   MESSAGE("ListOfSyr_Impl::ListOfSyr_Impl activate object instanceName("
301           << instanceName << ") interfaceName(" << interfaceName << ") --> "
302           << hex << (void *) this << dec )
303   beginService( "ListOfSyr_Impl::ListOfSyr_Impl" );
304   _thisObj = this ;
305   _id = _poa->activate_object(_thisObj);
306   sendMessage(NOTIF_STEP, "ListOfSyr_Impl is Created");
307   endService( "ListOfSyr_Impl::ListOfSyr_Impl" );
308 }
309
310 ListOfSyr_Impl::ListOfSyr_Impl() {
311 }
312
313 ListOfSyr_Impl::~ListOfSyr_Impl() {
314   beginService( "ListOfSyr_Impl::~ListOfSyr_Impl" );
315   endService( "ListOfSyr_Impl::~ListOfSyr_Impl" );
316 }
317
318 char* ListOfSyr_Impl::getVersion()
319 {
320 #if COMPONENT_DEVELOPMENT
321   return CORBA::string_dup(COMPONENT_VERSION_STR"dev");
322 #else
323   return CORBA::string_dup(COMPONENT_VERSION_STR);
324 #endif
325 }
326
327 SuperVisionTest::SeqOfSyr * ListOfSyr_Impl::GetSeqOfSyr() {
328   SuperVisionTest::SeqOfSyr_var aSeqOfSyr = new SuperVisionTest::SeqOfSyr( _SeqOfSyr ) ;
329   return ( aSeqOfSyr._retn() ) ;
330 }
331
332 void ListOfSyr_Impl::SetSeqOfSyr( const SuperVisionTest::SeqOfSyr & aSeqOfSyr ) {
333   _SeqOfSyr = aSeqOfSyr ;
334   return ;
335 }
336
337 extern "C"
338 {
339   PortableServer::ObjectId * SyrComponentEngine_factory
340      (CORBA::ORB_ptr orb,
341       PortableServer::POA_ptr poa, 
342       PortableServer::ObjectId * contId,
343       const char *instanceName,
344       const char *interfaceName)
345   {
346     MESSAGE("SyrComponentEngine_factory SyrComponentEngine ("
347             << instanceName << "," << interfaceName << "," << getpid() << ")");
348     SyrComponent_Impl * mySyrComponent 
349       = new SyrComponent_Impl(orb, poa, contId, instanceName, interfaceName);
350     return mySyrComponent->getId() ;
351   }
352 }
353
354 Syr_Impl::Syr_Impl( CORBA::ORB_ptr orb ,
355                     PortableServer::POA_ptr poa ,
356                     PortableServer::ObjectId * contId , 
357                     const char * instanceName ,
358                     const char * interfaceName , 
359                     const char * graphName ,
360                     const char * nodeName ,
361                     const CORBA::Long anOddInteger ) :
362   SyrComponent_Impl(orb, poa, contId, instanceName, interfaceName,false) {
363   Names( graphName , nodeName ) ;
364   MESSAGE("Syr_Impl::Syr_Impl activate object instanceName("
365           << instanceName << ") interfaceName(" << interfaceName << ") --> "
366           << hex << (void *) this << dec )
367   beginService( "Syr_Impl::Syr_Impl" );
368   _thisObj = this ;
369   _id = _poa->activate_object(_thisObj);
370   _InitialInteger = anOddInteger ;
371   _CurrentInteger = anOddInteger ;
372   _Count = 0 ;
373   sendMessage(NOTIF_STEP, "Syr_Impl is Created");
374   endService( "Syr_Impl::Syr_Impl" );
375 }
376
377 Syr_Impl::Syr_Impl() {
378 }
379
380 Syr_Impl::~Syr_Impl() {
381   beginService( "Syr_Impl::~Syr_Impl" );
382   endService( "Syr_Impl::~Syr_Impl" );
383 }
384
385 CORBA::Long Syr_Impl::Initial() {
386   beginService( " Syr_Impl::Initial" );
387   sendMessage(NOTIF_STEP, "Syr_Impl::Initial is Computing");
388 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
389   int S = 1 ;
390   while ( S ) {
391     S = sleep(S);
392   }
393   endService( " Syr_Impl::Initial"  );
394   return _InitialInteger ;
395 }
396
397 CORBA::Long Syr_Impl::Current() {
398   beginService( " Syr_Impl::Current" );
399   sendMessage(NOTIF_STEP, "Syr_Impl::Current is Computing");
400 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
401   int S = 1 ;
402   while ( S ) {
403     S = sleep(S);
404   }
405   endService( " Syr_Impl::Current"  );
406   return _CurrentInteger ;
407 }
408
409 CORBA::Long Syr_Impl::IsEven() {
410   bool RetVal ;
411   beginService( " Syr_Impl::IsEven" );
412   sendMessage(NOTIF_STEP, "Syr_Impl::IsEven is Computing");
413 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
414   int S = 1 ;
415   while ( S ) {
416     S = sleep(S);
417   }
418   RetVal = ( _CurrentInteger & 1 ) == 0 ;
419   MESSAGE( " Syr_Impl::IsEven " << _CurrentInteger << " " << RetVal );
420   endService( " Syr_Impl::IsEven"  );
421   return RetVal ;
422 }
423
424 CORBA::Long Syr_Impl::IsOne() {
425   bool RetVal ;
426   beginService( " Syr_Impl::IsOne" );
427   sendMessage(NOTIF_STEP, "Syr_Impl::IsOne is Computing");
428 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
429   int S = 1 ;
430   while ( S ) {
431     S = sleep(S);
432   }
433   RetVal = ( _CurrentInteger == 1 ) ;
434   MESSAGE( " Syr_Impl::IsOne " << _CurrentInteger << " " << RetVal );
435   endService( " Syr_Impl::IsOne"  );
436   return RetVal ;
437 }
438
439 CORBA::Long Syr_Impl::Count() {
440   beginService( " Syr_Impl::Count" );
441   sendMessage(NOTIF_STEP, "Syr_Impl::Count is Computing");
442 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
443   int S = 1 ;
444   while ( S ) {
445     S = sleep(S);
446   }
447   MESSAGE( " Syr_Impl::Count " << _Count );
448   endService( " Syr_Impl::Count"  );
449   return _Count ;
450 }
451
452 void Syr_Impl::M3p1() {
453   beginService( " Syr_Impl::M3p1" );
454   sendMessage(NOTIF_STEP, "Syr_Impl::M3p1 is Computing");
455 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
456   int S = 1 ;
457   while ( S ) {
458     S = sleep(S);
459   }
460   if ( IsEven() )
461     _CurrentInteger = 0 ;
462   else
463     _CurrentInteger = 3*_CurrentInteger + 1 ;
464   MESSAGE( " Syr_Impl::M3p1 " << _CurrentInteger );
465   endService( " Syr_Impl::M3p1"  );
466 }
467
468 void Syr_Impl::Div2() {
469   beginService( " Syr_Impl::Div2" );
470   sendMessage(NOTIF_STEP, "Syr_Impl::Div2 is Computing");
471 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
472   int S = 1 ;
473   while ( S ) {
474     S = sleep(S);
475   }
476   if ( !IsEven() )
477     _CurrentInteger = 0 ;
478   else
479     _CurrentInteger = ( _CurrentInteger >> 1 ) ;
480   MESSAGE( " Syr_Impl::Div2 " << _CurrentInteger );
481   endService( " Syr_Impl::Div2"  );
482 }
483
484 void Syr_Impl::Incr() {
485   beginService( " Syr_Impl::Incr" );
486   sendMessage(NOTIF_STEP, "Syr_Impl::Incr is Computing");
487 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
488   int S = 1 ;
489   while ( S ) {
490     S = sleep(S);
491   }
492   _Count = _Count + 1 ;
493   MESSAGE( " Syr_Impl::Incr " << _Count );
494   endService( " Syr_Impl::Incr"  );
495 }
496
497
498