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