Salome HOME
Revert "Synchronize adm files"
[samples/component.git] / src / AddComponent / AddComponent_Impl.cxx
1 // Copyright (C) 2007-2014  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 "Utils_ORB_INIT.hxx"
40 #include "Utils_SINGLETON.hxx"
41 #include "SALOME_NamingService.hxx"
42 #include "SALOME_LifeCycleCORBA.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) :
55   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
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 = new Adder_Impl( _orb , _poa, _contId,
188                             instanceName() , interfaceName() ,
189                             graphName() , nodeName() ) ;
190   SuperVisionTest::Adder_var iobject ;
191   PortableServer::ObjectId * id = myAdder->getId() ;
192   CORBA::Object_var obj = _poa->id_to_reference(*id);
193   iobject = SuperVisionTest::Adder::_narrow(obj) ;
194   endService( "AddComponent_Impl::Addition" );
195   return iobject._retn() ;
196 //  return SuperVisionTest::Adder::_duplicate(iobject) ;
197 }
198
199 CORBA::Boolean AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
200   beginService( "AddComponent_Impl::Addition" );
201   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
202   Adder_Impl * myAdder ;
203   myAdder = new Adder_Impl( _orb , _poa, _contId,
204                             instanceName() , interfaceName() ,
205                             graphName() , nodeName() ) ;
206   SuperVisionTest::Adder_var iobject ;
207   PortableServer::ObjectId * id = myAdder->getId() ;
208   CORBA::Object_var obj = _poa->id_to_reference(*id);
209   iobject = SuperVisionTest::Adder::_narrow(obj) ;
210   endService( "AddComponent_Impl::Addition" );
211   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
212   return true ;
213 }
214
215 void AddComponent_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
216                                          SuperVisionTest::Adder_out aAdder ) {
217   beginService( "AddComponent_Impl::Addition" );
218   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
219   Adder_Impl * myAdder ;
220   myAdder = new Adder_Impl( _orb , _poa, _contId,
221                             instanceName() , interfaceName() ,
222                             graphName() , nodeName() ) ;
223   SuperVisionTest::Adder_var iobject ;
224   PortableServer::ObjectId * id = myAdder->getId() ;
225   CORBA::Object_var obj = _poa->id_to_reference(*id);
226   iobject = SuperVisionTest::Adder::_narrow(obj) ;
227   endService( "AddComponent_Impl::Addition" );
228   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
229   FuncValue = true ;
230 }
231
232 CORBA::Boolean AddComponent_Impl::AdditionObjRefs( SuperVisionTest::AddComponent_ptr AddComponent1 ,
233                                                    SuperVisionTest::AddComponent_ptr Adder2 ,
234                                                    SuperVisionTest::AddComponent_ptr Adder3 ,
235                                                    SuperVisionTest::AddComponent_out RetAddComponent1 ,
236                                                    SuperVisionTest::AddComponent_out RetAdder2 ,
237                                                    SuperVisionTest::AddComponent_out RetAdder3 ) {
238   bool RetVal = true ;
239   beginService( "AddComponent_Impl::AdditionObjRefs" );
240   cout << "beginService AddComponent_Impl::AdditionObjRefs" << endl ;
241   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
242   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
243   CORBA::ORB_var orb = init(0 , 0 ) ;
244   char * IOR = orb->object_to_string( AddComponent1 );
245   cout << "AddComponent_Impl::AdditionObjRefs AddComponent1 " << AddComponent1 << " IOR "
246        << IOR << " nil " << CORBA::is_nil( AddComponent1 ) << endl ;
247   RetAddComponent1 = SuperVisionTest::AddComponent::_duplicate( AddComponent1 ) ;
248   IOR = orb->object_to_string( Adder2 );
249   cout << "AddComponent_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
250        << CORBA::is_nil( Adder2 ) << endl ;
251   RetAdder2 = SuperVisionTest::AddComponent::_duplicate( Adder2 ) ;
252   IOR = orb->object_to_string( Adder3 );
253   cout << "AddComponent_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
254        << CORBA::is_nil( Adder3 ) << endl ;
255   RetAdder3 = SuperVisionTest::AddComponent::_duplicate( Adder3 ) ;
256   if ( CORBA::is_nil( AddComponent1 ) || CORBA::is_nil( Adder2 ) ||
257        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAddComponent1 ) ||
258        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
259     RetVal = false ;
260   }
261   cout << "endService AddComponent_Impl::AdditionObjRefs" << endl ;
262   endService( "AddComponent_Impl::AdditionObjRefs" );
263   return RetVal ;
264 }
265
266
267 extern "C"
268 {
269   PortableServer::ObjectId * AddComponentEngine_factory
270      (CORBA::ORB_ptr orb,
271       PortableServer::POA_ptr poa, 
272       PortableServer::ObjectId * contId,
273       const char *instanceName,
274       const char *interfaceName)
275   {
276 #ifndef WIN32
277     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
278             << instanceName << "," << interfaceName << "," << getpid() << ")");
279 #else 
280     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
281             << instanceName << "," << interfaceName << "," << _getpid() << ")");
282 #endif
283     AddComponent_Impl * myAddComponent 
284       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
285     return myAddComponent->getId() ;
286   }
287 }
288