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