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");
141 //=============================================================================
145 //=============================================================================
147 char* Engines_Component_i::instanceName()
149 return CORBA::string_dup(_instanceName.c_str()) ;
152 //=============================================================================
156 //=============================================================================
158 char* Engines_Component_i::interfaceName()
160 return CORBA::string_dup(_interfaceName.c_str()) ;
163 //=============================================================================
167 //=============================================================================
169 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
171 ASSERT( studyId >= 0);
172 CORBA::Boolean ret = false;
179 if ( _studyId == studyId) ret = true;
183 //=============================================================================
187 //=============================================================================
189 CORBA::Long Engines_Component_i::getStudyId()
194 //=============================================================================
198 //=============================================================================
200 void Engines_Component_i::ping()
202 MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
206 //=============================================================================
210 //=============================================================================
212 void Engines_Component_i::destroy()
214 MESSAGE("Engines_Component_i::destroy()");
217 delete _notifSupplier;
220 delete _myConnexionToRegistry;
221 _myConnexionToRegistry = 0 ;
222 _poa->deactivate_object(*_id) ;
223 CORBA::release(_poa) ;
226 _thisObj->_remove_ref();
228 MESSAGE("Engines_Component_i::destroyed") ;
231 //=============================================================================
235 //=============================================================================
237 Engines::Container_ptr Engines_Component_i::GetContainerRef()
239 MESSAGE("Engines_Component_i::GetContainerRef");
240 CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
241 return Engines::Container::_narrow(o);
244 //=============================================================================
248 //=============================================================================
250 PortableServer::ObjectId * Engines_Component_i::getId()
252 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
256 //=============================================================================
260 //=============================================================================
262 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
265 for (CORBA::ULong i=0; i<dico.length(); i++)
267 std::string cle(dico[i].key);
268 _fieldsDict[cle] = dico[i].value;
272 //=============================================================================
276 //=============================================================================
278 Engines::FieldsDict* Engines_Component_i::getProperties()
280 Engines::FieldsDict_var copie = new Engines::FieldsDict;
281 copie->length(_fieldsDict.size());
282 map<std::string,CORBA::Any>::iterator it;
284 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
286 std::string cle((*it).first);
287 copie[i].key = CORBA::string_dup(cle.c_str());
288 copie[i].value = _fieldsDict[cle];
290 return copie._retn();
293 //=============================================================================
297 //=============================================================================
299 void Engines_Component_i::beginService(const char *serviceName)
301 MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
302 << endl << "Component instance : " << _instanceName << endl << endl);
303 _ThreadId = pthread_self() ;
305 _StartUsed = CpuUsed_impl() ;
308 _serviceName = serviceName ;
309 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
311 perror("pthread_setcanceltype ") ;
314 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
316 perror("pthread_setcancelstate ") ;
319 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
320 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
321 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
323 // --- for supervisor : all strings given with setProperties
324 // are set in environment
325 bool overwrite = true;
326 map<std::string,CORBA::Any>::iterator it;
327 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
329 std::string cle((*it).first);
330 if ((*it).second.type()->kind() == CORBA::tk_string)
333 (*it).second >>= value;
334 // --- todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
336 int ret = setenv(cle.c_str(), value, overwrite);
338 //CCRT porting : setenv not defined in stdlib.h
342 //char* cast because 1st arg of linux putenv function is not a const char* !
343 int ret=putenv((char *)s.c_str());
344 //End of CCRT porting
346 MESSAGE("--- setenv: "<<cle<<" = "<< value);
351 //=============================================================================
355 //=============================================================================
357 void Engines_Component_i::endService(const char *serviceName)
359 _ThreadCpuUsed = CpuUsed_impl() ;
360 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
361 << endl << " Component instance : " << _instanceName << " StartUsed "
362 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
366 //=============================================================================
370 //=============================================================================
372 void Engines_Component_i::Names( const char * graphName ,
373 const char * nodeName )
375 _graphName = graphName ;
376 _nodeName = nodeName ;
377 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
378 // << _nodeName << "' )");
381 //=============================================================================
385 //=============================================================================
387 char* Engines_Component_i::graphName()
389 return CORBA::string_dup( _graphName.c_str() ) ;
392 //=============================================================================
396 //=============================================================================
398 char* Engines_Component_i::nodeName()
400 return CORBA::string_dup( _nodeName.c_str() ) ;
403 //=============================================================================
407 //=============================================================================
409 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
415 if ( pthread_cancel( ThreadId ) )
417 perror("Killer pthread_cancel error") ;
422 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
423 << " pthread_canceled") ;
428 if ( pthread_kill( ThreadId , signum ) == -1 )
430 perror("Killer pthread_kill error") ;
435 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
436 << " pthread_killed(" << signum << ")") ;
443 //=============================================================================
447 //=============================================================================
449 bool Engines_Component_i::Kill_impl() {
450 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
451 // << " pid " << getpid() << " instanceName "
452 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
453 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
454 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
456 bool RetVal = false ;
457 if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
458 RetVal = Killer( _ThreadId , 0 ) ;
459 _ThreadId = (pthread_t ) -1 ;
464 //=============================================================================
468 //=============================================================================
470 bool Engines_Component_i::Stop_impl()
472 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
473 << " pid " << getpid() << " instanceName "
474 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
475 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
476 << dec << " _ThreadId " << _ThreadId );
478 bool RetVal = false ;
479 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
481 RetVal = Killer( _ThreadId , 0 ) ;
482 _ThreadId = (pthread_t ) -1 ;
487 //=============================================================================
491 //=============================================================================
493 bool Engines_Component_i::Suspend_impl()
495 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
496 << " pid " << getpid() << " instanceName "
497 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
498 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
499 << dec << " _ThreadId " << _ThreadId );
500 bool RetVal = false ;
501 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
509 RetVal = Killer( _ThreadId ,SIGINT ) ;
515 //=============================================================================
519 //=============================================================================
521 bool Engines_Component_i::Resume_impl()
523 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
524 << " pid " << getpid() << " instanceName "
525 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
526 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
527 << dec << " _ThreadId " << _ThreadId );
528 bool RetVal = false ;
529 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
545 //=============================================================================
549 //=============================================================================
553 theEngines_Component->SetCurCpu() ;
556 //=============================================================================
560 //=============================================================================
562 void Engines_Component_i::SetCurCpu()
564 _ThreadCpuUsed = CpuUsed() ;
565 // MESSAGE(pthread_self() <<
566 // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
569 //=============================================================================
573 //=============================================================================
575 long Engines_Component_i::CpuUsed()
578 struct rusage usage ;
579 if ( _ThreadId || _Executed )
581 if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
583 perror("Engines_Component_i::CpuUsed") ;
586 cpu = usage.ru_utime.tv_sec - _StartUsed ;
587 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
588 // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
589 // << " = " << cpu << endl ;
593 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
594 // << _ThreadId << " " << _serviceName<< " _StartUsed "
595 // << _StartUsed << endl ;
600 //=============================================================================
604 //=============================================================================
606 CORBA::Long Engines_Component_i::CpuUsed_impl()
609 if ( _ThreadId || _Executed )
613 if ( pthread_self() != _ThreadId )
620 // Get Cpu in the appropriate thread with that object !...
621 theEngines_Component = this ;
622 Killer( _ThreadId ,SIGUSR1 ) ;
624 cpu = _ThreadCpuUsed ;
628 _ThreadCpuUsed = CpuUsed() ;
629 cpu = _ThreadCpuUsed ;
630 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
631 // << _serviceName << " " << cpu << endl ;
636 cpu = _ThreadCpuUsed ;
637 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
638 // << _serviceName << " " << cpu<< endl ;
643 // cout << pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
644 // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
649 //=============================================================================
651 * Send message to event channel
653 //=============================================================================
655 void Engines_Component_i::sendMessage(const char *event_type,
658 _notifSupplier->Send(graphName(), nodeName(), event_type, message);
661 //=============================================================================
665 //=============================================================================
667 string Engines_Component_i::GetDynLibraryName(const char *componentName)
675 //=============================================================================
679 //=============================================================================
681 string Engines_Component_i::BuildComponentNameForNS(const char *ComponentName,
682 const char *ContainerName,
683 const char *hostname)
685 string ret=Engines_Container_i::BuildContainerNameForNS(ContainerName,hostname);