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