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