Salome HOME
Copyright update 2021
[samples/component.git] / src / AdditionComponent / AdditionComponent_Impl.cxx
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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
23 //  SuperVisionTest AdditionComponent : example of component that adds two numbers
24 //  File   : AdditionComponent_Impl.cxx
25 //  Author : Jean Rahuel, CEA
26 //  Module : SuperVisionTest
27 //
28 #include <stdio.h>
29 #ifndef WIN32
30 #include <unistd.h>
31 #else
32 #include <process.h>
33 #endif
34 #include <fstream>
35 #include <sstream>
36 #include <string>
37
38 #include "utilities.h"
39 #include "Utils_ORB_INIT.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "SALOME_NamingService.hxx"
42 #include "SALOME_LifeCycleCORBA.hxx"
43
44 #include "AdditionComponent_Impl.hxx"
45 #include "Addition_Adder_Impl.hxx"
46 #include "COMPONENT_version.h"
47
48 using namespace std;
49
50 AdditionInterface_Impl::AdditionInterface_Impl( CORBA::ORB_ptr orb,
51                                       PortableServer::POA_ptr poa,
52                                       PortableServer::ObjectId * contId, 
53                                       const char *instanceName,
54                                       const char *interfaceName) :
55   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
56   MESSAGE("AdditionInterface_Impl::AdditionInterface_Impl this " << hex << this << dec
57           << "activate object instanceName("
58           << instanceName << ") interfaceName(" << interfaceName << ")" )
59   _thisObj = this ;
60   _id = _poa->activate_object(_thisObj);
61   LastAddition = 0 ;
62 }
63
64 AdditionInterface_Impl::AdditionInterface_Impl() {
65   LastAddition = 0 ;
66 }
67
68 AdditionInterface_Impl::~AdditionInterface_Impl() {
69 }
70
71 char* AdditionInterface_Impl::getVersion()
72 {
73 #if COMPONENT_DEVELOPMENT
74   return CORBA::string_dup(COMPONENT_VERSION_STR"dev");
75 #else
76   return CORBA::string_dup(COMPONENT_VERSION_STR);
77 #endif
78 }
79
80 CORBA::Double AdditionInterface_Impl::Add( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
81   beginService( " AdditionInterface_Impl::Add" );
82   z = x + y ;
83   int S;
84   
85   sendMessage(NOTIF_STEP, "AdditionInterface_Impl::Add is Computing");
86 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
87   S = 5 ;
88 #ifndef WIN32
89   while ( S ) {
90     S = sleep( S ) ;
91   }
92 #else
93   Sleep(S*1000);
94 #endif  
95     MESSAGE( "AdditionInterface_Impl::Add( " <<  x << " , " << y << " , " << z << " ) returns " << (x - y) << " after " << S << " seconds" )
96   LastAddition = z ;
97   endService( " AdditionInterface_Impl::Add"  );
98   return (x - y) ;
99 }
100
101 CORBA::Double AdditionInterface_Impl::AddWithoutSleep( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
102   beginService( " AdditionInterface_Impl::AddWithoutSleep" );
103   z = x + y ;
104   LastAddition = z ;
105   endService( " AdditionInterface_Impl::AddWithoutSleep" );
106   return (x - y) ;
107 }
108
109 CORBA::Long AdditionInterface_Impl::Sigma( CORBA::Long n ) {
110   long sigma = 0 ;
111   int i , j ;
112   beginService( " AdditionInterface_Impl::Sigma" );
113   for ( j = 0 ; j < 1000000 ; j++ ) {
114     sigma = 0 ;
115     for ( i = 1 ; i <= n ; i++ ) {
116       sigma = sigma + i ;
117     }
118   }
119   endService( " AdditionInterface_Impl::Sigma"  );
120   return sigma ;
121 }
122
123 void AdditionInterface_Impl::Setx( CORBA::Double x ) {
124   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
125 #ifndef WIN32
126   while ( S ) {
127     S = sleep( S ) ;
128   }
129 #else
130   Sleep(S*1000);
131 #endif
132   xx = x ;
133 }
134
135 void AdditionInterface_Impl::Sety( CORBA::Double y ) {
136   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
137 #ifndef WIN32
138   while ( S ) {
139     S = sleep( S ) ;
140   }
141 #else
142   Sleep(S*1000);
143 #endif
144   yy = y ;
145 }
146
147 CORBA::Double AdditionInterface_Impl::Addxy() {
148   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
149 #ifndef WIN32
150   while ( S ) {
151     S = sleep( S ) ;
152   }
153 #else
154   Sleep(S*1000);
155 #endif
156   double zz = xx + yy ;
157   LastAddition = zz ;
158   return zz;
159 }
160
161 CORBA::Double AdditionInterface_Impl::AddyTox( CORBA::Double y ) {
162   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
163 #ifndef WIN32
164   while ( S ) {
165     S = sleep( S ) ;
166   }
167 #else
168   Sleep(S*1000);
169 #endif
170   double zz = xx + y ;
171   LastAddition = zz ;
172   return zz;
173 }
174
175 CORBA::Double AdditionInterface_Impl::LastResult() {
176   beginService( " AdditionInterface_Impl::LastResult" );
177   sendMessage(NOTIF_STEP, "AdditionInterface_Impl::LastResult is Computing");
178   endService( " AdditionInterface_Impl::LastResult"  );
179   return LastAddition ;
180 }
181
182 AdditionComponent::Adder_ptr AdditionInterface_Impl::Addition() {
183   beginService( "AdditionInterface_Impl::Addition" );
184   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
185   Adder_Impl * myAdder ;
186   myAdder = new Adder_Impl( _orb , _poa, _contId,
187                             instanceName() , interfaceName() ,
188                             graphName() , nodeName() ) ;
189   AdditionComponent::Adder_var iobject ;
190   PortableServer::ObjectId * id = myAdder->getId() ;
191   CORBA::Object_var obj = _poa->id_to_reference(*id);
192   iobject = AdditionComponent::Adder::_narrow(obj) ;
193   endService( "AdditionInterface_Impl::Addition" );
194   return iobject._retn() ;
195 //  return AdditionComponent::Adder::_duplicate(iobject) ;
196 }
197
198 bool AdditionInterface_Impl::AdditionObjRef1( AdditionComponent::Adder_out aAdder ) {
199   beginService( "AdditionInterface_Impl::Addition" );
200   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
201   Adder_Impl * myAdder ;
202   myAdder = new Adder_Impl( _orb , _poa, _contId,
203                             instanceName() , interfaceName() ,
204                             graphName() , nodeName() ) ;
205   AdditionComponent::Adder_var iobject ;
206   PortableServer::ObjectId * id = myAdder->getId() ;
207   CORBA::Object_var obj = _poa->id_to_reference(*id);
208   iobject = AdditionComponent::Adder::_narrow(obj) ;
209   endService( "AdditionInterface_Impl::Addition" );
210   aAdder = AdditionComponent::Adder::_duplicate(iobject) ;
211   return true ;
212 }
213
214 void AdditionInterface_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
215                                               AdditionComponent::Adder_out aAdder ) {
216   beginService( "AdditionInterface_Impl::Addition" );
217   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
218   Adder_Impl * myAdder ;
219   myAdder = new Adder_Impl( _orb , _poa, _contId,
220                             instanceName() , interfaceName() ,
221                             graphName() , nodeName() ) ;
222   AdditionComponent::Adder_var iobject ;
223   PortableServer::ObjectId * id = myAdder->getId() ;
224   CORBA::Object_var obj = _poa->id_to_reference(*id);
225   iobject = AdditionComponent::Adder::_narrow(obj) ;
226   endService( "AdditionInterface_Impl::Addition" );
227   aAdder = AdditionComponent::Adder::_duplicate(iobject) ;
228   FuncValue = true ;
229 }
230
231 CORBA::Boolean AdditionInterface_Impl::AdditionObjRefs( AdditionComponent::AdditionInterface_ptr AdditionInterface1 ,
232                                                         AdditionComponent::AdditionInterface_ptr Adder2 ,
233                                                         AdditionComponent::AdditionInterface_ptr Adder3 ,
234                                                         AdditionComponent::AdditionInterface_out RetAdditionInterface1 ,
235                                                         AdditionComponent::AdditionInterface_out RetAdder2 ,
236                                                         AdditionComponent::AdditionInterface_out RetAdder3 ) {
237   bool RetVal = true ;
238   beginService( "AdditionInterface_Impl::AdditionObjRefs" );
239   cout << "beginService AdditionInterface_Impl::AdditionObjRefs" << endl ;
240   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
241   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
242   CORBA::ORB_var orb = init(0 , 0 ) ;
243   char * IOR = orb->object_to_string( AdditionInterface1 );
244   cout << "AdditionInterface_Impl::AdditionObjRefs AdditionInterface1 " << AdditionInterface1 << " IOR "
245        << IOR << " nil " << CORBA::is_nil( AdditionInterface1 ) << endl ;
246   RetAdditionInterface1 = AdditionComponent::AdditionInterface::_duplicate( AdditionInterface1 ) ;
247   IOR = orb->object_to_string( Adder2 );
248   cout << "AdditionInterface_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
249        << CORBA::is_nil( Adder2 ) << endl ;
250   RetAdder2 = AdditionComponent::AdditionInterface::_duplicate( Adder2 ) ;
251   IOR = orb->object_to_string( Adder3 );
252   cout << "AdditionInterface_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
253        << CORBA::is_nil( Adder3 ) << endl ;
254   RetAdder3 = AdditionComponent::AdditionInterface::_duplicate( Adder3 ) ;
255   if ( CORBA::is_nil( AdditionInterface1 ) || CORBA::is_nil( Adder2 ) ||
256        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAdditionInterface1 ) ||
257        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
258     RetVal = false ;
259   }
260   cout << "endService AdditionInterface_Impl::AdditionObjRefs" << endl ;
261   endService( "AdditionInterface_Impl::AdditionObjRefs" );
262   return RetVal ;
263 }
264
265 //AdditionComponentEngine_factory
266 extern "C"
267 {
268   PortableServer::ObjectId * AdditionComponentEngine_factory
269      (CORBA::ORB_ptr orb,
270       PortableServer::POA_ptr poa, 
271       PortableServer::ObjectId * contId,
272       const char *instanceName,
273       const char *interfaceName)
274   {
275 #ifndef WIN32
276     MESSAGE("AdditionComponentEngine_factory AdditionInterfaceEngine ("
277             << instanceName << "," << interfaceName << "," << getpid() << ")");
278 #else
279     MESSAGE("AdditionComponentEngine_factory AdditionInterfaceEngine ("
280             << instanceName << "," << interfaceName << "," << _getpid() << ")");
281 #endif
282     AdditionInterface_Impl * myAdditionInterface 
283       = new AdditionInterface_Impl(orb, poa, contId, instanceName, interfaceName);
284     return myAdditionInterface->getId() ;
285   }
286 }
287