1 //=============================================================================
2 // File : InquireServersQThread.cxx
3 // Created : Mon Oct 21 17:26:42 2002
4 // Author : Vasily RUSYAEV
6 // Copyright : EDF 2001
8 //=============================================================================
12 #include "InquireServersQThread.h"
15 #include <qpushbutton.h>
16 #include <qabstractlayout.h>
20 #include <qmessagebox.h>
22 #include <qfileinfo.h>
23 #include <qstringlist.h>
25 //VRV: porting on Qt 3.0.5
26 #if QT_VERSION >= 0x030005
27 #include <qdesktopwidget.h>
29 //VRV: porting on Qt 3.0.5
33 #include <SALOMEconfig.h>
35 #include "Utils_ORB_INIT.hxx"
36 #include "Utils_SINGLETON.hxx"
37 #include "SALOME_NamingService.hxx"
38 #include "utilities.h"
41 #include CORBA_CLIENT_HEADER(SALOME_Session)
42 #include CORBA_CLIENT_HEADER(SALOME_Registry)
43 #include CORBA_CLIENT_HEADER(SALOMEDS)
44 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
45 #include CORBA_CLIENT_HEADER(SALOME_Component)
48 #define SPACING_SIZE 3
50 static QString findFile( QString filename );
51 static QString addSlash( const QString& path );
53 InquireServersGUI::InquireServersGUI()
54 : 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
143 //Also we should send QCloseEvent in order to close this widget (and remove from screen)
144 //QThread::postEvent ( this, new QCloseEvent() );
148 void InquireServersGUI::closeEvent ( QCloseEvent * pe)
150 //default implementation calls e->accept(), which hides this widget.
151 //See the QCloseEvent documentation for more details.
153 QApplication::flushX ();
154 QApplication::syncX ();
158 void InquireServersGUI::customEvent( QCustomEvent* pe )
162 case InquireEvent::ProgressEvent:
164 int* value = ( int* )(( InquireEvent*)pe)->data();
165 myPrgBar->setProgress( *value );
168 case InquireEvent::ProgressEventLabel:
170 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
171 myLabel->setText( *myString );
174 case InquireEvent::ProgressEventError:
176 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
177 QString appName = "SALOME Professional";
178 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
179 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
180 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
192 int InquireServersGUI::getExitStatus()
194 myThread->getExitStatus();
197 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
198 : receiver(r), myExitStatus(0)
204 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
205 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
206 cenv = getenv( "CSF_RepeatServerRequest" );
208 int val = atoi( cenv );
212 //define delay time between two attempts
213 myDelay = 1000000; // 1 second
214 QString str = "Loading: ";
215 myMessages[0] = "Checking naming service...";
216 myMessages[1] = str + "SALOME_Registry_Server" + "...";
217 myMessages[2] = str + "SALOMEDS_Server" + "...";
218 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
219 myMessages[4] = str + "SALOME_Session_Server" + "...";
224 r->getArgs( _argc, &_argv);
226 // NRI : Temporary solution for SuperVisionContainer
227 for ( int i=1; i<=_argc; i++) {
228 if (strcmp(_argv[i],"CPP")==0)
229 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
230 if (strcmp(_argv[i],"PYTHON")==0)
231 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
232 if (strcmp(_argv[i],"SUPERV")==0)
233 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
237 void InquireServersQThread::run()
241 for (int i=1; i<=myServersCount; i++)
243 if ( myMessages[i-1].isEmpty() ) continue;
244 QString *message = new QString(myMessages[i-1]);
245 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
246 QThread::usleep(200000);
248 bool result = AskServer(i,&errMsg);
251 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
252 if (i==myServersCount)
255 //myExitStatus should be 0 because all servers exist and work
257 //we should send QCloseEvent in order to close this widget (and remove from screen)
258 QThread::postEvent ( receiver , new QCloseEvent() );
263 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
264 //myExitStatus should be 1 because we didn't receive response from server
272 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
274 ASSERT(iteration<=myServersCount);
275 //will be set true if we get response from server
276 bool IsPassed = false;
277 QString errDescription;
281 //First checking - existence of Naming Service
282 for (int i = myRepeat; i ; i--)
286 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
287 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
288 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
289 if (CORBA::is_nil(_root_context))
295 catch(CORBA::COMM_FAILURE&)
297 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
301 MESSAGE("Unknown Exception: unable to contact the naming service");
303 QThread::usleep(myDelay);
306 *errMessage = new QString("unable to contact the naming service");
309 //checking - existence of SALOME_Registry_Server
311 //checking - existence of SALOMEDS_Server
313 //checking - existence of SALOME_ModuleCatalog_Server
315 //checking - existence of SALOME_Session_Server
317 //checking - existence of SALOME_Container FactoryServer
319 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
321 //checking - existence of SALOME_Container SuperVisionContainer
322 IsPassed = pingServer(iteration, errDescription);
324 *errMessage = new QString(errDescription);
330 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
332 ASSERT(iteration<=myServersCount);
335 for (int i = myRepeat; i ; i--)
339 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
340 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
341 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
347 CORBA::Object_var obj = NS.Resolve("/Registry");
348 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
349 if (!CORBA::is_nil(registry))
351 MESSAGE("/Registry is found");
354 MESSAGE("Registry was activated");
361 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
362 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
363 if (!CORBA::is_nil(studyManager))
365 MESSAGE("/myStudyManager is found");
366 studyManager->ping();
368 MESSAGE("StudyManager was activated");
375 CORBA::Object_var obj = NS.Resolve("Kernel/ModulCatalog");
376 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
377 if (!CORBA::is_nil(catalog))
379 MESSAGE("/Kernel/ModulCatalog is found");
382 MESSAGE("ModuleCatalog was activated");
389 CORBA::Object_var obj = NS.Resolve("Kernel/Session");
390 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
391 if (!CORBA::is_nil(session))
393 MESSAGE("/Kernel/Session is found");
396 MESSAGE("SALOME_Session was activated");
403 string hostname = GetHostname();
404 string containerName = "/Containers/";
405 containerName += hostname;
406 containerName += "/FactoryServer";
408 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
409 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
410 if (!CORBA::is_nil(FScontainer))
414 MESSAGE("FactoryServer container was activated");
421 string hostname = GetHostname();
422 string containerName = "/Containers/";
423 containerName += hostname;
424 containerName += "/FactoryServerPy";
426 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
427 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
428 if (!CORBA::is_nil(FSPcontainer))
430 FSPcontainer->ping();
432 MESSAGE("FactoryServerPy container was activated");
439 string hostname = GetHostname();
440 string containerName = "/Containers/";
441 containerName += hostname;
442 containerName += "/SuperVisionContainer";
444 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
445 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
446 if (!CORBA::is_nil(SVcontainer))
450 MESSAGE("SuperVisionContainer container was activated");
457 catch (ServiceUnreachable&)
459 MESSAGE("Caught exception: Naming Service Unreachable");
460 errorDescr = "Caught exception: Naming Service Unreachable";
462 catch (CORBA::COMM_FAILURE&)
464 MESSAGE("Caught CORBA::SystemException CommFailure.");
465 errorDescr = "Caught CORBA::SystemException CommFailure";
467 catch (CORBA::SystemException&)
469 MESSAGE("Caught CORBA::SystemException.");
470 errorDescr = "Caught CORBA::SystemException";
472 catch (CORBA::Exception&)
474 MESSAGE("Caught CORBA::Exception.");
475 errorDescr = "Caught CORBA::Exception";
479 MESSAGE("Caught unknown exception.");
480 errorDescr = "Caught unknown exception";
482 QThread::usleep(myDelay);
490 serverName = "SALOME_Registry_Server is not loaded. ";
493 serverName = "SALOMEDS_Server is not loaded. ";
496 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
499 serverName = "SALOME_Session_Server is not loaded. ";
502 serverName = "SALOME_Container FactoryServer is not loaded. ";
505 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
508 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
511 errMessage = serverName + errorDescr;
516 static const char* SEPARATOR = ":";
518 QString findFile( QString filename )
523 // Try ${SALOME_ROOT_DIR}/share/salome/resources directory
524 cenv = getenv( "KERNEL_ROOT_DIR" );
526 dir.sprintf( "%s", cenv );
527 if ( !dir.isEmpty() ) {
528 dir = addSlash(dir) ;
529 dir = dir + "share" ;
530 dir = addSlash(dir) ;
531 dir = dir + "salome" ;
532 dir = addSlash(dir) ;
533 dir = dir + "resources" ;
534 dir = addSlash(dir) ;
535 QFileInfo fileInfo( dir + filename );
536 if ( fileInfo.isFile() && fileInfo.exists() )
537 return fileInfo.filePath();
540 // Try CSF_ResourcesDefaults env.var directory ( or directory list )
541 cenv = getenv( "CSF_ResourcesDefaults" );
543 dir.sprintf( "%s", cenv );
544 if ( !dir.isEmpty() ) {
545 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
546 for ( int i = 0; i < dirList.count(); i++ ) {
547 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
548 if ( fileInfo.isFile() && fileInfo.exists() )
549 return fileInfo.filePath();
553 // Try ${HOME}/.salome/resources directory
554 cenv = getenv( "HOME" );
556 dir.sprintf( "%s", cenv );
557 if ( !dir.isEmpty() ) {
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 ${SALOME_SITE_DIR}/share/salome/resources directory
569 cenv = getenv( "SALOME_SITE_DIR" );
571 dir.sprintf( "%s", cenv );
572 if ( !dir.isEmpty() ) {
573 dir = addSlash(dir) ;
574 dir = dir + "share" ;
575 dir = addSlash(dir) ;
576 dir = dir + "salome" ;
577 dir = addSlash(dir) ;
578 dir = dir + "resources" ;
579 dir = addSlash(dir) ;
580 QFileInfo fileInfo( dir + filename );
581 if ( fileInfo.isFile() && fileInfo.exists() )
582 return fileInfo.filePath();
585 // Try ${SALOME_ROOT_DIR}/share/salome/resources directory
586 cenv = getenv( "SALOME_ROOT_DIR" );
588 dir.sprintf( "%s", cenv );
589 if ( !dir.isEmpty() ) {
590 dir = addSlash(dir) ;
591 dir = dir + "share" ;
592 dir = addSlash(dir) ;
593 dir = dir + "salome" ;
594 dir = addSlash(dir) ;
595 dir = dir + "resources" ;
596 dir = addSlash(dir) ;
597 QFileInfo fileInfo( dir + filename );
598 if ( fileInfo.isFile() && fileInfo.exists() )
599 return fileInfo.filePath();
604 QString addSlash( const QString& path )
606 if (!path.isNull()) {
612 if ( path.at(path.length()-1) != slash )