Salome HOME
Methods to check of instances of components
[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 void SyrComponent_Impl::CPP_SETLONG( const long aCount ) {
145   beginService( " SyrComponent_Impl::CPP_SETLONG" );
146   sendMessage(NOTIF_STEP, "SyrComponent_Impl::CPP_SETLONG 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   _Count = aCount ;
153   endService( " SyrComponent_Impl::CPP_SETLONG"  );
154   return ;
155 }
156
157 long SyrComponent_Impl::CPP_ADDTOLONG( const long anIncr ) {
158   beginService( " SyrComponent_Impl::CPP_ADDTOLONG" );
159   sendMessage(NOTIF_STEP, "SyrComponent_Impl::CPP_ADDTOLONG is Computing");
160 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
161   int S = 1 ;
162   while ( S ) {
163     S = sleep(S);
164   }
165   endService( " SyrComponent_Impl::CPP_ADDTOLONG"  );
166   return ( _Count + anIncr ) ;
167 }
168
169 long SyrComponent_Impl::C_MIN( const long aMinVal , const long anInteger ) {
170   beginService( " SyrComponent_Impl::C_MIN" );
171   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_MIN is Computing");
172 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
173   int S = 1 ;
174   while ( S ) {
175     S = sleep(S);
176   }
177   int min = aMinVal ;
178   if ( anInteger < min || aMinVal == 0 ) {
179     min = anInteger ;
180   }
181   string trace = "SyrComponent_Impl::C_MIN : " ;
182   char cmin[ 30 ] ;
183   sprintf( cmin , "%d" , min ) ;
184   trace += cmin ;
185   sendMessage(NOTIF_TRACE, trace.c_str() );
186   endService( " SyrComponent_Impl::C_MIN"  );
187   return min ;
188 }
189
190 long SyrComponent_Impl::C_MAX( const long aMaxVal , const long anInteger ) {
191   beginService( " SyrComponent_Impl::C_MAX" );
192   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_MAX is Computing");
193 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
194   int S = 1 ;
195   while ( S ) {
196     S = sleep(S);
197   }
198   int max = aMaxVal ;
199   if ( anInteger > max ) {
200     max = anInteger ;
201   }
202   string trace = "SyrComponent_Impl::C_MAX : " ;
203   char cmax[ 30 ] ;
204   sprintf( cmax , "%d" , max ) ;
205   trace += cmax ;
206   sendMessage(NOTIF_TRACE, trace.c_str() );
207   endService( " SyrComponent_Impl::C_MAX"  );
208   return max ;
209 }
210
211 SuperVisionTest::ListOfSyr_ptr SyrComponent_Impl::C_LISTOFSYR() {
212   beginService( " SyrComponent_Impl::C_LISTOFSYR" );
213   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_LISTOFSYR is Computing");
214 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
215   int S = 1 ;
216   while ( S ) {
217     S = sleep(S);
218   }
219   ListOfSyr_Impl * aNewListOfSyr = new ListOfSyr_Impl( _orb , _poa, _contId,
220                         instanceName() , interfaceName() ,
221                         graphName() , nodeName() ) ;
222   SuperVisionTest::ListOfSyr_var iobject = (SuperVisionTest::ListOfSyr_var ) NULL ;
223   PortableServer::ObjectId * id = aNewListOfSyr->getId() ;
224   CORBA::Object_var obj = _poa->id_to_reference(*id);
225   iobject = SuperVisionTest::ListOfSyr::_narrow(obj) ;
226   endService( " SyrComponent_Impl::C_LISTOFSYR"  );
227   return SuperVisionTest::ListOfSyr::_duplicate( iobject ) ;
228 //  return ( aNewListOfSyr._retn() ) ;
229 }
230
231 SuperVisionTest::ListOfSyr_ptr SyrComponent_Impl::C_AVERAGE(
232                        const SuperVisionTest::ListOfSyr_ptr aListOfSyr ,
233                        const long anInteger ,
234                        const long aCount ,
235                        double & anAverage ) {
236   beginService( " SyrComponent_Impl::C_AVERAGE" );
237   sendMessage(NOTIF_STEP, "SyrComponent_Impl::C_AVERAGE is Computing");
238 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
239   int S = 1 ;
240   while ( S ) {
241     S = sleep(S);
242   }
243   SuperVisionTest::SeqOfSyr * aSeqOfSyr = aListOfSyr->GetSeqOfSyr() ;
244   int len = aSeqOfSyr->length() ;
245   aSeqOfSyr->length( len + 1 ) ;
246   (*aSeqOfSyr)[ len ] = anInteger ;
247   aListOfSyr->SetSeqOfSyr( *aSeqOfSyr ) ;
248   anAverage = 0 ;
249   int i ;
250   for ( i = 0 ; i < len + 1 ; i++ ) {
251     anAverage += (*aSeqOfSyr)[ i ] ;
252   }
253   anAverage = anAverage / ( len + 1 ) ;
254   string end = " SyrComponent_Impl::C_AVERAGE " ;
255   char caverage[ 30 ] ;
256   sprintf( caverage , "%fd" , anAverage ) ;
257   end += caverage ;
258   endService( end.c_str() );
259   return SuperVisionTest::ListOfSyr::_duplicate( aListOfSyr ) ;
260 }
261
262 SuperVisionTest::Syr_ptr SyrComponent_Impl::Init( const long anOddInteger ) {
263   beginService( "SyrComponent_Impl::Init" );
264   sendMessage(NOTIF_STEP, "SyrComponent_Impl creates Syr_Impl");
265 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
266   int S = 1 ;
267   while ( S ) {
268     S = sleep(S);
269   }
270   Syr_Impl * mySyr ;
271   mySyr = new Syr_Impl( _orb , _poa, _contId,
272                         instanceName() , interfaceName() ,
273                         graphName() , nodeName() , anOddInteger ) ;
274   SuperVisionTest::Syr_var iobject = (SuperVisionTest::Syr_var ) NULL ;
275   PortableServer::ObjectId * id = mySyr->getId() ;
276   CORBA::Object_var obj = _poa->id_to_reference(*id);
277   iobject = SuperVisionTest::Syr::_narrow(obj) ;
278   endService( "SyrComponent_Impl::Init" );
279   return SuperVisionTest::Syr::_duplicate(iobject) ;
280 }
281
282 ListOfSyr_Impl::ListOfSyr_Impl( CORBA::ORB_ptr orb ,
283                     PortableServer::POA_ptr poa ,
284                     PortableServer::ObjectId * contId , 
285                     const char * instanceName ,
286                     const char * interfaceName , 
287                     const char * graphName ,
288                     const char * nodeName ) :
289   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
290   Names( graphName , nodeName ) ;
291   MESSAGE("ListOfSyr_Impl::ListOfSyr_Impl activate object instanceName("
292           << instanceName << ") interfaceName(" << interfaceName << ") --> "
293           << hex << (void *) this << dec )
294   beginService( "ListOfSyr_Impl::ListOfSyr_Impl" );
295   _thisObj = this ;
296   _id = _poa->activate_object(_thisObj);
297   sendMessage(NOTIF_STEP, "ListOfSyr_Impl is Created");
298   endService( "ListOfSyr_Impl::ListOfSyr_Impl" );
299 }
300
301 ListOfSyr_Impl::ListOfSyr_Impl() {
302 }
303
304 ListOfSyr_Impl::~ListOfSyr_Impl() {
305   beginService( "ListOfSyr_Impl::~ListOfSyr_Impl" );
306   endService( "ListOfSyr_Impl::~ListOfSyr_Impl" );
307 }
308
309 SuperVisionTest::SeqOfSyr * ListOfSyr_Impl::GetSeqOfSyr() {
310   SuperVisionTest::SeqOfSyr_var aSeqOfSyr = new SuperVisionTest::SeqOfSyr( _SeqOfSyr ) ;
311   return ( aSeqOfSyr._retn() ) ;
312 }
313
314 void ListOfSyr_Impl::SetSeqOfSyr( const SuperVisionTest::SeqOfSyr & aSeqOfSyr ) {
315   _SeqOfSyr = aSeqOfSyr ;
316   return ;
317 }
318
319 extern "C"
320 {
321   PortableServer::ObjectId * SyrComponentEngine_factory
322      (CORBA::ORB_ptr orb,
323       PortableServer::POA_ptr poa, 
324       PortableServer::ObjectId * contId,
325       const char *instanceName,
326       const char *interfaceName)
327   {
328     MESSAGE("SyrComponentEngine_factory SyrComponentEngine ("
329             << instanceName << "," << interfaceName << "," << getpid() << ")");
330     SyrComponent_Impl * mySyrComponent 
331       = new SyrComponent_Impl(orb, poa, contId, instanceName, interfaceName);
332     return mySyrComponent->getId() ;
333   }
334 }
335
336 Syr_Impl::Syr_Impl( CORBA::ORB_ptr orb ,
337                     PortableServer::POA_ptr poa ,
338                     PortableServer::ObjectId * contId , 
339                     const char * instanceName ,
340                     const char * interfaceName , 
341                     const char * graphName ,
342                     const char * nodeName ,
343                     const long anOddInteger ) :
344   SyrComponent_Impl(orb, poa, contId, instanceName, interfaceName,false) {
345   Names( graphName , nodeName ) ;
346   MESSAGE("Syr_Impl::Syr_Impl activate object instanceName("
347           << instanceName << ") interfaceName(" << interfaceName << ") --> "
348           << hex << (void *) this << dec )
349   beginService( "Syr_Impl::Syr_Impl" );
350   _thisObj = this ;
351   _id = _poa->activate_object(_thisObj);
352   _InitialInteger = anOddInteger ;
353   _CurrentInteger = anOddInteger ;
354   _Count = 0 ;
355   sendMessage(NOTIF_STEP, "Syr_Impl is Created");
356   endService( "Syr_Impl::Syr_Impl" );
357 }
358
359 Syr_Impl::Syr_Impl() {
360 }
361
362 Syr_Impl::~Syr_Impl() {
363   beginService( "Syr_Impl::~Syr_Impl" );
364   endService( "Syr_Impl::~Syr_Impl" );
365 }
366
367 long Syr_Impl::Initial() {
368   beginService( " Syr_Impl::Initial" );
369   sendMessage(NOTIF_STEP, "Syr_Impl::Initial 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   endService( " Syr_Impl::Initial"  );
376   return _InitialInteger ;
377 }
378
379 long Syr_Impl::Current() {
380   beginService( " Syr_Impl::Current" );
381   sendMessage(NOTIF_STEP, "Syr_Impl::Current is Computing");
382 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
383   int S = 1 ;
384   while ( S ) {
385     S = sleep(S);
386   }
387   endService( " Syr_Impl::Current"  );
388   return _CurrentInteger ;
389 }
390
391 long Syr_Impl::IsEven() {
392   bool RetVal ;
393   beginService( " Syr_Impl::IsEven" );
394   sendMessage(NOTIF_STEP, "Syr_Impl::IsEven is Computing");
395 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
396   int S = 1 ;
397   while ( S ) {
398     S = sleep(S);
399   }
400   RetVal = ( _CurrentInteger & 1 ) == 0 ;
401   MESSAGE( " Syr_Impl::IsEven " << _CurrentInteger << " " << RetVal );
402   endService( " Syr_Impl::IsEven"  );
403   return RetVal ;
404 }
405
406 long Syr_Impl::IsOne() {
407   bool RetVal ;
408   beginService( " Syr_Impl::IsOne" );
409   sendMessage(NOTIF_STEP, "Syr_Impl::IsOne is Computing");
410 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
411   int S = 1 ;
412   while ( S ) {
413     S = sleep(S);
414   }
415   RetVal = ( _CurrentInteger == 1 ) ;
416   MESSAGE( " Syr_Impl::IsOne " << _CurrentInteger << " " << RetVal );
417   endService( " Syr_Impl::IsOne"  );
418   return RetVal ;
419 }
420
421 long Syr_Impl::Count() {
422   beginService( " Syr_Impl::Count" );
423   sendMessage(NOTIF_STEP, "Syr_Impl::Count is Computing");
424 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
425   int S = 1 ;
426   while ( S ) {
427     S = sleep(S);
428   }
429   MESSAGE( " Syr_Impl::Count " << _Count );
430   endService( " Syr_Impl::Count"  );
431   return _Count ;
432 }
433
434 void Syr_Impl::M3p1() {
435   beginService( " Syr_Impl::M3p1" );
436   sendMessage(NOTIF_STEP, "Syr_Impl::M3p1 is Computing");
437 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
438   int S = 1 ;
439   while ( S ) {
440     S = sleep(S);
441   }
442   if ( IsEven() )
443     _CurrentInteger = 0 ;
444   else
445     _CurrentInteger = 3*_CurrentInteger + 1 ;
446   MESSAGE( " Syr_Impl::M3p1 " << _CurrentInteger );
447   endService( " Syr_Impl::M3p1"  );
448 }
449
450 void Syr_Impl::Div2() {
451   beginService( " Syr_Impl::Div2" );
452   sendMessage(NOTIF_STEP, "Syr_Impl::Div2 is Computing");
453 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
454   int S = 1 ;
455   while ( S ) {
456     S = sleep(S);
457   }
458   if ( !IsEven() )
459     _CurrentInteger = 0 ;
460   else
461     _CurrentInteger = ( _CurrentInteger >> 1 ) ;
462   MESSAGE( " Syr_Impl::Div2 " << _CurrentInteger );
463   endService( " Syr_Impl::Div2"  );
464 }
465
466 void Syr_Impl::Incr() {
467   beginService( " Syr_Impl::Incr" );
468   sendMessage(NOTIF_STEP, "Syr_Impl::Incr is Computing");
469 //  int S = 1+(int) (2.0*rand()/(RAND_MAX+1.0));
470   int S = 1 ;
471   while ( S ) {
472     S = sleep(S);
473   }
474   _Count = _Count + 1 ;
475   MESSAGE( " Syr_Impl::Incr " << _Count );
476   endService( " Syr_Impl::Incr"  );
477 }
478
479
480