Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/component.git] / src / AddComponent / AddComponent_Impl.cxx
1 // Copyright (C) 2007-2012  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.
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 #include <unistd.h>
30 #include <fstream>
31 #include <sstream>
32 #include <string>
33
34 #include "utilities.h"
35 #include "Utils_ORB_INIT.hxx"
36 #include "Utils_SINGLETON.hxx"
37 #include "SALOME_NamingService.hxx"
38 #include "SALOME_LifeCycleCORBA.hxx"
39
40 #include "AddComponent_Impl.hxx"
41 #include "Adder_Impl.hxx"
42
43 using namespace std;
44
45 AddComponent_Impl::AddComponent_Impl( CORBA::ORB_ptr orb,
46                                       PortableServer::POA_ptr poa,
47                                       PortableServer::ObjectId * contId, 
48                                       const char *instanceName,
49                                       const char *interfaceName) :
50   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
51   MESSAGE("AddComponent_Impl::AddComponent_Impl this " << hex << this << dec
52           << "activate object instanceName("
53           << instanceName << ") interfaceName(" << interfaceName << ")" )
54   _thisObj = this ;
55   _id = _poa->activate_object(_thisObj);
56   LastAddition = 0 ;
57 }
58
59 AddComponent_Impl::AddComponent_Impl() {
60   LastAddition = 0 ;
61 }
62
63 AddComponent_Impl::~AddComponent_Impl() {
64 }
65
66 CORBA::Double AddComponent_Impl::Add( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
67   beginService( " AddComponent_Impl::Add" );
68   z = x + y ;
69   int S;
70   
71   sendMessage(NOTIF_STEP, "AddComponent_Impl::Add is Computing");
72 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
73   S = 5 ;
74   while ( S ) {
75     S = sleep(S);
76   }
77   MESSAGE( "AddComponent_Impl::Add( " <<  x << " , " << y << " , " << z
78        << " ) returns " << (x - y) << " after " << S << " seconds" )
79   LastAddition = z ;
80   endService( " AddComponent_Impl::Add"  );
81   return (x - y) ;
82 }
83
84 CORBA::Double AddComponent_Impl::AddWithoutSleep( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
85   beginService( " AddComponent_Impl::AddWithoutSleep" );
86   z = x + y ;
87   LastAddition = z ;
88   endService( " AddComponent_Impl::AddWithoutSleep" );
89   return (x - y) ;
90 }
91
92 CORBA::Long AddComponent_Impl::Sigma( CORBA::Long n ) {
93   long sigma = 0 ;
94   int i , j ;
95   beginService( " AddComponent_Impl::Sigma" );
96   for ( j = 0 ; j < 1000000 ; j++ ) {
97     sigma = 0 ;
98     for ( i = 1 ; i <= n ; i++ ) {
99       sigma = sigma + i ;
100     }
101   }
102   endService( " AddComponent_Impl::Sigma"  );
103   return sigma ;
104 }
105
106 void AddComponent_Impl::Setx( CORBA::Double x ) {
107   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
108   while ( S ) {
109     S = sleep(S);
110   }
111   xx = x ;
112 }
113
114 void AddComponent_Impl::Sety( CORBA::Double y ) {
115   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
116   while ( S ) {
117     S = sleep(S);
118   }
119   yy = y ;
120 }
121
122 CORBA::Double AddComponent_Impl::Addxy() {
123   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
124   while ( S ) {
125     S = sleep(S);
126   }
127   double zz = xx + yy ;
128   LastAddition = zz ;
129   return zz;
130 }
131
132 CORBA::Double AddComponent_Impl::AddyTox( CORBA::Double y ) {
133   int S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
134   while ( S ) {
135     S = sleep(S);
136   }
137   double zz = xx + y ;
138   LastAddition = zz ;
139   return zz;
140 }
141
142 CORBA::Double AddComponent_Impl::LastResult() {
143   beginService( " AddComponent_Impl::LastResult" );
144   sendMessage(NOTIF_STEP, "AddComponent_Impl::LastResult is Computing");
145   endService( " AddComponent_Impl::LastResult"  );
146   return LastAddition ;
147 }
148
149 SuperVisionTest::Adder_ptr AddComponent_Impl::Addition() {
150   beginService( "AddComponent_Impl::Addition" );
151   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
152   Adder_Impl * myAdder ;
153   myAdder = new Adder_Impl( _orb , _poa, _contId,
154                             instanceName() , interfaceName() ,
155                             graphName() , nodeName() ) ;
156   SuperVisionTest::Adder_var iobject ;
157   PortableServer::ObjectId * id = myAdder->getId() ;
158   CORBA::Object_var obj = _poa->id_to_reference(*id);
159   iobject = SuperVisionTest::Adder::_narrow(obj) ;
160   endService( "AddComponent_Impl::Addition" );
161   return iobject._retn() ;
162 //  return SuperVisionTest::Adder::_duplicate(iobject) ;
163 }
164
165 CORBA::Boolean AddComponent_Impl::AdditionObjRef1( SuperVisionTest::Adder_out aAdder ) {
166   beginService( "AddComponent_Impl::Addition" );
167   sendMessage(NOTIF_STEP, "AddComponent_Impl creates Adder_Impl");
168   Adder_Impl * myAdder ;
169   myAdder = new Adder_Impl( _orb , _poa, _contId,
170                             instanceName() , interfaceName() ,
171                             graphName() , nodeName() ) ;
172   SuperVisionTest::Adder_var iobject ;
173   PortableServer::ObjectId * id = myAdder->getId() ;
174   CORBA::Object_var obj = _poa->id_to_reference(*id);
175   iobject = SuperVisionTest::Adder::_narrow(obj) ;
176   endService( "AddComponent_Impl::Addition" );
177   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
178   return true ;
179 }
180
181 void AddComponent_Impl::AdditionObjRef2( CORBA::Boolean & FuncValue ,
182                                          SuperVisionTest::Adder_out aAdder ) {
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   aAdder = SuperVisionTest::Adder::_duplicate(iobject) ;
195   FuncValue = true ;
196 }
197
198 CORBA::Boolean AddComponent_Impl::AdditionObjRefs( SuperVisionTest::AddComponent_ptr AddComponent1 ,
199                                                    SuperVisionTest::AddComponent_ptr Adder2 ,
200                                                    SuperVisionTest::AddComponent_ptr Adder3 ,
201                                                    SuperVisionTest::AddComponent_out RetAddComponent1 ,
202                                                    SuperVisionTest::AddComponent_out RetAdder2 ,
203                                                    SuperVisionTest::AddComponent_out RetAdder3 ) {
204   bool RetVal = true ;
205   beginService( "AddComponent_Impl::AdditionObjRefs" );
206   cout << "beginService AddComponent_Impl::AdditionObjRefs" << endl ;
207   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
208   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
209   CORBA::ORB_var orb = init(0 , 0 ) ;
210   char * IOR = orb->object_to_string( AddComponent1 );
211   cout << "AddComponent_Impl::AdditionObjRefs AddComponent1 " << AddComponent1 << " IOR "
212        << IOR << " nil " << CORBA::is_nil( AddComponent1 ) << endl ;
213   RetAddComponent1 = SuperVisionTest::AddComponent::_duplicate( AddComponent1 ) ;
214   IOR = orb->object_to_string( Adder2 );
215   cout << "AddComponent_Impl::AdditionObjRefs Adder2 " << Adder2 << " IOR " << IOR << " nil "
216        << CORBA::is_nil( Adder2 ) << endl ;
217   RetAdder2 = SuperVisionTest::AddComponent::_duplicate( Adder2 ) ;
218   IOR = orb->object_to_string( Adder3 );
219   cout << "AddComponent_Impl::AdditionObjRefs Adder3 " << Adder3 << " IOR " << IOR << " nil "
220        << CORBA::is_nil( Adder3 ) << endl ;
221   RetAdder3 = SuperVisionTest::AddComponent::_duplicate( Adder3 ) ;
222   if ( CORBA::is_nil( AddComponent1 ) || CORBA::is_nil( Adder2 ) ||
223        CORBA::is_nil( Adder3 ) || CORBA::is_nil( RetAddComponent1 ) ||
224        CORBA::is_nil( RetAdder2 ) || CORBA::is_nil( RetAdder3 ) ) {
225     RetVal = false ;
226   }
227   cout << "endService AddComponent_Impl::AdditionObjRefs" << endl ;
228   endService( "AddComponent_Impl::AdditionObjRefs" );
229   return RetVal ;
230 }
231
232
233 extern "C"
234 {
235   PortableServer::ObjectId * AddComponentEngine_factory
236      (CORBA::ORB_ptr orb,
237       PortableServer::POA_ptr poa, 
238       PortableServer::ObjectId * contId,
239       const char *instanceName,
240       const char *interfaceName)
241   {
242     MESSAGE("AddComponentEngine_factory AddComponentEngine ("
243             << instanceName << "," << interfaceName << "," << getpid() << ")");
244     AddComponent_Impl * myAddComponent 
245       = new AddComponent_Impl(orb, poa, contId, instanceName, interfaceName);
246     return myAddComponent->getId() ;
247   }
248 }
249