Salome HOME
DCQ : Merge with Ecole_Ete_a6.
[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 double AddComponent_Impl::Add( double x , double y , double & z ) {
77   beginService( " AddComponent_Impl::Add" );
78   z = x + y ;
79   int S;
80   
81   sendMessage(NOTIF_STEP, "AddComponent_Impl::Add is Computing");
82   S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
83   sleep(1);
84   MESSAGE( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
85        << " ) returns " << (x - y) << " after " << S << " seconds" )
86   LastAddition = z ;
87   endService( " AddComponent_Impl::Add"  );
88   return (x - y) ;
89 }
90
91 long AddComponent_Impl::Sigma( long n ) {
92   long sigma = 0 ;
93   int i , j ;
94   beginService( " AddComponent_Impl::Sigma" );
95   for ( j = 0 ; j < 1000000 ; j++ ) {
96     sigma = 0 ;
97     for ( i = 1 ; i <= n ; i++ ) {
98       sigma = sigma + i ;
99     }
100   }
101   endService( " AddComponent_Impl::Sigma"  );
102   return sigma ;
103 }
104
105 double AddComponent_Impl::LastResult() {
106   beginService( " AddComponent_Impl::LastResult" );
107   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
108   int S;
109   S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
110   sleep(1);
111   endService( " AddComponent_Impl::LastResult"  );
112   return LastAddition ;
113 }
114
115 extern "C"
116 {
117   PortableServer::ObjectId * AddComponentEngine_factory
118      (CORBA::ORB_ptr orb,
119       PortableServer::POA_ptr poa, 
120       PortableServer::ObjectId * contId,
121       const char *instanceName,
122       const char *interfaceName)
123   {
124     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
125             << instanceName << "," << interfaceName << "," << getpid() << ")");
126     AddComponent_Impl * myAddComponent 
127       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
128     return myAddComponent->getId() ;
129   }
130 }
131
132 Adder_Impl::Adder_Impl( CORBA::ORB_ptr orb ,
133                         PortableServer::POA_ptr poa ,
134                         PortableServer::ObjectId * contId , 
135                         const char * instanceName ,
136                         const char * interfaceName , 
137                         const char * graphName ,
138                         const char * nodeName ) :
139   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
140   Names( graphName , nodeName ) ;
141   MESSAGE("Adder_Impl::Adder_Impl activate object instanceName("
142           << instanceName << ") interfaceName(" << interfaceName << ") --> "
143           << hex << (void *) this << dec )
144   beginService( "Adder_Impl::Adder_Impl" );
145   _thisObj = this ;
146   _id = _poa->activate_object(_thisObj);
147   LastAddition = 0 ;
148   sendMessage(NOTIF_STEP, "Adder_Impl is Created");
149   endService( "Adder_Impl::Adder_Impl" );
150 }
151
152 Adder_Impl::Adder_Impl() {
153   LastAddition = 0 ;
154 }
155
156 Adder_Impl::~Adder_Impl() {
157   beginService( "Adder_Impl::~Adder_Impl" );
158   endService( "Adder_Impl::~Adder_Impl" );
159 }
160
161 double Adder_Impl::Add( double x , double y , double & z ) {
162   beginService( " Adder_Impl::Add" );
163   z = x + y ;
164   int S;
165   
166   sendMessage(NOTIF_STEP, "Adder_Impl::Add is Computing");
167   S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
168   sleep(1);
169   MESSAGE( "Adder_Impl::Add( " <<  x << " , " << y << " , " << z
170        << " ) returns " << -(x - y) << " after " << S << " seconds" )
171   LastAddition = z ;
172   endService( " Adder_Impl::Add"  );
173   return -(x - y) ;
174 }
175
176 double Adder_Impl::AddAndCompare( const double x , const double y ,
177                                   const SuperVisionTest::Adder_ptr anOtherAdder ,
178                                   double & z ) {
179   beginService( " Adder_Impl::AddAndCompare" );
180   z = x + y ;
181   int S;
182   
183   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Computing");
184   S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
185   sleep(1);
186   MESSAGE( "Adder_Impl::AddAndCompare( " <<  x << " , " << y << " , " << z
187        << " ) returns " << -(x - y) << " after " << S << " seconds" )
188   LastAddition = z ;
189   double ValFunc ;
190   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare will call anOtherAdder->LastValue()");
191   double RetVal ;
192   anOtherAdder->LastResult( RetVal ) ;
193   if ( RetVal > 0 ) {
194     ValFunc = (x - y) ;
195   }
196   else {
197     ValFunc = -(x - y) ;
198   }
199   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare has called anOtherAdder->LastValue()");
200   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Finished");
201   endService( " Adder_Impl::AddAndCompare"  );
202   return ValFunc ;
203 }
204
205 void Adder_Impl::SetLastResult( double z ) {
206   beginService( " Adder_Impl::SetLastResult" );
207   sendMessage(NOTIF_STEP, "Adder_Impl::SetLastResult is Computing");
208   int S;
209   S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
210   sleep(1);
211   LastAddition = z ;
212   endService( " Adder_Impl::SetLastResult"  );
213   return ;
214 }
215
216 void Adder_Impl::LastResult( double & z ) {
217   beginService( " Adder_Impl::LastResult" );
218   sendMessage(NOTIF_STEP, "Adder_Impl::LastResult is Computing");
219   int S;
220   S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
221   sleep(1);
222   z = LastAddition ;
223   endService( " Adder_Impl::LastResult"  );
224   return ;
225 }
226
227