1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : InquireServersQThread.cxx
6 // Author : Vasily RUSYAEV
10 #include "InquireServersQThread.h"
13 #include <qpushbutton.h>
14 #include <qabstractlayout.h>
18 #include <qmessagebox.h>
20 #include <qfileinfo.h>
21 #include <qstringlist.h>
23 //VRV: porting on Qt 3.0.5
24 #if QT_VERSION >= 0x030005
25 #include <qdesktopwidget.h>
27 //VRV: porting on Qt 3.0.5
31 #include <SALOMEconfig.h>
33 #include "Utils_ORB_INIT.hxx"
34 #include "Utils_SINGLETON.hxx"
35 #include "SALOME_NamingService.hxx"
36 #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();
199 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
200 : receiver(r), myExitStatus(0)
206 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
207 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
208 cenv = getenv( "CSF_RepeatServerRequest" );
210 int val = atoi( cenv );
214 //define delay time between two attempts
215 myDelay = 1000000; // 1 second
216 QString str = "Loading: ";
217 myMessages[0] = "Checking naming service...";
218 myMessages[1] = str + "SALOME_Registry_Server" + "...";
219 myMessages[2] = str + "SALOMEDS_Server" + "...";
220 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
221 myMessages[4] = str + "SALOME_Session_Server" + "...";
226 r->getArgs( _argc, &_argv);
228 // NRI : Temporary solution for SuperVisionContainer
229 for ( int i=1; i<=(_argc-1); i++) {
230 if (strcmp(_argv[i],"CPP")==0) {
231 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
234 if (strcmp(_argv[i],"PYTHON")==0) {
235 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
238 if (strcmp(_argv[i],"SUPERV")==0) {
239 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
242 if (strcmp(_argv[i],"GUI")==0) {
248 void InquireServersQThread::run()
252 for (int i=1; i<=8; i++)
254 if ( myMessages[i-1].isEmpty() ) {
257 //myExitStatus should be 0 because all servers exist and work
259 //we should send QCloseEvent in order to close this widget (and remove from screen)
260 QThread::postEvent ( receiver , new QCloseEvent() );
264 QString *message = new QString(myMessages[i-1]);
265 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
266 QThread::usleep(200000);
268 bool result = AskServer(i,&errMsg);
271 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
275 //myExitStatus should be 0 because all servers exist and work
277 //we should send QCloseEvent in order to close this widget (and remove from screen)
278 QThread::postEvent ( receiver , new QCloseEvent() );
283 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
284 //myExitStatus should be 1 because we didn't receive response from server
292 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
294 ASSERT(iteration<=myServersCount);
295 //will be set true if we get response from server
296 bool IsPassed = false;
297 QString errDescription;
301 //First checking - existence of Naming Service
302 for (int i = myRepeat; i ; i--)
306 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
307 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
308 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
309 if (CORBA::is_nil(_root_context))
315 catch(CORBA::COMM_FAILURE&)
317 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
321 MESSAGE("Unknown Exception: unable to contact the naming service");
323 QThread::usleep(myDelay);
326 *errMessage = new QString("unable to contact the naming service");
329 //checking - existence of SALOME_Registry_Server
331 //checking - existence of SALOMEDS_Server
333 //checking - existence of SALOME_ModuleCatalog_Server
335 //checking - existence of SALOME_Session_Server
337 //checking - existence of SALOME_Container FactoryServer
339 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
341 //checking - existence of SALOME_Container SuperVisionContainer
344 IsPassed = pingServer(iteration, errDescription);
346 *errMessage = new QString(errDescription);
352 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
354 ASSERT(iteration<=myServersCount);
357 for (int i = myRepeat; i ; i--)
361 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
362 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
363 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
369 CORBA::Object_var obj = NS.Resolve("/Registry");
370 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
371 if (!CORBA::is_nil(registry))
373 MESSAGE("/Registry is found");
376 MESSAGE("Registry was activated");
383 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
384 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
385 if (!CORBA::is_nil(studyManager))
392 MESSAGE("/myStudyManager is found");
393 studyManager->ping();
395 MESSAGE("StudyManager was activated");
402 CORBA::Object_var obj = NS.Resolve("Kernel/ModulCatalog");
403 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
404 if (!CORBA::is_nil(catalog))
406 MESSAGE("/Kernel/ModulCatalog is found");
409 MESSAGE("ModuleCatalog was activated");
416 CORBA::Object_var obj = NS.Resolve("Kernel/Session");
417 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
418 if (!CORBA::is_nil(session))
420 MESSAGE("/Kernel/Session is found");
423 MESSAGE("SALOME_Session was activated");
430 string hostname = GetHostname();
431 string containerName = "/Containers/";
432 containerName += hostname;
433 containerName += "/FactoryServer";
435 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
436 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
437 if (!CORBA::is_nil(FScontainer))
441 MESSAGE("FactoryServer container was activated");
448 string hostname = GetHostname();
449 string containerName = "/Containers/";
450 containerName += hostname;
451 containerName += "/FactoryServerPy";
453 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
454 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
455 if (!CORBA::is_nil(FSPcontainer))
457 FSPcontainer->ping();
459 MESSAGE("FactoryServerPy container was activated");
466 string hostname = GetHostname();
467 string containerName = "/Containers/";
468 containerName += hostname;
469 containerName += "/SuperVisionContainer";
471 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
472 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
473 if (!CORBA::is_nil(SVcontainer))
478 MESSAGE("SuperVisionContainer container was activated");
485 catch (ServiceUnreachable&)
487 MESSAGE("Caught exception: Naming Service Unreachable");
488 errorDescr = "Caught exception: Naming Service Unreachable";
490 catch (CORBA::COMM_FAILURE&)
492 MESSAGE("Caught CORBA::SystemException CommFailure.");
493 errorDescr = "Caught CORBA::SystemException CommFailure";
495 catch (CORBA::SystemException&)
497 MESSAGE("Caught CORBA::SystemException.");
498 errorDescr = "Caught CORBA::SystemException";
500 catch (CORBA::Exception&)
502 MESSAGE("Caught CORBA::Exception.");
503 errorDescr = "Caught CORBA::Exception";
507 MESSAGE("Caught unknown exception.");
508 errorDescr = "Caught unknown exception";
510 QThread::usleep(myDelay);
518 serverName = "SALOME_Registry_Server is not loaded. ";
521 serverName = "SALOMEDS_Server is not loaded. ";
524 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
527 serverName = "SALOME_Session_Server is not loaded. ";
530 serverName = "SALOME_Container FactoryServer is not loaded. ";
533 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
536 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
539 errMessage = serverName + errorDescr;
544 static const char* SEPARATOR = ":";
546 QString findFile( QString filename )
551 // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
552 cenv = getenv( "KERNEL_ROOT_DIR" );
554 dir.sprintf( "%s", cenv );
555 if ( !dir.isEmpty() ) {
556 dir = addSlash(dir) ;
557 dir = dir + "share" ;
558 dir = addSlash(dir) ;
559 dir = dir + "salome" ;
560 dir = addSlash(dir) ;
561 dir = dir + "resources" ;
562 dir = addSlash(dir) ;
563 QFileInfo fileInfo( dir + filename );
564 if ( fileInfo.isFile() && fileInfo.exists() )
565 return fileInfo.filePath();
568 // Try CSF_ResourcesDefaults env.var directory ( or directory list )
569 cenv = getenv( "CSF_ResourcesDefaults" );
571 dir.sprintf( "%s", cenv );
572 if ( !dir.isEmpty() ) {
573 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
574 for ( int i = 0; i < dirList.count(); i++ ) {
575 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
576 if ( fileInfo.isFile() && fileInfo.exists() )
577 return fileInfo.filePath();
581 // Try ${HOME}/.salome/resources directory
582 cenv = getenv( "HOME" );
584 dir.sprintf( "%s", cenv );
585 if ( !dir.isEmpty() ) {
586 dir = addSlash(dir) ;
587 dir = dir + ".salome" ;
588 dir = addSlash(dir) ;
589 dir = dir + "resources" ;
590 dir = addSlash(dir) ;
591 QFileInfo fileInfo( dir + filename );
592 if ( fileInfo.isFile() && fileInfo.exists() )
593 return fileInfo.filePath();
598 QString addSlash( const QString& path )
600 if (!path.isNull()) {
606 if ( path.at(path.length()-1) != slash )