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 // --- todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC...
198 int ret = setenv(cle.c_str(), value, overwrite);
200 //CCRT porting : setenv not defined in stdlib.h
204 //char* cast because 1st arg of linux putenv function is not a const char* !!!
205 int ret=putenv((char *)s.c_str());
206 //End of CCRT porting
208 MESSAGE("--- setenv: "<<cle<<" = "<< value);
213 void Engines_Component_i::endService(const char *serviceName)
215 _ThreadCpuUsed = CpuUsed_impl() ;
216 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName << endl
217 << " Component instance : " << _instanceName << " StartUsed " << _StartUsed << " _ThreadCpuUsed "
218 << _ThreadCpuUsed << endl << endl);
222 void Engines_Component_i::Names( const char * graphName ,
223 const char * nodeName ) {
224 _graphName = graphName ;
225 _nodeName = nodeName ;
226 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
227 // << _nodeName << "' )");
230 char* Engines_Component_i::graphName() {
231 return CORBA::string_dup( _graphName.c_str() ) ;
234 char* Engines_Component_i::nodeName() {
235 return CORBA::string_dup( _nodeName.c_str() ) ;
238 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum ) {
241 if ( pthread_cancel( ThreadId ) ) {
242 perror("Killer pthread_cancel error") ;
246 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_canceled") ;
250 if ( pthread_kill( ThreadId , signum ) == -1 ) {
251 perror("Killer pthread_kill error") ;
255 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_killed("
263 bool Engines_Component_i::Kill_impl() {
264 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
265 // << " pid " << getpid() << " instanceName "
266 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
267 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
268 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
270 bool RetVal = false ;
271 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
272 RetVal = Killer( _ThreadId , 0 ) ;
273 _ThreadId = (pthread_t ) -1 ;
278 bool Engines_Component_i::Stop_impl() {
279 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
280 << " pid " << getpid() << " instanceName "
281 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
282 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
283 << dec << " _ThreadId " << _ThreadId );
284 bool RetVal = false ;
285 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
286 RetVal = Killer( _ThreadId , 0 ) ;
287 _ThreadId = (pthread_t ) -1 ;
292 bool Engines_Component_i::Suspend_impl() {
293 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
294 << " pid " << getpid() << " instanceName "
295 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
296 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
297 << dec << " _ThreadId " << _ThreadId );
298 bool RetVal = false ;
299 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
304 RetVal = Killer( _ThreadId ,SIGINT ) ;
310 bool Engines_Component_i::Resume_impl() {
311 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
312 << " pid " << getpid() << " instanceName "
313 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
314 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
315 << dec << " _ThreadId " << _ThreadId );
316 bool RetVal = false ;
317 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
331 theEngines_Component->SetCurCpu() ;
333 void Engines_Component_i::SetCurCpu() {
334 _ThreadCpuUsed = CpuUsed() ;
335 // MESSAGE(pthread_self() << " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
338 #include <sys/time.h>
339 #include <sys/resource.h>
342 long Engines_Component_i::CpuUsed() {
344 struct rusage usage ;
345 if ( _ThreadId || _Executed ) {
346 if ( getrusage( RUSAGE_SELF , &usage ) == -1 ) {
347 perror("Engines_Component_i::CpuUsed") ;
350 cpu = usage.ru_utime.tv_sec - _StartUsed ;
351 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " " << _serviceName
352 // << usage.ru_utime.tv_sec << " - " << _StartUsed << " = " << cpu << endl ;
355 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId " << _ThreadId << " " << _serviceName
356 // << " _StartUsed " << _StartUsed << endl ;
361 CORBA::Long Engines_Component_i::CpuUsed_impl() {
363 if ( _ThreadId || _Executed ) {
364 if ( _ThreadId > 0 ) {
365 if ( pthread_self() != _ThreadId ) {
369 // Get Cpu in the appropriate thread with that object !...
370 theEngines_Component = this ;
371 Killer( _ThreadId ,SIGUSR1 ) ;
373 cpu = _ThreadCpuUsed ;
376 _ThreadCpuUsed = CpuUsed() ;
377 cpu = _ThreadCpuUsed ;
378 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl " << _serviceName << " " << cpu
383 cpu = _ThreadCpuUsed ;
384 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl " << _serviceName << " " << cpu
389 // cout << pthread_self() << "Engines_Component_i::CpuUsed_impl _ThreadId " << _ThreadId << " "
390 // << _serviceName << " _StartUsed " << _StartUsed << endl ;
395 // Send message to event channel
397 void Engines_Component_i::sendMessage(const char *event_type, const char *message) {
398 _notifSupplier->Send(graphName(), nodeName(), event_type, message);