1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : InquireServersQThread.cxx
6 // Author : Vasily RUSYAEV
11 #include "InquireServersQThread.h"
14 #include <qpushbutton.h>
15 #include <qabstractlayout.h>
19 #include <qmessagebox.h>
21 #include <qfileinfo.h>
22 #include <qstringlist.h>
24 //VRV: porting on Qt 3.0.5
25 #if QT_VERSION >= 0x030005
26 #include <qdesktopwidget.h>
28 //VRV: porting on Qt 3.0.5
32 #include <SALOMEconfig.h>
34 #include "Utils_ORB_INIT.hxx"
35 #include "Utils_SINGLETON.hxx"
36 #include "SALOME_NamingService.hxx"
37 #include "utilities.h"
40 #include CORBA_CLIENT_HEADER(SALOME_Session)
41 #include CORBA_CLIENT_HEADER(SALOME_Registry)
42 #include CORBA_CLIENT_HEADER(SALOMEDS)
43 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
44 #include CORBA_CLIENT_HEADER(SALOME_Component)
47 #define SPACING_SIZE 3
49 static QString findFile( QString filename );
50 static QString addSlash( const QString& path );
52 InquireServersGUI::InquireServersGUI()
53 : QVBox(0, "SFA splash", Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder )
56 myThread = new InquireServersQThread(this);
58 // 1. Polish the appearance
59 setMargin( MARGIN_SIZE );
60 setSpacing( SPACING_SIZE );
61 setFrameStyle( QFrame::Plain | QFrame::Box );
63 setMinimumSize( 200, 150 );
66 QFrame* frm = new QFrame( this );
67 frm->setFrameStyle( QFrame::Box | QFrame::Raised );
68 QHBoxLayout* frmLayout = new QHBoxLayout( frm );
69 frmLayout->setMargin( MARGIN_SIZE );
71 splash = new QLabel( frm, "splash" );
72 frmLayout->addWidget( splash );
74 QString path = findFile( "Application-Splash.png" );
75 splash->setPixmap( QPixmap( path ) );
78 myPrgBar = new QProgressBar( this, "QProgressBar" );
79 myPrgBar->setFixedWidth( splash->pixmap()->isNull() ? 180 : splash->sizeHint().width() );
80 //Sets the total number of steps .
81 myPrgBar->setTotalSteps ( myThread->getInquiredServers() );
82 myPrgBar->setProgress( 0 );
85 QWidget* aWgt1 = new QWidget( this );
86 QHBoxLayout* aHBoxLayout1 = new QHBoxLayout( aWgt1 );
87 myLabel = new QLabel( tr( "Loading:" ), aWgt1 );
88 myLabel->setFixedWidth( splash->pixmap()->isNull() ? 180 : splash->sizeHint().width() );
89 myLabel->setAlignment( AlignLeft );
90 QFont theFont = myLabel->font();
91 theFont.setBold(true);
92 myLabel->setFont( theFont );
93 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
94 aHBoxLayout1->addWidget( myLabel );
95 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
98 QWidget* aWgt = new QWidget( this );
99 QHBoxLayout* aHBoxLayout = new QHBoxLayout( aWgt );
100 QPushButton* myCancelBtn = new QPushButton( tr( "Cancel" ), aWgt );
101 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
102 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
103 aHBoxLayout->addWidget( myCancelBtn );
104 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
107 #if QT_VERSION >= 0x030005
108 QDesktopWidget *d = QApplication::desktop();
110 QWidget *d = QApplication::desktop();
112 //VRV: porting on Qt 3.0.5
114 int w = d->width(); // returns desktop width
115 int h = d->height(); // returns desktop height
116 QSize mySize = sizeHint (); // returns widget size
117 int Xc = ( w - mySize.width() ) / 2;
118 int Yc = ( h - mySize.height() ) / 2;
124 InquireServersGUI::~InquireServersGUI()
129 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
131 _argc = qApp->argc();
132 *_argv = qApp->argv();
135 //=================================================================================
136 // function : ClickOnCancel()
137 // purpose : cancel loading of SALOME
138 //=================================================================================
139 void InquireServersGUI::ClickOnCancel()
141 //it's necessary to stop asking servers
144 //Also we should send QCloseEvent in order to close this widget (and remove from screen)
145 //QThread::postEvent ( this, new QCloseEvent() );
149 void InquireServersGUI::closeEvent ( QCloseEvent * pe)
151 //default implementation calls e->accept(), which hides this widget.
152 //See the QCloseEvent documentation for more details.
154 QApplication::flushX ();
155 QApplication::syncX ();
159 void InquireServersGUI::customEvent( QCustomEvent* pe )
163 case InquireEvent::ProgressEvent:
165 int* value = ( int* )(( InquireEvent*)pe)->data();
166 myPrgBar->setProgress( *value );
169 case InquireEvent::ProgressEventLabel:
171 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
172 myLabel->setText( *myString );
175 case InquireEvent::ProgressEventError:
177 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
178 QString appName = "SALOME Professional";
179 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
180 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
181 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
193 int InquireServersGUI::getExitStatus()
195 myThread->getExitStatus();
198 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
199 : receiver(r), myExitStatus(0)
205 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
206 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
207 cenv = getenv( "CSF_RepeatServerRequest" );
209 int val = atoi( cenv );
213 //define delay time between two attempts
214 myDelay = 1000000; // 1 second
215 QString str = "Loading: ";
216 myMessages[0] = "Checking naming service...";
217 myMessages[1] = str + "SALOME_Registry_Server" + "...";
218 myMessages[2] = str + "SALOME_Container SuperVisionContainer" + "...";
219 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
220 myMessages[4] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
221 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
222 myMessages[6] = str + "SALOMEDS_Server" + "...";
223 myMessages[7] = str + "SALOME_Session_Server" + "...";
225 r->getArgs( _argc, &_argv);
227 // NRI : Temporary solution for SuperVisionContainer
228 for ( int i=1; i<=(_argc-1); i++) {
229 if (strcmp(_argv[i],"CPP")==0) {
230 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
233 if (strcmp(_argv[i],"PYTHON")==0) {
234 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
237 if (strcmp(_argv[i],"SUPERV")==0) {
238 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
241 if (strcmp(_argv[i],"GUI")==0) {
247 void InquireServersQThread::run()
251 for (int i=1; i<=8; i++)
253 if ( myMessages[i-1].isEmpty() ) {
256 //myExitStatus should be 0 because all servers exist and work
258 //we should send QCloseEvent in order to close this widget (and remove from screen)
259 QThread::postEvent ( receiver , new QCloseEvent() );
263 QString *message = new QString(myMessages[i-1]);
264 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
265 QThread::usleep(200000);
267 bool result = AskServer(i,&errMsg);
270 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
274 //myExitStatus should be 0 because all servers exist and work
276 //we should send QCloseEvent in order to close this widget (and remove from screen)
277 QThread::postEvent ( receiver , new QCloseEvent() );
282 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
283 //myExitStatus should be 1 because we didn't receive response from server
291 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
293 ASSERT(iteration<=myServersCount);
294 //will be set true if we get response from server
295 bool IsPassed = false;
296 QString errDescription;
300 //First checking - existence of Naming Service
301 for (int i = myRepeat; i ; i--)
305 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
306 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
307 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
308 if (CORBA::is_nil(_root_context))
314 catch(CORBA::COMM_FAILURE&)
316 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
320 MESSAGE("Unknown Exception: unable to contact the naming service");
322 QThread::usleep(myDelay);
325 *errMessage = new QString("unable to contact the naming service");
328 //checking - existence of SALOME_Registry_Server
330 //checking - existence of SALOME_Container SuperVisionContainer
332 //checking - existence of SALOME_ModuleCatalog_Server
334 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
336 //checking - existence of SALOME_Container FactoryServer
338 //checking - existence of SALOMEDS_Server
340 //checking - existence of SALOME_Session_Server
343 IsPassed = pingServer(iteration, errDescription);
345 *errMessage = new QString(errDescription);
351 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
353 ASSERT(iteration<=myServersCount);
356 for (int i = myRepeat; i ; i--)
360 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
361 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
362 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
368 CORBA::Object_var obj = NS.Resolve("/Registry");
369 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
370 if (!CORBA::is_nil(registry))
372 MESSAGE("/Registry is found");
375 MESSAGE("Registry was activated");
382 string hostname = GetHostname();
383 string containerName = "/Containers/";
384 containerName += hostname;
385 containerName += "/SuperVisionContainer";
387 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
388 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
389 if (!CORBA::is_nil(SVcontainer))
393 MESSAGE("SuperVisionContainer container was activated");
400 CORBA::Object_var obj = NS.Resolve("Kernel/ModulCatalog");
401 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
402 if (!CORBA::is_nil(catalog))
404 MESSAGE("/Kernel/ModulCatalog is found");
407 MESSAGE("ModuleCatalog was activated");
414 CORBA::Object_var obj = NS.Resolve("Kernel/Session");
415 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
416 if (!CORBA::is_nil(session))
418 MESSAGE("/Kernel/Session is found");
421 MESSAGE("SALOME_Session was activated");
428 string hostname = GetHostname();
429 string containerName = "/Containers/";
430 containerName += hostname;
431 containerName += "/FactoryServer";
433 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
434 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
435 if (!CORBA::is_nil(FScontainer))
439 MESSAGE("FactoryServer container was activated");
446 string hostname = GetHostname();
447 string containerName = "/Containers/";
448 containerName += hostname;
449 containerName += "/FactoryServerPy";
451 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
452 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
453 if (!CORBA::is_nil(FSPcontainer))
455 FSPcontainer->ping();
457 MESSAGE("FactoryServerPy container was activated");
464 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
465 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
466 if (!CORBA::is_nil(studyManager))
468 MESSAGE("/myStudyManager is found");
469 studyManager->ping();
471 MESSAGE("StudyManager was activated");
478 catch (ServiceUnreachable&)
480 MESSAGE("Caught exception: Naming Service Unreachable");
481 errorDescr = "Caught exception: Naming Service Unreachable";
483 catch (CORBA::COMM_FAILURE&)
485 MESSAGE("Caught CORBA::SystemException CommFailure.");
486 errorDescr = "Caught CORBA::SystemException CommFailure";
488 catch (CORBA::SystemException&)
490 MESSAGE("Caught CORBA::SystemException.");
491 errorDescr = "Caught CORBA::SystemException";
493 catch (CORBA::Exception&)
495 MESSAGE("Caught CORBA::Exception.");
496 errorDescr = "Caught CORBA::Exception";
500 MESSAGE("Caught unknown exception.");
501 errorDescr = "Caught unknown exception";
503 QThread::usleep(myDelay);
511 serverName = "SALOME_Registry_Server is not loaded. ";
514 serverName = "SALOMEDS_Server is not loaded. ";
517 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
520 serverName = "SALOME_Session_Server is not loaded. ";
523 serverName = "SALOME_Container FactoryServer is not loaded. ";
526 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
529 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
532 errMessage = serverName + errorDescr;
537 static const char* SEPARATOR = ":";
539 QString findFile( QString filename )
544 // Try CSF_ResourcesDefaults env.var directory ( or directory list )
545 cenv = getenv( "CSF_ResourcesDefaults" );
547 dir.sprintf( "%s", cenv );
548 if ( !dir.isEmpty() ) {
549 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
550 for ( int i = 0; i < dirList.count(); i++ ) {
551 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
552 if ( fileInfo.isFile() && fileInfo.exists() )
553 return fileInfo.filePath();
557 // Try ${HOME}/.salome/resources directory
558 cenv = getenv( "HOME" );
560 dir.sprintf( "%s", cenv );
561 if ( !dir.isEmpty() ) {
562 dir = addSlash(dir) ;
563 dir = dir + ".salome" ;
564 dir = addSlash(dir) ;
565 dir = dir + "resources" ;
566 dir = addSlash(dir) ;
567 QFileInfo fileInfo( dir + filename );
568 if ( fileInfo.isFile() && fileInfo.exists() )
569 return fileInfo.filePath();
572 // Try ${SALOME_SITE_DIR}/share/salome/resources directory
573 cenv = getenv( "SALOME_SITE_DIR" );
575 dir.sprintf( "%s", cenv );
576 if ( !dir.isEmpty() ) {
577 dir = addSlash(dir) ;
578 dir = dir + "share" ;
579 dir = addSlash(dir) ;
580 dir = dir + "salome" ;
581 dir = addSlash(dir) ;
582 dir = dir + "resources" ;
583 dir = addSlash(dir) ;
584 QFileInfo fileInfo( dir + filename );
585 if ( fileInfo.isFile() && fileInfo.exists() )
586 return fileInfo.filePath();
589 // Try ${SALOME_ROOT_DIR}/share/salome/resources directory
590 cenv = getenv( "SALOME_ROOT_DIR" );
592 dir.sprintf( "%s", cenv );
593 if ( !dir.isEmpty() ) {
594 dir = addSlash(dir) ;
595 dir = dir + "share" ;
596 dir = addSlash(dir) ;
597 dir = dir + "salome" ;
598 dir = addSlash(dir) ;
599 dir = dir + "resources" ;
600 dir = addSlash(dir) ;
601 QFileInfo fileInfo( dir + filename );
602 if ( fileInfo.isFile() && fileInfo.exists() )
603 return fileInfo.filePath();
608 QString addSlash( const QString& path )
610 if (!path.isNull()) {
616 if ( path.at(path.length()-1) != slash )