Back to index

salome-gui  6.5.0
Session_ServerCheck.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 // File   : Session_ServerCheck.cxx
00024 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00025 //
00026 #include "Session_ServerCheck.hxx"
00027 
00028 #include <SALOMEconfig.h>
00029 #include CORBA_CLIENT_HEADER(SALOME_Session)
00030 #include CORBA_CLIENT_HEADER(SALOME_Registry)
00031 #include CORBA_CLIENT_HEADER(SALOMEDS)
00032 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
00033 #include CORBA_CLIENT_HEADER(SALOME_Component)
00034 
00035 #include "Utils_ORB_INIT.hxx"
00036 #include "Utils_SINGLETON.hxx"
00037 #include "SALOME_NamingService.hxx"
00038 #include "Basics_Utils.hxx"
00039 #include "utilities.h"
00040 
00041 #include <QApplication> 
00042 #include <QWaitCondition>
00043 #include <QMutexLocker>
00044 #include <QStringList>
00045 
00046 //
00047 // Default settings
00048 //
00049 
00060 const int __DEFAULT__ATTEMPTS__ = 300;
00061 
00072 const int __DEFAULT__DELAY__ = 50000;
00073 
00080 class Session_ServerCheck::Locker
00081 {
00082 public:
00086   Locker( Session_ServerCheck* sc ) 
00087     : myChecker( sc )
00088   {
00089     myChecker->myMutex->lock();
00090     myChecker->myMutex->unlock();
00091   }
00095   ~Locker()
00096   {
00097     myChecker->myWC->wakeAll();
00098     myChecker->usleep( myChecker->myDelay );
00099   }
00100 private:
00101   Session_ServerCheck* myChecker;
00102 };
00103 
00128 Session_ServerCheck::Session_ServerCheck( QMutex* mutex, QWaitCondition* wc )
00129 : QThread(),
00130   myMutex( mutex ),
00131   myWC( wc ),
00132   myCheckCppContainer( false ),
00133   myCheckPyContainer( false ),
00134   myCheckSVContainer( false ),
00135   myAttempts( __DEFAULT__ATTEMPTS__ ),
00136   myDelay   ( __DEFAULT__DELAY__ ),
00137   myCurrentStep( 0 )
00138 {
00139   char* cenv;
00140   // try to get nb of attempts from environment variable
00141   if ( ( cenv = getenv( "CSF_RepeatServerRequest" ) ) && atoi( cenv ) > 0 )
00142     myAttempts = atoi( cenv );
00143   // try to get delay between attempts from environment variable
00144   if ( ( cenv = getenv( "CSF_DelayServerRequest" ) ) && atoi( cenv ) > 0 )
00145     myDelay = atoi( cenv );
00146 
00147   // parse command line check if it is necessary to wait SALOME containers
00148   QStringList args = QApplication::arguments();
00149   for ( int i = 1; i < args.count(); i++ ) {
00150     myCheckCppContainer = myCheckCppContainer || args[i] == "CPP";
00151     myCheckPyContainer  = myCheckPyContainer  || args[i] == "PY";
00152     myCheckSVContainer  = myCheckSVContainer  || args[i] == "SUPERV";
00153   }
00154   
00155   // start thread
00156   start();
00157 }
00158 
00162 Session_ServerCheck::~Session_ServerCheck()
00163 {
00164   terminate();
00165   while( isRunning() );
00166 }
00167 
00172 QString Session_ServerCheck::currentMessage()
00173 {
00174   static QStringList messages;
00175   if ( messages.isEmpty() ) {
00176     messages << tr( "Waiting for naming service..." ); 
00177     messages << tr( "Waiting for registry server..." );
00178     messages << tr( "Waiting for study server..." );
00179     messages << tr( "Waiting for module catalogue server..." );
00180     messages << tr( "Waiting for session server..." );
00181     messages << tr( "Waiting for C++ container..." );
00182     messages << tr( "Waiting for Python container..." );
00183     messages << tr( "Waiting for Supervision container..." );
00184   }
00185   QMutexLocker locker( &myDataMutex );
00186   QString msg;
00187   int idx = myCurrentStep / myAttempts;
00188   if ( idx >= 0 && idx < messages.count() )
00189     msg = messages[ idx ];
00190   return msg;
00191 }
00192 
00197 QString Session_ServerCheck::error()
00198 {
00199   QMutexLocker locker( &myDataMutex );
00200   return myError;
00201 }
00202 
00207 int Session_ServerCheck::currentStep()
00208 {
00209   QMutexLocker locker( &myDataMutex );
00210   return myCurrentStep;
00211 }
00212 
00217 int Session_ServerCheck::totalSteps()
00218 {
00219   QMutexLocker locker( &myDataMutex );
00220   int cnt = 5;                       // base servers
00221   if ( myCheckCppContainer ) cnt++;  // + C++ container
00222   if ( myCheckPyContainer )  cnt++;  // + Python container
00223   if ( myCheckSVContainer )  cnt++;  // + supervision container
00224   return cnt * myAttempts;
00225 }
00226 
00231 void Session_ServerCheck::setStep( const int step )
00232 {
00233   QMutexLocker locker( &myDataMutex );
00234   myCurrentStep = step;
00235 }
00236 
00241 void Session_ServerCheck::setError( const QString& msg )
00242 {
00243   QMutexLocker locker( &myDataMutex );
00244   myError = msg;
00245 }
00246 
00250 void Session_ServerCheck::run()
00251 {
00252   // start check servers
00253   int current = 0;
00254   QString error;
00255   int    argc = QApplication::instance()->argc();
00256   char** argv = QApplication::instance()->argv();
00257 
00258   // 1. Check naming service
00259   for ( int i = 0; i < myAttempts; i++ ) {
00260     Locker locker( this );
00261 
00262     setStep( current * myAttempts + i );
00263 
00264     try {
00265       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00266       CORBA::Object_var obj = orb->resolve_initial_references( "NameService" );
00267       CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow( obj );
00268       if ( !CORBA::is_nil( _root_context ) ) {
00269         setStep( ++current * myAttempts );
00270         break;
00271       }
00272     }
00273     catch( CORBA::COMM_FAILURE& ) {
00274       MESSAGE( "CORBA::COMM_FAILURE: unable to contact the naming service" );
00275     }
00276     catch( ... ) {
00277       MESSAGE( "Unknown Exception: unable to contact the naming service" );
00278     }
00279 
00280     if ( i == myAttempts-1 ) {
00281       setError( tr( "Unable to contact the naming service.\n" ) );
00282       return;
00283     }
00284   }
00285 
00286   // 2. Check registry server
00287   for ( int i = 0; i < myAttempts ; i++ ) {
00288     Locker locker( this );
00289 
00290     setStep( current * myAttempts + i );
00291 
00292     try {
00293       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00294       SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00295       ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00296       NS.init_orb( orb );
00297       CORBA::Object_var obj = NS.Resolve( "/Registry" );
00298       Registry::Components_var registry = Registry::Components::_narrow( obj );
00299       if ( !CORBA::is_nil( registry ) ) {
00300         MESSAGE( "/Registry is found" );
00301         registry->ping();
00302         MESSAGE( "Registry was activated" );
00303         setStep( ++current * myAttempts );
00304         break;
00305       }
00306     }
00307     catch ( ServiceUnreachable& ) {
00308       MESSAGE( "Caught exception: Naming Service unreachable." );
00309       error = "Naming service unreachable";
00310     }
00311     catch ( CORBA::COMM_FAILURE& ) {
00312       MESSAGE( "Caught CORBA::SystemException CommFailure." );
00313       error = "Caught CORBA::SystemException CommFailure.";
00314     }
00315     catch ( CORBA::SystemException& ) {
00316       MESSAGE( "Caught CORBA::SystemException." );
00317       error = "Caught CORBA::SystemException.";
00318     }
00319     catch ( CORBA::Exception& ) {
00320       MESSAGE( "Caught CORBA::Exception." );
00321       error = "Caught CORBA::Exception.";
00322     }
00323     catch (...) {
00324       MESSAGE( "Caught unknown exception." );
00325       error = "Caught unknown exception.";
00326     }
00327 
00328     if ( i == myAttempts-1 ) {
00329       setError( tr( "Registry server is not found.\n%1" ).arg ( error ) );
00330       return;
00331     }
00332   }
00333 
00334   // 3. Check data server
00335   for ( int i = 0; i < myAttempts ; i++ ) {
00336     Locker locker( this );
00337 
00338     setStep( current * myAttempts + i );
00339 
00340     try {
00341       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00342       SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00343       ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00344       NS.init_orb( orb );
00345       CORBA::Object_var obj = NS.Resolve( "/myStudyManager" );
00346       SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow( obj );
00347       if ( !CORBA::is_nil( studyManager ) ) {
00348         MESSAGE( "/myStudyManager is found" );
00349         studyManager->ping();
00350         MESSAGE( "StudyManager was activated" );
00351         setStep( ++current * myAttempts );
00352         break;
00353       }
00354     }
00355     catch ( ServiceUnreachable& ) {
00356       MESSAGE( "Caught exception: Naming Service unreachable." );
00357       error = "Naming service unreachable";
00358     }
00359     catch ( CORBA::COMM_FAILURE& ) {
00360       MESSAGE( "Caught CORBA::SystemException CommFailure." );
00361       error = "Caught CORBA::SystemException CommFailure.";
00362     }
00363     catch ( CORBA::SystemException& ) {
00364       MESSAGE( "Caught CORBA::SystemException." );
00365       error = "Caught CORBA::SystemException.";
00366     }
00367     catch ( CORBA::Exception& ) {
00368       MESSAGE( "Caught CORBA::Exception." );
00369       error = "Caught CORBA::Exception.";
00370     }
00371     catch (...) {
00372       MESSAGE( "Caught unknown exception." );
00373       error = "Caught unknown exception.";
00374     }
00375 
00376     if ( i == myAttempts-1 ) {
00377       setError( tr( "Study server is not found.\n%1" ).arg ( error ) );
00378       return;
00379     }
00380   }
00381   
00382   // 4. Check module catalogue server
00383   for ( int i = 0; i < myAttempts ; i++ ) {
00384     Locker locker( this );
00385 
00386     setStep( current * myAttempts + i );
00387 
00388     try {
00389       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00390       SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00391       ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00392       NS.init_orb( orb );
00393       CORBA::Object_var obj = NS.Resolve( "/Kernel/ModulCatalog" );
00394       SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow( obj );
00395       if ( !CORBA::is_nil( catalog ) ){
00396         MESSAGE( "/Kernel/ModulCatalog is found" );
00397         catalog->ping();
00398         MESSAGE( "ModuleCatalog was activated" );
00399         setStep( ++current * myAttempts );
00400         break;
00401       }
00402     }
00403     catch ( ServiceUnreachable& ) {
00404       MESSAGE( "Caught exception: Naming Service unreachable." );
00405       error = "Naming service unreachable";
00406     }
00407     catch ( CORBA::COMM_FAILURE& ) {
00408       MESSAGE( "Caught CORBA::SystemException CommFailure." );
00409       error = "Caught CORBA::SystemException CommFailure.";
00410     }
00411     catch ( CORBA::SystemException& ) {
00412       MESSAGE( "Caught CORBA::SystemException." );
00413       error = "Caught CORBA::SystemException.";
00414     }
00415     catch ( CORBA::Exception& ) {
00416       MESSAGE( "Caught CORBA::Exception." );
00417       error = "Caught CORBA::Exception.";
00418     }
00419     catch (...) {
00420       MESSAGE( "Caught unknown exception." );
00421       error = "Caught unknown exception.";
00422     }
00423 
00424     if ( i == myAttempts-1 ) {
00425       setError( tr( "Module catalogue server is not found.\n%1" ).arg ( error ) );
00426       return;
00427     }
00428   }
00429 
00430   // 5. Check data server
00431   for ( int i = 0; i < myAttempts ; i++ ) {
00432     Locker locker( this );
00433 
00434     setStep( current * myAttempts + i );
00435 
00436     try {
00437       CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00438       SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00439       ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00440       NS.init_orb( orb );
00441       CORBA::Object_var obj = NS.Resolve( "/Kernel/Session" );
00442       SALOME::Session_var session = SALOME::Session::_narrow( obj );
00443       if ( !CORBA::is_nil( session ) ) {
00444         MESSAGE( "/Kernel/Session is found" );
00445         session->ping();
00446         MESSAGE( "SALOME_Session was activated" );
00447         setStep( ++current * myAttempts );
00448         break;
00449       }
00450     }
00451     catch ( ServiceUnreachable& ) {
00452       MESSAGE( "Caught exception: Naming Service unreachable." );
00453       error = "Naming service unreachable";
00454     }
00455     catch ( CORBA::COMM_FAILURE& ) {
00456       MESSAGE( "Caught CORBA::SystemException CommFailure." );
00457       error = "Caught CORBA::SystemException CommFailure.";
00458     }
00459     catch ( CORBA::SystemException& ) {
00460       MESSAGE( "Caught CORBA::SystemException." );
00461       error = "Caught CORBA::SystemException.";
00462     }
00463     catch ( CORBA::Exception& ) {
00464       MESSAGE( "Caught CORBA::Exception." );
00465       error = "Caught CORBA::Exception.";
00466     }
00467     catch (...) {
00468       MESSAGE( "Caught unknown exception." );
00469       error = "Caught unknown exception.";
00470     }
00471 
00472     if ( i == myAttempts-1 ) {
00473       setError( tr( "Session server is not found.\n%1" ).arg ( error ) );
00474       return;
00475     }
00476   }
00477 
00478   // 6. Check C++ container
00479   if ( myCheckCppContainer ) {
00480     for ( int i = 0; i < myAttempts ; i++ ) {
00481       Locker locker( this );
00482       
00483       setStep( current * myAttempts + i );
00484 
00485       try {
00486         CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00487         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00488         ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00489         NS.init_orb( orb );
00490         QString containerName = QString( "/Containers/%1/FactoryServer" ).arg( Kernel_Utils::GetHostname().c_str() );
00491         CORBA::Object_var obj = NS.Resolve( containerName.toLatin1() );
00492         Engines::Container_var FScontainer = Engines::Container::_narrow( obj );
00493         if ( !CORBA::is_nil( FScontainer ) ) {
00494           MESSAGE( containerName.toLatin1().constData() << " is found" );
00495           FScontainer->ping();
00496           MESSAGE( "FactoryServer container was activated" );
00497           setStep( ++current * myAttempts );
00498           break;
00499         }
00500       }
00501       catch ( ServiceUnreachable& ) {
00502         MESSAGE( "Caught exception: Naming Service unreachable." );
00503         error = "Naming service unreachable";
00504       }
00505       catch ( CORBA::COMM_FAILURE& ) {
00506         MESSAGE( "Caught CORBA::SystemException CommFailure." );
00507         error = "Caught CORBA::SystemException CommFailure.";
00508       }
00509       catch ( CORBA::SystemException& ) {
00510         MESSAGE( "Caught CORBA::SystemException." );
00511         error = "Caught CORBA::SystemException.";
00512       }
00513       catch ( CORBA::Exception& ) {
00514         MESSAGE( "Caught CORBA::Exception." );
00515         error = "Caught CORBA::Exception.";
00516       }
00517       catch (...) {
00518         MESSAGE( "Caught unknown exception." );
00519         error = "Caught unknown exception.";
00520       }
00521       
00522       if ( i == myAttempts-1 ) {
00523         setError( tr( "C++ container is not found.\n%1" ).arg ( error ) );
00524         return;
00525       }
00526     }
00527   }
00528 
00529   // 7. Check Python container
00530   if ( myCheckPyContainer ) {
00531     for ( int i = 0; i < myAttempts ; i++ ) {
00532       Locker locker( this );
00533       
00534       setStep( current * myAttempts + i );
00535 
00536       try {
00537         CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00538         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00539         ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00540         NS.init_orb( orb );
00541         QString containerName = QString( "/Containers/%1/FactoryServerPy" ).arg( Kernel_Utils::GetHostname().c_str() );
00542         CORBA::Object_var obj = NS.Resolve( containerName.toLatin1() );
00543         Engines::Container_var FSPcontainer = Engines::Container::_narrow( obj );
00544         if ( !CORBA::is_nil( FSPcontainer ) ) {
00545           MESSAGE( containerName.toLatin1().constData() << " is found" );
00546           FSPcontainer->ping();
00547           MESSAGE("FactoryServerPy container was activated");
00548           setStep( ++current * myAttempts );
00549           break;
00550         }
00551       }
00552       catch ( ServiceUnreachable& ) {
00553         MESSAGE( "Caught exception: Naming Service unreachable." );
00554         error = "Naming service unreachable";
00555       }
00556       catch ( CORBA::COMM_FAILURE& ) {
00557         MESSAGE( "Caught CORBA::SystemException CommFailure." );
00558         error = "Caught CORBA::SystemException CommFailure.";
00559       }
00560       catch ( CORBA::SystemException& ) {
00561         MESSAGE( "Caught CORBA::SystemException." );
00562         error = "Caught CORBA::SystemException.";
00563       }
00564       catch ( CORBA::Exception& ) {
00565         MESSAGE( "Caught CORBA::Exception." );
00566         error = "Caught CORBA::Exception.";
00567       }
00568       catch (...) {
00569         MESSAGE( "Caught unknown exception." );
00570         error = "Caught unknown exception.";
00571       }
00572 
00573       if ( i == myAttempts-1 ) {
00574         setError( tr( "Python container is not found.\n%1" ).arg ( error ) );
00575         return;
00576       }
00577     }
00578   }
00579 
00580   // 8. Check supervision container
00581   if ( myCheckSVContainer ) {
00582     for ( int i = 0; i < myAttempts ; i++ ) {
00583       Locker locker( this );
00584       
00585       setStep( current * myAttempts + i );
00586 
00587       try {
00588         CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
00589         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance();
00590         ASSERT( SINGLETON_<SALOME_NamingService>::IsAlreadyExisting() );
00591         NS.init_orb( orb );
00592         QString containerName = QString( "/Containers/%1/SuperVisionContainer" ).arg( Kernel_Utils::GetHostname().c_str() );
00593         CORBA::Object_var obj = NS.Resolve( containerName.toLatin1() );
00594         Engines::Container_var SVcontainer = Engines::Container::_narrow( obj );
00595         if ( !CORBA::is_nil( SVcontainer ) ) {
00596           MESSAGE( containerName.toLatin1().constData() << " is found" );
00597           SVcontainer->ping();
00598           MESSAGE("SuperVisionContainer container was activated");
00599           setStep( ++current * myAttempts );
00600           break;
00601         }
00602       }
00603       catch ( ServiceUnreachable& ) {
00604         MESSAGE( "Caught exception: Naming Service unreachable." );
00605         error = "Naming service unreachable";
00606       }
00607       catch ( CORBA::COMM_FAILURE& ) {
00608         MESSAGE( "Caught CORBA::SystemException CommFailure." );
00609         error = "Caught CORBA::SystemException CommFailure.";
00610       }
00611       catch ( CORBA::SystemException& ) {
00612         MESSAGE( "Caught CORBA::SystemException." );
00613         error = "Caught CORBA::SystemException.";
00614       }
00615       catch ( CORBA::Exception& ) {
00616         MESSAGE( "Caught CORBA::Exception." );
00617         error = "Caught CORBA::Exception.";
00618       }
00619       catch (...) {
00620         MESSAGE( "Caught unknown exception." );
00621         error = "Caught unknown exception.";
00622       }
00623     
00624       if ( i == myAttempts-1 ) {
00625         setError( tr( "Supervision container is not found.\n%1" ).arg ( error ) );
00626         return;
00627       }
00628     }
00629   }
00630 }