Salome HOME
Merge from BR_V5_DEV 17Feb09
[samples/component.git] / src / AdditionComponent / AdditionComponent_Impl.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SuperVisionTest AdditionComponent : example of component that adds two numbers
23 //  File   : AdditionComponent_Impl.cxx
24 //  Author : Jean Rahuel, CEA
25 //  Module : SuperVisionTest
26 //
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <fstream>
30 #include <sstream>
31 #include <string>
32
33 #include "utilities.h"
34 #include "Utils_ORB_INIT.hxx"
35 #include "Utils_SINGLETON.hxx"
36 #include "SALOME_NamingService.hxx"
37 #include "SALOME_LifeCycleCORBA.hxx"
38
39 #include "AdditionComponent_Impl.hxx"
40 #include "Adder_Impl.hxx"
41
42 using namespace std;
43
44 AdditionInterface_Impl::AdditionInterface_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("AdditionInterface_Impl::AdditionInterface_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 AdditionInterface_Impl::AdditionInterface_Impl() {
59   LastAddition = 0 ;
60 }
61
62 AdditionInterface_Impl::~AdditionInterface_Impl() {
63 }
64
65 CORBA::Double AdditionInterface_Impl::Add( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
66   beginService( " AdditionInterface_Impl::Add" );
67   z = x + y ;
68   int S;
69   
70   sendMessage(NOTIF_STEP, "AdditionInterface_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( "AdditionInterface_Impl::Add( " <<  x << " , " << y << " , " << z
77        << " ) returns " << (x - y) << " after " << S << " seconds" )
78   LastAddition = z ;
79   endService( " AdditionInterface_Impl::Add"  );
80   return (x - y) ;
81 }
82
83 CORBA::Double AdditionInterface_Impl::AddWithoutSleep( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
84   beginService( " AdditionInterface_Impl::AddWithoutSleep" );
85   z = x + y ;
86   LastAddition = z ;
87   endService( " AdditionInterface_Impl::AddWithoutSleep" );
88   return (x - y) ;
89 }
90
91 CORBA::Long AdditionInterface_Impl::Sigma( CORBA::Long n ) {
92   long sigma = 0 ;
93   int i , j ;
94   beginService( " AdditionInterface_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( " AdditionInterface_Impl::Sigma"  );
102   return sigma ;
103 }
104
105 void AdditionInterface_Impl::Setx( CORBA::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 AdditionInterface_Impl::Sety( CORBA::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 CORBA::Double AdditionInterface_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 CORBA::Double AdditionInterface_Impl::AddyTox( CORBA::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 CORBA::Double AdditionInterface_Impl::LastResult() {
142   beginService( " AdditionInterface_Impl::LastResult" );
143   sendMessage(NOTIF_STEP, "AdditionInterface_Impl::LastResult is Computing");
144   endService( " AdditionInterface_Impl::LastResult"  );
145   return LastAddition ;
146 }
147
148 AdditionComponent::Adder_ptr AdditionInterface_Impl::Addition() {
149   beginService( "AdditionInterface_Impl::Addition" );
150   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
151   Adder_Impl * myAdder ;
152   myAdder = new Adder_Impl( _orb , _poa, _contId,
153                             instanceName() , interfaceName() ,
154                             graphName() , nodeName() ) ;
155   AdditionComponent::Adder_var iobject ;
156   PortableServer::ObjectId * id = myAdder->getId() ;
157   CORBA::Object_var obj = _poa->id_to_reference(*id);
158   iobject = AdditionComponent::Adder::_narrow(obj) ;
159   endService( "AdditionInterface_Impl::Addition" );
160   return iobject._retn() ;
161 //  return AdditionComponent::Adder::_duplicate(iobject) ;
162 }
163
164 bool AdditionInterface_Impl::AdditionObjRef1( AdditionComponent::Adder_out aAdder ) {
165   beginService( "AdditionInterface_Impl::Addition" );
166   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
167   Adder_Impl * myAdder ;
168   myAdder = new Adder_Impl( _orb , _poa, _contId,
169                             instanceName() , interfaceName() ,
170                             graphName() , nodeName() ) ;
171   AdditionComponent::Adder_var iobject ;
172   PortableServer::ObjectId * id = myAdder->getId() ;
173   CORBA::Object_var obj = _poa->id_to_reference(*id);
174   iobject = AdditionComponent::Adder::_narrow(obj) ;
175   endService( "AdditionInterface_Impl::Addition" );
176   aAdder = AdditionComponent::Adder::_duplicate(iobject) ;
177   return true ;
178 }
179
180 void AdditionInterface_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
181                                               AdditionComponent::Adder_out aAdder ) {
182   beginService( "AdditionInterface_Impl::Addition" );
183   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
184   Adder_Impl * myAdder ;
185   myAdder = new Adder_Impl( _orb , _poa, _contId,
186                             instanceName() , interfaceName() ,
187                             graphName() , nodeName() ) ;
188   AdditionComponent::Adder_var iobject ;
189   PortableServer::ObjectId * id = myAdder->getId() ;
190   CORBA::Object_var obj = _poa->id_to_reference(*id);
191   iobject = AdditionComponent::Adder::_narrow(obj) ;
192   endService( "AdditionInterface_Impl::Addition" );
193   aAdder = AdditionComponent::Adder::_duplicate(iobject) ;
194   FuncValue = true ;
195 }
196
197 CORBA::Boolean AdditionInterface_Impl::AdditionObjRefs( AdditionComponent::AdditionInterface_ptr AdditionInterface1 ,
198                                                         AdditionComponent::AdditionInterface_ptr Adder2 ,
199                                                         AdditionComponent::AdditionInterface_ptr Adder3 ,
200                                                         AdditionComponent::AdditionInterface_out RetAdditionInterface1 ,
201                                                         AdditionComponent::AdditionInterface_out RetAdder2 ,
202                                                         AdditionComponent::AdditionInterface_out RetAdder3 ) {
203   bool RetVal = true ;
204   beginService( "AdditionInterface_Impl::AdditionObjRefs" );
205   cout << "beginService AdditionInterface_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( AdditionInterface1 );
210   cout << "AdditionInterface_Impl::AdditionObjRefs AdditionInterface1 " << AdditionInterface1 << " IOR "
211        << IOR << " nil " << CORBA::is_nil( AdditionInterface1 ) << endl ;
212   RetAdditionInterface1 = AdditionComponent::AdditionInterface::_duplicate( AdditionInterface1 ) ;
213   IOR = orb->object_to_string( Adder2 );
214   cout << "AdditionInterface_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
215        << CORBA::is_nil( Adder2 ) << endl ;
216   RetAdder2 = AdditionComponent::AdditionInterface::_duplicate( Adder2 ) ;
217   IOR = orb->object_to_string( Adder3 );
218   cout << "AdditionInterface_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
219        << CORBA::is_nil( Adder3 ) << endl ;
220   RetAdder3 = AdditionComponent::AdditionInterface::_duplicate( Adder3 ) ;
221   if ( CORBA::is_nil( AdditionInterface1 ) || CORBA::is_nil( Adder2 ) ||
222        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAdditionInterface1 ) ||
223        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
224     RetVal = false ;
225   }
226   cout << "endService AdditionInterface_Impl::AdditionObjRefs" << endl ;
227   endService( "AdditionInterface_Impl::AdditionObjRefs" );
228   return RetVal ;
229 }
230
231 //AdditionComponentEngine_factory
232 extern "C"
233 {
234   PortableServer::ObjectId * AdditionComponentEngine_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("AdditionComponentEngine_factory AdditionInterfaceEngine ("
242             << instanceName << "," << interfaceName << "," << getpid() << ")");
243     AdditionInterface_Impl * myAdditionInterface 
244       = new AdditionInterface_Impl(orb, poa, contId, instanceName, interfaceName);
245     return myAdditionInterface->getId() ;
246   }
247 }
248