Salome HOME
Join modifications from branch V3_1_0_For_CCRT
[samples/component.git] / src / SyrComponent / SyrComponent_Impl.cxx
1 //  SuperVisionTest SyrComponent : example of component performing some mathinatical operations
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
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 "utilities.h"
35
36 #include "SyrComponent_Impl.hxx"
37 #include "Adder_Impl.hxx"
38
39 using namespace std;
40
41 SyrComponent_Impl::SyrComponent_Impl( CORBA::ORB_ptr orb ,
42                                       PortableServer::POA_ptr poa ,
43                                       PortableServer::ObjectId * contId , 
44                                       const char *instanceName ,
45                                       const char *interfaceName ,
46                                       const bool kactivate ) :
47   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
48   MESSAGE("SyrComponent_Impl::SyrComponent_Impl this " << hex << this << dec
49           << "activate object instanceName("
50           << instanceName << ") interfaceName(" << interfaceName << ")" )
51   if ( kactivate ) {
52     _thisObj = this ;
53     _id = _poa->activate_object(_thisObj);
54   }
55 }
56
57 SyrComponent_Impl::SyrComponent_Impl() {
58 }
59
60 SyrComponent_Impl::~SyrComponent_Impl() {
61 }
62
63 CORBA::Long SyrComponent_Impl::C_ISEVEN( CORBA::Long anInteger ) {
64   bool RetVal ;
65   beginService( " SyrComponent_Impl::C_ISEVEN" );
66   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_ISEVEN is Computing");
67 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
68   int S = 1 ;
69   while ( S ) {
70     S = sleep(S);
71   }
72   RetVal = ( anInteger & 1 ) == 0 ;
73   endService( " SyrComponent_Impl::C_ISEVEN"  );
74   return RetVal ;
75 }
76
77 CORBA::Long SyrComponent_Impl::C_ISONE( CORBA::Long anOddInteger ) {
78   bool RetVal ;
79   beginService( " SyrComponent_Impl::C_ISONE" );
80   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_ISONE is Computing");
81 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
82   int S = 1 ;
83   while ( S ) {
84     S = sleep(S);
85   }
86   RetVal = ( anOddInteger == 1 ) ;
87   endService( " SyrComponent_Impl::C_ISONE"  );
88   return RetVal ;
89 }
90
91 CORBA::Long SyrComponent_Impl::C_M3( CORBA::Long anOddInteger ) {
92   beginService( " SyrComponent_Impl::C_M3" );
93   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_M3 is Computing");
94 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
95   int S = 1 ;
96   while ( S ) {
97     S = sleep(S);
98   }
99   endService( " SyrComponent_Impl::C_M3"  );
100   if ( C_ISEVEN( anOddInteger ) )
101     return 0 ;
102   return ( 3*anOddInteger ) ;
103 }
104
105 CORBA::Long SyrComponent_Impl::C_M3P1( CORBA::Long anOddInteger ) {
106   beginService( " SyrComponent_Impl::C_M3P1" );
107   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_M3P1 is Computing");
108 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
109   int S = 1 ;
110   while ( S ) {
111     S = sleep(S);
112   }
113   endService( " SyrComponent_Impl::C_M3P1"  );
114   if ( C_ISEVEN( anOddInteger ) )
115     return 0 ;
116   return ( 3*anOddInteger + 1 ) ;
117 }
118
119 CORBA::Long SyrComponent_Impl::C_DIV2( CORBA::Long anEvenInteger ) {
120   beginService( " SyrComponent_Impl::C_DIV2" );
121   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_DIV2 is Computing");
122 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
123   int S = 1 ;
124   while ( S ) {
125     S = sleep(S);
126   }
127   endService( " SyrComponent_Impl::C_DIV2"  );
128   if ( !C_ISEVEN( anEvenInteger ) )
129     return 0 ;
130   return ( anEvenInteger >> 1 ) ;
131 }
132
133 CORBA::Long SyrComponent_Impl::C_INCR( CORBA::Long aCount ) {
134   beginService( " SyrComponent_Impl::C_INCR" );
135   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_INCR is Computing");
136 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
137   int S = 1 ;
138   while ( S ) {
139     S = sleep(S);
140   }
141   endService( " SyrComponent_Impl::C_INCR"  );
142   return ( aCount + 1 ) ;
143 }
144
145 void SyrComponent_Impl::CPP_SETLONG( CORBA::Long aCount ) {
146   beginService( " SyrComponent_Impl::CPP_SETLONG" );
147   sendMessage(NOTIF_STEP, "SyrComponent_Impl::CPP_SETLONG is Computing");
148 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
149   int S = 1 ;
150   while ( S ) {
151     S = sleep(S);
152   }
153   _Count = aCount ;
154   endService( " SyrComponent_Impl::CPP_SETLONG"  );
155   return ;
156 }
157
158 CORBA::Long SyrComponent_Impl::CPP_ADDTOLONG( CORBA::Long anIncr ) {
159   beginService( " SyrComponent_Impl::CPP_ADDTOLONG" );
160   sendMessage(NOTIF_STEP, "SyrComponent_Impl::CPP_ADDTOLONG is Computing");
161 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
162   int S = 1 ;
163   while ( S ) {
164     S = sleep(S);
165   }
166   endService( " SyrComponent_Impl::CPP_ADDTOLONG"  );
167   return ( _Count + anIncr ) ;
168 }
169
170 CORBA::Long SyrComponent_Impl::C_MIN( CORBA::Long aMinVal , CORBA::Long anInteger ) {
171   beginService( " SyrComponent_Impl::C_MIN" );
172   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_MIN is Computing");
173 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
174   int S = 1 ;
175   while ( S ) {
176     S = sleep(S);
177   }
178   int min = aMinVal ;
179   if ( anInteger < min || aMinVal == 0 ) {
180     min = anInteger ;
181   }
182   string trace = "SyrComponent_Impl::C_MIN : " ;
183   char cmin[ 30 ] ;
184   sprintf( cmin , "%d" , min ) ;
185   trace += cmin ;
186   sendMessage(NOTIF_TRACE, trace.c_str() );
187   endService( " SyrComponent_Impl::C_MIN"  );
188   return min ;
189 }
190
191 CORBA::Long SyrComponent_Impl::C_MAX( CORBA::Long aMaxVal , CORBA::Long anInteger ) {
192   beginService( " SyrComponent_Impl::C_MAX" );
193   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_MAX is Computing");
194 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
195   int S = 1 ;
196   while ( S ) {
197     S = sleep(S);
198   }
199   int max = aMaxVal ;
200   if ( anInteger > max ) {
201     max = anInteger ;
202   }
203   string trace = "SyrComponent_Impl::C_MAX : " ;
204   char cmax[ 30 ] ;
205   sprintf( cmax , "%d" , max ) ;
206   trace += cmax ;
207   sendMessage(NOTIF_TRACE, trace.c_str() );
208   endService( " SyrComponent_Impl::C_MAX"  );
209   return max ;
210 }
211
212 SuperVisionTest::ListOfSyr_ptr SyrComponent_Impl::C_LISTOFSYR() {
213   beginService( " SyrComponent_Impl::C_LISTOFSYR" );
214   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_LISTOFSYR is Computing");
215 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
216   int S = 1 ;
217   while ( S ) {
218     S = sleep(S);
219   }
220   ListOfSyr_Impl * aNewListOfSyr = new ListOfSyr_Impl( _orb , _poa, _contId,
221                         instanceName() , interfaceName() ,
222                         graphName() , nodeName() ) ;
223   SuperVisionTest::ListOfSyr_var iobject = (SuperVisionTest::ListOfSyr_var ) NULL ;
224   PortableServer::ObjectId * id = aNewListOfSyr->getId() ;
225   CORBA::Object_var obj = _poa->id_to_reference(*id);
226   iobject = SuperVisionTest::ListOfSyr::_narrow(obj) ;
227   endService( " SyrComponent_Impl::C_LISTOFSYR"  );
228   return SuperVisionTest::ListOfSyr::_duplicate( iobject ) ;
229 //  return ( aNewListOfSyr._retn() ) ;
230 }
231
232 SuperVisionTest::ListOfSyr_ptr SyrComponent_Impl::C_AVERAGE(
233                        SuperVisionTest::ListOfSyr_ptr aListOfSyr ,
234                        CORBA::Long anInteger ,
235                        CORBA::Long aCount ,
236                        CORBA::Double & anAverage ) {
237   beginService( " SyrComponent_Impl::C_AVERAGE" );
238   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_AVERAGE is Computing");
239 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
240   int S = 1 ;
241   while ( S ) {
242     S = sleep(S);
243   }
244   SuperVisionTest::SeqOfSyr * aSeqOfSyr = aListOfSyr->GetSeqOfSyr() ;
245   int len = aSeqOfSyr->length() ;
246   aSeqOfSyr->length( len + 1 ) ;
247   (*aSeqOfSyr)[ len ] = anInteger ;
248   aListOfSyr->SetSeqOfSyr( *aSeqOfSyr ) ;
249   anAverage = 0 ;
250   int i ;
251   for ( i = 0 ; i < len + 1 ; i++ ) {
252     anAverage += (*aSeqOfSyr)[ i ] ;
253   }
254   anAverage = anAverage / ( len + 1 ) ;
255   string end = " SyrComponent_Impl::C_AVERAGE " ;
256   char caverage[ 30 ] ;
257   sprintf( caverage , "%fd" , anAverage ) ;
258   end += caverage ;
259   endService( end.c_str() );
260   return SuperVisionTest::ListOfSyr::_duplicate( aListOfSyr ) ;
261 }
262
263 SuperVisionTest::Syr_ptr SyrComponent_Impl::Init( CORBA::Long anOddInteger ) {
264   beginService( "SyrComponent_Impl::Init" );
265   sendMessage(NOTIF_STEP, "SyrComponent_Impl creates Syr_Impl");
266 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
267   int S = 1 ;
268   while ( S ) {
269     S = sleep(S);
270   }
271   Syr_Impl * mySyr ;
272   mySyr = new Syr_Impl( _orb , _poa, _contId,
273                         instanceName() , interfaceName() ,
274                         graphName() , nodeName() , anOddInteger ) ;
275   SuperVisionTest::Syr_var iobject = (SuperVisionTest::Syr_var ) NULL ;
276   PortableServer::ObjectId * id = mySyr->getId() ;
277   CORBA::Object_var obj = _poa->id_to_reference(*id);
278   iobject = SuperVisionTest::Syr::_narrow(obj) ;
279   endService( "SyrComponent_Impl::Init" );
280   return SuperVisionTest::Syr::_duplicate(iobject) ;
281 }
282
283 ListOfSyr_Impl::ListOfSyr_Impl( CORBA::ORB_ptr orb ,
284                     PortableServer::POA_ptr poa ,
285                     PortableServer::ObjectId * contId , 
286                     const char * instanceName ,
287                     const char * interfaceName , 
288                     const char * graphName ,
289                     const char * nodeName ) :
290   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
291   Names( graphName , nodeName ) ;
292   MESSAGE("ListOfSyr_Impl::ListOfSyr_Impl activate object instanceName("
293           << instanceName << ") interfaceName(" << interfaceName << ") --> "
294           << hex << (void *) this << dec )
295   beginService( "ListOfSyr_Impl::ListOfSyr_Impl" );
296   _thisObj = this ;
297   _id = _poa->activate_object(_thisObj);
298   sendMessage(NOTIF_STEP, "ListOfSyr_Impl is Created");
299   endService( "ListOfSyr_Impl::ListOfSyr_Impl" );
300 }
301
302 ListOfSyr_Impl::ListOfSyr_Impl() {
303 }
304
305 ListOfSyr_Impl::~ListOfSyr_Impl() {
306   beginService( "ListOfSyr_Impl::~ListOfSyr_Impl" );
307   endService( "ListOfSyr_Impl::~ListOfSyr_Impl" );
308 }
309
310 SuperVisionTest::SeqOfSyr * ListOfSyr_Impl::GetSeqOfSyr() {
311   SuperVisionTest::SeqOfSyr_var aSeqOfSyr = new SuperVisionTest::SeqOfSyr( _SeqOfSyr ) ;
312   return ( aSeqOfSyr._retn() ) ;
313 }
314
315 void ListOfSyr_Impl::SetSeqOfSyr( const SuperVisionTest::SeqOfSyr & aSeqOfSyr ) {
316   _SeqOfSyr = aSeqOfSyr ;
317   return ;
318 }
319
320 extern "C"
321 {
322   PortableServer::ObjectId * SyrComponentEngine_factory
323      (CORBA::ORB_ptr orb,
324       PortableServer::POA_ptr poa, 
325       PortableServer::ObjectId * contId,
326       const char *instanceName,
327       const char *interfaceName)
328   {
329     MESSAGE("SyrComponentEngine_factory SyrComponentEngine ("
330             << instanceName << "," << interfaceName << "," << getpid() << ")");
331     SyrComponent_Impl * mySyrComponent 
332       = new SyrComponent_Impl(orb, poa, contId, instanceName, interfaceName);
333     return mySyrComponent->getId() ;
334   }
335 }
336
337 Syr_Impl::Syr_Impl( CORBA::ORB_ptr orb ,
338                     PortableServer::POA_ptr poa ,
339                     PortableServer::ObjectId * contId , 
340                     const char * instanceName ,
341                     const char * interfaceName , 
342                     const char * graphName ,
343                     const char * nodeName ,
344                     const CORBA::Long anOddInteger ) :
345   SyrComponent_Impl(orb, poa, contId, instanceName, interfaceName,false) {
346   Names( graphName , nodeName ) ;
347   MESSAGE("Syr_Impl::Syr_Impl activate object instanceName("
348           << instanceName << ") interfaceName(" << interfaceName << ") --> "
349           << hex << (void *) this << dec )
350   beginService( "Syr_Impl::Syr_Impl" );
351   _thisObj = this ;
352   _id = _poa->activate_object(_thisObj);
353   _InitialInteger = anOddInteger ;
354   _CurrentInteger = anOddInteger ;
355   _Count = 0 ;
356   sendMessage(NOTIF_STEP, "Syr_Impl is Created");
357   endService( "Syr_Impl::Syr_Impl" );
358 }
359
360 Syr_Impl::Syr_Impl() {
361 }
362
363 Syr_Impl::~Syr_Impl() {
364   beginService( "Syr_Impl::~Syr_Impl" );
365   endService( "Syr_Impl::~Syr_Impl" );
366 }
367
368 CORBA::Long Syr_Impl::Initial() {
369   beginService( " Syr_Impl::Initial" );
370   sendMessage(NOTIF_STEP, "Syr_Impl::Initial is Computing");
371 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
372   int S = 1 ;
373   while ( S ) {
374     S = sleep(S);
375   }
376   endService( " Syr_Impl::Initial"  );
377   return _InitialInteger ;
378 }
379
380 CORBA::Long Syr_Impl::Current() {
381   beginService( " Syr_Impl::Current" );
382   sendMessage(NOTIF_STEP, "Syr_Impl::Current is Computing");
383 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
384   int S = 1 ;
385   while ( S ) {
386     S = sleep(S);
387   }
388   endService( " Syr_Impl::Current"  );
389   return _CurrentInteger ;
390 }
391
392 CORBA::Long Syr_Impl::IsEven() {
393   bool RetVal ;
394   beginService( " Syr_Impl::IsEven" );
395   sendMessage(NOTIF_STEP, "Syr_Impl::IsEven is Computing");
396 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
397   int S = 1 ;
398   while ( S ) {
399     S = sleep(S);
400   }
401   RetVal = ( _CurrentInteger & 1 ) == 0 ;
402   MESSAGE( " Syr_Impl::IsEven " << _CurrentInteger << " " << RetVal );
403   endService( " Syr_Impl::IsEven"  );
404   return RetVal ;
405 }
406
407 CORBA::Long Syr_Impl::IsOne() {
408   bool RetVal ;
409   beginService( " Syr_Impl::IsOne" );
410   sendMessage(NOTIF_STEP, "Syr_Impl::IsOne is Computing");
411 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
412   int S = 1 ;
413   while ( S ) {
414     S = sleep(S);
415   }
416   RetVal = ( _CurrentInteger == 1 ) ;
417   MESSAGE( " Syr_Impl::IsOne " << _CurrentInteger << " " << RetVal );
418   endService( " Syr_Impl::IsOne"  );
419   return RetVal ;
420 }
421
422 CORBA::Long Syr_Impl::Count() {
423   beginService( " Syr_Impl::Count" );
424   sendMessage(NOTIF_STEP, "Syr_Impl::Count is Computing");
425 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
426   int S = 1 ;
427   while ( S ) {
428     S = sleep(S);
429   }
430   MESSAGE( " Syr_Impl::Count " << _Count );
431   endService( " Syr_Impl::Count"  );
432   return _Count ;
433 }
434
435 void Syr_Impl::M3p1() {
436   beginService( " Syr_Impl::M3p1" );
437   sendMessage(NOTIF_STEP, "Syr_Impl::M3p1 is Computing");
438 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
439   int S = 1 ;
440   while ( S ) {
441     S = sleep(S);
442   }
443   if ( IsEven() )
444     _CurrentInteger = 0 ;
445   else
446     _CurrentInteger = 3*_CurrentInteger + 1 ;
447   MESSAGE( " Syr_Impl::M3p1 " << _CurrentInteger );
448   endService( " Syr_Impl::M3p1"  );
449 }
450
451 void Syr_Impl::Div2() {
452   beginService( " Syr_Impl::Div2" );
453   sendMessage(NOTIF_STEP, "Syr_Impl::Div2 is Computing");
454 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
455   int S = 1 ;
456   while ( S ) {
457     S = sleep(S);
458   }
459   if ( !IsEven() )
460     _CurrentInteger = 0 ;
461   else
462     _CurrentInteger = ( _CurrentInteger >> 1 ) ;
463   MESSAGE( " Syr_Impl::Div2 " << _CurrentInteger );
464   endService( " Syr_Impl::Div2"  );
465 }
466
467 void Syr_Impl::Incr() {
468   beginService( " Syr_Impl::Incr" );
469   sendMessage(NOTIF_STEP, "Syr_Impl::Incr is Computing");
470 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
471   int S = 1 ;
472   while ( S ) {
473     S = sleep(S);
474   }
475   _Count = _Count + 1 ;
476   MESSAGE( " Syr_Impl::Incr " << _Count );
477   endService( " Syr_Impl::Incr"  );
478 }
479
480
481