Salome HOME
Methodes de test of Object References
[samples/component.git] / src / AddComponent / AddComponent_Impl.cxx
1 //  SuperVisionTest AddComponent : example of component that adds two numbers
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   : AddComponent_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 #include "Utils_ORB_INIT.hxx"
37 #include "Utils_SINGLETON.hxx"
38 #include "SALOME_NamingService.hxx"
39 #include "SALOME_LifeCycleCORBA.hxx"
40
41 #include "AddComponent_Impl.hxx"
42 #include "Adder_Impl.hxx"
43
44 AddComponent_Impl::AddComponent_Impl( CORBA::ORB_ptr orb,
45                                       PortableServer::POA_ptr poa,
46                                       PortableServer::ObjectId * contId, 
47                                       const char *instanceName,
48                                       const char *interfaceName) :
49   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
50   MESSAGE("AddComponent_Impl::AddComponent_Impl this " << hex << this << dec
51           << "activate object instanceName("
52           << instanceName << ") interfaceName(" << interfaceName << ")" )
53   _thisObj = this ;
54   _id = _poa->activate_object(_thisObj);
55   LastAddition = 0 ;
56   CallCount = 0 ;
57 }
58
59 AddComponent_Impl::AddComponent_Impl() {
60   LastAddition = 0 ;
61 }
62
63 AddComponent_Impl::~AddComponent_Impl() {
64 }
65
66 SuperVisionTest::Adder_ptr AddComponent_Impl::Addition() {
67   beginService( "AddComponent_Impl::Addition" );
68   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
69   Adder_Impl * myAdder ;
70   myAdder = new Adder_Impl( _orb , _poa, _contId,
71                             instanceName() , interfaceName() ,
72                             graphName() , nodeName() ) ;
73   SuperVisionTest::Adder_var iobject ;
74   PortableServer::ObjectId * id = myAdder->getId() ;
75   CORBA::Object_var obj = _poa->id_to_reference(*id);
76   iobject = SuperVisionTest::Adder::_narrow(obj) ;
77   endService( "AddComponent_Impl::Addition" );
78   return iobject._retn() ;
79 //  return SuperVisionTest::Adder::_duplicate(iobject) ;
80 }
81
82 bool AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
83   beginService( "AddComponent_Impl::Addition" );
84   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
85   Adder_Impl * myAdder ;
86   myAdder = new Adder_Impl( _orb , _poa, _contId,
87                             instanceName() , interfaceName() ,
88                             graphName() , nodeName() ) ;
89   SuperVisionTest::Adder_var iobject ;
90   PortableServer::ObjectId * id = myAdder->getId() ;
91   CORBA::Object_var obj = _poa->id_to_reference(*id);
92   iobject = SuperVisionTest::Adder::_narrow(obj) ;
93   endService( "AddComponent_Impl::Addition" );
94   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
95   return true ;
96 }
97
98 void AddComponent_Impl::AdditionObjRef2( bool & FuncValue ,
99                                          SuperVisionTest::Adder_out aAdder ) {
100   beginService( "AddComponent_Impl::Addition" );
101   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
102   Adder_Impl * myAdder ;
103   myAdder = new Adder_Impl( _orb , _poa, _contId,
104                             instanceName() , interfaceName() ,
105                             graphName() , nodeName() ) ;
106   SuperVisionTest::Adder_var iobject ;
107   PortableServer::ObjectId * id = myAdder->getId() ;
108   CORBA::Object_var obj = _poa->id_to_reference(*id);
109   iobject = SuperVisionTest::Adder::_narrow(obj) ;
110   endService( "AddComponent_Impl::Addition" );
111   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
112   FuncValue = true ;
113 }
114
115 double AddComponent_Impl::Add( double x , double y , double & z ) {
116   beginService( " AddComponent_Impl::Add" );
117   z = x + y ;
118   int S;
119   
120   sendMessage(NOTIF_STEP, "AddComponent_Impl::Add is Computing");
121 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
122   S = 5 ;
123   while ( S ) {
124     S = sleep(S);
125   }
126   MESSAGE( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
127        << " ) returns " << (x - y) << " after " << S << " seconds" )
128   LastAddition = z ;
129   endService( " AddComponent_Impl::Add"  );
130   return (x - y) ;
131 }
132
133 double AddComponent_Impl::AddWithoutSleep( double x , double y , double & z ) {
134   beginService( " AddComponent_Impl::AddWithoutSleep" );
135   z = x + y ;
136   LastAddition = z ;
137   if ( !strcmp( graphName() , "GraphGOTOAddMemory" ) ) {
138     CallCount += 1 ;
139     if ( CallCount == 10000 ) {
140       MESSAGE( " AddComponent_Impl::AddWithoutSleep pthread_exit CallCount " << CallCount );
141       endService( " AddComponent_Impl::AddWithoutSleep pthread_exit" );
142       pthread_exit( (void * ) NULL ) ;
143     }
144   }
145   MESSAGE( " AddComponent_Impl::AddWithoutSleep CallCount " << CallCount );
146   endService( " AddComponent_Impl::AddWithoutSleep" );
147   return (x - y) ;
148 }
149
150 long AddComponent_Impl::Sigma( long n ) {
151   long sigma = 0 ;
152   int i , j ;
153   beginService( " AddComponent_Impl::Sigma" );
154   for ( j = 0 ; j < 1000000 ; j++ ) {
155     sigma = 0 ;
156     for ( i = 1 ; i <= n ; i++ ) {
157       sigma = sigma + i ;
158     }
159   }
160   endService( " AddComponent_Impl::Sigma"  );
161   return sigma ;
162 }
163
164 double AddComponent_Impl::LastResult() {
165   beginService( " AddComponent_Impl::LastResult" );
166   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
167   int S;
168 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
169   S = 5 ;
170   while ( S ) {
171     S = sleep(S);
172   }
173   endService( " AddComponent_Impl::LastResult"  );
174   return LastAddition ;
175 }
176
177 bool AddComponent_Impl::AdditionObjRefs( const SuperVisionTest::AddComponent_ptr AddComponent1 ,
178                                          const SuperVisionTest::AddComponent_ptr Adder2 ,
179                                          const SuperVisionTest::AddComponent_ptr Adder3 ,
180                                          SuperVisionTest::AddComponent_out RetAddComponent1 ,
181                                          SuperVisionTest::AddComponent_out RetAdder2 ,
182                                          SuperVisionTest::AddComponent_out RetAdder3 ) {
183   bool RetVal = true ;
184   beginService( "AddComponent_Impl::AdditionObjRefs" );
185   cout << "beginService AddComponent_Impl::AdditionObjRefs" << endl ;
186   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
187   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
188   CORBA::ORB_var orb = init(0 , 0 ) ;
189   char * IOR = orb->object_to_string( AddComponent1 );
190   cout << "AddComponent_Impl::AdditionObjRefs AddComponent1 " << AddComponent1 << " IOR "
191        << IOR << " nil " << CORBA::is_nil( AddComponent1 ) << endl ;
192   RetAddComponent1 = SuperVisionTest::AddComponent::_duplicate( AddComponent1 ) ;
193   IOR = orb->object_to_string( Adder2 );
194   cout << "AddComponent_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
195        << CORBA::is_nil( Adder2 ) << endl ;
196   RetAdder2 = SuperVisionTest::AddComponent::_duplicate( Adder2 ) ;
197   IOR = orb->object_to_string( Adder3 );
198   cout << "AddComponent_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
199        << CORBA::is_nil( Adder3 ) << endl ;
200   RetAdder3 = SuperVisionTest::AddComponent::_duplicate( Adder3 ) ;
201   if ( CORBA::is_nil( AddComponent1 ) || CORBA::is_nil( Adder2 ) ||
202        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAddComponent1 ) ||
203        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
204     RetVal = false ;
205   }
206   cout << "endService AddComponent_Impl::AdditionObjRefs" << endl ;
207   endService( "AddComponent_Impl::AdditionObjRefs" );
208   return RetVal ;
209 }
210
211
212 extern "C"
213 {
214   PortableServer::ObjectId * AddComponentEngine_factory
215      (CORBA::ORB_ptr orb,
216       PortableServer::POA_ptr poa, 
217       PortableServer::ObjectId * contId,
218       const char *instanceName,
219       const char *interfaceName)
220   {
221     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
222             << instanceName << "," << interfaceName << "," << getpid() << ")");
223     AddComponent_Impl * myAddComponent 
224       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
225     return myAddComponent->getId() ;
226   }
227 }
228
229 Adder_Impl::Adder_Impl( CORBA::ORB_ptr orb ,
230                         PortableServer::POA_ptr poa ,
231                         PortableServer::ObjectId * contId , 
232                         const char * instanceName ,
233                         const char * interfaceName , 
234                         const char * graphName ,
235                         const char * nodeName ) :
236   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
237   Names( graphName , nodeName ) ;
238   MESSAGE("Adder_Impl::Adder_Impl activate object instanceName("
239           << instanceName << ") interfaceName(" << interfaceName << ") --> "
240           << hex << (void *) this << dec )
241   beginService( "Adder_Impl::Adder_Impl" );
242   _thisObj = this ;
243   _id = _poa->activate_object(_thisObj);
244   LastAddition = 0 ;
245   sendMessage(NOTIF_STEP, "Adder_Impl is Created");
246   endService( "Adder_Impl::Adder_Impl" );
247 }
248
249 Adder_Impl::Adder_Impl() {
250   LastAddition = 0 ;
251 }
252
253 Adder_Impl::~Adder_Impl() {
254   beginService( "Adder_Impl::~Adder_Impl" );
255   endService( "Adder_Impl::~Adder_Impl" );
256 }
257
258 void Adder_Impl::destroy() {
259   _poa->deactivate_object(*_id) ;
260   CORBA::release(_poa) ;
261   delete(_id) ;
262   _thisObj->_remove_ref();
263 }
264
265 double Adder_Impl::Add( double x , double y , double & z ) {
266   beginService( " Adder_Impl::Add" );
267   z = x + y ;
268   int S;
269   
270   sendMessage(NOTIF_STEP, "Adder_Impl::Add is Computing");
271 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
272   S = 5 ;
273   while ( S ) {
274     S = sleep(S);
275   }
276   MESSAGE( "Adder_Impl::Add( " <<  x << " , " << y << " , " << z
277        << " ) returns " << -(x - y) << " after " << S << " seconds" )
278   LastAddition = z ;
279   endService( " Adder_Impl::Add"  );
280   return -(x - y) ;
281 }
282
283 double Adder_Impl::AddWithoutSleep( double x , double y , double & z ) {
284   beginService( " Adder_Impl::AddWithoutSleep" );
285   z = x + y ;
286   endService( " Adder_Impl::AddWithoutSleep"  );
287   return -(x - y) ;
288 }
289
290 double Adder_Impl::AddAndCompare( const double x , const double y ,
291                                   const SuperVisionTest::Adder_ptr anOtherAdder ,
292                                   double & z ) {
293   beginService( " Adder_Impl::AddAndCompare" );
294   z = x + y ;
295   int S;
296   
297   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Computing");
298 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
299   S = 5 ;
300   while ( S ) {
301     S = sleep(S);
302   }
303   MESSAGE( "Adder_Impl::AddAndCompare( " <<  x << " , " << y << " , " << z
304        << " ) returns " << -(x - y) << " after " << S << " seconds" )
305   LastAddition = z ;
306   double ValFunc ;
307   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare will call anOtherAdder->LastValue()");
308   double RetVal ;
309   anOtherAdder->LastResult( RetVal ) ;
310   if ( RetVal > 0 ) {
311     ValFunc = (x - y) ;
312   }
313   else {
314     ValFunc = -(x - y) ;
315   }
316   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare has called anOtherAdder->LastValue()");
317   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Finished");
318   endService( " Adder_Impl::AddAndCompare"  );
319   return ValFunc ;
320 }
321
322 void Adder_Impl::SetLastResult( double z ) {
323   beginService( " Adder_Impl::SetLastResult" );
324   sendMessage(NOTIF_STEP, "Adder_Impl::SetLastResult is Computing");
325   int S;
326 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
327   S = 5 ;
328   while ( S ) {
329     S = sleep(S);
330   }
331   LastAddition = z ;
332   endService( " Adder_Impl::SetLastResult"  );
333   return ;
334 }
335
336 void Adder_Impl::LastResult( double & z ) {
337   beginService( " Adder_Impl::LastResult" );
338   sendMessage(NOTIF_STEP, "Adder_Impl::LastResult is Computing");
339   int S;
340 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
341   S = 5 ;
342   while ( S ) {
343     S = sleep(S);
344   }
345   z = LastAddition ;
346   endService( " Adder_Impl::LastResult"  );
347   return ;
348 }
349
350 Engines::Component_ptr Adder_Impl::LccAddComponent( const char * aContainer ,
351                                                     const char * aComponentName ) {
352   beginService( "Adder_Impl::LccAddComponent" );
353   Engines::Component_ptr objComponent ;
354   objComponent = Engines::Component::_nil() ;
355
356   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
357   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
358   CORBA::ORB_var orb = init(0 , 0 ) ;
359   SALOME_NamingService *_NS ;
360   _NS = new SALOME_NamingService();
361   _NS->init_orb( CORBA::ORB::_duplicate(orb) ) ;
362           
363   SALOME_LifeCycleCORBA LCC( _NS ) ;
364   objComponent = LCC.FindOrLoad_Component( aContainer ,
365                                            aComponentName );
366   endService( "Adder_Impl::LccAddComponent"  );
367   return objComponent ;
368 }
369