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"
39 #include "utilities.h"
43 #include <sys/resource.h>
46 #include <sys/timeb.h>
53 extern bool _Sleeping ;
54 static Engines_Component_i * theEngines_Component ;
56 bool Engines_Component_i::_isMultiStudy = true;
57 bool Engines_Component_i::_isMultiInstance = false;
59 //=============================================================================
61 * Default constructor, not for use
63 //=============================================================================
65 Engines_Component_i::Engines_Component_i()
68 INFOS("Default Constructor...");
71 //=============================================================================
73 * Standard Constructor for generic Component, used in derived class
74 * Connection to Registry and Notification
75 * \param orb Object Request broker given by Container
76 * \parap poa Portable Object Adapter from Container (normally root_poa)
77 * \param contId container CORBA id inside the server
78 * \param instanceName unique instance name for this object (see Container_i)
79 * \param interfaceName component class name
80 * \param notif use of notification
82 //=============================================================================
84 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
85 PortableServer::POA_ptr poa,
86 PortableServer::ObjectId * contId,
87 const char *instanceName,
88 const char *interfaceName,
90 _instanceName(instanceName),
91 _interfaceName(interfaceName),
92 _myConnexionToRegistry(0),
100 MESSAGE("Component constructor with instanceName "<< _instanceName);
101 //SCRUTE(pd_refCount);
102 _orb = CORBA::ORB::_duplicate(orb);
103 _poa = PortableServer::POA::_duplicate(poa);
105 CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
106 const CORBA::String_var ior = _orb->object_to_string(o);
107 _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession",
108 _instanceName.c_str());
110 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
111 //SCRUTE(pd_refCount);
114 //=============================================================================
116 * Standard constructor for parallel component
117 * Connection Notification (no connection to Registry !)
118 * \param orb Object Request broker given by Container
119 * \parap poa Portable Object Adapter from Container (normally root_poa)
120 * \param contId container CORBA id inside the server
121 * \param instanceName unique instance name for this object (see Container_i)
122 * \param interfaceName component class name
123 * \param flag not used...
124 * \param notif use of notification
126 //=============================================================================
128 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
129 PortableServer::POA_ptr poa,
130 PortableServer::ObjectId * contId,
131 const char *instanceName,
132 const char *interfaceName,
135 _instanceName(instanceName),
136 _interfaceName(interfaceName),
137 _myConnexionToRegistry(0),
145 _orb = CORBA::ORB::_duplicate(orb);
146 _poa = PortableServer::POA::_duplicate(poa);
149 _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
152 //=============================================================================
154 * Destructor: call Container for decrement of instances count.
155 * When instances count falls to 0, the container tries to remove the
156 * component library (dlclose)
158 //=============================================================================
160 Engines_Component_i::~Engines_Component_i()
162 MESSAGE("Component destructor");
163 Engines_Container_i::decInstanceCnt(_interfaceName);
166 //=============================================================================
168 * CORBA method: return name of the instance, unique in this Container
170 //=============================================================================
172 char* Engines_Component_i::instanceName()
174 return CORBA::string_dup(_instanceName.c_str()) ;
177 //=============================================================================
179 * CORBA method: return name of the component class
181 //=============================================================================
183 char* Engines_Component_i::interfaceName()
185 return CORBA::string_dup(_interfaceName.c_str()) ;
188 //=============================================================================
190 * CORBA method: Get study Id
191 * \return -1: not initialised (Internal Error)
192 * 0: multistudy component instance
193 * >0: study id associated to this instance
195 //=============================================================================
197 CORBA::Long Engines_Component_i::getStudyId()
202 //=============================================================================
204 * CORBA method: Test if instance is alive and responds
206 //=============================================================================
208 void Engines_Component_i::ping()
210 MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
214 //=============================================================================
216 * CORBA method: Deactivate this instance. CORBA object is deactivated (do not
217 * respond any more to CORBA calls), the connection to Regsitry is removed
218 * (Registry informed of deactivation), internal server reference counter on
219 * the derived servant class is decremented, to allow destruction of the class
220 * (delete) by POA, when there are no more references.
221 * -- TO BE USED BY CONTAINER ONLY (Container housekeeping) --
223 //=============================================================================
225 void Engines_Component_i::destroy()
227 MESSAGE("Engines_Component_i::destroy()");
228 //SCRUTE(pd_refCount);
230 delete _notifSupplier;
233 delete _myConnexionToRegistry;
234 _myConnexionToRegistry = 0 ;
235 _poa->deactivate_object(*_id) ;
236 CORBA::release(_poa) ;
238 //SCRUTE(pd_refCount);
239 _thisObj->_remove_ref();
240 //SCRUTE(pd_refCount);
241 MESSAGE("Engines_Component_i::destroyed") ;
244 //=============================================================================
246 * CORBA method: return CORBA reference of the Container
249 //=============================================================================
251 Engines::Container_ptr Engines_Component_i::GetContainerRef()
253 // MESSAGE("Engines_Component_i::GetContainerRef");
254 CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
255 return Engines::Container::_narrow(o);
258 //=============================================================================
261 * Gives a sequence of (key=string,value=any) to the component.
262 * Base class component stores the sequence in a map.
263 * The map is cleared before.
264 * This map is for use by derived classes.
265 * \param dico sequence of (key=string,value=any)
267 //=============================================================================
269 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
272 for (CORBA::ULong i=0; i<dico.length(); i++)
274 std::string cle(dico[i].key);
275 _fieldsDict[cle] = dico[i].value;
279 //=============================================================================
282 * returns a previously stored map (key=string,value=any) as a sequence.
283 * (see setProperties)
285 //=============================================================================
287 Engines::FieldsDict* Engines_Component_i::getProperties()
289 Engines::FieldsDict_var copie = new Engines::FieldsDict;
290 copie->length(_fieldsDict.size());
291 map<std::string,CORBA::Any>::iterator it;
293 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
295 std::string cle((*it).first);
296 copie[i].key = CORBA::string_dup(cle.c_str());
297 copie[i].value = _fieldsDict[cle];
299 return copie._retn();
302 //=============================================================================
304 * CORBA method: used by Supervision to give names to this instance
306 //=============================================================================
308 void Engines_Component_i::Names( const char * graphName ,
309 const char * nodeName )
311 _graphName = graphName ;
312 _nodeName = nodeName ;
313 // MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
314 // << _nodeName << "' )");
317 //=============================================================================
319 * CORBA method: used in Supervision
321 //=============================================================================
323 bool Engines_Component_i::Kill_impl()
325 // MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
326 // << " pid " << getpid() << " instanceName "
327 // << _instanceName.c_str() << " interface " << _interfaceName.c_str()
328 // << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
329 // << dec << " _ThreadId " << _ThreadId << " this " << hex << this
332 bool RetVal = false ;
334 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
336 RetVal = Killer( _ThreadId , 0 ) ;
337 _ThreadId = (pthread_t ) -1 ;
341 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
343 RetVal = Killer( *_ThreadId , 0 ) ;
344 _ThreadId = (pthread_t* ) 0 ;
351 //=============================================================================
353 * CORBA method: used in Supervision
355 //=============================================================================
357 bool Engines_Component_i::Stop_impl()
359 MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
360 << " pid " << getpid() << " instanceName "
361 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
362 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
363 << dec << " _ThreadId " << _ThreadId );
366 bool RetVal = false ;
368 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
370 RetVal = Killer( _ThreadId , 0 ) ;
371 _ThreadId = (pthread_t ) -1 ;
374 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
376 RetVal = Killer( *_ThreadId , 0 ) ;
377 _ThreadId = (pthread_t* ) 0 ;
383 //=============================================================================
385 * CORBA method: used in Supervision
387 //=============================================================================
389 bool Engines_Component_i::Suspend_impl()
391 MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
392 << " pid " << getpid() << " instanceName "
393 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
394 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
395 << dec << " _ThreadId " << _ThreadId );
397 bool RetVal = false ;
399 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
401 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
411 RetVal = Killer( _ThreadId ,SIGINT ) ;
413 RetVal = Killer( *_ThreadId ,SIGINT ) ;
415 //if ( RetVal ) _Sleeping = true;
422 //=============================================================================
424 * CORBA method: used in Supervision
426 //=============================================================================
428 bool Engines_Component_i::Resume_impl()
430 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
431 << " pid " << getpid() << " instanceName "
432 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
433 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
434 << dec << " _ThreadId " << _ThreadId );
435 bool RetVal = false ;
437 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
439 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
455 //=============================================================================
459 //=============================================================================
461 CORBA::Long Engines_Component_i::CpuUsed_impl()
464 if ( _ThreadId || _Executed )
469 if ( pthread_self() != _ThreadId )
471 if ( pthread_self().p != _ThreadId->p )
479 // Get Cpu in the appropriate thread with that object !...
480 theEngines_Component = this ;
482 Killer( _ThreadId ,SIGUSR1 ) ;
484 Killer( *_ThreadId ,SIGUSR11 ) ;
487 cpu = _ThreadCpuUsed ;
491 _ThreadCpuUsed = CpuUsed() ;
492 cpu = _ThreadCpuUsed ;
493 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
494 // << _serviceName << " " << cpu << endl ;
499 cpu = _ThreadCpuUsed ;
500 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
501 // << _serviceName << " " << cpu<< endl ;
506 // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
507 // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
513 //=============================================================================
515 * C++ method: return Container Servant
517 //=============================================================================
519 Engines_Container_i *Engines_Component_i::GetContainerPtr()
521 return dynamic_cast<Engines_Container_i*>(_poa->id_to_servant(*_contId)) ;
524 //=============================================================================
526 * C++ method: set study Id
527 * \param studyId 0 if instance is not associated to a study,
528 * >0 otherwise (== study id)
529 * \return true if the set of study Id is OK
530 * must be set once by Container, at instance creation,
531 * and cannot be changed after.
533 //=============================================================================
535 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
537 ASSERT( studyId >= 0);
538 CORBA::Boolean ret = false;
539 if (_studyId < 0) // --- not yet initialized
545 if ( _studyId == studyId) ret = true;
549 //=============================================================================
551 * C++ method: return CORBA instance id, the id is set in derived class
552 * constructor, when instance is activated.
554 //=============================================================================
556 PortableServer::ObjectId * Engines_Component_i::getId()
558 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
562 //=============================================================================
564 * C++ method: used by derived classes for supervision
566 //=============================================================================
568 void Engines_Component_i::beginService(const char *serviceName)
570 MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
571 << endl << "Component instance : " << _instanceName << endl << endl);
573 _ThreadId = pthread_self() ;
575 _ThreadId = new pthread_t;
576 _ThreadId->p = pthread_self().p ;
577 _ThreadId->x = pthread_self().x ;
580 _StartUsed = CpuUsed_impl() ;
583 _serviceName = serviceName ;
584 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
586 perror("pthread_setcanceltype ") ;
589 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
591 perror("pthread_setcancelstate ") ;
594 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
595 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
596 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
598 // --- for supervisor : all strings given with setProperties
599 // are set in environment
600 bool overwrite = true;
601 map<std::string,CORBA::Any>::iterator it;
602 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
604 std::string cle((*it).first);
605 if ((*it).second.type()->kind() == CORBA::tk_string)
608 (*it).second >>= value;
609 // ---todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
611 int ret = setenv(cle.c_str(), value, overwrite);
613 //CCRT porting : setenv not defined in stdlib.h
617 // char* cast because 1st arg of linux putenv function
618 // is not a const char* !
619 int ret=putenv((char *)s.c_str());
620 //End of CCRT porting
622 MESSAGE("--- setenv: "<<cle<<" = "<< value);
627 //=============================================================================
629 * C++ method: used by derived classes for supervision
631 //=============================================================================
633 void Engines_Component_i::endService(const char *serviceName)
635 _ThreadCpuUsed = CpuUsed_impl() ;
636 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
637 << endl << " Component instance : " << _instanceName << " StartUsed "
638 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
642 //=============================================================================
644 * C++ method: -- CHECK IF USED --
646 //=============================================================================
648 char* Engines_Component_i::graphName()
650 return CORBA::string_dup( _graphName.c_str() ) ;
653 //=============================================================================
655 * C++ method: -- CHECK IF USED --
657 //=============================================================================
659 char* Engines_Component_i::nodeName()
661 return CORBA::string_dup( _nodeName.c_str() ) ;
664 //=============================================================================
666 * C++ method: used in Supervision (see kill_impl)
668 //=============================================================================
670 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
680 if ( pthread_cancel( ThreadId ) )
682 perror("Killer pthread_cancel error") ;
687 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
688 << " pthread_canceled") ;
693 if ( pthread_kill( ThreadId , signum ) == -1 )
695 perror("Killer pthread_kill error") ;
700 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
701 << " pthread_killed(" << signum << ")") ;
708 //=============================================================================
712 //=============================================================================
716 theEngines_Component->SetCurCpu() ;
719 //=============================================================================
723 //=============================================================================
725 void Engines_Component_i::SetCurCpu()
727 _ThreadCpuUsed = CpuUsed() ;
728 // MESSAGE(pthread_self() <<
729 // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
732 //=============================================================================
736 //=============================================================================
738 long Engines_Component_i::CpuUsed()
742 struct rusage usage ;
743 if ( _ThreadId || _Executed )
745 if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
747 perror("Engines_Component_i::CpuUsed") ;
750 cpu = usage.ru_utime.tv_sec - _StartUsed ;
751 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
752 // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
753 // << " = " << cpu << endl ;
757 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
758 // << _ThreadId << " " << _serviceName<< " _StartUsed "
759 // << _StartUsed << endl ;
762 // NOT implementet yet
769 //=============================================================================
771 * C++ method: Send message to event channel
773 //=============================================================================
775 void Engines_Component_i::sendMessage(const char *event_type,
778 _notifSupplier->Send(graphName(), nodeName(), event_type, message);
781 //=============================================================================
783 * C++ method: return standard library name built on component name
785 //=============================================================================
787 string Engines_Component_i::GetDynLibraryName(const char *componentName)
795 //=============================================================================
797 * C++ method: DumpPython default implementation
799 //=============================================================================
801 Engines::TMPFile* Engines_Component_i::DumpPython(CORBA::Object_ptr theStudy,
802 CORBA::Boolean isPublished,
803 CORBA::Boolean& isValidScript)
805 char* aScript = "def RebuildData(theStudy): pass";
806 char* aBuffer = new char[strlen(aScript)+1];
807 strcpy(aBuffer, aScript);
808 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
809 int aBufferSize = strlen(aBuffer)+1;
810 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
811 isValidScript = true;
812 return aStreamFile._retn();