Salome HOME
bos #26457 Factorization of ORB initialization
[samples/component.git] / src / AddComponent / AddComponent_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 AddComponent : example of component that adds two numbers
24 //  File   : AddComponent_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 "AddComponent_Impl.hxx"
44 #include "Adder_Impl.hxx"
45 #include "COMPONENT_version.h"
46
47 using namespace std;
48
49 AddComponent_Impl::AddComponent_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("AddComponent_Impl::AddComponent_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 AddComponent_Impl::AddComponent_Impl() {
64   LastAddition = 0 ;
65 }
66
67 AddComponent_Impl::~AddComponent_Impl() {
68 }
69
70 char* AddComponent_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 AddComponent_Impl::Add( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
80   beginService( " AddComponent_Impl::Add" );
81   z = x + y ;
82   int S;
83   
84   sendMessage(NOTIF_STEP, "AddComponent_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( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
95        << " ) returns " << (x - y) << " after " << S << " seconds" )
96   LastAddition = z ;
97   endService( " AddComponent_Impl::Add"  );
98   return (x - y) ;
99 }
100
101 CORBA::Double AddComponent_Impl::AddWithoutSleep( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
102   beginService( " AddComponent_Impl::AddWithoutSleep" );
103   z = x + y ;
104   LastAddition = z ;
105   endService( " AddComponent_Impl::AddWithoutSleep" );
106   return (x - y) ;
107 }
108
109 CORBA::Long AddComponent_Impl::Sigma( CORBA::Long n ) {
110   long sigma = 0 ;
111   int i , j ;
112   beginService( " AddComponent_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( " AddComponent_Impl::Sigma"  );
120   return sigma ;
121 }
122
123 void AddComponent_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 AddComponent_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 AddComponent_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 AddComponent_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 AddComponent_Impl::LastResult() {
176   beginService( " AddComponent_Impl::LastResult" );
177   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
178   endService( " AddComponent_Impl::LastResult"  );
179   return LastAddition ;
180 }
181
182 SuperVisionTest::Adder_ptr AddComponent_Impl::Addition() {
183   beginService( "AddComponent_Impl::Addition" );
184   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
185   Adder_Impl * myAdder ;
186   myAdder = new Adder_Impl( _orb , _poa, _contId,
187                             instanceName() , interfaceName() ,
188                             graphName() , nodeName() ) ;
189   SuperVisionTest::Adder_var iobject ;
190   PortableServer::ObjectId * id = myAdder->getId() ;
191   CORBA::Object_var obj = _poa->id_to_reference(*id);
192   iobject = SuperVisionTest::Adder::_narrow(obj) ;
193   endService( "AddComponent_Impl::Addition" );
194   return iobject._retn() ;
195 //  return SuperVisionTest::Adder::_duplicate(iobject) ;
196 }
197
198 CORBA::Boolean AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
199   beginService( "AddComponent_Impl::Addition" );
200   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
201   Adder_Impl * myAdder ;
202   myAdder = new Adder_Impl( _orb , _poa, _contId,
203                             instanceName() , interfaceName() ,
204                             graphName() , nodeName() ) ;
205   SuperVisionTest::Adder_var iobject ;
206   PortableServer::ObjectId * id = myAdder->getId() ;
207   CORBA::Object_var obj = _poa->id_to_reference(*id);
208   iobject = SuperVisionTest::Adder::_narrow(obj) ;
209   endService( "AddComponent_Impl::Addition" );
210   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
211   return true ;
212 }
213
214 void AddComponent_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
215                                          SuperVisionTest::Adder_out aAdder ) {
216   beginService( "AddComponent_Impl::Addition" );
217   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
218   Adder_Impl * myAdder ;
219   myAdder = new Adder_Impl( _orb , _poa, _contId,
220                             instanceName() , interfaceName() ,
221                             graphName() , nodeName() ) ;
222   SuperVisionTest::Adder_var iobject ;
223   PortableServer::ObjectId * id = myAdder->getId() ;
224   CORBA::Object_var obj = _poa->id_to_reference(*id);
225   iobject = SuperVisionTest::Adder::_narrow(obj) ;
226   endService( "AddComponent_Impl::Addition" );
227   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
228   FuncValue = true ;
229 }
230
231 CORBA::Boolean AddComponent_Impl::AdditionObjRefs( SuperVisionTest::AddComponent_ptr AddComponent1 ,
232                                                    SuperVisionTest::AddComponent_ptr Adder2 ,
233                                                    SuperVisionTest::AddComponent_ptr Adder3 ,
234                                                    SuperVisionTest::AddComponent_out RetAddComponent1 ,
235                                                    SuperVisionTest::AddComponent_out RetAdder2 ,
236                                                    SuperVisionTest::AddComponent_out RetAdder3 ) {
237   bool RetVal = true ;
238   beginService( "AddComponent_Impl::AdditionObjRefs" );
239   cout << "beginService AddComponent_Impl::AdditionObjRefs" << endl ;
240   CORBA::ORB_var orb = KERNEL::GetRefToORB();
241   char * IOR = orb->object_to_string( AddComponent1 );
242   cout << "AddComponent_Impl::AdditionObjRefs AddComponent1 " << AddComponent1 << " IOR "
243        << IOR << " nil " << CORBA::is_nil( AddComponent1 ) << endl ;
244   RetAddComponent1 = SuperVisionTest::AddComponent::_duplicate( AddComponent1 ) ;
245   IOR = orb->object_to_string( Adder2 );
246   cout << "AddComponent_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
247        << CORBA::is_nil( Adder2 ) << endl ;
248   RetAdder2 = SuperVisionTest::AddComponent::_duplicate( Adder2 ) ;
249   IOR = orb->object_to_string( Adder3 );
250   cout << "AddComponent_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
251        << CORBA::is_nil( Adder3 ) << endl ;
252   RetAdder3 = SuperVisionTest::AddComponent::_duplicate( Adder3 ) ;
253   if ( CORBA::is_nil( AddComponent1 ) || CORBA::is_nil( Adder2 ) ||
254        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAddComponent1 ) ||
255        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
256     RetVal = false ;
257   }
258   cout << "endService AddComponent_Impl::AdditionObjRefs" << endl ;
259   endService( "AddComponent_Impl::AdditionObjRefs" );
260   return RetVal ;
261 }
262
263
264 extern "C"
265 {
266   PortableServer::ObjectId * AddComponentEngine_factory
267      (CORBA::ORB_ptr orb,
268       PortableServer::POA_ptr poa, 
269       PortableServer::ObjectId * contId,
270       const char *instanceName,
271       const char *interfaceName)
272   {
273 #ifndef WIN32
274     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
275             << instanceName << "," << interfaceName << "," << getpid() << ")");
276 #else 
277     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
278             << instanceName << "," << interfaceName << "," << _getpid() << ")");
279 #endif
280     AddComponent_Impl * myAddComponent 
281       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
282     return myAddComponent->getId() ;
283   }
284 }
285