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