Salome HOME
Merge from V6_main_20120808 08Aug12
[samples/component.git] / src / AddComponent / Adder_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 "Adder_Impl.hxx"
41
42 using namespace std;
43
44 Adder_Impl::Adder_Impl( CORBA::ORB_ptr orb ,
45                         PortableServer::POA_ptr poa ,
46                         PortableServer::ObjectId * contId , 
47                         const char * instanceName ,
48                         const char * interfaceName , 
49                         const char * graphName ,
50                         const char * nodeName ) :
51   Engines_Component_i(orb, poa, contId, instanceName, interfaceName,1,true) {
52   Names( graphName , nodeName ) ;
53   MESSAGE("Adder_Impl::Adder_Impl activate object instanceName("
54           << instanceName << ") interfaceName(" << interfaceName << ") --> "
55           << hex << (void *) this << dec )
56   beginService( "Adder_Impl::Adder_Impl" );
57   _thisObj = this ;
58   _id = _poa->activate_object(_thisObj);
59   LastAddition = 0 ;
60   sendMessage(NOTIF_STEP, "Adder_Impl is Created");
61   endService( "Adder_Impl::Adder_Impl" );
62 }
63
64 Adder_Impl::Adder_Impl() {
65   LastAddition = 0 ;
66 }
67
68 Adder_Impl::~Adder_Impl() {
69   beginService( "Adder_Impl::~Adder_Impl" );
70   endService( "Adder_Impl::~Adder_Impl" );
71 }
72
73 void Adder_Impl::destroy() {
74   _poa->deactivate_object(*_id) ;
75   CORBA::release(_poa) ;
76   delete(_id) ;
77   _thisObj->_remove_ref();
78 }
79
80 CORBA::Double Adder_Impl::Add( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
81   beginService( " Adder_Impl::Add" );
82   z = x + y ;
83   int S;
84   
85   sendMessage(NOTIF_STEP, "Adder_Impl::Add is Computing");
86 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
87   S = 5 ;
88   while ( S ) {
89     S = sleep(S);
90   }
91   MESSAGE( "Adder_Impl::Add( " <<  x << " , " << y << " , " << z
92        << " ) returns " << -(x - y) << " after " << S << " seconds" )
93   LastAddition = z ;
94   endService( " Adder_Impl::Add"  );
95   return -(x - y) ;
96 }
97
98 CORBA::Double Adder_Impl::AddWithoutSleep( CORBA::Double x , CORBA::Double y , CORBA::Double & z ) {
99   beginService( " Adder_Impl::AddWithoutSleep" );
100   z = x + y ;
101   endService( " Adder_Impl::AddWithoutSleep"  );
102   return -(x - y) ;
103 }
104
105 CORBA::Double Adder_Impl::AddAndCompare( CORBA::Double x , CORBA::Double y ,
106                                          SuperVisionTest::Adder_ptr anOtherAdder ,
107                                          CORBA::Double & z ) {
108   beginService( " Adder_Impl::AddAndCompare" );
109   z = x + y ;
110   int S;
111   
112   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Computing");
113 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
114   S = 5 ;
115   while ( S ) {
116     S = sleep(S);
117   }
118   MESSAGE( "Adder_Impl::AddAndCompare( " <<  x << " , " << y << " , " << z
119        << " ) returns " << -(x - y) << " after " << S << " seconds" )
120   LastAddition = z ;
121   double ValFunc ;
122   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare will call anOtherAdder->LastValue()");
123   double RetVal ;
124   anOtherAdder->LastResult( RetVal ) ;
125   if ( RetVal > 0 ) {
126     ValFunc = (x - y) ;
127   }
128   else {
129     ValFunc = -(x - y) ;
130   }
131   sendMessage(NOTIF_TRACE, "Adder_Impl::AddAndCompare has called anOtherAdder->LastValue()");
132   sendMessage(NOTIF_STEP, "Adder_Impl::AddAndCompare is Finished");
133   endService( " Adder_Impl::AddAndCompare"  );
134   return ValFunc ;
135 }
136
137 void Adder_Impl::SetLastResult( CORBA::Double z ) {
138   beginService( " Adder_Impl::SetLastResult" );
139   sendMessage(NOTIF_STEP, "Adder_Impl::SetLastResult is Computing");
140   int S;
141 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
142   S = 5 ;
143   while ( S ) {
144     S = sleep(S);
145   }
146   LastAddition = z ;
147   endService( " Adder_Impl::SetLastResult"  );
148   return ;
149 }
150
151 void Adder_Impl::LastResult( CORBA::Double & z ) {
152   beginService( " Adder_Impl::LastResult" );
153   sendMessage(NOTIF_STEP, "Adder_Impl::LastResult is Computing");
154   int S;
155 //  S = 1+(int) (15.0*rand()/(RAND_MAX+1.0));
156   S = 5 ;
157   while ( S ) {
158     S = sleep(S);
159   }
160   z = LastAddition ;
161   endService( " Adder_Impl::LastResult"  );
162   return ;
163 }
164
165 Engines::EngineComponent_ptr Adder_Impl::LccAddComponent( const char * aContainer ,
166                                                           const char * aComponentName ) {
167   beginService( "Adder_Impl::LccAddComponent" );
168   Engines::EngineComponent_ptr objComponent ;
169   objComponent = Engines::EngineComponent::_nil() ;
170
171   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
172   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
173   CORBA::ORB_var orb = init(0 , 0 ) ;
174   SALOME_NamingService *_NS ;
175   _NS = new SALOME_NamingService();
176   _NS->init_orb( CORBA::ORB::_duplicate(orb) ) ;
177           
178   SALOME_LifeCycleCORBA LCC( _NS ) ;
179   objComponent = LCC.FindOrLoad_Component( aContainer ,
180                                            aComponentName );
181   endService( "Adder_Impl::LccAddComponent"  );
182   return objComponent ;
183 }