Back to index

salome-kernel  6.5.0
Component_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Container : implementation of container and engine for Kernel
00024 //  File   : Component_i.cxx
00025 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
00026 //  Module : SALOME
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/Component_i.cxx,v 1.15.2.13.6.4.2.1 2012-04-12 14:05:03 vsr Exp $
00028 //#define private protected  // for pd_refCount trace
00029 //
00030 #include "SALOME_Component_i.hxx"
00031 #include "SALOME_Container_i.hxx"
00032 #include "RegistryConnexion.hxx"
00033 #include "Basics_Utils.hxx"
00034 #include <stdio.h>
00035 #ifndef WIN32
00036 #include <dlfcn.h>
00037 #endif
00038 #include <cstdlib>
00039 #include "utilities.h"
00040 
00041 #ifndef WIN32
00042 #include <sys/time.h>
00043 #include <sys/resource.h>
00044 #include <unistd.h>
00045 #else
00046 #include <sys/timeb.h>
00047 int SIGUSR11 = 1000;
00048 #include <process.h>
00049 #endif
00050 
00051 extern bool _Sleeping ;
00052 static Engines_Component_i * theEngines_Component ;
00053 
00054 bool Engines_Component_i::_isMultiStudy = true;
00055 bool Engines_Component_i::_isMultiInstance = false;
00056 
00062 //=============================================================================
00066 //=============================================================================
00067 
00068 Engines_Component_i::Engines_Component_i():_myConnexionToRegistry(0), _notifSupplier(0), _id(0)
00069 {
00070   //ASSERT(0);
00071   MESSAGE("Default Constructor, not for normal use...");
00072 }
00073 
00074 //=============================================================================
00086 //=============================================================================
00087 
00088 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
00089                                          PortableServer::POA_ptr poa, 
00090                                          PortableServer::ObjectId * contId, 
00091                                          const char *instanceName,
00092                                          const char *interfaceName,
00093                                          bool notif,
00094                                          bool regist ) :
00095   _instanceName(instanceName),
00096   _interfaceName(interfaceName),
00097   _myConnexionToRegistry(0),
00098   _notifSupplier(0),
00099   _ThreadId(0) ,
00100   _ThreadCpuUsed(0) ,
00101   _Executed(false) ,
00102   _graphName("") ,
00103   _nodeName(""),
00104   _studyId(-1),
00105   _id(0),
00106   _contId(0),
00107   _CanceledThread(false)
00108 {
00109   MESSAGE("Component constructor with instanceName "<< _instanceName);
00110   _orb = CORBA::ORB::_duplicate(orb);
00111   _poa = PortableServer::POA::_duplicate(poa);
00112   _contId = contId ;
00113   CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
00114   _container=Engines::Container::_narrow(o);
00115   setContainerName();
00116 
00117   if(regist)
00118     {
00119       const CORBA::String_var ior = _orb->object_to_string(o);
00120       _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession",
00121                                                      _instanceName.c_str());
00122     }
00123 
00124   if(notif)
00125     _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
00126 }
00127 
00128 //=============================================================================
00140 //=============================================================================
00141 
00142 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
00143                                          PortableServer::POA_ptr poa,
00144                                          Engines::Container_ptr container,
00145                                          const char *instanceName,
00146                                          const char *interfaceName,
00147                                          bool notif,
00148                                          bool regist) :
00149   _instanceName(instanceName),
00150   _interfaceName(interfaceName),
00151   _myConnexionToRegistry(0),
00152   _notifSupplier(0),
00153   _ThreadId(0) ,
00154   _ThreadCpuUsed(0) ,
00155   _Executed(false) ,
00156   _graphName("") ,
00157   _nodeName(""),
00158   _studyId(-1),
00159   _id(0),
00160   _contId(0),
00161   _CanceledThread(false)
00162 {
00163   MESSAGE("Component constructor with instanceName "<< _instanceName);
00164   _orb = CORBA::ORB::_duplicate(orb);
00165   _poa = PortableServer::POA::_duplicate(poa);
00166   _container=Engines::Container::_duplicate(container);
00167   setContainerName();
00168   const CORBA::String_var ior = _orb->object_to_string(_container);
00169   if(regist)
00170     _myConnexionToRegistry = new RegistryConnexion(0, 0, ior,"theSession", _instanceName.c_str());
00171   if(notif)
00172     _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
00173 
00174 }
00175 
00176 
00177 //=============================================================================
00183 //=============================================================================
00184 
00185 Engines_Component_i::~Engines_Component_i()
00186 {
00187   MESSAGE("Component destructor");
00188   Engines_Container_i::decInstanceCnt(_interfaceName);
00189   if(_myConnexionToRegistry)delete _myConnexionToRegistry;
00190   _myConnexionToRegistry = 0 ;
00191 
00192   if(_id) delete _id;
00193   _id=0;
00194 
00195   if(_notifSupplier)
00196     {
00197       SCRUTE(_notifSupplier->_refcount_value());
00198       PortableServer::POA_var poa=_notifSupplier->_default_POA();
00199       PortableServer::ObjectId_var anObjectId = poa->servant_to_id(_notifSupplier);
00200       poa->deactivate_object(anObjectId.in());
00201       SCRUTE(_notifSupplier->_refcount_value());
00202       _notifSupplier->_remove_ref();
00203     }
00204 }
00205 
00206 //=============================================================================
00210 //=============================================================================
00211 
00212 char* Engines_Component_i::instanceName()
00213 {
00214    return CORBA::string_dup(_instanceName.c_str()) ;
00215 }
00216 
00217 //=============================================================================
00221 //=============================================================================
00222 
00223 char* Engines_Component_i::interfaceName()
00224 {
00225   return CORBA::string_dup(_interfaceName.c_str()) ;
00226 }
00227 
00228 //=============================================================================
00235 //=============================================================================
00236 
00237 CORBA::Long Engines_Component_i::getStudyId()
00238 {
00239   return _studyId;
00240 }
00241 
00242 //=============================================================================
00246 //=============================================================================
00247 
00248 void Engines_Component_i::ping()
00249 {
00250 #ifndef WIN32
00251   MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
00252           << pthread_self());
00253 #else
00254   MESSAGE("Engines_Component_i::ping() pid "<< _getpid()<< " threadid "
00255           << pthread_self().p );
00256 #endif
00257 }
00258 
00259 //=============================================================================
00268 //=============================================================================
00269 
00270 void Engines_Component_i::destroy()
00271 {
00272   MESSAGE("Engines_Component_i::destroy()");
00273   //SCRUTE(_refcount_value());
00274   _poa->deactivate_object(*_id);
00275   //SCRUTE(_refcount_value());
00276   _remove_ref();
00277   //SCRUTE(_refcount_value());
00278   MESSAGE("Engines_Component_i::destroyed") ;
00279 }
00280 
00281 //=============================================================================
00286 //=============================================================================
00287 
00288 Engines::Container_ptr Engines_Component_i::GetContainerRef()
00289 {
00290   return Engines::Container::_duplicate(_container);
00291 }
00292 
00293 //=============================================================================
00302 //=============================================================================
00303 
00304 void Engines_Component_i::setProperties(const Engines::FieldsDict& dico)
00305 {
00306   _fieldsDict.clear();
00307   for (CORBA::ULong i=0; i<dico.length(); i++)
00308     {
00309       std::string cle(dico[i].key);
00310       _fieldsDict[cle] = dico[i].value;
00311     }
00312 }
00313 
00314 //=============================================================================
00320 //=============================================================================
00321 
00322 Engines::FieldsDict* Engines_Component_i::getProperties()
00323 {
00324   Engines::FieldsDict_var copie = new Engines::FieldsDict;
00325   copie->length(_fieldsDict.size());
00326   std::map<std::string,CORBA::Any>::iterator it;
00327   CORBA::ULong i = 0;
00328   for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++, i++)
00329     {
00330       std::string cle((*it).first);
00331       copie[i].key = CORBA::string_dup(cle.c_str());
00332       copie[i].value = _fieldsDict[cle];
00333     }
00334   return copie._retn();
00335 }
00336 
00337 //=============================================================================
00342 //=============================================================================
00343 
00344 void Engines_Component_i::SetOption(const char*, const char*)
00345 {
00346 }
00347 
00348 //=============================================================================
00353 //=============================================================================
00354 
00355 char* Engines_Component_i::GetOption(const char*)
00356 {
00357   return CORBA::string_dup("") ;
00358 }
00359 
00360 //=============================================================================
00364 //=============================================================================
00365 
00366 void Engines_Component_i::Names( const char * graphName ,
00367                                  const char * nodeName )
00368 {
00369   _graphName = graphName ;
00370   _nodeName = nodeName ;
00371   //  MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
00372   //          << _nodeName << "' )");
00373 }
00374 
00375 //=============================================================================
00379 //=============================================================================
00380 
00381 bool Engines_Component_i::Kill_impl() 
00382 {
00383 //  MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
00384 //          << " pid " << getpid() << " instanceName "
00385 //          << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00386 //          << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << hex << _id
00387 //          << dec << " _ThreadId " << _ThreadId << " this " << hex << this
00388 //          << dec ) ;
00389 
00390   bool RetVal = false ;
00391 #ifndef WIN32
00392   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
00393     {
00394       RetVal = Killer( _ThreadId , SIGUSR2 ) ;
00395       _ThreadId = (pthread_t ) -1 ;
00396     }
00397 
00398 #else
00399   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
00400     {
00401       RetVal = Killer( *_ThreadId , 0 ) ;
00402       _ThreadId = (pthread_t* ) 0 ;
00403     }
00404 
00405 #endif
00406   return RetVal ;
00407 }
00408 
00409 //=============================================================================
00413 //=============================================================================
00414 
00415 bool Engines_Component_i::Stop_impl()
00416 {
00417 #ifndef WIN32
00418   MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
00419           << " pid " << getpid() << " instanceName "
00420           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00421           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << std::hex << _id
00422           << std::dec << " _ThreadId " << _ThreadId );
00423 #else
00424   MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self().p
00425           << " pid " << _getpid() << " instanceName "
00426           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00427           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << std::hex << _id
00428           << std::dec << " _ThreadId " << _ThreadId );
00429 #endif
00430   
00431 
00432   bool RetVal = false ;
00433 #ifndef WIN32
00434   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
00435     {
00436       RetVal = Killer( _ThreadId , 0 ) ;
00437       _ThreadId = (pthread_t ) -1 ;
00438     }
00439 #else
00440   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
00441     {
00442       RetVal = Killer( *_ThreadId , 0 ) ;
00443       _ThreadId = (pthread_t* ) 0 ;
00444     }
00445 #endif
00446   return RetVal ;
00447 }
00448 
00449 //=============================================================================
00453 //=============================================================================
00454 
00455 bool Engines_Component_i::Suspend_impl()
00456 {
00457 #ifndef WIN32
00458   MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
00459           << " pid " << getpid() << " instanceName "
00460           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00461           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << std::hex << _id
00462           << std::dec << " _ThreadId " << _ThreadId );
00463 #else
00464   MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self().p
00465           << " pid " << _getpid() << " instanceName "
00466           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00467           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << std::hex << _id
00468           << std::dec << " _ThreadId " << _ThreadId );
00469 #endif
00470 
00471   bool RetVal = false ;
00472 #ifndef WIN32
00473   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
00474 #else
00475   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
00476 #endif
00477     {
00478       if ( _Sleeping )
00479         {
00480           return false ;
00481         }
00482     else 
00483       {
00484 #ifndef WIN32
00485         RetVal = Killer( _ThreadId ,SIGINT ) ;
00486 #else
00487         RetVal = Killer( *_ThreadId ,SIGINT ) ;
00488 #endif
00489         //if ( RetVal ) _Sleeping = true;
00490 
00491       }
00492     }
00493   return RetVal ;
00494 }
00495 
00496 //=============================================================================
00500 //=============================================================================
00501 
00502 bool Engines_Component_i::Resume_impl()
00503 {
00504 #ifndef WIN32
00505   MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
00506           << " pid " << getpid() << " instanceName "
00507           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00508           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << std::hex << _id
00509           << std::dec << " _ThreadId " << _ThreadId );
00510 #else
00511   MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self().p
00512           << " pid " << _getpid() << " instanceName "
00513           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
00514           << " machineName " << Kernel_Utils::GetHostname().c_str()<< " _id " << std::hex << _id
00515           << std::dec << " _ThreadId " << _ThreadId );
00516 #endif
00517   bool RetVal = false ;
00518 #ifndef WIN32
00519   if ( _ThreadId > 0 && pthread_self() != _ThreadId )
00520 #else
00521   if ( _ThreadId > 0 && pthread_self().p != _ThreadId->p )
00522 #endif
00523     {
00524     if ( _Sleeping ) 
00525       {
00526         _Sleeping = false ;
00527         RetVal = true ;
00528       }
00529     else
00530       {
00531         RetVal = false ;
00532       }
00533     }
00534   return RetVal ;
00535 }
00536 
00537 //=============================================================================
00541 //=============================================================================
00542 
00543 CORBA::Long Engines_Component_i::CpuUsed_impl()
00544 {
00545   long cpu = 0 ;
00546   if ( _ThreadId || _Executed )
00547     {
00548     if ( _ThreadId > 0 )
00549       {
00550 #ifndef WIN32
00551       if ( pthread_self() != _ThreadId )
00552 #else
00553       if ( pthread_self().p != _ThreadId->p )
00554 #endif
00555         {
00556         if ( _Sleeping )
00557           {
00558           }
00559         else
00560           {
00561             // Get Cpu in the appropriate thread with that object !...
00562             theEngines_Component = this ;
00563 #ifndef WIN32
00564             Killer( _ThreadId ,SIGUSR1 ) ;
00565 #else
00566             Killer( *_ThreadId ,SIGUSR11 ) ;
00567 #endif
00568           }
00569         cpu = _ThreadCpuUsed ;
00570         }
00571       else
00572         {
00573           _ThreadCpuUsed = CpuUsed() ;
00574           cpu = _ThreadCpuUsed ;
00575           // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
00576           //      << _serviceName << " " << cpu << endl ;
00577       }
00578     }
00579     else 
00580       {
00581         cpu = _ThreadCpuUsed ;
00582         // cout << pthread_self() << " Engines_Component_i::CpuUsed_impl "
00583         //      << _serviceName << " " << cpu<< endl ;
00584       }
00585     }
00586   else
00587     {
00588       // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
00589       //     <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
00590     }
00591   return cpu ;
00592 }
00593 
00594 
00595 //=============================================================================
00599 //=============================================================================
00600 
00601 Engines_Container_i *Engines_Component_i::GetContainerPtr()
00602 {
00603   PortableServer::ObjectId_var  contId=_poa->reference_to_id(_container);
00604   return dynamic_cast<Engines_Container_i*>(_poa->id_to_servant(contId)) ;
00605 }
00606 
00607 //=============================================================================
00616 //=============================================================================
00617 
00618 CORBA::Boolean Engines_Component_i::setStudyId(CORBA::Long studyId)
00619 {
00620   ASSERT( studyId >= 0);
00621   CORBA::Boolean ret = false;
00622   if (_studyId < 0) // --- not yet initialized 
00623     {
00624       _studyId = studyId;
00625       ret = true;
00626     }
00627   else
00628     if ( _studyId == studyId) ret = true;
00629   return ret;
00630 }
00631 
00632 //=============================================================================
00637 //=============================================================================
00638 
00639 PortableServer::ObjectId * Engines_Component_i::getId()
00640 {
00641 //  MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
00642   return _id ;
00643 }
00644 
00645 //=============================================================================
00649 //=============================================================================
00650 
00651 void Engines_Component_i::beginService(const char *serviceName)
00652 {
00653   std::cerr << "beginService for " << serviceName << " Component instance : " << _instanceName << std::endl;
00654 
00655 #ifndef WIN32
00656   _ThreadId = pthread_self() ;
00657 #else
00658   _ThreadId = new pthread_t;
00659   _ThreadId->p = pthread_self().p ;
00660   _ThreadId->x = pthread_self().x ;
00661 #endif
00662   _StartUsed = 0 ;
00663   _StartUsed = CpuUsed_impl() ;
00664   _ThreadCpuUsed = 0 ;
00665   _Executed = true ;
00666   _serviceName = serviceName ;
00667   theEngines_Component = this ;
00668   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
00669     {
00670       perror("pthread_setcanceltype ") ;
00671       exit(0) ;
00672     }
00673   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
00674     {
00675       perror("pthread_setcancelstate ") ;
00676       exit(0) ;
00677     }
00678 
00679   // --- all strings given with setProperties are set in environment
00680   std::map<std::string,CORBA::Any>::iterator it;
00681   for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
00682     {
00683       std::string cle((*it).first);
00684       if ((*it).second.type()->kind() == CORBA::tk_string)
00685         {
00686           const char* value;
00687           (*it).second >>= value;
00688           std::string s(cle);
00689           s+='=';
00690           s+=value;
00691           putenv((char *)s.c_str());
00692           MESSAGE("--- setenv: "<<cle<<" = "<< value);
00693         }
00694     }
00695 }
00696 
00697 //=============================================================================
00701 //=============================================================================
00702 
00703 void Engines_Component_i::endService(const char *serviceName)
00704 {
00705   if ( !_CanceledThread )
00706     _ThreadCpuUsed = CpuUsed_impl() ;
00707 
00708   float cpus=_ThreadCpuUsed/1000.;
00709   std::cerr << "endService for " << serviceName << " Component instance : " << _instanceName ;
00710   std::cerr << " Cpu Used: " << cpus << " (s) " << std::endl;
00711   MESSAGE("Send EndService notification for " << serviceName
00712           << std::endl << " Component instance : " << _instanceName << " StartUsed "
00713           << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << std::endl <<std::endl);
00714   _ThreadId = 0 ;
00715 }
00716 
00717 //=============================================================================
00721 //=============================================================================
00722 
00723 char* Engines_Component_i::graphName()
00724 {
00725   return CORBA::string_dup( _graphName.c_str() ) ;
00726 }
00727 
00728 //=============================================================================
00732 //=============================================================================
00733 
00734 char* Engines_Component_i::nodeName()
00735 {
00736   return CORBA::string_dup( _nodeName.c_str() ) ;
00737 }
00738 
00739 //=============================================================================
00743 //=============================================================================
00744 
00745 bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
00746 {
00747 #ifndef WIN32
00748   if ( ThreadId )
00749 #else
00750   if ( ThreadId.p )
00751 #endif
00752     {
00753       if ( signum == 0 )
00754         {
00755           if ( pthread_cancel( ThreadId ) )
00756             {
00757               perror("Killer pthread_cancel error") ;
00758               return false ;
00759             }
00760           else
00761             {
00762 #ifdef WNT
00763               MESSAGE("Killer : ThreadId " << ThreadId.p << " pthread_canceled") ;
00764 #else
00765               MESSAGE("Killer : ThreadId " << ThreadId << " pthread_canceled") ;
00766 #endif
00767             }
00768         }
00769       else
00770         {
00771           if ( pthread_kill( ThreadId , signum ) == -1 )
00772             {
00773               perror("Killer pthread_kill error") ;
00774               return false ;
00775             }
00776           else 
00777             {
00778 #ifdef WNT
00779               MESSAGE("Killer : ThreadId " << ThreadId.p << " pthread_killed(" << signum << ")") ;
00780 #else
00781               MESSAGE("Killer : ThreadId " << ThreadId << " pthread_killed(" << signum << ")") ;
00782 #endif
00783             }
00784         }
00785     }
00786   return true ;
00787 }
00788 
00789 void SetCpuUsed();
00790 void CallCancelThread();
00791 
00792 //=============================================================================
00796 //=============================================================================
00797 
00798 void SetCpuUsed()
00799 {
00800   if ( theEngines_Component )
00801     theEngines_Component->SetCurCpu() ;
00802 }
00803 
00804 //=============================================================================
00808 //=============================================================================
00809 
00810 void Engines_Component_i::SetCurCpu()
00811 {
00812   _ThreadCpuUsed =  CpuUsed() ;
00813   //  MESSAGE(pthread_self() << 
00814   //  " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
00815 }
00816 
00817 //=============================================================================
00821 //=============================================================================
00822 
00823 long Engines_Component_i::CpuUsed()
00824 {
00825   long cpu = 0 ;
00826 #ifndef WIN32
00827   struct rusage usage ;
00828   if ( _ThreadId || _Executed )
00829     {
00830       if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
00831         {
00832           perror("Engines_Component_i::CpuUsed") ;
00833           return 0 ;
00834         }
00835       //cpu time is calculated in millisecond (user+system times)
00836       cpu = usage.ru_utime.tv_sec*1000 +usage.ru_utime.tv_usec/1000;
00837       cpu = cpu+ usage.ru_stime.tv_sec*1000 +usage.ru_stime.tv_usec/1000;
00838       cpu=cpu-_StartUsed ;
00839       // std::cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
00840       //      << _serviceName   << usage.ru_utime.tv_sec << " - " << _StartUsed
00841       //      << " = " << cpu << std::endl ;
00842     }
00843   else
00844     {
00845       // std::cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
00846       //      << _ThreadId << " " << _serviceName<< " _StartUsed " 
00847       //      << _StartUsed << std::endl ;
00848     }
00849 #else 
00850         // NOT implementet yet
00851 #endif
00852 
00853 
00854   return cpu ;
00855 }
00856 
00857 void CallCancelThread()
00858 {
00859   if ( theEngines_Component )
00860     theEngines_Component->CancelThread() ;
00861 }
00862 
00863 //=============================================================================
00867 //=============================================================================
00868 
00869 void Engines_Component_i::CancelThread()
00870 {
00871   _CanceledThread = true;
00872 }
00873 
00874 //=============================================================================
00878 //=============================================================================
00879 
00880 void Engines_Component_i::sendMessage(const char *event_type,
00881                                       const char *message)
00882 {
00883     _notifSupplier->Send(_graphName.c_str(), _nodeName.c_str(), event_type, message);
00884 }
00885 
00886 //=============================================================================
00890 //=============================================================================
00891 
00892 std::string Engines_Component_i::GetDynLibraryName(const char *componentName)
00893 {
00894   std::string ret="lib";
00895   ret+=componentName;
00896   ret+="Engine.so";
00897   return ret;
00898 }
00899 
00900 //=============================================================================
00904 //=============================================================================
00905 
00906 Engines::TMPFile* Engines_Component_i::DumpPython(CORBA::Object_ptr theStudy, 
00907                                                   CORBA::Boolean isPublished, 
00908                                                   CORBA::Boolean isMultiFile, 
00909                                                   CORBA::Boolean& isValidScript)
00910 {
00911   const char* aScript = isMultiFile ? "def RebuildData(theStudy): pass" : "";
00912   char* aBuffer = new char[strlen(aScript)+1];
00913   strcpy(aBuffer, aScript);
00914   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
00915   int aBufferSize = strlen(aBuffer)+1;
00916   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1); 
00917   isValidScript = true;
00918   return aStreamFile._retn(); 
00919 }
00920 
00921 Engines::Salome_file_ptr 
00922 Engines_Component_i::getInputFileToService(const char* service_name, 
00923                                            const char* Salome_file_name) 
00924 {
00925   // Try to find the service, if it doesn't exist, we throw an exception.
00926   _Service_file_map_it = _Input_Service_file_map.find(service_name);
00927   if (_Service_file_map_it ==  _Input_Service_file_map.end()) {
00928     SALOME::ExceptionStruct es;
00929     es.type = SALOME::INTERNAL_ERROR;
00930     es.text = "service doesn't have salome files";
00931     throw SALOME::SALOME_Exception(es);
00932   }
00933   _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
00934 
00935   // Try to find the Salome_file ...
00936   _Salome_file_map_it = _map->find(Salome_file_name);
00937   if (_Salome_file_map_it ==  _map->end()) {
00938     SALOME::ExceptionStruct es;
00939     es.type = SALOME::INTERNAL_ERROR;
00940     es.text = "service doesn't have this Salome_file";
00941     throw SALOME::SALOME_Exception(es);
00942   }
00943   Salome_file_i * Sfile = (*_map)[Salome_file_name];
00944 
00945   return Sfile->_this();
00946 }
00947 
00948 Engines::Salome_file_ptr 
00949 Engines_Component_i::setInputFileToService(const char* service_name, 
00950                                            const char* Salome_file_name) 
00951 {
00952   // Try to find the service, if it doesn't exist, we add it.
00953   _Service_file_map_it = _Input_Service_file_map.find(service_name);
00954   if (_Service_file_map_it ==  _Input_Service_file_map.end()) {
00955     _t_Salome_file_map * _map = new _t_Salome_file_map();
00956     _Input_Service_file_map[service_name] = _map;
00957   }
00958   _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
00959   
00960   // Try to find the Salome_file ...
00961   _Salome_file_map_it = _map->find(Salome_file_name);
00962   if (_Salome_file_map_it ==  _map->end()) {
00963     Salome_file_i * Sfile = new Salome_file_i();
00964     Engines::Container_ptr container = this->GetContainerRef();
00965     Sfile->setContainer(Engines::Container::_duplicate(container));
00966     (*_map)[Salome_file_name] = Sfile;
00967   }
00968 
00969   Salome_file_i * Sfile = (*_map)[Salome_file_name];
00970   return Sfile->_this();
00971 }
00972 
00973 void 
00974 Engines_Component_i::checkInputFilesToService(const char* service_name) 
00975 {
00976   // Try to find the service, if it doesn't exist, nothing to do.
00977   _Service_file_map_it = _Input_Service_file_map.find(service_name);
00978   if (_Service_file_map_it !=  _Input_Service_file_map.end()) {
00979     _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
00980     _t_Salome_file_map::iterator begin = _map->begin();
00981     _t_Salome_file_map::iterator end = _map->end();
00982 
00983     for(;begin!=end;begin++) {
00984       Salome_file_i * file = begin->second;
00985       std::string file_port_name = begin->first;
00986       configureSalome_file(service_name, file_port_name, file);
00987       file->recvFiles();
00988     }
00989   }
00990 }
00991 
00992 Engines::Salome_file_ptr 
00993 Engines_Component_i::getOutputFileToService(const char* service_name, 
00994                                             const char* Salome_file_name) 
00995 {
00996   // Try to find the service, if it doesn't exist, we throw an exception.
00997   _Service_file_map_it = _Output_Service_file_map.find(service_name);
00998   if (_Service_file_map_it ==  _Output_Service_file_map.end()) {
00999     SALOME::ExceptionStruct es;
01000     es.type = SALOME::INTERNAL_ERROR;
01001     es.text = "service doesn't have salome files";
01002     throw SALOME::SALOME_Exception(es);
01003   }
01004   _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
01005 
01006   // Try to find the Salome_file ...
01007   _Salome_file_map_it = _map->find(Salome_file_name);
01008   if (_Salome_file_map_it ==  _map->end()) {
01009     SALOME::ExceptionStruct es;
01010     es.type = SALOME::INTERNAL_ERROR;
01011     es.text = "service doesn't have this Salome_file";
01012     throw SALOME::SALOME_Exception(es);
01013   }
01014   Salome_file_i * Sfile = (*_map)[Salome_file_name];
01015 
01016   return Sfile->_this();
01017 }
01018 
01019 Engines::Salome_file_ptr 
01020 Engines_Component_i::setOutputFileToService(const char* service_name, 
01021                                            const char* Salome_file_name) 
01022 {
01023   // Try to find the service, if it doesn't exist, we add it.
01024   _Service_file_map_it = _Output_Service_file_map.find(service_name);
01025   if (_Service_file_map_it ==  _Output_Service_file_map.end()) {
01026     _t_Salome_file_map * _map = new _t_Salome_file_map();
01027     _Output_Service_file_map[service_name] = _map;
01028   }
01029   _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
01030   
01031   // Try to find the Salome_file ...
01032   _Salome_file_map_it = _map->find(Salome_file_name);
01033   if (_Salome_file_map_it ==  _map->end()) {
01034     Salome_file_i * Sfile = new Salome_file_i();
01035     Engines::Container_ptr container = this->GetContainerRef();
01036     Sfile->setContainer(Engines::Container::_duplicate(container));
01037     (*_map)[Salome_file_name] = Sfile;
01038   }
01039 
01040   Salome_file_i * Sfile = (*_map)[Salome_file_name];
01041   return Sfile->_this();
01042 }
01043 
01044 void 
01045 Engines_Component_i::checkOutputFilesToService(const char* service_name) 
01046 {
01047   // Try to find the service, if it doesn't exist, nothing to do.
01048   _Service_file_map_it = _Output_Service_file_map.find(service_name);
01049   if (_Service_file_map_it !=  _Output_Service_file_map.end()) {
01050     _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
01051     _t_Salome_file_map::iterator begin = _map->begin();
01052     _t_Salome_file_map::iterator end = _map->end();
01053 
01054     for(;begin!=end;begin++) {
01055       Salome_file_i * file = begin->second;
01056       std::string file_port_name = begin->first;
01057       configureSalome_file(service_name, file_port_name, file);
01058       file->recvFiles();
01059     }
01060   }
01061 
01062 }
01063 
01064 //=============================================================================
01071 //=============================================================================
01072 void
01073 Engines_Component_i::configureSalome_file(std::string service_name,
01074                                           std::string file_port_name,
01075                                           Salome_file_i * file) 
01076 {
01077   // By default this method does nothing
01078 }
01079 
01080 //=============================================================================
01086 //=============================================================================
01087 std::string Engines_Component_i::getContainerName()
01088 {
01089   return _containerName;
01090 }
01091 //=============================================================================
01097 //=============================================================================
01098 void Engines_Component_i::setContainerName()
01099 {
01100   CORBA::String_var containerName=_container->name();
01101   std::string name(containerName);
01102   name.erase(0,12);
01103   std::string::size_type slash =name.find_first_of('/');
01104   if(slash != std::string::npos)
01105     name[slash]='_';
01106   _containerName=name;
01107 }