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 // for pd_refCount trace
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 //=============================================================================
53 * Default constructor, not for use
55 //=============================================================================
57 Engines_Component_i::Engines_Component_i()
62 //=============================================================================
64 * Standard Constructor for generic Component, used in derived class
65 * Connection to Registry and Notification
66 * \param orb Object Request broker given by Container
67 * \parap poa Portable Object Adapter from Container (normally root_poa)
68 * \param contId container CORBA id inside the server
69 * \param instanceName unique instance name for this object (see Container_i)
70 * \param interfaceName component class name
71 * \param notif use of notification
73 //=============================================================================
75 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
76 PortableServer::POA_ptr poa,
77 PortableServer::ObjectId * contId,
78 const char *instanceName,
79 const char *interfaceName,
81 _instanceName(instanceName),
82 _interfaceName(interfaceName),
83 _myConnexionToRegistry(0),
91 MESSAGE("Component constructor with instanceName "<< _instanceName);
92 //SCRUTE(pd_refCount);
93 _orb = CORBA::ORB::_duplicate(orb);
94 _poa = PortableServer::POA::_duplicate(poa);
96 CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
97 const CORBA::String_var ior = _orb->object_to_string(o);
98 _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession",
99 _instanceName.c_str());
101 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
102 //SCRUTE(pd_refCount);
105 //=============================================================================
107 * Standard constructor for parallel component
108 * Connection Notification (no connection to Registry !)
109 * \param orb Object Request broker given by Container
110 * \parap poa Portable Object Adapter from Container (normally root_poa)
111 * \param contId container CORBA id inside the server
112 * \param instanceName unique instance name for this object (see Container_i)
113 * \param interfaceName component class name
114 * \param flag not used...
115 * \param notif use of notification
117 //=============================================================================
119 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
120 PortableServer::POA_ptr poa,
121 PortableServer::ObjectId * contId,
122 const char *instanceName,
123 const char *interfaceName,
126 _instanceName(instanceName),
127 _interfaceName(interfaceName),
128 _myConnexionToRegistry(0),
136 _orb = CORBA::ORB::_duplicate(orb);
137 _poa = PortableServer::POA::_duplicate(poa);
140 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
143 //=============================================================================
145 * Destructor: call Container for decrement of instances count.
146 * When instances count falls to 0, the container tries to remove the
147 * component library (dlclose)
149 //=============================================================================
151 Engines_Component_i::~Engines_Component_i()
153 MESSAGE("Component destructor");
154 Engines_Container_i::decInstanceCnt(_interfaceName);
157 //=============================================================================
159 * CORBA method: return name of the instance, unique in this Container
161 //=============================================================================
163 char* Engines_Component_i::instanceName()
165 return CORBA::string_dup(_instanceName.c_str()) ;
168 //=============================================================================
170 * CORBA method: return name of the component class
172 //=============================================================================
174 char* Engines_Component_i::interfaceName()
176 return CORBA::string_dup(_interfaceName.c_str()) ;
179 //=============================================================================
181 * CORBA method: Get study Id
182 * \return -1: not initialised (Internal Error)
183 * 0: multistudy component instance
184 * >0: study id associated to this instance
186 //=============================================================================
188 CORBA::Long Engines_Component_i::getStudyId()
193 //=============================================================================
195 * CORBA method: Test if instance is alive and responds
197 //=============================================================================
199 void Engines_Component_i::ping()
201 MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
205 //=============================================================================
207 * CORBA method: Deactivate this instance. CORBA object is deactivated (do not
208 * respond any more to CORBA calls), the connection to Regsitry is removed
209 * (Registry informed of deactivation), internal server reference counter on
210 * the derived servant class is decremented, to allow destruction of the class
211 * (delete) by POA, when there are no more references.
212 * -- TO BE USED BY CONTAINER ONLY (Container housekeeping) --
214 //=============================================================================
216 void Engines_Component_i::destroy()
218 MESSAGE("Engines_Component_i::destroy()");
219 //SCRUTE(pd_refCount);
221 delete _notifSupplier;
224 delete _myConnexionToRegistry;
225 _myConnexionToRegistry = 0 ;
226 _poa->deactivate_object(*_id) ;
227 CORBA::release(_poa) ;
229 //SCRUTE(pd_refCount);
230 _thisObj->_remove_ref();
231 //SCRUTE(pd_refCount);
232 MESSAGE("Engines_Component_i::destroyed") ;
235 //=============================================================================
237 * CORBA method: return CORBA reference of the Container
240 //=============================================================================
242 Engines::Container_ptr Engines_Component_i::GetContainerRef()
244 MESSAGE("Engines_Component_i::GetContainerRef");
245 CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
246 return Engines::Container::_narrow(o);
249 //=============================================================================
252 * Gives a sequence of (key=string,value=any) to the component.
253 * Base class component stores the sequence in a map.
254 * The map is cleared before.
255 * This map is for use by derived classes.
256 * \param dico sequence of (key=string,value=any)
258 //=============================================================================
260 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
263 for (CORBA::ULong i=0; i<dico.length(); i++)
265 std::string cle(dico[i].key);
266 _fieldsDict[cle] = dico[i].value;
270 //=============================================================================
273 * returns a previously stored map (key=string,value=any) as a sequence.
274 * (see setProperties)
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 //=============================================================================
295 * CORBA method: used by Supervision to give names to this instance
297 //=============================================================================
299 void Engines_Component_i::Names( const char * graphName ,
300 const char * nodeName )
302 _graphName = graphName ;
303 _nodeName = nodeName ;
304 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
305 // << _nodeName << "' )");
308 //=============================================================================
310 * CORBA method: used in Supervision
312 //=============================================================================
314 bool Engines_Component_i::Kill_impl()
316 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
317 // << " pid " << getpid() << " instanceName "
318 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
319 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
320 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
323 bool RetVal = false ;
324 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
326 RetVal = Killer( _ThreadId , 0 ) ;
327 _ThreadId = (pthread_t ) -1 ;
332 //=============================================================================
334 * CORBA method: used in Supervision
336 //=============================================================================
338 bool Engines_Component_i::Stop_impl()
340 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
341 << " pid " << getpid() << " instanceName "
342 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
343 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
344 << dec << " _ThreadId " << _ThreadId );
347 bool RetVal = false ;
348 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
350 RetVal = Killer( _ThreadId , 0 ) ;
351 _ThreadId = (pthread_t ) -1 ;
356 //=============================================================================
358 * CORBA method: used in Supervision
360 //=============================================================================
362 bool Engines_Component_i::Suspend_impl()
364 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
365 << " pid " << getpid() << " instanceName "
366 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
367 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
368 << dec << " _ThreadId " << _ThreadId );
370 bool RetVal = false ;
371 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
379 RetVal = Killer( _ThreadId ,SIGINT ) ;
385 //=============================================================================
387 * CORBA method: used in Supervision
389 //=============================================================================
391 bool Engines_Component_i::Resume_impl()
393 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
394 << " pid " << getpid() << " instanceName "
395 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
396 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
397 << dec << " _ThreadId " << _ThreadId );
398 bool RetVal = false ;
399 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
414 //=============================================================================
418 //=============================================================================
420 CORBA::Long Engines_Component_i::CpuUsed_impl()
423 if ( _ThreadId || _Executed )
427 if ( pthread_self() != _ThreadId )
434 // Get Cpu in the appropriate thread with that object !...
435 theEngines_Component = this ;
436 Killer( _ThreadId ,SIGUSR1 ) ;
438 cpu = _ThreadCpuUsed ;
442 _ThreadCpuUsed = CpuUsed() ;
443 cpu = _ThreadCpuUsed ;
444 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
445 // << _serviceName << " " << cpu << endl ;
450 cpu = _ThreadCpuUsed ;
451 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
452 // << _serviceName << " " << cpu<< endl ;
457 // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
458 // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
463 //=============================================================================
465 * C++ method: set study Id
466 * \param studyId 0 if instance is not associated to a study,
467 * >0 otherwise (== study id)
468 * \return true if the set of study Id is OK
469 * must be set once by Container, and cannot be changed after.
471 //=============================================================================
473 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
475 ASSERT( studyId >= 0);
476 CORBA::Boolean ret = false;
483 if ( _studyId == studyId) ret = true;
487 //=============================================================================
489 * C++ method: return CORBA instance id, the id is set in derived class
490 * constructor, when instance is activated.
492 //=============================================================================
494 PortableServer::ObjectId * Engines_Component_i::getId()
496 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
500 //=============================================================================
502 * C++ method: used by derived classes for supervision
504 //=============================================================================
506 void Engines_Component_i::beginService(const char *serviceName)
508 MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
509 << endl << "Component instance : " << _instanceName << endl << endl);
510 _ThreadId = pthread_self() ;
512 _StartUsed = CpuUsed_impl() ;
515 _serviceName = serviceName ;
516 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
518 perror("pthread_setcanceltype ") ;
521 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
523 perror("pthread_setcancelstate ") ;
526 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
527 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
528 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
530 // --- for supervisor : all strings given with setProperties
531 // are set in environment
532 bool overwrite = true;
533 map<std::string,CORBA::Any>::iterator it;
534 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
536 std::string cle((*it).first);
537 if ((*it).second.type()->kind() == CORBA::tk_string)
540 (*it).second >>= value;
541 // ---todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
543 int ret = setenv(cle.c_str(), value, overwrite);
545 //CCRT porting : setenv not defined in stdlib.h
549 // char* cast because 1st arg of linux putenv function
550 // is not a const char* !
551 int ret=putenv((char *)s.c_str());
552 //End of CCRT porting
554 MESSAGE("--- setenv: "<<cle<<" = "<< value);
559 //=============================================================================
561 * C++ method: used by derived classes for supervision
563 //=============================================================================
565 void Engines_Component_i::endService(const char *serviceName)
567 _ThreadCpuUsed = CpuUsed_impl() ;
568 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
569 << endl << " Component instance : " << _instanceName << " StartUsed "
570 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
574 //=============================================================================
576 * C++ method: -- CHECK IF USED --
578 //=============================================================================
580 char* Engines_Component_i::graphName()
582 return CORBA::string_dup( _graphName.c_str() ) ;
585 //=============================================================================
587 * C++ method: -- CHECK IF USED --
589 //=============================================================================
591 char* Engines_Component_i::nodeName()
593 return CORBA::string_dup( _nodeName.c_str() ) ;
596 //=============================================================================
598 * C++ method: used in Supervision (see kill_impl)
600 //=============================================================================
602 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
608 if ( pthread_cancel( ThreadId ) )
610 perror("Killer pthread_cancel error") ;
615 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
616 << " pthread_canceled") ;
621 if ( pthread_kill( ThreadId , signum ) == -1 )
623 perror("Killer pthread_kill error") ;
628 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
629 << " pthread_killed(" << signum << ")") ;
636 //=============================================================================
640 //=============================================================================
644 theEngines_Component->SetCurCpu() ;
647 //=============================================================================
651 //=============================================================================
653 void Engines_Component_i::SetCurCpu()
655 _ThreadCpuUsed = CpuUsed() ;
656 // MESSAGE(pthread_self() <<
657 // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
660 //=============================================================================
664 //=============================================================================
666 long Engines_Component_i::CpuUsed()
669 struct rusage usage ;
670 if ( _ThreadId || _Executed )
672 if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
674 perror("Engines_Component_i::CpuUsed") ;
677 cpu = usage.ru_utime.tv_sec - _StartUsed ;
678 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
679 // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
680 // << " = " << cpu << endl ;
684 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
685 // << _ThreadId << " " << _serviceName<< " _StartUsed "
686 // << _StartUsed << endl ;
691 //=============================================================================
693 * C++ method: Send message to event channel
695 //=============================================================================
697 void Engines_Component_i::sendMessage(const char *event_type,
700 _notifSupplier->Send(graphName(), nodeName(), event_type, message);
703 //=============================================================================
707 //=============================================================================
709 string Engines_Component_i::GetDynLibraryName(const char *componentName)
717 //=============================================================================
721 //=============================================================================
723 string Engines_Component_i::BuildComponentNameForNS(const char *ComponentName,
724 const char *ContainerName,
725 const char *hostname)
728 Engines_Container_i::BuildContainerNameForNS(ContainerName,hostname);