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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
39 #include "utilities.h"
43 #include <sys/resource.h>
46 #include <sys/timeb.h>
54 extern bool _Sleeping ;
55 static Engines_Component_i * theEngines_Component ;
57 bool Engines_Component_i::_isMultiStudy = true;
58 bool Engines_Component_i::_isMultiInstance = false;
60 //=============================================================================
62 * Default constructor, not for use
64 //=============================================================================
66 Engines_Component_i::Engines_Component_i()
69 MESSAGE("Default Constructor, not for normal use...");
72 //=============================================================================
74 * Standard Constructor for generic Component, used in derived class
75 * Connection to Registry and Notification
76 * \param orb Object Request broker given by Container
77 * \param poa Portable Object Adapter from Container (normally root_poa)
78 * \param contId container CORBA id inside the server
79 * \param instanceName unique instance name for this object (see Container_i)
80 * \param interfaceName component class name
81 * \param notif use of notification
83 //=============================================================================
85 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
86 PortableServer::POA_ptr poa,
87 PortableServer::ObjectId * contId,
88 const char *instanceName,
89 const char *interfaceName,
91 _instanceName(instanceName),
92 _interfaceName(interfaceName),
93 _myConnexionToRegistry(0),
100 _CanceledThread(false)
102 MESSAGE("Component constructor with instanceName "<< _instanceName);
103 //SCRUTE(pd_refCount);
104 _orb = CORBA::ORB::_duplicate(orb);
105 _poa = PortableServer::POA::_duplicate(poa);
107 CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
108 const CORBA::String_var ior = _orb->object_to_string(o);
109 _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession",
110 _instanceName.c_str());
112 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
113 //SCRUTE(pd_refCount);
116 //=============================================================================
118 * Standard constructor for parallel component
119 * Connection Notification (no connection to Registry !)
120 * \param orb Object Request broker given by Container
121 * \param poa Portable Object Adapter from Container (normally root_poa)
122 * \param contId container CORBA id inside the server
123 * \param instanceName unique instance name for this object (see Container_i)
124 * \param interfaceName component class name
125 * \param flag not used...
126 * \param notif use of notification
128 //=============================================================================
130 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
131 PortableServer::POA_ptr poa,
132 PortableServer::ObjectId * contId,
133 const char *instanceName,
134 const char *interfaceName,
137 _instanceName(instanceName),
138 _interfaceName(interfaceName),
139 _myConnexionToRegistry(0),
146 _CanceledThread(false)
148 _orb = CORBA::ORB::_duplicate(orb);
149 _poa = PortableServer::POA::_duplicate(poa);
152 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
155 //=============================================================================
157 * Destructor: call Container for decrement of instances count.
158 * When instances count falls to 0, the container tries to remove the
159 * component library (dlclose)
161 //=============================================================================
163 Engines_Component_i::~Engines_Component_i()
165 MESSAGE("Component destructor");
166 Engines_Container_i::decInstanceCnt(_interfaceName);
169 //=============================================================================
171 * CORBA method: return name of the instance, unique in this Container
173 //=============================================================================
175 char* Engines_Component_i::instanceName()
177 return CORBA::string_dup(_instanceName.c_str()) ;
180 //=============================================================================
182 * CORBA method: return name of the component class
184 //=============================================================================
186 char* Engines_Component_i::interfaceName()
188 return CORBA::string_dup(_interfaceName.c_str()) ;
191 //=============================================================================
193 * CORBA method: Get study Id
194 * \return -1: not initialised (Internal Error)
195 * 0: multistudy component instance
196 * >0: study id associated to this instance
198 //=============================================================================
200 CORBA::Long Engines_Component_i::getStudyId()
205 //=============================================================================
207 * CORBA method: Test if instance is alive and responds
209 //=============================================================================
211 void Engines_Component_i::ping()
214 MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
217 MESSAGE("Engines_Component_i::ping() pid "<< _getpid()<< " threadid "
218 << pthread_self().p );
222 //=============================================================================
224 * CORBA method: Deactivate this instance. CORBA object is deactivated (do not
225 * respond any more to CORBA calls), the connection to Regsitry is removed
226 * (Registry informed of deactivation), internal server reference counter on
227 * the derived servant class is decremented, to allow destruction of the class
228 * (delete) by POA, when there are no more references.
229 * -- TO BE USED BY CONTAINER ONLY (Container housekeeping) --
231 //=============================================================================
233 void Engines_Component_i::destroy()
235 MESSAGE("Engines_Component_i::destroy()");
236 //SCRUTE(pd_refCount);
238 delete _notifSupplier;
241 delete _myConnexionToRegistry;
242 _myConnexionToRegistry = 0 ;
243 _poa->deactivate_object(*_id) ;
244 CORBA::release(_poa) ;
246 //SCRUTE(pd_refCount);
247 _thisObj->_remove_ref();
248 //SCRUTE(pd_refCount);
249 MESSAGE("Engines_Component_i::destroyed") ;
252 //=============================================================================
254 * CORBA method: return CORBA reference of the Container
257 //=============================================================================
259 Engines::Container_ptr Engines_Component_i::GetContainerRef()
261 // MESSAGE("Engines_Component_i::GetContainerRef");
262 CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
263 return Engines::Container::_narrow(o);
266 //=============================================================================
269 * Gives a sequence of (key=string,value=any) to the component.
270 * Base class component stores the sequence in a map.
271 * The map is cleared before.
272 * This map is for use by derived classes.
273 * \param dico sequence of (key=string,value=any)
275 //=============================================================================
277 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
280 for (CORBA::ULong i=0; i<dico.length(); i++)
282 std::string cle(dico[i].key);
283 _fieldsDict[cle] = dico[i].value;
287 //=============================================================================
290 * returns a previously stored map (key=string,value=any) as a sequence.
291 * (see setProperties)
293 //=============================================================================
295 Engines::FieldsDict* Engines_Component_i::getProperties()
297 Engines::FieldsDict_var copie = new Engines::FieldsDict;
298 copie->length(_fieldsDict.size());
299 map<std::string,CORBA::Any>::iterator it;
301 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
303 std::string cle((*it).first);
304 copie[i].key = CORBA::string_dup(cle.c_str());
305 copie[i].value = _fieldsDict[cle];
307 return copie._retn();
310 //=============================================================================
312 * CORBA method: used by Supervision to give names to this instance
314 //=============================================================================
316 void Engines_Component_i::Names( const char * graphName ,
317 const char * nodeName )
319 _graphName = graphName ;
320 _nodeName = nodeName ;
321 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
322 // << _nodeName << "' )");
325 //=============================================================================
327 * CORBA method: used in Supervision
329 //=============================================================================
331 bool Engines_Component_i::Kill_impl()
333 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
334 // << " pid " << getpid() << " instanceName "
335 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
336 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
337 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
340 bool RetVal = false ;
342 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
344 RetVal = Killer( _ThreadId , SIGUSR2 ) ;
345 _ThreadId = (pthread_t ) -1 ;
349 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
351 RetVal = Killer( *_ThreadId , 0 ) ;
352 _ThreadId = (pthread_t* ) 0 ;
359 //=============================================================================
361 * CORBA method: used in Supervision
363 //=============================================================================
365 bool Engines_Component_i::Stop_impl()
368 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
369 << " pid " << getpid() << " instanceName "
370 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
371 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
372 << dec << " _ThreadId " << _ThreadId );
374 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self().p
375 << " pid " << _getpid() << " instanceName "
376 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
377 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
378 << dec << " _ThreadId " << _ThreadId );
382 bool RetVal = false ;
384 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
386 RetVal = Killer( _ThreadId , 0 ) ;
387 _ThreadId = (pthread_t ) -1 ;
390 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
392 RetVal = Killer( *_ThreadId , 0 ) ;
393 _ThreadId = (pthread_t* ) 0 ;
399 //=============================================================================
401 * CORBA method: used in Supervision
403 //=============================================================================
405 bool Engines_Component_i::Suspend_impl()
408 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
409 << " pid " << getpid() << " instanceName "
410 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
411 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
412 << dec << " _ThreadId " << _ThreadId );
414 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self().p
415 << " pid " << _getpid() << " instanceName "
416 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
417 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
418 << dec << " _ThreadId " << _ThreadId );
421 bool RetVal = false ;
423 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
425 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
435 RetVal = Killer( _ThreadId ,SIGINT ) ;
437 RetVal = Killer( *_ThreadId ,SIGINT ) ;
439 //if ( RetVal ) _Sleeping = true;
446 //=============================================================================
448 * CORBA method: used in Supervision
450 //=============================================================================
452 bool Engines_Component_i::Resume_impl()
455 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
456 << " pid " << getpid() << " instanceName "
457 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
458 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
459 << dec << " _ThreadId " << _ThreadId );
461 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self().p
462 << " pid " << _getpid() << " instanceName "
463 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
464 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
465 << dec << " _ThreadId " << _ThreadId );
467 bool RetVal = false ;
469 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
471 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
487 //=============================================================================
491 //=============================================================================
493 CORBA::Long Engines_Component_i::CpuUsed_impl()
496 if ( _ThreadId || _Executed )
501 if ( pthread_self() != _ThreadId )
503 if ( pthread_self().p != _ThreadId->p )
511 // Get Cpu in the appropriate thread with that object !...
512 theEngines_Component = this ;
514 Killer( _ThreadId ,SIGUSR1 ) ;
516 Killer( *_ThreadId ,SIGUSR11 ) ;
519 cpu = _ThreadCpuUsed ;
523 _ThreadCpuUsed = CpuUsed() ;
524 cpu = _ThreadCpuUsed ;
525 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
526 // << _serviceName << " " << cpu << endl ;
531 cpu = _ThreadCpuUsed ;
532 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
533 // << _serviceName << " " << cpu<< endl ;
538 // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
539 // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
545 //=============================================================================
547 * C++ method: return Container Servant
549 //=============================================================================
551 Engines_Container_i *Engines_Component_i::GetContainerPtr()
553 return dynamic_cast<Engines_Container_i*>(_poa->id_to_servant(*_contId)) ;
556 //=============================================================================
558 * C++ method: set study Id
559 * \param studyId 0 if instance is not associated to a study,
560 * >0 otherwise (== study id)
561 * \return true if the set of study Id is OK
562 * must be set once by Container, at instance creation,
563 * and cannot be changed after.
565 //=============================================================================
567 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
569 ASSERT( studyId >= 0);
570 CORBA::Boolean ret = false;
571 if (_studyId < 0) // --- not yet initialized
577 if ( _studyId == studyId) ret = true;
581 //=============================================================================
583 * C++ method: return CORBA instance id, the id is set in derived class
584 * constructor, when instance is activated.
586 //=============================================================================
588 PortableServer::ObjectId * Engines_Component_i::getId()
590 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
594 //=============================================================================
596 * C++ method: used by derived classes for supervision
598 //=============================================================================
600 void Engines_Component_i::beginService(const char *serviceName)
603 MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
604 << endl << "Component instance : " << _instanceName << endl << endl);
606 MESSAGE(pthread_self().p << "Send BeginService notification for " <<serviceName
607 << endl << "Component instance : " << _instanceName << endl << endl);
610 _ThreadId = pthread_self() ;
612 _ThreadId = new pthread_t;
613 _ThreadId->p = pthread_self().p ;
614 _ThreadId->x = pthread_self().x ;
617 _StartUsed = CpuUsed_impl() ;
620 _serviceName = serviceName ;
621 theEngines_Component = this ;
622 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
624 perror("pthread_setcanceltype ") ;
627 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
629 perror("pthread_setcancelstate ") ;
632 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
633 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
634 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
636 // --- for supervisor : all strings given with setProperties
637 // are set in environment
638 bool overwrite = true;
639 map<std::string,CORBA::Any>::iterator it;
640 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
642 std::string cle((*it).first);
643 if ((*it).second.type()->kind() == CORBA::tk_string)
646 (*it).second >>= value;
647 // ---todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
649 int ret = setenv(cle.c_str(), value, overwrite);
651 //CCRT porting : setenv not defined in stdlib.h
655 // char* cast because 1st arg of linux putenv function
656 // is not a const char* !
657 int ret=putenv((char *)s.c_str());
658 //End of CCRT porting
660 MESSAGE("--- setenv: "<<cle<<" = "<< value);
665 //=============================================================================
667 * C++ method: used by derived classes for supervision
669 //=============================================================================
671 void Engines_Component_i::endService(const char *serviceName)
673 if ( !_CanceledThread )
674 _ThreadCpuUsed = CpuUsed_impl() ;
677 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
678 << endl << " Component instance : " << _instanceName << " StartUsed "
679 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
681 MESSAGE(pthread_self().p << " Send EndService notification for " << serviceName
682 << endl << " Component instance : " << _instanceName << " StartUsed "
683 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
688 //=============================================================================
690 * C++ method: -- CHECK IF USED --
692 //=============================================================================
694 char* Engines_Component_i::graphName()
696 return CORBA::string_dup( _graphName.c_str() ) ;
699 //=============================================================================
701 * C++ method: -- CHECK IF USED --
703 //=============================================================================
705 char* Engines_Component_i::nodeName()
707 return CORBA::string_dup( _nodeName.c_str() ) ;
710 //=============================================================================
712 * C++ method: used in Supervision (see kill_impl)
714 //=============================================================================
716 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
726 if ( pthread_cancel( ThreadId ) )
728 perror("Killer pthread_cancel error") ;
734 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
735 << " pthread_canceled") ;
737 MESSAGE(pthread_self().p << "Killer : ThreadId " << ThreadId.p
738 << " pthread_canceled") ;
744 if ( pthread_kill( ThreadId , signum ) == -1 )
746 perror("Killer pthread_kill error") ;
752 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
753 << " pthread_killed(" << signum << ")") ;
755 MESSAGE(pthread_self().p << "Killer : ThreadId " << ThreadId.p
756 << " pthread_killed(" << signum << ")") ;
764 //=============================================================================
768 //=============================================================================
772 if ( theEngines_Component )
773 theEngines_Component->SetCurCpu() ;
776 //=============================================================================
780 //=============================================================================
782 void Engines_Component_i::SetCurCpu()
784 _ThreadCpuUsed = CpuUsed() ;
785 // MESSAGE(pthread_self() <<
786 // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
789 //=============================================================================
793 //=============================================================================
795 long Engines_Component_i::CpuUsed()
799 struct rusage usage ;
800 if ( _ThreadId || _Executed )
802 if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
804 perror("Engines_Component_i::CpuUsed") ;
807 cpu = usage.ru_utime.tv_sec - _StartUsed ;
808 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
809 // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
810 // << " = " << cpu << endl ;
814 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
815 // << _ThreadId << " " << _serviceName<< " _StartUsed "
816 // << _StartUsed << endl ;
819 // NOT implementet yet
826 void CallCancelThread()
828 if ( theEngines_Component )
829 theEngines_Component->CancelThread() ;
832 //=============================================================================
836 //=============================================================================
838 void Engines_Component_i::CancelThread()
840 _CanceledThread = true;
843 //=============================================================================
845 * C++ method: Send message to event channel
847 //=============================================================================
849 void Engines_Component_i::sendMessage(const char *event_type,
852 _notifSupplier->Send(graphName(), nodeName(), event_type, message);
855 //=============================================================================
857 * C++ method: return standard library name built on component name
859 //=============================================================================
861 string Engines_Component_i::GetDynLibraryName(const char *componentName)
869 //=============================================================================
871 * C++ method: DumpPython default implementation
873 //=============================================================================
875 Engines::TMPFile* Engines_Component_i::DumpPython(CORBA::Object_ptr theStudy,
876 CORBA::Boolean isPublished,
877 CORBA::Boolean& isValidScript)
879 char* aScript = "def RebuildData(theStudy): pass";
880 char* aBuffer = new char[strlen(aScript)+1];
881 strcpy(aBuffer, aScript);
882 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
883 int aBufferSize = strlen(aBuffer)+1;
884 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
885 isValidScript = true;
886 return aStreamFile._retn();