Salome HOME
New Methods for test of ObjRef
[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
37 #include "AddComponent_Impl.hxx"
38 #include "Adder_Impl.hxx"
39
40 AddComponent_Impl::AddComponent_Impl( CORBA::ORB_ptr orb,
41                                       PortableServer::POA_ptr poa,
42                                       PortableServer::ObjectId * contId, 
43                                       const char *instanceName,
44                                       const char *interfaceName) :
45   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
46   MESSAGE("AddComponent_Impl::AddComponent_Impl this " << hex << this << dec
47           << "activate object instanceName("
48           << instanceName << ") interfaceName(" << interfaceName << ")" )
49   _thisObj = this ;
50   _id = _poa->activate_object(_thisObj);
51   LastAddition = 0 ;
52 }
53
54 AddComponent_Impl::AddComponent_Impl() {
55   LastAddition = 0 ;
56 }
57
58 AddComponent_Impl::~AddComponent_Impl() {
59 }
60
61 SuperVisionTest::Adder_ptr AddComponent_Impl::Addition() {
62   beginService( "AddComponent_Impl::Addition" );
63   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
64   Adder_Impl * myAdder ;
65   myAdder = new Adder_Impl( _orb , _poa, _contId,
66                             instanceName() , interfaceName() ,
67                             graphName() , nodeName() ) ;
68   SuperVisionTest::Adder_var iobject ;
69   PortableServer::ObjectId * id = myAdder->getId() ;
70   CORBA::Object_var obj = _poa->id_to_reference(*id);
71   iobject = SuperVisionTest::Adder::_narrow(obj) ;
72   endService( "AddComponent_Impl::Addition" );
73   return SuperVisionTest::Adder::_duplicate(iobject) ;
74 }
75
76 bool AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
77   beginService( "AddComponent_Impl::Addition" );
78   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
79   Adder_Impl * myAdder ;
80   myAdder = new Adder_Impl( _orb , _poa, _contId,
81                             instanceName() , interfaceName() ,
82                             graphName() , nodeName() ) ;
83   SuperVisionTest::Adder_var iobject ;
84   PortableServer::ObjectId * id = myAdder->getId() ;
85   CORBA::Object_var obj = _poa->id_to_reference(*id);
86   iobject = SuperVisionTest::Adder::_narrow(obj) ;
87   endService( "AddComponent_Impl::Addition" );
88   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
89   return true ;
90 }
91
92 void AddComponent_Impl::AdditionObjRef2( bool & FuncValue ,
93                                          SuperVisionTest::Adder_out aAdder ) {
94   beginService( "AddComponent_Impl::Addition" );
95   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
96   Adder_Impl * myAdder ;
97   myAdder = new Adder_Impl( _orb , _poa, _contId,
98                             instanceName() , interfaceName() ,
99                             graphName() , nodeName() ) ;
100   SuperVisionTest::Adder_var iobject ;
101   PortableServer::ObjectId * id = myAdder->getId() ;
102   CORBA::Object_var obj = _poa->id_to_reference(*id);
103   iobject = SuperVisionTest::Adder::_narrow(obj) ;
104   endService( "AddComponent_Impl::Addition" );
105   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
106   FuncValue = true ;
107 }
108
109 double AddComponent_Impl::Add( double x , double y , double & z ) {
110   beginService( " AddComponent_Impl::Add" );
111   z = x + y ;
112   int S;
113   
114   sendMessage(NOTIF_STEP, "AddComponent_Impl::Add is Computing");
115 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
116   S = 5 ;
117   while ( S ) {
118     S = sleep(S);
119   }
120   MESSAGE( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
121        << " ) returns " << (x - y) << " after " << S << " seconds" )
122   LastAddition = z ;
123   endService( " AddComponent_Impl::Add"  );
124   return (x - y) ;
125 }
126
127 long AddComponent_Impl::Sigma( long n ) {
128   long sigma = 0 ;
129   int i , j ;
130   beginService( " AddComponent_Impl::Sigma" );
131   for ( j = 0 ; j < 1000000 ; j++ ) {
132     sigma = 0 ;
133     for ( i = 1 ; i <= n ; i++ ) {
134       sigma = sigma + i ;
135     }
136   }
137   endService( " AddComponent_Impl::Sigma"  );
138   return sigma ;
139 }
140
141 double AddComponent_Impl::LastResult() {
142   beginService( " AddComponent_Impl::LastResult" );
143   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
144   int S;
145 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
146   S = 5 ;
147   while ( S ) {
148     S = sleep(S);
149   }
150   endService( " AddComponent_Impl::LastResult"  );
151   return LastAddition ;
152 }
153
154 extern "C"
155 {
156   PortableServer::ObjectId * AddComponentEngine_factory
157      (CORBA::ORB_ptr orb,
158       PortableServer::POA_ptr poa, 
159       PortableServer::ObjectId * contId,
160       const char *instanceName,
161       const char *interfaceName)
162   {
163     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
164             << instanceName << "," << interfaceName << "," << getpid() << ")");
165     AddComponent_Impl * myAddComponent 
166       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
167     return myAddComponent->getId() ;
168   }
169 }
170
171 Adder_Impl::Adder_Impl( CORBA::ORB_ptr orb ,
172                         PortableServer::POA_ptr poa ,
173                         PortableServer::ObjectId * contId , 
174                         const char * instanceName ,
175                         const char * interfaceName , 
176                         const char * graphName ,
177                         const char * nodeName ) :
178   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
179   Names( graphName , nodeName ) ;
180   MESSAGE("Adder_Impl::Adder_Impl activate object instanceName("
181           << instanceName << ") interfaceName(" << interfaceName << ") --> "
182           << hex << (void *) this << dec )
183   beginService( "Adder_Impl::Adder_Impl" );
184   _thisObj = this ;
185   _id = _poa->activate_object(_thisObj);
186   LastAddition = 0 ;
187   sendMessage(NOTIF_STEP, "Adder_Impl is Created");
188   endService( "Adder_Impl::Adder_Impl" );
189 }
190
191 Adder_Impl::Adder_Impl() {
192   LastAddition = 0 ;
193 }
194
195 Adder_Impl::~Adder_Impl() {
196   beginService( "Adder_Impl::~Adder_Impl" );
197   endService( "Adder_Impl::~Adder_Impl" );
198 }
199
200 double Adder_Impl::Add( double x , double y , double & z ) {
201   beginService( " Adder_Impl::Add" );
202   z = x + y ;
203   int S;
204   
205   sendMessage(NOTIF_STEP, "Adder_Impl::Add is Computing");
206 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
207   S = 5 ;
208   while ( S ) {
209     S = sleep(S);
210   }
211   MESSAGE( "Adder_Impl::Add( " <<  x << " , " << y << " , " << z
212        << " ) returns " << -(x - y) << " after " << S << " seconds" )
213   LastAddition = z ;
214   endService( " Adder_Impl::Add"  );
215   return -(x - y) ;
216 }
217
218 double Adder_Impl::AddAndCompare( const double x , const double y ,
219                                   const SuperVisionTest::Adder_ptr anOtherAdder ,
220                                   double & z ) {
221   beginService( " Adder_Impl::AddAndCompare" );
222   z = x + y ;
223   int S;
224   
225   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Computing");
226 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
227   S = 5 ;
228   while ( S ) {
229     S = sleep(S);
230   }
231   MESSAGE( "Adder_Impl::AddAndCompare( " <<  x << " , " << y << " , " << z
232        << " ) returns " << -(x - y) << " after " << S << " seconds" )
233   LastAddition = z ;
234   double ValFunc ;
235   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare will call anOtherAdder->LastValue()");
236   double RetVal ;
237   anOtherAdder->LastResult( RetVal ) ;
238   if ( RetVal > 0 ) {
239     ValFunc = (x - y) ;
240   }
241   else {
242     ValFunc = -(x - y) ;
243   }
244   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare has called anOtherAdder->LastValue()");
245   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Finished");
246   endService( " Adder_Impl::AddAndCompare"  );
247   return ValFunc ;
248 }
249
250 void Adder_Impl::SetLastResult( double z ) {
251   beginService( " Adder_Impl::SetLastResult" );
252   sendMessage(NOTIF_STEP, "Adder_Impl::SetLastResult is Computing");
253   int S;
254 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
255   S = 5 ;
256   while ( S ) {
257     S = sleep(S);
258   }
259   LastAddition = z ;
260   endService( " Adder_Impl::SetLastResult"  );
261   return ;
262 }
263
264 void Adder_Impl::LastResult( double & z ) {
265   beginService( " Adder_Impl::LastResult" );
266   sendMessage(NOTIF_STEP, "Adder_Impl::LastResult is Computing");
267   int S;
268 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
269   S = 5 ;
270   while ( S ) {
271     S = sleep(S);
272   }
273   z = LastAddition ;
274   endService( " Adder_Impl::LastResult"  );
275   return ;
276 }
277
278