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 ) ;
421 //=============================================================================
423 * CORBA method: used in Supervision
425 //=============================================================================
427 bool Engines_Component_i::Resume_impl()
429 MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
430 << " pid " << getpid() << " instanceName "
431 << _instanceName.c_str() << " interface " << _interfaceName.c_str()
432 << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
433 << dec << " _ThreadId " << _ThreadId );
434 bool RetVal = false ;
436 if ( _ThreadId > 0 && pthread_self() != _ThreadId )
438 if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
454 //=============================================================================
458 //=============================================================================
460 CORBA::Long Engines_Component_i::CpuUsed_impl()
463 if ( _ThreadId || _Executed )
468 if ( pthread_self() != _ThreadId )
470 if ( pthread_self().p != _ThreadId->p )
478 // Get Cpu in the appropriate thread with that object !...
479 theEngines_Component = this ;
481 Killer( _ThreadId ,SIGUSR1 ) ;
483 Killer( *_ThreadId ,SIGUSR11 ) ;
486 cpu = _ThreadCpuUsed ;
490 _ThreadCpuUsed = CpuUsed() ;
491 cpu = _ThreadCpuUsed ;
492 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
493 // << _serviceName << " " << cpu << endl ;
498 cpu = _ThreadCpuUsed ;
499 // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
500 // << _serviceName << " " << cpu<< endl ;
505 // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
506 // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
511 //=============================================================================
513 * C++ method: set study Id
514 * \param studyId 0 if instance is not associated to a study,
515 * >0 otherwise (== study id)
516 * \return true if the set of study Id is OK
517 * must be set once by Container, and cannot be changed after.
519 //=============================================================================
521 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
523 ASSERT( studyId >= 0);
524 CORBA::Boolean ret = false;
531 if ( _studyId == studyId) ret = true;
535 //=============================================================================
537 * C++ method: return CORBA instance id, the id is set in derived class
538 * constructor, when instance is activated.
540 //=============================================================================
542 PortableServer::ObjectId * Engines_Component_i::getId()
544 // MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
548 //=============================================================================
550 * C++ method: used by derived classes for supervision
552 //=============================================================================
554 void Engines_Component_i::beginService(const char *serviceName)
556 MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
557 << endl << "Component instance : " << _instanceName << endl << endl);
559 _ThreadId = pthread_self() ;
561 _ThreadId = new pthread_t;
562 _ThreadId->p = pthread_self().p ;
563 _ThreadId->x = pthread_self().x ;
566 _StartUsed = CpuUsed_impl() ;
569 _serviceName = serviceName ;
570 if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
572 perror("pthread_setcanceltype ") ;
575 if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
577 perror("pthread_setcancelstate ") ;
580 // MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
581 // << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
582 // << " _graphName " << _graphName << " _nodeName " << _nodeName );
584 // --- for supervisor : all strings given with setProperties
585 // are set in environment
586 bool overwrite = true;
587 map<std::string,CORBA::Any>::iterator it;
588 for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
590 std::string cle((*it).first);
591 if ((*it).second.type()->kind() == CORBA::tk_string)
594 (*it).second >>= value;
595 // ---todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
597 int ret = setenv(cle.c_str(), value, overwrite);
599 //CCRT porting : setenv not defined in stdlib.h
603 // char* cast because 1st arg of linux putenv function
604 // is not a const char* !
605 int ret=putenv((char *)s.c_str());
606 //End of CCRT porting
608 MESSAGE("--- setenv: "<<cle<<" = "<< value);
613 //=============================================================================
615 * C++ method: used by derived classes for supervision
617 //=============================================================================
619 void Engines_Component_i::endService(const char *serviceName)
621 _ThreadCpuUsed = CpuUsed_impl() ;
622 MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
623 << endl << " Component instance : " << _instanceName << " StartUsed "
624 << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
628 //=============================================================================
630 * C++ method: -- CHECK IF USED --
632 //=============================================================================
634 char* Engines_Component_i::graphName()
636 return CORBA::string_dup( _graphName.c_str() ) ;
639 //=============================================================================
641 * C++ method: -- CHECK IF USED --
643 //=============================================================================
645 char* Engines_Component_i::nodeName()
647 return CORBA::string_dup( _nodeName.c_str() ) ;
650 //=============================================================================
652 * C++ method: used in Supervision (see kill_impl)
654 //=============================================================================
656 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
666 if ( pthread_cancel( ThreadId ) )
668 perror("Killer pthread_cancel error") ;
673 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
674 << " pthread_canceled") ;
679 if ( pthread_kill( ThreadId , signum ) == -1 )
681 perror("Killer pthread_kill error") ;
686 MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
687 << " pthread_killed(" << signum << ")") ;
694 //=============================================================================
698 //=============================================================================
702 theEngines_Component->SetCurCpu() ;
705 //=============================================================================
709 //=============================================================================
711 void Engines_Component_i::SetCurCpu()
713 _ThreadCpuUsed = CpuUsed() ;
714 // MESSAGE(pthread_self() <<
715 // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
718 //=============================================================================
722 //=============================================================================
724 long Engines_Component_i::CpuUsed()
728 struct rusage usage ;
729 if ( _ThreadId || _Executed )
731 if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
733 perror("Engines_Component_i::CpuUsed") ;
736 cpu = usage.ru_utime.tv_sec - _StartUsed ;
737 // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
738 // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
739 // << " = " << cpu << endl ;
743 // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
744 // << _ThreadId << " " << _serviceName<< " _StartUsed "
745 // << _StartUsed << endl ;
748 // NOT implementet yet
755 //=============================================================================
757 * C++ method: Send message to event channel
759 //=============================================================================
761 void Engines_Component_i::sendMessage(const char *event_type,
764 _notifSupplier->Send(graphName(), nodeName(), event_type, message);
767 //=============================================================================
771 //=============================================================================
773 string Engines_Component_i::GetDynLibraryName(const char *componentName)
781 //=============================================================================
785 //=============================================================================
787 string Engines_Component_i::BuildComponentNameForNS(const char *ComponentName,
788 const char *ContainerName,
789 const char *hostname)
792 Engines_Container_i::BuildContainerNameForNS(ContainerName,hostname);
797 //=============================================================================
799 * C++ method: DumpPython default implementation
801 //=============================================================================
803 Engines::TMPFile* Engines_Component_i::DumpPython(CORBA::Object_ptr theStudy,
804 CORBA::Boolean isPublished,
805 CORBA::Boolean& isValidScript)
807 char* aScript = "def RebuildData(theStudy): pass";
808 char* aBuffer = new char[strlen(aScript)+1];
809 strcpy(aBuffer, aScript);
810 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
811 int aBufferSize = strlen(aBuffer)+1;
812 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
813 isValidScript = true;
814 return aStreamFile._retn();