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