Salome HOME
Changes according to idl
[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 }
57
58 AddComponent_Impl::AddComponent_Impl() {
59   LastAddition = 0 ;
60 }
61
62 AddComponent_Impl::~AddComponent_Impl() {
63 }
64
65 double AddComponent_Impl::Add( double x , double y , double & z ) {
66   beginService( " AddComponent_Impl::Add" );
67   z = x + y ;
68   int S;
69   
70   sendMessage(NOTIF_STEP, "AddComponent_Impl::Add is Computing");
71 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
72   S = 5 ;
73   while ( S ) {
74     S = sleep(S);
75   }
76   MESSAGE( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
77        << " ) returns " << (x - y) << " after " << S << " seconds" )
78   LastAddition = z ;
79   endService( " AddComponent_Impl::Add"  );
80   return (x - y) ;
81 }
82
83 double AddComponent_Impl::AddWithoutSleep( double x , double y , double & z ) {
84   beginService( " AddComponent_Impl::AddWithoutSleep" );
85   z = x + y ;
86   LastAddition = z ;
87   endService( " AddComponent_Impl::AddWithoutSleep" );
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 void AddComponent_Impl::Setx( double x ) {
106   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
107   while ( S ) {
108     S = sleep(S);
109   }
110   xx = x ;
111 }
112
113 void AddComponent_Impl::Sety( double y ) {
114   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
115   while ( S ) {
116     S = sleep(S);
117   }
118   yy = y ;
119 }
120
121 double AddComponent_Impl::Addxy() {
122   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
123   while ( S ) {
124     S = sleep(S);
125   }
126   double zz = xx + yy ;
127   LastAddition = zz ;
128   return zz;
129 }
130
131 double AddComponent_Impl::AddyTox( double y ) {
132   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
133   while ( S ) {
134     S = sleep(S);
135   }
136   double zz = xx + y ;
137   LastAddition = zz ;
138   return zz;
139 }
140
141 double AddComponent_Impl::LastResult() {
142   beginService( " AddComponent_Impl::LastResult" );
143   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
144   endService( " AddComponent_Impl::LastResult"  );
145   return LastAddition ;
146 }
147
148 SuperVisionTest::Adder_ptr AddComponent_Impl::Addition() {
149   beginService( "AddComponent_Impl::Addition" );
150   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
151   Adder_Impl * myAdder ;
152   myAdder = new Adder_Impl( _orb , _poa, _contId,
153                             instanceName() , interfaceName() ,
154                             graphName() , nodeName() ) ;
155   SuperVisionTest::Adder_var iobject ;
156   PortableServer::ObjectId * id = myAdder->getId() ;
157   CORBA::Object_var obj = _poa->id_to_reference(*id);
158   iobject = SuperVisionTest::Adder::_narrow(obj) ;
159   endService( "AddComponent_Impl::Addition" );
160   return iobject._retn() ;
161 //  return SuperVisionTest::Adder::_duplicate(iobject) ;
162 }
163
164 bool AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
165   beginService( "AddComponent_Impl::Addition" );
166   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
167   Adder_Impl * myAdder ;
168   myAdder = new Adder_Impl( _orb , _poa, _contId,
169                             instanceName() , interfaceName() ,
170                             graphName() , nodeName() ) ;
171   SuperVisionTest::Adder_var iobject ;
172   PortableServer::ObjectId * id = myAdder->getId() ;
173   CORBA::Object_var obj = _poa->id_to_reference(*id);
174   iobject = SuperVisionTest::Adder::_narrow(obj) ;
175   endService( "AddComponent_Impl::Addition" );
176   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
177   return true ;
178 }
179
180 void AddComponent_Impl::AdditionObjRef2( bool & FuncValue ,
181                                          SuperVisionTest::Adder_out aAdder ) {
182   beginService( "AddComponent_Impl::Addition" );
183   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
184   Adder_Impl * myAdder ;
185   myAdder = new Adder_Impl( _orb , _poa, _contId,
186                             instanceName() , interfaceName() ,
187                             graphName() , nodeName() ) ;
188   SuperVisionTest::Adder_var iobject ;
189   PortableServer::ObjectId * id = myAdder->getId() ;
190   CORBA::Object_var obj = _poa->id_to_reference(*id);
191   iobject = SuperVisionTest::Adder::_narrow(obj) ;
192   endService( "AddComponent_Impl::Addition" );
193   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
194   FuncValue = true ;
195 }
196
197 bool AddComponent_Impl::AdditionObjRefs( const SuperVisionTest::AddComponent_ptr AddComponent1 ,
198                                          const SuperVisionTest::AddComponent_ptr Adder2 ,
199                                          const SuperVisionTest::AddComponent_ptr Adder3 ,
200                                          SuperVisionTest::AddComponent_out RetAddComponent1 ,
201                                          SuperVisionTest::AddComponent_out RetAdder2 ,
202                                          SuperVisionTest::AddComponent_out RetAdder3 ) {
203   bool RetVal = true ;
204   beginService( "AddComponent_Impl::AdditionObjRefs" );
205   cout << "beginService AddComponent_Impl::AdditionObjRefs" << endl ;
206   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
207   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
208   CORBA::ORB_var orb = init(0 , 0 ) ;
209   char * IOR = orb->object_to_string( AddComponent1 );
210   cout << "AddComponent_Impl::AdditionObjRefs AddComponent1 " << AddComponent1 << " IOR "
211        << IOR << " nil " << CORBA::is_nil( AddComponent1 ) << endl ;
212   RetAddComponent1 = SuperVisionTest::AddComponent::_duplicate( AddComponent1 ) ;
213   IOR = orb->object_to_string( Adder2 );
214   cout << "AddComponent_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
215        << CORBA::is_nil( Adder2 ) << endl ;
216   RetAdder2 = SuperVisionTest::AddComponent::_duplicate( Adder2 ) ;
217   IOR = orb->object_to_string( Adder3 );
218   cout << "AddComponent_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
219        << CORBA::is_nil( Adder3 ) << endl ;
220   RetAdder3 = SuperVisionTest::AddComponent::_duplicate( Adder3 ) ;
221   if ( CORBA::is_nil( AddComponent1 ) || CORBA::is_nil( Adder2 ) ||
222        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAddComponent1 ) ||
223        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
224     RetVal = false ;
225   }
226   cout << "endService AddComponent_Impl::AdditionObjRefs" << endl ;
227   endService( "AddComponent_Impl::AdditionObjRefs" );
228   return RetVal ;
229 }
230
231
232 extern "C"
233 {
234   PortableServer::ObjectId * AddComponentEngine_factory
235      (CORBA::ORB_ptr orb,
236       PortableServer::POA_ptr poa, 
237       PortableServer::ObjectId * contId,
238       const char *instanceName,
239       const char *interfaceName)
240   {
241     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
242             << instanceName << "," << interfaceName << "," << getpid() << ")");
243     AddComponent_Impl * myAddComponent 
244       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
245     return myAddComponent->getId() ;
246   }
247 }
248