]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/Component_i.cxx
Salome HOME
merge from BR_V51_BS 19/02/09
[modules/kernel.git] / src / Container / Component_i.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME Container : implementation of container and engine for Kernel
23 //  File   : Component_i.cxx
24 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
25 //  Module : SALOME
26 //  $Header$
27 //#define private protected  // for pd_refCount trace
28 //
29 #include "SALOME_Component_i.hxx"
30 #include "SALOME_Container_i.hxx"
31 #include "RegistryConnexion.hxx"
32 #include "Basics_Utils.hxx"
33 #include <stdio.h>
34 #ifndef WIN32
35 #include <dlfcn.h>
36 #endif
37 #include <cstdlib>
38 #include "utilities.h"
39
40 #ifndef WIN32
41 #include <sys/time.h>
42 #include <sys/resource.h>
43 #include <unistd.h>
44 #else
45 #include <sys/timeb.h>
46 int SIGUSR11 = 1000;
47 #include <process.h>
48 #endif
49
50
51 using namespace std;
52
53 extern bool _Sleeping ;
54 static Engines_Component_i * theEngines_Component ;
55
56 bool Engines_Component_i::_isMultiStudy = true;
57 bool Engines_Component_i::_isMultiInstance = false;
58
59 /*! \class Engines_Component_i
60  *  \brief C++ implementation of Engines::Component interface
61  *
62  */
63
64 //=============================================================================
65 /*! 
66  *  Default constructor, not for use
67  */
68 //=============================================================================
69
70 Engines_Component_i::Engines_Component_i():_myConnexionToRegistry(0), _notifSupplier(0)
71 {
72   //ASSERT(0);
73   MESSAGE("Default Constructor, not for normal use...");
74 }
75
76 //=============================================================================
77 /*!  \brief  Standard Constructor for generic Component, used in derived class
78  *
79  *  Connection to Registry and Notification
80  *  \param orb Object Request broker given by Container
81  *  \param poa Portable Object Adapter from Container (normally root_poa)
82  *  \param contId container CORBA id inside the server
83  *  \param instanceName unique instance name for this object (see Container_i)
84  *  \param interfaceName component class name
85  *  \param notif use of notification
86  *  \param regist (true or false) use of registry (default true)
87  */
88 //=============================================================================
89
90 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
91                                          PortableServer::POA_ptr poa, 
92                                          PortableServer::ObjectId * contId, 
93                                          const char *instanceName,
94                                          const char *interfaceName,
95                                          bool notif,
96                                          bool regist ) :
97   _instanceName(instanceName),
98   _interfaceName(interfaceName),
99   _myConnexionToRegistry(0),
100   _notifSupplier(0),
101   _ThreadId(0) ,
102   _ThreadCpuUsed(0) ,
103   _Executed(false) ,
104   _graphName("") ,
105   _nodeName(""),
106   _studyId(-1),
107   _CanceledThread(false)
108 {
109   MESSAGE("Component constructor with instanceName "<< _instanceName);
110   _orb = CORBA::ORB::_duplicate(orb);
111   _poa = PortableServer::POA::_duplicate(poa);
112   _contId = contId ;
113   CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
114   _container=Engines::Container::_narrow(o);
115
116   if(regist)
117     {
118       const CORBA::String_var ior = _orb->object_to_string(o);
119       _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession",
120                                                      _instanceName.c_str());
121     }
122
123   _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
124 }
125
126 //=============================================================================
127 /*!  \brief  Standard Constructor for standalone Component, used in derived class
128  *
129  *  Connection to Registry and Notification
130  *  \param orb Object Request broker given by Container
131  *  \param poa Portable Object Adapter from Container (normally root_poa)
132  *  \param container container CORBA reference
133  *  \param instanceName unique instance name for this object (see Container_i)
134  *  \param interfaceName component class name
135  *  \param notif use of notification
136  *  \param regist (true or false) use of registry (default true)
137  */
138 //=============================================================================
139
140 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
141                                          PortableServer::POA_ptr poa,
142                                          Engines::Container_ptr container,
143                                          const char *instanceName,
144                                          const char *interfaceName,
145                                          bool notif,
146                                          bool regist) :
147   _instanceName(instanceName),
148   _interfaceName(interfaceName),
149   _myConnexionToRegistry(0),
150   _notifSupplier(0),
151   _ThreadId(0) ,
152   _ThreadCpuUsed(0) ,
153   _Executed(false) ,
154   _graphName("") ,
155   _nodeName(""),
156   _studyId(-1),
157   _CanceledThread(false)
158 {
159   MESSAGE("Component constructor with instanceName "<< _instanceName);
160   _orb = CORBA::ORB::_duplicate(orb);
161   _poa = PortableServer::POA::_duplicate(poa);
162   _container=Engines::Container::_duplicate(container);
163   try
164     {
165       _contId=_poa->reference_to_id(container);
166     }
167   catch(PortableServer::POA::WrongAdapter)
168     {
169       //not created by this poa
170       _contId = 0;
171     }
172   const CORBA::String_var ior = _orb->object_to_string(_container);
173   if(regist)
174     _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession", _instanceName.c_str());
175   _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
176 }
177
178
179 //=============================================================================
180 /*! 
181  *  Destructor: call Container for decrement of instances count.
182  *  When instances count falls to 0, the container tries to remove the
183  *  component library (dlclose)
184  */
185 //=============================================================================
186
187 Engines_Component_i::~Engines_Component_i()
188 {
189   MESSAGE("Component destructor");
190   Engines_Container_i::decInstanceCnt(_interfaceName);
191   if(_myConnexionToRegistry)delete _myConnexionToRegistry;
192   if(_notifSupplier)delete _notifSupplier;
193 }
194
195 //=============================================================================
196 /*! 
197  *  CORBA method: return name of the instance, unique in this Container
198  */
199 //=============================================================================
200
201 char* Engines_Component_i::instanceName()
202 {
203    return CORBA::string_dup(_instanceName.c_str()) ;
204 }
205
206 //=============================================================================
207 /*! 
208  *  CORBA method: return name of the component class
209  */
210 //=============================================================================
211
212 char* Engines_Component_i::interfaceName()
213 {
214   return CORBA::string_dup(_interfaceName.c_str()) ;
215 }
216
217 //=============================================================================
218 /*! 
219  *  CORBA method: Get study Id
220  *  \return -1: not initialised (Internal Error)
221  *           0: multistudy component instance
222  *          >0: study id associated to this instance
223  */
224 //=============================================================================
225
226 CORBA::Long Engines_Component_i::getStudyId()
227 {
228   return _studyId;
229 }
230
231 //=============================================================================
232 /*! 
233  *  CORBA method: Test if instance is alive and responds
234  */
235 //=============================================================================
236
237 void Engines_Component_i::ping()
238 {
239 #ifndef WIN32
240   MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
241           << pthread_self());
242 #else
243   MESSAGE("Engines_Component_i::ping() pid "<< _getpid()<< " threadid "
244           << pthread_self().p );
245 #endif
246 }
247
248 //=============================================================================
249 /*! 
250  *  CORBA method: Deactivate this instance. CORBA object is deactivated (do not
251  *  respond any more to CORBA calls), the connection to Regsitry is removed
252  *  (Registry informed of deactivation), internal server reference counter on
253  *  the derived servant class is decremented, to allow destruction of the class
254  *  (delete) by POA, when there are no more references.
255  *  -- TO BE USED BY CONTAINER ONLY (Container housekeeping) --
256  */
257 //=============================================================================
258
259 void Engines_Component_i::destroy()
260 {
261   MESSAGE("Engines_Component_i::destroy()");
262   //SCRUTE(pd_refCount);
263
264   if(_myConnexionToRegistry)delete _myConnexionToRegistry;
265   _myConnexionToRegistry = 0 ;
266   if(_notifSupplier)delete _notifSupplier;
267   _notifSupplier = 0;
268   if(_id)
269     delete(_id) ;
270   //SCRUTE(pd_refCount);
271   _thisObj->_remove_ref();
272   //SCRUTE(pd_refCount);
273   MESSAGE("Engines_Component_i::destroyed") ;
274 }
275
276 //=============================================================================
277 /*! 
278  *  CORBA method: return CORBA reference of the Container
279  *
280  */
281 //=============================================================================
282
283 Engines::Container_ptr Engines_Component_i::GetContainerRef()
284 {
285   return Engines::Container::_duplicate(_container);
286 }
287
288 //=============================================================================
289 /*! 
290  *  CORBA method: 
291  *  Gives a sequence of (key=string,value=any) to the component. 
292  *  Base class component stores the sequence in a map.
293  *  The map is cleared before.
294  *  This map is for use by derived classes. 
295  *  \param dico sequence of (key=string,value=any)
296  */
297 //=============================================================================
298
299 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
300 {
301   _fieldsDict.clear();
302   for (CORBA::ULong i=0; i<dico.length(); i++)
303     {
304       std::string cle(dico[i].key);
305       _fieldsDict[cle] = dico[i].value;
306     }
307 }
308
309 //=============================================================================
310 /*! 
311  *  CORBA method: 
312  *  returns a previously stored map (key=string,value=any) as a sequence.
313  *  (see setProperties)
314  */
315 //=============================================================================
316
317 Engines::FieldsDict* Engines_Component_i::getProperties()
318 {
319   Engines::FieldsDict_var copie = new Engines::FieldsDict;
320   copie->length(_fieldsDict.size());
321   map<std::string,CORBA::Any>::iterator it;
322   CORBA::ULong i = 0;
323   for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
324     {
325       std::string cle((*it).first);
326       copie[i].key = CORBA::string_dup(cle.c_str());
327       copie[i].value = _fieldsDict[cle];
328     }
329   return copie._retn();
330 }
331
332 //=============================================================================
333 /*! 
334  *  CORBA method: used by Supervision to give names to this instance
335  */
336 //=============================================================================
337
338 void Engines_Component_i::Names( const char * graphName ,
339                                  const char * nodeName )
340 {
341   _graphName = graphName ;
342   _nodeName = nodeName ;
343   //  MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
344   //          << _nodeName << "' )");
345 }
346
347 //=============================================================================
348 /*! 
349  *  CORBA method: used in Supervision
350  */
351 //=============================================================================
352
353 bool Engines_Component_i::Kill_impl() 
354 {
355 //  MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
356 //          << " pid " << getpid() << " instanceName "
357 //          << _instanceName.c_str() << " interface " << _interfaceName.c_str()
358 //          << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
359 //          << dec << " _ThreadId " << _ThreadId << " this " << hex << this
360 //          << dec ) ;
361
362   bool RetVal = false ;
363 #ifndef WIN32
364   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
365     {
366       RetVal = Killer( _ThreadId , SIGUSR2 ) ;
367       _ThreadId = (pthread_t ) -1 ;
368     }
369
370 #else
371   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
372     {
373       RetVal = Killer( *_ThreadId , 0 ) ;
374       _ThreadId = (pthread_t* ) 0 ;
375     }
376
377 #endif
378   return RetVal ;
379 }
380
381 //=============================================================================
382 /*! 
383  *  CORBA method: used in Supervision
384  */
385 //=============================================================================
386
387 bool Engines_Component_i::Stop_impl()
388 {
389 #ifndef WIN32
390   MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
391           << " pid " << getpid() << " instanceName "
392           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
393           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
394           << dec << " _ThreadId " << _ThreadId );
395 #else
396   MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self().p
397           << " pid " << _getpid() << " instanceName "
398           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
399           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
400           << dec << " _ThreadId " << _ThreadId );
401 #endif
402   
403
404   bool RetVal = false ;
405 #ifndef WIN32
406   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
407     {
408       RetVal = Killer( _ThreadId , 0 ) ;
409       _ThreadId = (pthread_t ) -1 ;
410     }
411 #else
412   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
413     {
414       RetVal = Killer( *_ThreadId , 0 ) ;
415       _ThreadId = (pthread_t* ) 0 ;
416     }
417 #endif
418   return RetVal ;
419 }
420
421 //=============================================================================
422 /*! 
423  *  CORBA method: used in Supervision
424  */
425 //=============================================================================
426
427 bool Engines_Component_i::Suspend_impl()
428 {
429 #ifndef WIN32
430   MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
431           << " pid " << getpid() << " instanceName "
432           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
433           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
434           << dec << " _ThreadId " << _ThreadId );
435 #else
436   MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self().p
437           << " pid " << _getpid() << " instanceName "
438           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
439           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
440           << dec << " _ThreadId " << _ThreadId );
441 #endif
442
443   bool RetVal = false ;
444 #ifndef WIN32
445   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
446 #else
447   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
448 #endif
449     {
450       if ( _Sleeping )
451         {
452           return false ;
453         }
454     else 
455       {
456 #ifndef WIN32
457         RetVal = Killer( _ThreadId ,SIGINT ) ;
458 #else
459         RetVal = Killer( *_ThreadId ,SIGINT ) ;
460 #endif
461         //if ( RetVal ) _Sleeping = true;
462
463       }
464     }
465   return RetVal ;
466 }
467
468 //=============================================================================
469 /*! 
470  *  CORBA method: used in Supervision
471  */
472 //=============================================================================
473
474 bool Engines_Component_i::Resume_impl()
475 {
476 #ifndef WIN32
477   MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
478           << " pid " << getpid() << " instanceName "
479           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
480           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
481           << dec << " _ThreadId " << _ThreadId );
482 #else
483   MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self().p
484           << " pid " << _getpid() << " instanceName "
485           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
486           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
487           << dec << " _ThreadId " << _ThreadId );
488 #endif
489   bool RetVal = false ;
490 #ifndef WIN32
491   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
492 #else
493   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
494 #endif
495     {
496     if ( _Sleeping ) 
497       {
498         _Sleeping = false ;
499         RetVal = true ;
500       }
501     else
502       {
503         RetVal = false ;
504       }
505     }
506   return RetVal ;
507 }
508
509 //=============================================================================
510 /*! 
511  *  CORBA method: 
512  */
513 //=============================================================================
514
515 CORBA::Long Engines_Component_i::CpuUsed_impl()
516 {
517   long cpu = 0 ;
518   if ( _ThreadId || _Executed )
519     {
520     if ( _ThreadId > 0 )
521       {
522 #ifndef WIN32
523       if ( pthread_self() != _ThreadId )
524 #else
525       if ( pthread_self().p != _ThreadId->p )
526 #endif
527         {
528         if ( _Sleeping )
529           {
530           }
531         else
532           {
533             // Get Cpu in the appropriate thread with that object !...
534             theEngines_Component = this ;
535 #ifndef WIN32
536             Killer( _ThreadId ,SIGUSR1 ) ;
537 #else
538             Killer( *_ThreadId ,SIGUSR11 ) ;
539 #endif
540           }
541         cpu = _ThreadCpuUsed ;
542         }
543       else
544         {
545           _ThreadCpuUsed = CpuUsed() ;
546           cpu = _ThreadCpuUsed ;
547           // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
548           //      << _serviceName << " " << cpu << endl ;
549       }
550     }
551     else 
552       {
553         cpu = _ThreadCpuUsed ;
554         // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
555         //      << _serviceName << " " << cpu<< endl ;
556       }
557     }
558   else
559     {
560       // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
561       //     <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
562     }
563   return cpu ;
564 }
565
566
567 //=============================================================================
568 /*! 
569  *  C++ method: return Container Servant
570  */
571 //=============================================================================
572
573 Engines_Container_i *Engines_Component_i::GetContainerPtr()
574 {
575   return dynamic_cast<Engines_Container_i*>(_poa->id_to_servant(*_contId)) ;
576 }
577
578 //=============================================================================
579 /*! 
580  *  C++ method: set study Id
581  *  \param studyId         0 if instance is not associated to a study, 
582  *                         >0 otherwise (== study id)
583  *  \return true if the set of study Id is OK
584  *  must be set once by Container, at instance creation,
585  *  and cannot be changed after.
586  */
587 //=============================================================================
588
589 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
590 {
591   ASSERT( studyId >= 0);
592   CORBA::Boolean ret = false;
593   if (_studyId < 0) // --- not yet initialized 
594     {
595       _studyId = studyId;
596       ret = true;
597     }
598   else
599     if ( _studyId == studyId) ret = true;
600   return ret;
601 }
602
603 //=============================================================================
604 /*! 
605  *  C++ method: return CORBA instance id, the id is set in derived class
606  *  constructor, when instance is activated.
607  */
608 //=============================================================================
609
610 PortableServer::ObjectId * Engines_Component_i::getId()
611 {
612 //  MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
613   return _id ;
614 }
615
616 //=============================================================================
617 /*! 
618  *  C++ method: used by derived classes for supervision
619  */
620 //=============================================================================
621
622 void Engines_Component_i::beginService(const char *serviceName)
623 {
624 #ifndef WIN32
625   MESSAGE(pthread_self() << "Send BeginService notification for " <<serviceName
626           << endl << "Component instance : " << _instanceName << endl << endl);
627 #else
628   MESSAGE(pthread_self().p << "Send BeginService notification for " <<serviceName
629           << endl << "Component instance : " << _instanceName << endl << endl);
630 #endif
631 #ifndef WIN32
632   _ThreadId = pthread_self() ;
633 #else
634   _ThreadId = new pthread_t;
635   _ThreadId->p = pthread_self().p ;
636   _ThreadId->x = pthread_self().x ;
637 #endif
638   _StartUsed = 0 ;
639   _StartUsed = CpuUsed_impl() ;
640   _ThreadCpuUsed = 0 ;
641   _Executed = true ;
642   _serviceName = serviceName ;
643   theEngines_Component = this ;
644   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
645     {
646       perror("pthread_setcanceltype ") ;
647       exit(0) ;
648     }
649   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
650     {
651       perror("pthread_setcancelstate ") ;
652       exit(0) ;
653     }
654 //  MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
655 //          << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
656 //          << " _graphName " << _graphName << " _nodeName " << _nodeName );
657
658   // --- for supervisor : all strings given with setProperties
659   //     are set in environment
660   bool overwrite = true;
661   map<std::string,CORBA::Any>::iterator it;
662   for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
663     {
664       std::string cle((*it).first);
665       if ((*it).second.type()->kind() == CORBA::tk_string)
666         {
667           const char* value;
668           (*it).second >>= value;
669           // ---todo: replace __GNUC__ test by an autoconf macro AC_CHECK_FUNC.
670 #if defined __GNUC__
671 //        int ret = setenv(cle.c_str(), value, overwrite);
672           setenv(cle.c_str(), value, overwrite);
673 #else
674           //CCRT porting : setenv not defined in stdlib.h
675           std::string s(cle);
676           s+='=';
677           s+=value;
678           // char* cast because 1st arg of linux putenv function
679           // is not a const char* !
680 //        int ret=putenv((char *)s.c_str());
681           putenv((char *)s.c_str());
682           //End of CCRT porting
683 #endif
684           MESSAGE("--- setenv: "<<cle<<" = "<< value);
685         }
686     }
687 }
688
689 //=============================================================================
690 /*! 
691  *  C++ method: used by derived classes for supervision
692  */
693 //=============================================================================
694
695 void Engines_Component_i::endService(const char *serviceName)
696 {
697   if ( !_CanceledThread )
698     _ThreadCpuUsed = CpuUsed_impl() ;
699
700 #ifndef WIN32
701   MESSAGE(pthread_self() << " Send EndService notification for " << serviceName
702           << endl << " Component instance : " << _instanceName << " StartUsed "
703           << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
704 #else
705   MESSAGE(pthread_self().p << " Send EndService notification for " << serviceName
706           << endl << " Component instance : " << _instanceName << " StartUsed "
707     << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << endl <<endl);
708 #endif
709   _ThreadId = 0 ;
710 }
711
712 //=============================================================================
713 /*! 
714  *  C++ method: -- CHECK IF USED --
715  */
716 //=============================================================================
717
718 char* Engines_Component_i::graphName()
719 {
720   return CORBA::string_dup( _graphName.c_str() ) ;
721 }
722
723 //=============================================================================
724 /*! 
725  *  C++ method: -- CHECK IF USED --
726  */
727 //=============================================================================
728
729 char* Engines_Component_i::nodeName()
730 {
731   return CORBA::string_dup( _nodeName.c_str() ) ;
732 }
733
734 //=============================================================================
735 /*! 
736  *  C++ method: used in Supervision (see kill_impl)
737  */
738 //=============================================================================
739
740 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
741 {
742 #ifndef WIN32
743   if ( ThreadId )
744 #else
745   if ( ThreadId.p )
746 #endif
747     {
748       if ( signum == 0 )
749         {
750           if ( pthread_cancel( ThreadId ) )
751             {
752               perror("Killer pthread_cancel error") ;
753               return false ;
754             }
755           else
756             {
757 #ifndef WIN32
758               MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
759                       << " pthread_canceled") ;
760 #else
761         MESSAGE(pthread_self().p << "Killer : ThreadId " << ThreadId.p
762                       << " pthread_canceled") ;
763 #endif
764             }
765         }
766       else
767         {
768           if ( pthread_kill( ThreadId , signum ) == -1 )
769             {
770               perror("Killer pthread_kill error") ;
771               return false ;
772             }
773           else 
774             {
775 #ifndef WIN32
776         MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId
777                       << " pthread_killed(" << signum << ")") ;
778 #else
779         MESSAGE(pthread_self().p << "Killer : ThreadId " << ThreadId.p
780                       << " pthread_killed(" << signum << ")") ;
781 #endif
782             }
783         }
784     }
785   return true ;
786 }
787
788 //=============================================================================
789 /*! 
790  *  C++ method:
791  */ 
792 //=============================================================================
793
794 void SetCpuUsed()
795 {
796   if ( theEngines_Component )
797     theEngines_Component->SetCurCpu() ;
798 }
799
800 //=============================================================================
801 /*! 
802  *  C++ method:
803  */
804 //=============================================================================
805
806 void Engines_Component_i::SetCurCpu()
807 {
808   _ThreadCpuUsed =  CpuUsed() ;
809   //  MESSAGE(pthread_self() << 
810   //  " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
811 }
812
813 //=============================================================================
814 /*! 
815  *  C++ method:
816  */
817 //=============================================================================
818
819 long Engines_Component_i::CpuUsed()
820 {
821   long cpu = 0 ;
822 #ifndef WIN32
823   struct rusage usage ;
824   if ( _ThreadId || _Executed )
825     {
826       if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
827         {
828           perror("Engines_Component_i::CpuUsed") ;
829           return 0 ;
830         }
831       cpu = usage.ru_utime.tv_sec - _StartUsed ;
832       // cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
833       //      << _serviceName   << usage.ru_utime.tv_sec << " - " << _StartUsed
834       //      << " = " << cpu << endl ;
835     }
836   else
837     {
838       // cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
839       //      << _ThreadId << " " << _serviceName<< " _StartUsed " 
840       //      << _StartUsed << endl ;
841     }
842 #else
843         // NOT implementet yet
844 #endif
845
846
847   return cpu ;
848 }
849
850 void CallCancelThread()
851 {
852   if ( theEngines_Component )
853     theEngines_Component->CancelThread() ;
854 }
855
856 //=============================================================================
857 /*!
858  *  C++ method:
859  */
860 //=============================================================================
861
862 void Engines_Component_i::CancelThread()
863 {
864   _CanceledThread = true;
865 }
866
867 //=============================================================================
868 /*! 
869  *  C++ method: Send message to event channel
870  */
871 //=============================================================================
872
873 void Engines_Component_i::sendMessage(const char *event_type,
874                                       const char *message)
875 {
876     _notifSupplier->Send(_graphName.c_str(), _nodeName.c_str(), event_type, message);
877 }
878
879 //=============================================================================
880 /*! 
881  *  C++ method: return standard library name built on component name
882  */
883 //=============================================================================
884
885 string Engines_Component_i::GetDynLibraryName(const char *componentName)
886 {
887   string ret="lib";
888   ret+=componentName;
889   ret+="Engine.so";
890   return ret;
891 }
892
893 //=============================================================================
894 /*! 
895  *  C++ method: DumpPython default implementation
896  */
897 //=============================================================================
898
899 Engines::TMPFile* Engines_Component_i::DumpPython(CORBA::Object_ptr theStudy, 
900                                                   CORBA::Boolean isPublished, 
901                                                   CORBA::Boolean& isValidScript)
902 {
903   const char* aScript = "def RebuildData(theStudy): pass";
904   char* aBuffer = new char[strlen(aScript)+1];
905   strcpy(aBuffer, aScript);
906   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
907   int aBufferSize = strlen(aBuffer)+1;
908   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1); 
909   isValidScript = true;
910   return aStreamFile._retn(); 
911 }
912
913 Engines::Salome_file_ptr 
914 Engines_Component_i::getInputFileToService(const char* service_name, 
915                                            const char* Salome_file_name) 
916 {
917   // Try to find the service, if it doesn't exist, we throw an exception.
918   _Service_file_map_it = _Input_Service_file_map.find(service_name);
919   if (_Service_file_map_it ==  _Input_Service_file_map.end()) {
920     SALOME::ExceptionStruct es;
921     es.type = SALOME::INTERNAL_ERROR;
922     es.text = "service doesn't have salome files";
923     throw SALOME::SALOME_Exception(es);
924   }
925   _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
926
927   // Try to find the Salome_file ...
928   _Salome_file_map_it = _map->find(Salome_file_name);
929   if (_Salome_file_map_it ==  _map->end()) {
930     SALOME::ExceptionStruct es;
931     es.type = SALOME::INTERNAL_ERROR;
932     es.text = "service doesn't have this Salome_file";
933     throw SALOME::SALOME_Exception(es);
934   }
935   Salome_file_i * Sfile = (*_map)[Salome_file_name];
936
937   return Sfile->_this();
938 }
939
940 Engines::Salome_file_ptr 
941 Engines_Component_i::setInputFileToService(const char* service_name, 
942                                            const char* Salome_file_name) 
943 {
944   // Try to find the service, if it doesn't exist, we add it.
945   _Service_file_map_it = _Input_Service_file_map.find(service_name);
946   if (_Service_file_map_it ==  _Input_Service_file_map.end()) {
947     _t_Salome_file_map * _map = new _t_Salome_file_map();
948     _Input_Service_file_map[service_name] = _map;
949   }
950   _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
951   
952   // Try to find the Salome_file ...
953   _Salome_file_map_it = _map->find(Salome_file_name);
954   if (_Salome_file_map_it ==  _map->end()) {
955     Salome_file_i * Sfile = new Salome_file_i();
956     Engines::Container_ptr container = this->GetContainerRef();
957     Sfile->setContainer(Engines::Container::_duplicate(container));
958     (*_map)[Salome_file_name] = Sfile;
959   }
960
961   Salome_file_i * Sfile = (*_map)[Salome_file_name];
962   return Sfile->_this();
963 }
964
965 void 
966 Engines_Component_i::checkInputFilesToService(const char* service_name) 
967 {
968   // Try to find the service, if it doesn't exist, nothing to do.
969   _Service_file_map_it = _Input_Service_file_map.find(service_name);
970   if (_Service_file_map_it !=  _Input_Service_file_map.end()) {
971     _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
972     _t_Salome_file_map::iterator begin = _map->begin();
973     _t_Salome_file_map::iterator end = _map->end();
974
975     for(;begin!=end;begin++) {
976       Salome_file_i * file = begin->second;
977       std::string file_port_name = begin->first;
978       configureSalome_file(service_name, file_port_name, file);
979       file->recvFiles();
980     }
981   }
982 }
983
984 Engines::Salome_file_ptr 
985 Engines_Component_i::getOutputFileToService(const char* service_name, 
986                                             const char* Salome_file_name) 
987 {
988   // Try to find the service, if it doesn't exist, we throw an exception.
989   _Service_file_map_it = _Output_Service_file_map.find(service_name);
990   if (_Service_file_map_it ==  _Output_Service_file_map.end()) {
991     SALOME::ExceptionStruct es;
992     es.type = SALOME::INTERNAL_ERROR;
993     es.text = "service doesn't have salome files";
994     throw SALOME::SALOME_Exception(es);
995   }
996   _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
997
998   // Try to find the Salome_file ...
999   _Salome_file_map_it = _map->find(Salome_file_name);
1000   if (_Salome_file_map_it ==  _map->end()) {
1001     SALOME::ExceptionStruct es;
1002     es.type = SALOME::INTERNAL_ERROR;
1003     es.text = "service doesn't have this Salome_file";
1004     throw SALOME::SALOME_Exception(es);
1005   }
1006   Salome_file_i * Sfile = (*_map)[Salome_file_name];
1007
1008   return Sfile->_this();
1009 }
1010
1011 Engines::Salome_file_ptr 
1012 Engines_Component_i::setOutputFileToService(const char* service_name, 
1013                                            const char* Salome_file_name) 
1014 {
1015   // Try to find the service, if it doesn't exist, we add it.
1016   _Service_file_map_it = _Output_Service_file_map.find(service_name);
1017   if (_Service_file_map_it ==  _Output_Service_file_map.end()) {
1018     _t_Salome_file_map * _map = new _t_Salome_file_map();
1019     _Output_Service_file_map[service_name] = _map;
1020   }
1021   _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
1022   
1023   // Try to find the Salome_file ...
1024   _Salome_file_map_it = _map->find(Salome_file_name);
1025   if (_Salome_file_map_it ==  _map->end()) {
1026     Salome_file_i * Sfile = new Salome_file_i();
1027     Engines::Container_ptr container = this->GetContainerRef();
1028     Sfile->setContainer(Engines::Container::_duplicate(container));
1029     (*_map)[Salome_file_name] = Sfile;
1030   }
1031
1032   Salome_file_i * Sfile = (*_map)[Salome_file_name];
1033   return Sfile->_this();
1034 }
1035
1036 void 
1037 Engines_Component_i::checkOutputFilesToService(const char* service_name) 
1038 {
1039   // Try to find the service, if it doesn't exist, nothing to do.
1040   _Service_file_map_it = _Output_Service_file_map.find(service_name);
1041   if (_Service_file_map_it !=  _Output_Service_file_map.end()) {
1042     _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
1043     _t_Salome_file_map::iterator begin = _map->begin();
1044     _t_Salome_file_map::iterator end = _map->end();
1045
1046     for(;begin!=end;begin++) {
1047       Salome_file_i * file = begin->second;
1048       std::string file_port_name = begin->first;
1049       configureSalome_file(service_name, file_port_name, file);
1050       file->recvFiles();
1051     }
1052   }
1053
1054 }
1055
1056 //=============================================================================
1057 /*! 
1058  *  C++ method: used to configure the Salome_file into the runtime.
1059  *  \param service_name name of the service that use this Salome_file
1060  *  \param file_port_name name of the Salome_file
1061  *  \param file Salome_file C++ object
1062  */
1063 //=============================================================================
1064 void
1065 Engines_Component_i::configureSalome_file(std::string service_name,
1066                                           std::string file_port_name,
1067                                           Salome_file_i * file) 
1068 {
1069   // By default this method does nothing
1070 }
1071