1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : InquireServersQThread.cxx
6 // Author : Vasily RUSYAEV
10 #include "utilities.h"
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"
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 | WType_TopLevel | WStyle_StaysOnTop | WX11BypassWM )
57 myThread = new InquireServersQThread(this);
59 // 1. Polish the appearance
60 setMargin( MARGIN_SIZE );
61 setSpacing( SPACING_SIZE );
62 setFrameStyle( QFrame::Plain | QFrame::Box );
64 setMinimumSize( 200, 150 );
67 QFrame* frm = new QFrame( this );
68 frm->setFrameStyle( QFrame::Box | QFrame::Raised );
69 QHBoxLayout* frmLayout = new QHBoxLayout( frm );
70 frmLayout->setMargin( MARGIN_SIZE );
72 splash = new QLabel( frm, "splash" );
73 frmLayout->addWidget( splash );
75 QString path = findFile( "Application-Splash.png" );
76 splash->setPixmap( QPixmap( path ) );
79 myPrgBar = new QProgressBar( this, "QProgressBar" );
80 myPrgBar->setFixedWidth( splash->pixmap()->isNull() ? 180 : splash->sizeHint().width() );
81 //Sets the total number of steps .
82 myPrgBar->setTotalSteps ( myThread->getInquiredServers() );
83 myPrgBar->setProgress( 0 );
86 QWidget* aWgt1 = new QWidget( this );
87 QHBoxLayout* aHBoxLayout1 = new QHBoxLayout( aWgt1 );
88 myLabel = new QLabel( tr( "Loading:" ), aWgt1 );
89 myLabel->setFixedWidth( splash->pixmap()->isNull() ? 180 : splash->sizeHint().width() );
90 myLabel->setAlignment( AlignLeft );
91 QFont theFont = myLabel->font();
92 theFont.setBold(true);
93 myLabel->setFont( theFont );
94 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
95 aHBoxLayout1->addWidget( myLabel );
96 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
99 QWidget* aWgt = new QWidget( this );
100 QHBoxLayout* aHBoxLayout = new QHBoxLayout( aWgt );
101 QPushButton* myCancelBtn = new QPushButton( tr( "Cancel" ), aWgt );
102 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
103 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
104 aHBoxLayout->addWidget( myCancelBtn );
105 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
108 #if QT_VERSION >= 0x030005
109 QDesktopWidget *d = QApplication::desktop();
111 QWidget *d = QApplication::desktop();
113 //VRV: porting on Qt 3.0.5
115 int w = d->width(); // returns desktop width
116 int h = d->height(); // returns desktop height
117 QSize mySize = sizeHint (); // returns widget size
118 int Xc = ( w - mySize.width() ) / 2;
119 int Yc = ( h - mySize.height() ) / 2;
125 InquireServersGUI::~InquireServersGUI()
130 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
132 _argc = qApp->argc();
133 *_argv = qApp->argv();
136 //=================================================================================
137 // function : ClickOnCancel()
138 // purpose : cancel loading of SALOME
139 //=================================================================================
140 void InquireServersGUI::ClickOnCancel()
142 //it's necessary to stop asking servers
145 //Also we should send QCloseEvent in order to close this widget (and remove from screen)
146 //QThread::postEvent ( this, new QCloseEvent() );
150 void InquireServersGUI::closeEvent ( QCloseEvent * pe)
152 //default implementation calls e->accept(), which hides this widget.
153 //See the QCloseEvent documentation for more details.
155 QApplication::flushX ();
156 QApplication::syncX ();
160 void InquireServersGUI::customEvent( QCustomEvent* pe )
164 case InquireEvent::ProgressEvent:
166 int* value = ( int* )(( InquireEvent*)pe)->data();
167 myPrgBar->setProgress( *value );
170 case InquireEvent::ProgressEventLabel:
172 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
173 myLabel->setText( *myString );
176 case InquireEvent::ProgressEventError:
178 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
179 QString appName = "SALOME Professional";
180 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
181 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
182 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
194 int InquireServersGUI::getExitStatus()
196 return 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],"PY")==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 ${SALOME_SITE_DIR}/share/${SALOME_SITE_NAME}/resources directory
552 cenv = getenv( "SALOME_SITE_DIR" );
554 dir.sprintf( "%s", cenv );
555 if ( !dir.isEmpty() ) {
556 dir = addSlash(dir) ;
557 dir = dir + "share" ;
558 dir = addSlash(dir) ;
559 cenv = getenv( "SALOME_SITE_NAME" );
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 ${KERNEL_ROOT_DIR}/share/salome/resources directory
573 cenv = getenv( "KERNEL_ROOT_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 CSF_ResourcesDefaults env.var directory ( or directory list )
590 cenv = getenv( "CSF_ResourcesDefaults" );
592 dir.sprintf( "%s", cenv );
593 if ( !dir.isEmpty() ) {
594 QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
595 for ( int i = 0; i < dirList.count(); i++ ) {
596 QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
597 if ( fileInfo.isFile() && fileInfo.exists() )
598 return fileInfo.filePath();
602 // Try ${HOME}/.salome/resources directory
603 cenv = getenv( "HOME" );
605 dir.sprintf( "%s", cenv );
606 if ( !dir.isEmpty() ) {
607 dir = addSlash(dir) ;
608 dir = dir + ".salome" ;
609 dir = addSlash(dir) ;
610 dir = dir + "resources" ;
611 dir = addSlash(dir) ;
612 QFileInfo fileInfo( dir + filename );
613 if ( fileInfo.isFile() && fileInfo.exists() )
614 return fileInfo.filePath();
619 QString addSlash( const QString& path )
621 if (!path.isNull()) {
627 if ( path.at(path.length()-1) != slash )