Salome HOME
[bos #32517][EDF] Dynamic log messages switched on and off by SALOME_VERBOSE environm...
[samples/component.git] / src / AddComponent / AddComponent_Impl.cxx
1 // Copyright (C) 2007-2022  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 #include "SALOME_Container_i.hxx"
43
44 #include "AddComponent_Impl.hxx"
45 #include "Adder_Impl.hxx"
46 #include "COMPONENT_version.h"
47
48 using namespace std;
49
50 AddComponent_Impl::AddComponent_Impl( CORBA::ORB_ptr orb,
51                                       PortableServer::POA_ptr poa,
52                                       PortableServer::ObjectId * contId, 
53                                       const char *instanceName,
54               const char *interfaceName, bool withRegistry) :
55   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,withRegistry) {
56   MESSAGE("AddComponent_Impl::AddComponent_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 AddComponent_Impl::AddComponent_Impl() {
65   LastAddition = 0 ;
66 }
67
68 AddComponent_Impl::~AddComponent_Impl() {
69 }
70
71 char* AddComponent_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 AddComponent_Impl::Add( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
81   beginService( " AddComponent_Impl::Add" );
82   z = x + y ;
83   int S;
84   
85   sendMessage(NOTIF_STEP, "AddComponent_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( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
96        << " ) returns " << (x - y) << " after " << S << " seconds" );
97   LastAddition = z ;
98   endService( " AddComponent_Impl::Add"  );
99   return (x - y) ;
100 }
101
102 CORBA::Double AddComponent_Impl::AddWithoutSleep( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
103   beginService( " AddComponent_Impl::AddWithoutSleep" );
104   z = x + y ;
105   LastAddition = z ;
106   endService( " AddComponent_Impl::AddWithoutSleep" );
107   return (x - y) ;
108 }
109
110 CORBA::Long AddComponent_Impl::Sigma( CORBA::Long n ) {
111   long sigma = 0 ;
112   int i , j ;
113   beginService( " AddComponent_Impl::Sigma" );
114   for ( j = 0 ; j < 1000000 ; j++ ) {
115     sigma = 0 ;
116     for ( i = 1 ; i <= n ; i++ ) {
117       sigma = sigma + i ;
118     }
119   }
120   endService( " AddComponent_Impl::Sigma"  );
121   return sigma ;
122 }
123
124 void AddComponent_Impl::Setx( CORBA::Double x ) {
125   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
126 #ifndef WIN32
127   while ( S ) {
128     S = sleep( S ) ;
129   }
130 #else
131   Sleep(S*1000);
132 #endif
133   xx = x ;
134 }
135
136 void AddComponent_Impl::Sety( CORBA::Double y ) {
137   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
138 #ifndef WIN32
139   while ( S ) {
140     S = sleep( S ) ;
141   }
142 #else
143   Sleep(S*1000);
144 #endif
145   yy = y ;
146 }
147
148 CORBA::Double AddComponent_Impl::Addxy() {
149   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
150 #ifndef WIN32
151   while ( S ) {
152     S = sleep( S ) ;
153   }
154 #else
155   Sleep(S*1000);
156 #endif
157   double zz = xx + yy ;
158   LastAddition = zz ;
159   return zz;
160 }
161
162 CORBA::Double AddComponent_Impl::AddyTox( CORBA::Double y ) {
163   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
164 #ifndef WIN32
165   while ( S ) {
166     S = sleep( S ) ;
167   }
168 #else
169   Sleep(S*1000);
170 #endif
171   double zz = xx + y ;
172   LastAddition = zz ;
173   return zz;
174 }
175
176 CORBA::Double AddComponent_Impl::LastResult() {
177   beginService( " AddComponent_Impl::LastResult" );
178   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
179   endService( " AddComponent_Impl::LastResult"  );
180   return LastAddition ;
181 }
182
183 SuperVisionTest::Adder_ptr AddComponent_Impl::Addition() {
184   beginService( "AddComponent_Impl::Addition" );
185   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
186   Adder_Impl * myAdder ;
187   myAdder = this->BuildNewAdderImplObj();
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   return iobject._retn() ;
194 //  return SuperVisionTest::Adder::_duplicate(iobject) ;
195 }
196
197 Adder_Impl *AddComponent_Impl::BuildNewAdderImplObj()
198 {
199   Engines::Container_var cont = this->GetContainerRef();
200   if( cont->is_SSL_mode() )
201   {
202     return new Adder_Impl_SSL( _orb , _poa, _contId, instanceName() , interfaceName() , graphName() , nodeName() ) ;
203   }
204   else
205   {
206     return new Adder_Impl_No_SSL( _orb , _poa, _contId, instanceName() , interfaceName() , graphName() , nodeName() ) ;
207   }
208 }
209
210 CORBA::Boolean AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
211   beginService( "AddComponent_Impl::Addition" );
212   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
213   Adder_Impl * myAdder = nullptr;
214   myAdder = this->BuildNewAdderImplObj();
215   SuperVisionTest::Adder_var iobject ;
216   PortableServer::ObjectId * id = myAdder->getId() ;
217   CORBA::Object_var obj = _poa->id_to_reference(*id);
218   iobject = SuperVisionTest::Adder::_narrow(obj) ;
219   endService( "AddComponent_Impl::Addition" );
220   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
221   return true ;
222 }
223
224 void AddComponent_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
225                                          SuperVisionTest::Adder_out aAdder ) {
226   beginService( "AddComponent_Impl::Addition" );
227   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
228   Adder_Impl * myAdder ;
229   myAdder = this->BuildNewAdderImplObj();
230   SuperVisionTest::Adder_var iobject ;
231   PortableServer::ObjectId * id = myAdder->getId() ;
232   CORBA::Object_var obj = _poa->id_to_reference(*id);
233   iobject = SuperVisionTest::Adder::_narrow(obj) ;
234   endService( "AddComponent_Impl::Addition" );
235   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
236   FuncValue = true ;
237 }
238
239 CORBA::Boolean AddComponent_Impl::AdditionObjRefs( SuperVisionTest::AddComponent_ptr AddComponent1 ,
240                                                    SuperVisionTest::AddComponent_ptr Adder2 ,
241                                                    SuperVisionTest::AddComponent_ptr Adder3 ,
242                                                    SuperVisionTest::AddComponent_out RetAddComponent1 ,
243                                                    SuperVisionTest::AddComponent_out RetAdder2 ,
244                                                    SuperVisionTest::AddComponent_out RetAdder3 ) {
245   bool RetVal = true ;
246   beginService( "AddComponent_Impl::AdditionObjRefs" );
247   cout << "beginService AddComponent_Impl::AdditionObjRefs" << endl ;
248   CORBA::ORB_var orb = KERNEL::GetRefToORB();
249   char * IOR = orb->object_to_string( AddComponent1 );
250   cout << "AddComponent_Impl::AdditionObjRefs AddComponent1 " << AddComponent1 << " IOR "
251        << IOR << " nil " << CORBA::is_nil( AddComponent1 ) << endl ;
252   RetAddComponent1 = SuperVisionTest::AddComponent::_duplicate( AddComponent1 ) ;
253   IOR = orb->object_to_string( Adder2 );
254   cout << "AddComponent_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
255        << CORBA::is_nil( Adder2 ) << endl ;
256   RetAdder2 = SuperVisionTest::AddComponent::_duplicate( Adder2 ) ;
257   IOR = orb->object_to_string( Adder3 );
258   cout << "AddComponent_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
259        << CORBA::is_nil( Adder3 ) << endl ;
260   RetAdder3 = SuperVisionTest::AddComponent::_duplicate( Adder3 ) ;
261   if ( CORBA::is_nil( AddComponent1 ) || CORBA::is_nil( Adder2 ) ||
262        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAddComponent1 ) ||
263        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
264     RetVal = false ;
265   }
266   cout << "endService AddComponent_Impl::AdditionObjRefs" << endl ;
267   endService( "AddComponent_Impl::AdditionObjRefs" );
268   return RetVal ;
269 }
270
271
272 extern "C"
273 {
274   PortableServer::ObjectId * AddComponentEngine_factory
275      (CORBA::ORB_ptr orb,
276       PortableServer::POA_ptr poa, 
277       PortableServer::ObjectId * contId,
278       const char *instanceName,
279       const char *interfaceName)
280   {
281 #ifndef WIN32
282     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
283             << instanceName << "," << interfaceName << "," << getpid() << ")");
284 #else 
285     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
286             << instanceName << "," << interfaceName << "," << _getpid() << ")");
287 #endif
288     CORBA::Object_var o = poa->id_to_reference(*contId);
289     Engines::Container_var cont = Engines::Container::_narrow(o);
290     AddComponent_Impl * myAddComponent = nullptr;
291     if(cont->is_SSL_mode())
292     {
293       myAddComponent = new AddComponent_Impl_SSL(orb, poa, contId, instanceName, interfaceName);
294     }
295     else
296     {
297       myAddComponent = new AddComponent_Impl_No_SSL(orb, poa, contId, instanceName, interfaceName);
298     }
299     return myAddComponent->getId() ;
300   }
301 }
302