1 // SALOME Container : implementation of container and engine for Kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Component_i.cxx
25 // Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
29 #include "SALOME_Component_i.hxx"
30 #include "RegistryConnexion.hxx"
35 #include "utilities.h"
38 extern bool _Sleeping ;
39 static Engines_Component_i * theEngines_Component ;
41 Engines_Component_i::Engines_Component_i()
43 // MESSAGE("Component constructor");
46 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
47 PortableServer::POA_ptr poa,
48 PortableServer::ObjectId * contId,
49 const char *instanceName,
50 const char *interfaceName,
52 _instanceName(instanceName),
53 _interfaceName(interfaceName),
54 _myConnexionToRegistry(0),
55 _ThreadId(0) , _ThreadCpuUsed(0) , _Executed(false) , _graphName("") , _nodeName("") {
56 MESSAGE("Component constructor with instanceName "<< _instanceName);
57 _orb = CORBA::ORB::_duplicate(orb);
58 _poa = PortableServer::POA::_duplicate(poa);
60 CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
61 const CORBA::String_var ior = _orb->object_to_string(o);
62 _myConnexionToRegistry = new RegistryConnexion(0, 0, ior, "theSession", _instanceName.c_str());
64 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
67 // Constructeur pour composant parallele: ne pas faire appel au registry!!
68 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
69 PortableServer::POA_ptr poa,
70 PortableServer::ObjectId * contId,
71 const char *instanceName,
72 const char *interfaceName,
75 : _instanceName(instanceName),
76 _interfaceName(interfaceName),
77 _myConnexionToRegistry(0),
78 _ThreadId(0) , _ThreadCpuUsed(0) , _Executed(false) , _graphName("") , _nodeName("") {
79 // MESSAGE("Component constructor with instanceName "<< _instanceName);
80 _orb = CORBA::ORB::_duplicate(orb);
81 _poa = PortableServer::POA::_duplicate(poa);
83 // CORBA::Object_var myself = this->_this(); //appel a _this = increment reference
85 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
88 Engines_Component_i::~Engines_Component_i()
90 MESSAGE("Component destructor");
91 // delete _myConnexionToRegistry;
92 // _myConnexionToRegistry = 0 ;
95 char* Engines_Component_i::instanceName() {
96 return CORBA::string_dup(_instanceName.c_str()) ;
99 char* Engines_Component_i::interfaceName() {
100 return CORBA::string_dup(_interfaceName.c_str()) ;
103 void Engines_Component_i::ping()
105 MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
109 void Engines_Component_i::destroy()
111 MESSAGE("Engines_Component_i::destroy()");
113 delete _notifSupplier;
116 delete _myConnexionToRegistry;
117 _myConnexionToRegistry = 0 ;
118 _poa->deactivate_object(*_id) ;
119 CORBA::release(_poa) ;
121 _thisObj->_remove_ref();
122 MESSAGE("Engines_Component_i::destroyed") ;
125 Engines::Container_ptr Engines_Component_i::GetContainerRef()
127 MESSAGE("Engines_Component_i::GetContainerRef");
128 CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
129 return Engines::Container::_narrow(o);
132 PortableServer::ObjectId * Engines_Component_i::getId()
134 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
138 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
141 for (CORBA::ULong i=0; i<dico.length(); i++)
143 std::string cle(dico[i].key);
144 _fieldsDict[cle] = dico[i].value;
148 Engines::FieldsDict* Engines_Component_i::getProperties()
150 Engines::FieldsDict_var copie = new Engines::FieldsDict;
151 copie->length(_fieldsDict.size());
152 map<std::string,CORBA::Any>::iterator it;
154 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
156 std::string cle((*it).first);
157 copie[i].key = CORBA::string_dup(cle.c_str());
158 copie[i].value = _fieldsDict[cle];
160 return copie._retn();
163 void Engines_Component_i::beginService(const char *serviceName)
165 MESSAGE(pthread_self() << "Send BeginService notification for " << serviceName << endl
166 << "Component instance : " << _instanceName << endl << endl);
167 _ThreadId = pthread_self() ;
169 _StartUsed = CpuUsed_impl() ;
172 _serviceName = serviceName ;
173 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) ) {
174 perror("pthread_setcanceltype ") ;
177 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) ) {
178 perror("pthread_setcancelstate ") ;
181 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
182 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
183 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
185 // --- for supervisor : all strings given with setProperties
186 // are set in environment
187 bool overwrite = true;
188 map<std::string,CORBA::Any>::iterator it;
189 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
191 std::string cle((*it).first);
192 if ((*it).second.type()->kind() == CORBA::tk_string)
195 (*it).second >>= value;
196 int ret = setenv(cle.c_str(), value, overwrite);
197 MESSAGE("--- setenv: "<<cle<<" = "<< value);
202 void Engines_Component_i::endService(const char *serviceName)
204 _ThreadCpuUsed = CpuUsed_impl() ;
205 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName << endl
206 << " Component instance : " << _instanceName << " StartUsed " << _StartUsed << " _ThreadCpuUsed "
207 << _ThreadCpuUsed << endl << endl);
211 void Engines_Component_i::Names( const char * graphName ,
212 const char * nodeName ) {
213 _graphName = graphName ;
214 _nodeName = nodeName ;
215 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
216 // << _nodeName << "' )");
219 char* Engines_Component_i::graphName() {
220 return CORBA::string_dup( _graphName.c_str() ) ;
223 char* Engines_Component_i::nodeName() {
224 return CORBA::string_dup( _nodeName.c_str() ) ;
227 bool Engines_Component_i::Killer( int ThreadId , int signum ) {
230 if ( pthread_cancel( ThreadId ) ) {
231 perror("Killer pthread_cancel error") ;
235 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_canceled") ;
239 if ( pthread_kill( ThreadId , signum ) == -1 ) {
240 perror("Killer pthread_kill error") ;
244 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_killed("
252 bool Engines_Component_i::Kill_impl() {
253 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
254 // << " pid " << getpid() << " instanceName "
255 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
256 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
257 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
259 bool RetVal = false ;
260 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
261 RetVal = Killer( _ThreadId , 0 ) ;
262 _ThreadId = (pthread_t ) -1 ;
267 bool Engines_Component_i::Stop_impl() {
268 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
269 << " pid " << getpid() << " instanceName "
270 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
271 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
272 << dec << " _ThreadId " << _ThreadId );
273 bool RetVal = false ;
274 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
275 RetVal = Killer( _ThreadId , 0 ) ;
276 _ThreadId = (pthread_t ) -1 ;
281 bool Engines_Component_i::Suspend_impl() {
282 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
283 << " pid " << getpid() << " instanceName "
284 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
285 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
286 << dec << " _ThreadId " << _ThreadId );
287 bool RetVal = false ;
288 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
293 RetVal = Killer( _ThreadId ,SIGINT ) ;
299 bool Engines_Component_i::Resume_impl() {
300 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
301 << " pid " << getpid() << " instanceName "
302 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
303 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
304 << dec << " _ThreadId " << _ThreadId );
305 bool RetVal = false ;
306 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
320 theEngines_Component->SetCurCpu() ;
322 void Engines_Component_i::SetCurCpu() {
323 _ThreadCpuUsed = CpuUsed() ;
324 // MESSAGE(pthread_self() << " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
327 #include <sys/time.h>
328 #include <sys/resource.h>
331 long Engines_Component_i::CpuUsed() {
333 struct rusage usage ;
334 if ( _ThreadId || _Executed ) {
335 if ( getrusage( RUSAGE_SELF , &usage ) == -1 ) {
336 perror("Engines_Component_i::CpuUsed") ;
339 cpu = usage.ru_utime.tv_sec - _StartUsed ;
340 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " " << _serviceName
341 // << usage.ru_utime.tv_sec << " - " << _StartUsed << " = " << cpu << endl ;
344 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId " << _ThreadId << " " << _serviceName
345 // << " _StartUsed " << _StartUsed << endl ;
350 long Engines_Component_i::CpuUsed_impl() {
352 if ( _ThreadId || _Executed ) {
353 if ( _ThreadId > 0 ) {
354 if ( pthread_self() != _ThreadId ) {
358 // Get Cpu in the appropriate thread with that object !...
359 theEngines_Component = this ;
360 Killer( _ThreadId ,SIGUSR1 ) ;
362 cpu = _ThreadCpuUsed ;
365 _ThreadCpuUsed = CpuUsed() ;
366 cpu = _ThreadCpuUsed ;
367 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl " << _serviceName << " " << cpu
372 cpu = _ThreadCpuUsed ;
373 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl " << _serviceName << " " << cpu
378 // cout << pthread_self() << "Engines_Component_i::CpuUsed_impl _ThreadId " << _ThreadId << " "
379 // << _serviceName << " _StartUsed " << _StartUsed << endl ;
384 // Send message to event channel
386 void Engines_Component_i::sendMessage(const char *event_type, const char *message) {
387 _notifSupplier->Send(graphName(), nodeName(), event_type, message);