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 #define private protected
30 #include "SALOME_Component_i.hxx"
31 #include "SALOME_Container_i.hxx"
32 #include "RegistryConnexion.hxx"
37 #include "utilities.h"
40 #include <sys/resource.h>
45 extern bool _Sleeping ;
46 static Engines_Component_i * theEngines_Component ;
48 bool Engines_Component_i::_isMultiStudy = true;
49 bool Engines_Component_i::_isMultiInstance = false;
51 //=============================================================================
55 //=============================================================================
57 Engines_Component_i::Engines_Component_i()
59 // MESSAGE("Component constructor");
62 //=============================================================================
66 //=============================================================================
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,
74 _instanceName(instanceName),
75 _interfaceName(interfaceName),
76 _myConnexionToRegistry(0),
84 MESSAGE("Component constructor with instanceName "<< _instanceName);
86 _orb = CORBA::ORB::_duplicate(orb);
87 _poa = PortableServer::POA::_duplicate(poa);
89 CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
90 const CORBA::String_var ior = _orb->object_to_string(o);
91 _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession",
92 _instanceName.c_str());
94 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
98 //=============================================================================
102 //=============================================================================
104 // Constructeur pour composant parallele: ne pas faire appel au registry!!
105 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
106 PortableServer::POA_ptr poa,
107 PortableServer::ObjectId * contId,
108 const char *instanceName,
109 const char *interfaceName,
112 _instanceName(instanceName),
113 _interfaceName(interfaceName),
114 _myConnexionToRegistry(0),
122 // MESSAGE("Component constructor with instanceName "<< _instanceName);
123 _orb = CORBA::ORB::_duplicate(orb);
124 _poa = PortableServer::POA::_duplicate(poa);
127 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
130 //=============================================================================
134 //=============================================================================
136 Engines_Component_i::~Engines_Component_i()
138 MESSAGE("Component destructor");
139 Engines_Container_i::decInstanceCnt(_interfaceName);
142 //=============================================================================
146 //=============================================================================
148 char* Engines_Component_i::instanceName()
150 return CORBA::string_dup(_instanceName.c_str()) ;
153 //=============================================================================
157 //=============================================================================
159 char* Engines_Component_i::interfaceName()
161 return CORBA::string_dup(_interfaceName.c_str()) ;
164 //=============================================================================
168 //=============================================================================
170 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
172 ASSERT( studyId >= 0);
173 CORBA::Boolean ret = false;
180 if ( _studyId == studyId) ret = true;
184 //=============================================================================
188 //=============================================================================
190 CORBA::Long Engines_Component_i::getStudyId()
195 //=============================================================================
199 //=============================================================================
201 void Engines_Component_i::ping()
203 MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
207 //=============================================================================
211 //=============================================================================
213 void Engines_Component_i::destroy()
215 MESSAGE("Engines_Component_i::destroy()");
218 delete _notifSupplier;
221 delete _myConnexionToRegistry;
222 _myConnexionToRegistry = 0 ;
223 _poa->deactivate_object(*_id) ;
224 CORBA::release(_poa) ;
227 _thisObj->_remove_ref();
229 MESSAGE("Engines_Component_i::destroyed") ;
232 //=============================================================================
236 //=============================================================================
238 Engines::Container_ptr Engines_Component_i::GetContainerRef()
240 MESSAGE("Engines_Component_i::GetContainerRef");
241 CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
242 return Engines::Container::_narrow(o);
245 //=============================================================================
249 //=============================================================================
251 PortableServer::ObjectId * Engines_Component_i::getId()
253 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
257 //=============================================================================
261 //=============================================================================
263 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
266 for (CORBA::ULong i=0; i<dico.length(); i++)
268 std::string cle(dico[i].key);
269 _fieldsDict[cle] = dico[i].value;
273 //=============================================================================
277 //=============================================================================
279 Engines::FieldsDict* Engines_Component_i::getProperties()
281 Engines::FieldsDict_var copie = new Engines::FieldsDict;
282 copie->length(_fieldsDict.size());
283 map<std::string,CORBA::Any>::iterator it;
285 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
287 std::string cle((*it).first);
288 copie[i].key = CORBA::string_dup(cle.c_str());
289 copie[i].value = _fieldsDict[cle];
291 return copie._retn();
294 //=============================================================================
298 //=============================================================================
300 void Engines_Component_i::beginService(const char *serviceName)
302 MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
303 << endl << "Component instance : " << _instanceName << endl << endl);
304 _ThreadId = pthread_self() ;
306 _StartUsed = CpuUsed_impl() ;
309 _serviceName = serviceName ;
310 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
312 perror("pthread_setcanceltype ") ;
315 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
317 perror("pthread_setcancelstate ") ;
320 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
321 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
322 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
324 // --- for supervisor : all strings given with setProperties
325 // are set in environment
326 bool overwrite = true;
327 map<std::string,CORBA::Any>::iterator it;
328 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
330 std::string cle((*it).first);
331 if ((*it).second.type()->kind() == CORBA::tk_string)
334 (*it).second >>= value;
335 // --- todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
337 int ret = setenv(cle.c_str(), value, overwrite);
339 //CCRT porting : setenv not defined in stdlib.h
343 //char* cast because 1st arg of linux putenv function is not a const char* !
344 int ret=putenv((char *)s.c_str());
345 //End of CCRT porting
347 MESSAGE("--- setenv: "<<cle<<" = "<< value);
352 //=============================================================================
356 //=============================================================================
358 void Engines_Component_i::endService(const char *serviceName)
360 _ThreadCpuUsed = CpuUsed_impl() ;
361 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
362 << endl << " Component instance : " << _instanceName << " StartUsed "
363 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
367 //=============================================================================
371 //=============================================================================
373 void Engines_Component_i::Names( const char * graphName ,
374 const char * nodeName )
376 _graphName = graphName ;
377 _nodeName = nodeName ;
378 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
379 // << _nodeName << "' )");
382 //=============================================================================
386 //=============================================================================
388 char* Engines_Component_i::graphName()
390 return CORBA::string_dup( _graphName.c_str() ) ;
393 //=============================================================================
397 //=============================================================================
399 char* Engines_Component_i::nodeName()
401 return CORBA::string_dup( _nodeName.c_str() ) ;
404 //=============================================================================
408 //=============================================================================
410 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
416 if ( pthread_cancel( ThreadId ) )
418 perror("Killer pthread_cancel error") ;
423 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
424 << " pthread_canceled") ;
429 if ( pthread_kill( ThreadId , signum ) == -1 )
431 perror("Killer pthread_kill error") ;
436 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
437 << " pthread_killed(" << signum << ")") ;
444 //=============================================================================
448 //=============================================================================
450 bool Engines_Component_i::Kill_impl() {
451 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
452 // << " pid " << getpid() << " instanceName "
453 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
454 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
455 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
457 bool RetVal = false ;
458 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
459 RetVal = Killer( _ThreadId , 0 ) ;
460 _ThreadId = (pthread_t ) -1 ;
465 //=============================================================================
469 //=============================================================================
471 bool Engines_Component_i::Stop_impl()
473 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
474 << " pid " << getpid() << " instanceName "
475 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
476 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
477 << dec << " _ThreadId " << _ThreadId );
479 bool RetVal = false ;
480 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
482 RetVal = Killer( _ThreadId , 0 ) ;
483 _ThreadId = (pthread_t ) -1 ;
488 //=============================================================================
492 //=============================================================================
494 bool Engines_Component_i::Suspend_impl()
496 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
497 << " pid " << getpid() << " instanceName "
498 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
499 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
500 << dec << " _ThreadId " << _ThreadId );
501 bool RetVal = false ;
502 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
510 RetVal = Killer( _ThreadId ,SIGINT ) ;
516 //=============================================================================
520 //=============================================================================
522 bool Engines_Component_i::Resume_impl()
524 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
525 << " pid " << getpid() << " instanceName "
526 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
527 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
528 << dec << " _ThreadId " << _ThreadId );
529 bool RetVal = false ;
530 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
546 //=============================================================================
550 //=============================================================================
554 theEngines_Component->SetCurCpu() ;
557 //=============================================================================
561 //=============================================================================
563 void Engines_Component_i::SetCurCpu()
565 _ThreadCpuUsed = CpuUsed() ;
566 // MESSAGE(pthread_self() <<
567 // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
570 //=============================================================================
574 //=============================================================================
576 long Engines_Component_i::CpuUsed()
579 struct rusage usage ;
580 if ( _ThreadId || _Executed )
582 if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
584 perror("Engines_Component_i::CpuUsed") ;
587 cpu = usage.ru_utime.tv_sec - _StartUsed ;
588 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
589 // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
590 // << " = " << cpu << endl ;
594 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
595 // << _ThreadId << " " << _serviceName<< " _StartUsed "
596 // << _StartUsed << endl ;
601 //=============================================================================
605 //=============================================================================
607 CORBA::Long Engines_Component_i::CpuUsed_impl()
610 if ( _ThreadId || _Executed )
614 if ( pthread_self() != _ThreadId )
621 // Get Cpu in the appropriate thread with that object !...
622 theEngines_Component = this ;
623 Killer( _ThreadId ,SIGUSR1 ) ;
625 cpu = _ThreadCpuUsed ;
629 _ThreadCpuUsed = CpuUsed() ;
630 cpu = _ThreadCpuUsed ;
631 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
632 // << _serviceName << " " << cpu << endl ;
637 cpu = _ThreadCpuUsed ;
638 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
639 // << _serviceName << " " << cpu<< endl ;
644 // cout << pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
645 // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
650 //=============================================================================
652 * Send message to event channel
654 //=============================================================================
656 void Engines_Component_i::sendMessage(const char *event_type,
659 _notifSupplier->Send(graphName(), nodeName(), event_type, message);
662 //=============================================================================
666 //=============================================================================
668 string Engines_Component_i::GetDynLibraryName(const char *componentName)
676 //=============================================================================
680 //=============================================================================
682 string Engines_Component_i::BuildComponentNameForNS(const char *ComponentName,
683 const char *ContainerName,
684 const char *hostname)
686 string ret=Engines_Container_i::BuildContainerNameForNS(ContainerName,hostname);