1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : InquireServersQThread.cxx
6 // Author : Vasily RUSYAEV
11 #include "InquireServersQThread.h"
13 #include <qapplication.h>
14 #include <qpushbutton.h>
15 #include <qabstractlayout.h>
19 #include <qmessagebox.h>
21 #include <qfileinfo.h>
22 #include <qstringlist.h>
24 #include <qprogressbar.h>
26 //VRV: porting on Qt 3.0.5
27 #if QT_VERSION >= 0x030005
28 #include <qdesktopwidget.h>
30 //VRV: porting on Qt 3.0.5
34 #include <SALOMEconfig.h>
36 #include "Utils_ORB_INIT.hxx"
37 #include "Utils_SINGLETON.hxx"
38 #include "SALOME_NamingService.hxx"
39 #include "utilities.h"
42 #include CORBA_CLIENT_HEADER(SALOME_Session)
43 #include CORBA_CLIENT_HEADER(SALOME_Registry)
44 #include CORBA_CLIENT_HEADER(SALOMEDS)
45 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
46 #include CORBA_CLIENT_HEADER(SALOME_Component)
49 #define SPACING_SIZE 3
51 InquireServersGUI::InquireServersGUI()
52 : QVBox(0, "SFA splash", Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder | WType_TopLevel | WStyle_StaysOnTop | WX11BypassWM )
55 myThread = new InquireServersQThread( this );
57 // 1. Polish the appearance
58 setMargin( MARGIN_SIZE );
59 setSpacing( SPACING_SIZE );
60 setFrameStyle( QFrame::Plain | QFrame::Box );
62 setMinimumSize( 200, 150 );
65 QPalette pal = palette();
66 QColorGroup cg = pal.active();
67 cg.setColor( QColorGroup::Foreground, Qt::darkBlue );
68 cg.setColor( QColorGroup::Background, Qt::white );
69 pal.setActive( cg ); pal.setInactive( cg ); pal.setDisabled( cg );
73 mySplashFrame = new QFrame( this );
74 mySplashFrame->setFrameStyle( QFrame::Box | QFrame::Raised );
75 QHBoxLayout* frmLayout = new QHBoxLayout( mySplashFrame );
76 frmLayout->setMargin( MARGIN_SIZE );
77 mySplash = new QLabel( mySplashFrame, "splash" );
78 frmLayout->addWidget( mySplash );
81 //QPixmap pix = SUIT_ResourceMgr( "SalomeApp" ).loadPixmap( "SalomeApp", tr( "ABOUT" ) );
82 //splash->setPixmap( pix );
85 myPrgBar = new QProgressBar( this, "QProgressBar" );
86 myPrgBar->setFixedWidth( 180 );
87 //Sets the total number of steps .
88 myPrgBar->setPercentageVisible( false );
89 myPrgBar->setIndicatorFollowsStyle( false );
90 myPrgBar->setFixedHeight( 8 );
91 myPrgBar->setFrameStyle( QFrame::Box | QFrame::Plain );
92 myPrgBar->setMargin( 0 );
93 pal = myPrgBar->palette(); cg = pal.active();
94 cg.setColor( QColorGroup::Highlight, Qt::red );
95 pal.setActive( cg ); pal.setInactive( cg ); pal.setDisabled( cg ); myPrgBar->setPalette( pal );
96 myPrgBar->setTotalSteps ( myThread->getInquiredServers() );
97 myPrgBar->setProgress( 0 );
100 QWidget* aWgt1 = new QWidget( this );
101 QHBoxLayout* aHBoxLayout1 = new QHBoxLayout( aWgt1 );
102 myLabel = new QLabel( tr( "Loading:" ), aWgt1 );
103 myLabel->setFixedWidth( 180 );
104 myLabel->setAlignment( AlignLeft );
105 QFont theFont = myLabel->font();
106 theFont.setBold(true);
107 myLabel->setFont( theFont );
108 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
109 aHBoxLayout1->addWidget( myLabel );
110 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
112 // 5. <Cancel> button
113 QWidget* aWgt = new QWidget( this );
114 QHBoxLayout* aHBoxLayout = new QHBoxLayout( aWgt );
115 QPushButton* myCancelBtn = new QPushButton( tr( "Cancel" ), aWgt );
116 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
117 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
118 aHBoxLayout->addWidget( myCancelBtn );
119 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
122 #if QT_VERSION >= 0x030005
123 QDesktopWidget *d = QApplication::desktop();
125 QWidget *d = QApplication::desktop();
127 //VRV: porting on Qt 3.0.5
129 int w = d->width(); // returns desktop width
130 int h = d->height(); // returns desktop height
131 QSize mySize = sizeHint (); // returns widget size
132 int Xc = ( w - mySize.width() ) / 2;
133 int Yc = ( h - mySize.height() ) / 2;
139 void InquireServersGUI::setPixmap( QPixmap pix )
143 mySplash->setPixmap( pix );
144 int w = mySplash->sizeHint().width() + MARGIN_SIZE*2;
145 myPrgBar->setFixedWidth( w );
146 myLabel->setFixedWidth( w );
150 InquireServersGUI::~InquireServersGUI()
155 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
157 _argc = qApp->argc();
158 *_argv = qApp->argv();
161 //=================================================================================
162 // function : ClickOnCancel()
163 // purpose : cancel loading of SALOME
164 //=================================================================================
165 void InquireServersGUI::ClickOnCancel()
167 myThread->stop(); //it's necessary to stop asking servers
172 void InquireServersGUI::customEvent( QCustomEvent* pe )
176 case InquireEvent::ProgressEvent:
178 int* value = ( int* )(( InquireEvent*)pe)->data();
179 myPrgBar->setProgress( *value );
182 case InquireEvent::ProgressEventLabel:
184 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
185 myLabel->setText( *myString );
188 case InquireEvent::ProgressEventError:
190 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
191 QString appName = "SALOME Professional";
192 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
193 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
194 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
206 int InquireServersGUI::getExitStatus()
208 return myThread->getExitStatus();
211 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
212 : receiver(r), myExitStatus(0)
218 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
219 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
220 cenv = getenv( "CSF_RepeatServerRequest" );
222 int val = atoi( cenv );
226 //define delay time between two attempts
227 myDelay = 1000000; // 1 second
228 QString str = "Loading: ";
229 myMessages[0] = "Checking naming service...";
230 myMessages[1] = str + "SALOME_Registry_Server" + "...";
231 myMessages[2] = str + "SALOMEDS_Server" + "...";
232 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
233 myMessages[4] = str + "SALOME_Session_Server" + "...";
238 r->getArgs( _argc, &_argv);
240 // NRI : Temporary solution for SuperVisionContainer
241 for ( int i=1; i<=(_argc-1); i++) {
242 if (strcmp(_argv[i],"CPP")==0) {
243 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
246 if (strcmp(_argv[i],"PY")==0) {
247 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
250 if (strcmp(_argv[i],"SUPERV")==0) {
251 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
254 // if (strcmp(_argv[i],"GUI")==0) {
260 void InquireServersQThread::run()
262 while ( IsChecking && receiver )
264 for (int i=1; i<=8; i++)
266 if ( myMessages[i-1].isEmpty() )
271 myExitStatus = 0; //myExitStatus should be 0 because all servers exist and work
272 sleep( 1 ); // sleep( 1 second ) in order to see 100%. in other case it closes on 85%..
278 QString *message = new QString(myMessages[i-1]);
279 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
280 QThread::usleep(200000);
282 bool result = AskServer(i,&errMsg);
285 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
289 myExitStatus = 0; //myExitStatus should be 0 because all servers exist and work
290 sleep( 1 ); // sleep( 1 second ) in order to see 100%. in other case it closes on 85%..
296 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
304 qApp->exit( myExitStatus );
307 void InquireServersQThread::stop()
313 InquireServersQThread::~InquireServersQThread()
317 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
319 if ( iteration > myServersCount )
320 return true; // we did not launch server with number iteration, so checking for it is not neccessary
322 ASSERT(iteration<=myServersCount);
324 //will be set true if we get response from server
325 bool IsPassed = false;
326 QString errDescription;
330 //First checking - existence of Naming Service
331 for (int i = myRepeat; i ; i--)
335 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
336 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
337 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
338 if (CORBA::is_nil(_root_context))
344 catch(CORBA::COMM_FAILURE&)
346 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
350 MESSAGE("Unknown Exception: unable to contact the naming service");
352 QThread::usleep(myDelay);
355 *errMessage = new QString("unable to contact the naming service");
358 //checking - existence of SALOME_Registry_Server
360 //checking - existence of SALOMEDS_Server
362 //checking - existence of SALOME_ModuleCatalog_Server
364 //checking - existence of SALOME_Session_Server
366 //checking - existence of SALOME_Container FactoryServer
368 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
370 //checking - existence of SALOME_Container SuperVisionContainer
373 IsPassed = pingServer(iteration, errDescription);
375 *errMessage = new QString(errDescription);
381 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
383 ASSERT(iteration<=myServersCount);
386 for (int i = myRepeat; i ; i--)
390 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
391 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
392 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
398 CORBA::Object_var obj = NS.Resolve("/Registry");
399 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
400 if (!CORBA::is_nil(registry))
402 MESSAGE("/Registry is found");
405 MESSAGE("Registry was activated");
412 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
413 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
414 if (!CORBA::is_nil(studyManager))
421 MESSAGE("/myStudyManager is found");
422 studyManager->ping();
424 MESSAGE("StudyManager was activated");
431 CORBA::Object_var obj = NS.Resolve("/Kernel/ModulCatalog");
432 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
433 if (!CORBA::is_nil(catalog))
435 MESSAGE("/Kernel/ModulCatalog is found");
438 MESSAGE("ModuleCatalog was activated");
445 CORBA::Object_var obj = NS.Resolve("/Kernel/Session");
446 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
447 if (!CORBA::is_nil(session))
449 MESSAGE("/Kernel/Session is found");
452 MESSAGE("SALOME_Session was activated");
459 string hostname = GetHostname();
460 string containerName = "/Containers/";
461 containerName += hostname;
462 containerName += "/FactoryServer";
464 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
465 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
466 if (!CORBA::is_nil(FScontainer))
470 MESSAGE("FactoryServer container was activated");
477 string hostname = GetHostname();
478 string containerName = "/Containers/";
479 containerName += hostname;
480 containerName += "/FactoryServerPy";
482 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
483 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
484 if (!CORBA::is_nil(FSPcontainer))
486 FSPcontainer->ping();
488 MESSAGE("FactoryServerPy container was activated");
495 string hostname = GetHostname();
496 string containerName = "/Containers/";
497 containerName += hostname;
498 containerName += "/SuperVisionContainer";
500 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
501 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
502 if (!CORBA::is_nil(SVcontainer))
507 MESSAGE("SuperVisionContainer container was activated");
514 catch (ServiceUnreachable&)
516 MESSAGE("Caught exception: Naming Service Unreachable");
517 errorDescr = "Caught exception: Naming Service Unreachable";
519 catch (CORBA::COMM_FAILURE&)
521 MESSAGE("Caught CORBA::SystemException CommFailure.");
522 errorDescr = "Caught CORBA::SystemException CommFailure";
524 catch (CORBA::SystemException&)
526 MESSAGE("Caught CORBA::SystemException.");
527 errorDescr = "Caught CORBA::SystemException";
529 catch (CORBA::Exception&)
531 MESSAGE("Caught CORBA::Exception.");
532 errorDescr = "Caught CORBA::Exception";
536 MESSAGE("Caught unknown exception.");
537 errorDescr = "Caught unknown exception";
539 QThread::usleep(myDelay);
547 serverName = "SALOME_Registry_Server is not loaded. ";
550 serverName = "SALOMEDS_Server is not loaded. ";
553 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
556 serverName = "SALOME_Session_Server is not loaded. ";
559 serverName = "SALOME_Container FactoryServer is not loaded. ";
562 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
565 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
568 errMessage = serverName + errorDescr;