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