2 // File: InquireServersQThread.cxx
3 // Created: Mon Oct 21 17:26:42 2002
4 // Author: Vasily RUSYAEV <vrv@nnov.matra-dtv.fr>
7 #include "InquireServersQThread.h"
10 #include <qpushbutton.h>
11 #include <qabstractlayout.h>
15 #include <qmessagebox.h>
17 #include <qfileinfo.h>
18 #include <qstringlist.h>
20 //VRV: porting on Qt 3.0.5
21 #if QT_VERSION >= 0x030005
22 #include <qdesktopwidget.h>
24 //VRV: porting on Qt 3.0.5
28 #include <SALOMEconfig.h>
30 #include "Utils_ORB_INIT.hxx"
31 #include "Utils_SINGLETON.hxx"
32 #include "SALOME_NamingService.hxx"
33 #include "utilities.h"
36 #include CORBA_CLIENT_HEADER(SALOME_Session)
37 #include CORBA_CLIENT_HEADER(SALOME_Registry)
38 #include CORBA_CLIENT_HEADER(SALOMEDS)
39 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
40 #include CORBA_CLIENT_HEADER(SALOME_Component)
43 #define SPACING_SIZE 3
45 static QString findFile( QString filename );
46 static QString addSlash( const QString& path );
48 InquireServersGUI::InquireServersGUI()
49 : QVBox(0, "SFA splash", Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder )
51 myThread = new InquireServersQThread(this);
53 // 1. Polish the appearance
54 setMargin( MARGIN_SIZE );
55 setSpacing( SPACING_SIZE );
56 setFrameStyle( QFrame::Plain | QFrame::Box );
58 setMinimumSize( 200, 150 );
61 QFrame* frm = new QFrame( this );
62 frm->setFrameStyle( QFrame::Box | QFrame::Raised );
63 QHBoxLayout* frmLayout = new QHBoxLayout( frm );
64 frmLayout->setMargin( MARGIN_SIZE );
66 splash = new QLabel( frm, "splash" );
67 frmLayout->addWidget( splash );
69 QString path = findFile( "Application-Splash.png" );
70 splash->setPixmap( QPixmap( path ) );
73 myPrgBar = new QProgressBar( this, "QProgressBar" );
74 myPrgBar->setFixedWidth( splash->pixmap()->isNull() ? 180 : splash->sizeHint().width() );
75 //Sets the total number of steps .
76 myPrgBar->setTotalSteps ( myThread->getInquiredServers() );
77 myPrgBar->setProgress( 0 );
80 QWidget* aWgt1 = new QWidget( this );
81 QHBoxLayout* aHBoxLayout1 = new QHBoxLayout( aWgt1 );
82 myLabel = new QLabel( tr( "Loading:" ), aWgt1 );
83 myLabel->setFixedWidth( splash->pixmap()->isNull() ? 180 : splash->sizeHint().width() );
84 myLabel->setAlignment( AlignLeft );
85 QFont theFont = myLabel->font();
86 theFont.setBold(true);
87 myLabel->setFont( theFont );
88 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
89 aHBoxLayout1->addWidget( myLabel );
90 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
93 QWidget* aWgt = new QWidget( this );
94 QHBoxLayout* aHBoxLayout = new QHBoxLayout( aWgt );
95 QPushButton* myCancelBtn = new QPushButton( tr( "Cancel" ), aWgt );
96 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
97 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
98 aHBoxLayout->addWidget( myCancelBtn );
99 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
102 #if QT_VERSION >= 0x030005
103 QDesktopWidget *d = QApplication::desktop();
105 QWidget *d = QApplication::desktop();
107 //VRV: porting on Qt 3.0.5
109 int w = d->width(); // returns desktop width
110 int h = d->height(); // returns desktop height
111 QSize mySize = sizeHint (); // returns widget size
112 int Xc = ( w - mySize.width() ) / 2;
113 int Yc = ( h - mySize.height() ) / 2;
119 InquireServersGUI::~InquireServersGUI()
124 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
126 _argc = qApp->argc();
127 *_argv = qApp->argv();
130 //=================================================================================
131 // function : ClickOnCancel()
132 // purpose : cancel loading of SALOME
133 //=================================================================================
134 void InquireServersGUI::ClickOnCancel()
136 //it's necessary to stop asking servers
138 //Also we should send QCloseEvent in order to close this widget (and remove from screen)
139 //QThread::postEvent ( this, new QCloseEvent() );
143 void InquireServersGUI::closeEvent ( QCloseEvent * pe)
145 //default implementation calls e->accept(), which hides this widget.
146 //See the QCloseEvent documentation for more details.
148 QApplication::flushX ();
149 QApplication::syncX ();
153 void InquireServersGUI::customEvent( QCustomEvent* pe )
157 case InquireEvent::ProgressEvent:
159 int* value = ( int* )(( InquireEvent*)pe)->data();
160 myPrgBar->setProgress( *value );
163 case InquireEvent::ProgressEventLabel:
165 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
166 myLabel->setText( *myString );
169 case InquireEvent::ProgressEventError:
171 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
172 QString appName = "SALOME Professional";
173 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
174 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
175 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
187 int InquireServersGUI::getExitStatus()
189 myThread->getExitStatus();
192 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
193 : receiver(r), myExitStatus(0)
199 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
200 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
201 cenv = getenv( "CSF_RepeatServerRequest" );
203 int val = atoi( cenv );
207 //define delay time between two attempts
208 myDelay = 1000000; // 1 second
209 QString str = "Loading: ";
210 myMessages[0] = "Checking naming service...";
211 myMessages[1] = str + "SALOME_Registry_Server" + "...";
212 myMessages[2] = str + "SALOMEDS_Server" + "...";
213 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
214 myMessages[4] = str + "SALOME_Session_Server" + "...";
215 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
216 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
217 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
219 r->getArgs( _argc, &_argv);
223 void InquireServersQThread::run()
227 for (int i=1; i<=myServersCount; i++)
229 QString *message = new QString(myMessages[i-1]);
230 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
231 QThread::usleep(200000);
233 bool result = AskServer(i,&errMsg);
236 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
237 if (i==myServersCount)
240 //myExitStatus should be 0 because all servers exist and work
242 //we should send QCloseEvent in order to close this widget (and remove from screen)
243 QThread::postEvent ( receiver , new QCloseEvent() );
248 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
249 //myExitStatus should be 1 because we didn't receive response from server
257 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
259 ASSERT(iteration<=myServersCount);
260 //will be set true if we get response from server
261 bool IsPassed = false;
262 QString errDescription;
266 //First checking - existence of Naming Service
267 for (int i = myRepeat; i ; i--)
271 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
272 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
273 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
274 if (CORBA::is_nil(_root_context))
280 catch(CORBA::COMM_FAILURE&)
282 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
286 MESSAGE("Unknown Exception: unable to contact the naming service");
288 QThread::usleep(myDelay);
291 *errMessage = new QString("unable to contact the naming service");
294 //checking - existence of SALOME_Registry_Server
296 //checking - existence of SALOMEDS_Server
298 //checking - existence of SALOME_ModuleCatalog_Server
300 //checking - existence of SALOME_Session_Server
302 //checking - existence of SALOME_Container FactoryServer
304 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
306 //checking - existence of SALOME_Container SuperVisionContainer
307 IsPassed = pingServer(iteration, errDescription);
309 *errMessage = new QString(errDescription);
315 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
317 ASSERT(iteration<=myServersCount);
320 for (int i = myRepeat; i ; i--)
324 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
325 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
326 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
332 CORBA::Object_var obj = NS.Resolve("/Registry");
333 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
334 if (!CORBA::is_nil(registry))
336 MESSAGE("/Registry is found");
339 MESSAGE("Registry was activated");
346 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
347 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
348 if (!CORBA::is_nil(studyManager))
350 MESSAGE("/myStudyManager is found");
351 studyManager->ping();
353 MESSAGE("StudyManager was activated");
360 CORBA::Object_var obj = NS.Resolve("Kernel/ModulCatalog");
361 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
362 if (!CORBA::is_nil(catalog))
364 MESSAGE("/Kernel/ModulCatalog is found");
367 MESSAGE("ModuleCatalog was activated");
374 CORBA::Object_var obj = NS.Resolve("Kernel/Session");
375 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
376 if (!CORBA::is_nil(session))
378 MESSAGE("/Kernel/Session is found");
381 MESSAGE("SALOME_Session was activated");
388 string hostname = GetHostname();
389 string containerName = "/Containers/";
390 containerName += hostname;
391 containerName += "/FactoryServer";
393 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
394 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
395 if (!CORBA::is_nil(FScontainer))
399 MESSAGE("FactoryServer container was activated");
406 string hostname = GetHostname();
407 string containerName = "/Containers/";
408 containerName += hostname;
409 containerName += "/FactoryServerPy";
411 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
412 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
413 if (!CORBA::is_nil(FSPcontainer))
415 FSPcontainer->ping();
417 MESSAGE("FactoryServerPy container was activated");
424 string hostname = GetHostname();
425 string containerName = "/Containers/";
426 containerName += hostname;
427 containerName += "/SuperVisionContainer";
429 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
430 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
431 if (!CORBA::is_nil(SVcontainer))
435 MESSAGE("SuperVisionContainer container was activated");
442 catch (ServiceUnreachable&)
444 MESSAGE("Caught exception: Naming Service Unreachable");
445 errorDescr = "Caught exception: Naming Service Unreachable";
447 catch (CORBA::COMM_FAILURE&)
449 MESSAGE("Caught CORBA::SystemException CommFailure.");
450 errorDescr = "Caught CORBA::SystemException CommFailure";
452 catch (CORBA::SystemException&)
454 MESSAGE("Caught CORBA::SystemException.");
455 errorDescr = "Caught CORBA::SystemException";
457 catch (CORBA::Exception&)
459 MESSAGE("Caught CORBA::Exception.");
460 errorDescr = "Caught CORBA::Exception";
464 MESSAGE("Caught unknown exception.");
465 errorDescr = "Caught unknown exception";
467 QThread::usleep(myDelay);
475 serverName = "SALOME_Registry_Server is not loaded. ";
478 serverName = "SALOMEDS_Server is not loaded. ";
481 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
484 serverName = "SALOME_Session_Server is not loaded. ";
487 serverName = "SALOME_Container FactoryServer is not loaded. ";
490 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
493 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
496 errMessage = serverName + errorDescr;
501 static const char* SEPARATOR = ":";
503 QString findFile( QString filename )
508 // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
509 cenv = getenv( "KERNEL_ROOT_DIR" );
511 dir.sprintf( "%s", cenv );
512 if ( !dir.isEmpty() ) {
513 dir = addSlash(dir) ;
514 dir = dir + "share" ;
515 dir = addSlash(dir) ;
516 dir = dir + "salome" ;
517 dir = addSlash(dir) ;
518 dir = dir + "resources" ;
519 dir = addSlash(dir) ;
520 QFileInfo fileInfo( dir + filename );
521 if ( fileInfo.isFile() && fileInfo.exists() )
522 return fileInfo.filePath();
525 // Try CSF_ResourcesDefaults env.var directory ( or directory list )
526 cenv = getenv( "CSF_ResourcesDefaults" );
528 dir.sprintf( "%s", cenv );
529 if ( !dir.isEmpty() ) {
530 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
531 for ( int i = 0; i < dirList.count(); i++ ) {
532 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
533 if ( fileInfo.isFile() && fileInfo.exists() )
534 return fileInfo.filePath();
538 // Try ${HOME}/.salome/resources directory
539 cenv = getenv( "HOME" );
541 dir.sprintf( "%s", cenv );
542 if ( !dir.isEmpty() ) {
543 dir = addSlash(dir) ;
544 dir = dir + ".salome" ;
545 dir = addSlash(dir) ;
546 dir = dir + "resources" ;
547 dir = addSlash(dir) ;
548 QFileInfo fileInfo( dir + filename );
549 if ( fileInfo.isFile() && fileInfo.exists() )
550 return fileInfo.filePath();
553 // Try ${SALOME_SITE_DIR}/share/salome/resources directory
554 cenv = getenv( "SALOME_SITE_DIR" );
556 dir.sprintf( "%s", cenv );
557 if ( !dir.isEmpty() ) {
558 dir = addSlash(dir) ;
559 dir = dir + "share" ;
560 dir = addSlash(dir) ;
561 dir = dir + "salome" ;
562 dir = addSlash(dir) ;
563 dir = dir + "resources" ;
564 dir = addSlash(dir) ;
565 QFileInfo fileInfo( dir + filename );
566 if ( fileInfo.isFile() && fileInfo.exists() )
567 return fileInfo.filePath();
570 // Try ${SALOME_ROOT_DIR}/share/salome/resources directory
571 cenv = getenv( "SALOME_ROOT_DIR" );
573 dir.sprintf( "%s", cenv );
574 if ( !dir.isEmpty() ) {
575 dir = addSlash(dir) ;
576 dir = dir + "share" ;
577 dir = addSlash(dir) ;
578 dir = dir + "salome" ;
579 dir = addSlash(dir) ;
580 dir = dir + "resources" ;
581 dir = addSlash(dir) ;
582 QFileInfo fileInfo( dir + filename );
583 if ( fileInfo.isFile() && fileInfo.exists() )
584 return fileInfo.filePath();
589 QString addSlash( const QString& path )
591 if (!path.isNull()) {
597 if ( path.at(path.length()-1) != slash )