Salome HOME
updated copyright message
[samples/component.git] / src / AdditionComponent / AdditionComponent_Impl.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 #include "SALOME_Container_i.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, bool withRegistry) :
55   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,withRegistry) {
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 = this->BuildNewAdderImplObj() ;
187   AdditionComponent::Adder_var iobject ;
188   PortableServer::ObjectId * id = myAdder->getId() ;
189   CORBA::Object_var obj = _poa->id_to_reference(*id);
190   iobject = AdditionComponent::Adder::_narrow(obj) ;
191   endService( "AdditionInterface_Impl::Addition" );
192   return iobject._retn() ;
193 //  return AdditionComponent::Adder::_duplicate(iobject) ;
194 }
195
196 Adder_Impl *AdditionInterface_Impl::BuildNewAdderImplObj()
197 {
198   Engines::Container_var cont = this->GetContainerRef();
199   if( cont->is_SSL_mode() )
200   {
201     return new Adder_Impl_SSL( _orb , _poa, _contId, instanceName() , interfaceName() , graphName() , nodeName() ) ;
202   }
203   else
204   {
205     return new Adder_Impl_No_SSL( _orb , _poa, _contId, instanceName() , interfaceName() , graphName() , nodeName() ) ;
206   }
207 }
208
209 bool AdditionInterface_Impl::AdditionObjRef1( AdditionComponent::Adder_out aAdder ) {
210   beginService( "AdditionInterface_Impl::Addition" );
211   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
212   Adder_Impl * myAdder = nullptr;
213   myAdder = this->BuildNewAdderImplObj() ;
214   AdditionComponent::Adder_var iobject ;
215   PortableServer::ObjectId * id = myAdder->getId() ;
216   CORBA::Object_var obj = _poa->id_to_reference(*id);
217   iobject = AdditionComponent::Adder::_narrow(obj) ;
218   endService( "AdditionInterface_Impl::Addition" );
219   aAdder = AdditionComponent::Adder::_duplicate(iobject) ;
220   return true ;
221 }
222
223 void AdditionInterface_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
224                                               AdditionComponent::Adder_out aAdder ) {
225   beginService( "AdditionInterface_Impl::Addition" );
226   sendMessage(NOTIF_STEP, "AdditionInterface_Impl creates Adder_Impl");
227   Adder_Impl * myAdder ;
228   myAdder = this->BuildNewAdderImplObj() ;
229   AdditionComponent::Adder_var iobject ;
230   PortableServer::ObjectId * id = myAdder->getId() ;
231   CORBA::Object_var obj = _poa->id_to_reference(*id);
232   iobject = AdditionComponent::Adder::_narrow(obj) ;
233   endService( "AdditionInterface_Impl::Addition" );
234   aAdder = AdditionComponent::Adder::_duplicate(iobject) ;
235   FuncValue = true ;
236 }
237
238 CORBA::Boolean AdditionInterface_Impl::AdditionObjRefs( AdditionComponent::AdditionInterface_ptr AdditionInterface1 ,
239                                                         AdditionComponent::AdditionInterface_ptr Adder2 ,
240                                                         AdditionComponent::AdditionInterface_ptr Adder3 ,
241                                                         AdditionComponent::AdditionInterface_out RetAdditionInterface1 ,
242                                                         AdditionComponent::AdditionInterface_out RetAdder2 ,
243                                                         AdditionComponent::AdditionInterface_out RetAdder3 ) {
244   bool RetVal = true ;
245   beginService( "AdditionInterface_Impl::AdditionObjRefs" );
246   cout << "beginService AdditionInterface_Impl::AdditionObjRefs" << endl ;
247   CORBA::ORB_var orb = KERNEL::GetRefToORB();
248   char * IOR = orb->object_to_string( AdditionInterface1 );
249   cout << "AdditionInterface_Impl::AdditionObjRefs AdditionInterface1 " << AdditionInterface1 << " IOR "
250        << IOR << " nil " << CORBA::is_nil( AdditionInterface1 ) << endl ;
251   RetAdditionInterface1 = AdditionComponent::AdditionInterface::_duplicate( AdditionInterface1 ) ;
252   IOR = orb->object_to_string( Adder2 );
253   cout << "AdditionInterface_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
254        << CORBA::is_nil( Adder2 ) << endl ;
255   RetAdder2 = AdditionComponent::AdditionInterface::_duplicate( Adder2 ) ;
256   IOR = orb->object_to_string( Adder3 );
257   cout << "AdditionInterface_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
258        << CORBA::is_nil( Adder3 ) << endl ;
259   RetAdder3 = AdditionComponent::AdditionInterface::_duplicate( Adder3 ) ;
260   if ( CORBA::is_nil( AdditionInterface1 ) || CORBA::is_nil( Adder2 ) ||
261        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAdditionInterface1 ) ||
262        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
263     RetVal = false ;
264   }
265   cout << "endService AdditionInterface_Impl::AdditionObjRefs" << endl ;
266   endService( "AdditionInterface_Impl::AdditionObjRefs" );
267   return RetVal ;
268 }
269
270 //AdditionComponentEngine_factory
271 extern "C"
272 {
273   PortableServer::ObjectId * AdditionComponentEngine_factory
274      (CORBA::ORB_ptr orb,
275       PortableServer::POA_ptr poa, 
276       PortableServer::ObjectId * contId,
277       const char *instanceName,
278       const char *interfaceName)
279   {
280 #ifndef WIN32
281     MESSAGE("AdditionComponentEngine_factory AdditionInterfaceEngine ("
282             << instanceName << "," << interfaceName << "," << getpid() << ")");
283 #else
284     MESSAGE("AdditionComponentEngine_factory AdditionInterfaceEngine ("
285             << instanceName << "," << interfaceName << "," << _getpid() << ")");
286 #endif
287     AdditionInterface_Impl * myAdditionInterface = nullptr;
288     CORBA::Object_var o = poa->id_to_reference(*contId);
289     Engines::Container_var cont = Engines::Container::_narrow(o);
290     if(cont->is_SSL_mode())
291     {
292       myAdditionInterface = new AdditionInterface_Impl_SSL(orb, poa, contId, instanceName, interfaceName);
293     }
294     else
295     {
296       myAdditionInterface = new AdditionInterface_Impl_No_SSL(orb, poa, contId, instanceName, interfaceName);
297     }
298     return myAdditionInterface->getId() ;
299   }
300 }
301