1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : InquireServersQThread.cxx
6 // Author : Vasily RUSYAEV
10 #include "InquireServersQThread.h"
12 #include <qapplication.h>
13 #include <qpushbutton.h>
14 #include <qabstractlayout.h>
18 #include <qmessagebox.h>
20 #include <qfileinfo.h>
21 #include <qstringlist.h>
23 #include <qprogressbar.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"
43 #include CORBA_CLIENT_HEADER(SALOME_Session)
44 #include CORBA_CLIENT_HEADER(SALOME_Registry)
45 #include CORBA_CLIENT_HEADER(SALOMEDS)
46 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
47 #include CORBA_CLIENT_HEADER(SALOME_Component)
50 #define SPACING_SIZE 3
52 InquireServersGUI::InquireServersGUI()
53 : QVBox(0, "SFA splash", Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder | WType_TopLevel | WStyle_StaysOnTop | WX11BypassWM )
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 QPalette pal = palette();
67 QColorGroup cg = pal.active();
68 cg.setColor( QColorGroup::Foreground, Qt::darkBlue );
69 cg.setColor( QColorGroup::Background, Qt::white );
70 pal.setActive( cg ); pal.setInactive( cg ); pal.setDisabled( cg );
74 mySplashFrame = new QFrame( this );
75 mySplashFrame->setFrameStyle( QFrame::Box | QFrame::Raised );
76 QHBoxLayout* frmLayout = new QHBoxLayout( mySplashFrame );
77 frmLayout->setMargin( MARGIN_SIZE );
78 mySplash = new QLabel( mySplashFrame, "splash" );
79 frmLayout->addWidget( mySplash );
82 //QPixmap pix = SUIT_ResourceMgr( "SalomeApp" ).loadPixmap( "SalomeApp", tr( "ABOUT" ) );
83 //splash->setPixmap( pix );
86 myPrgBar = new QProgressBar( this, "QProgressBar" );
87 myPrgBar->setFixedWidth( 180 );
88 //Sets the total number of steps .
89 myPrgBar->setPercentageVisible( false );
90 myPrgBar->setIndicatorFollowsStyle( false );
91 myPrgBar->setFixedHeight( 8 );
92 myPrgBar->setFrameStyle( QFrame::Box | QFrame::Plain );
93 myPrgBar->setMargin( 0 );
94 pal = myPrgBar->palette(); cg = pal.active();
95 cg.setColor( QColorGroup::Highlight, Qt::red );
96 pal.setActive( cg ); pal.setInactive( cg ); pal.setDisabled( cg ); myPrgBar->setPalette( pal );
97 myPrgBar->setTotalSteps ( myThread->getInquiredServers() );
98 myPrgBar->setProgress( 0 );
101 QWidget* aWgt1 = new QWidget( this );
102 QHBoxLayout* aHBoxLayout1 = new QHBoxLayout( aWgt1 );
103 myLabel = new QLabel( tr( "Loading:" ), aWgt1 );
104 myLabel->setFixedWidth( 180 );
105 myLabel->setAlignment( AlignLeft );
106 QFont theFont = myLabel->font();
107 theFont.setBold(true);
108 myLabel->setFont( theFont );
109 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
110 aHBoxLayout1->addWidget( myLabel );
111 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
113 // 5. <Cancel> button
114 QWidget* aWgt = new QWidget( this );
115 QHBoxLayout* aHBoxLayout = new QHBoxLayout( aWgt );
116 QPushButton* myCancelBtn = new QPushButton( tr( "Cancel" ), aWgt );
117 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
118 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
119 aHBoxLayout->addWidget( myCancelBtn );
120 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
123 #if QT_VERSION >= 0x030005
124 QDesktopWidget *d = QApplication::desktop();
126 QWidget *d = QApplication::desktop();
128 //VRV: porting on Qt 3.0.5
130 int w = d->width(); // returns desktop width
131 int h = d->height(); // returns desktop height
132 QSize mySize = sizeHint (); // returns widget size
133 int Xc = ( w - mySize.width() ) / 2;
134 int Yc = ( h - mySize.height() ) / 2;
140 void InquireServersGUI::setPixmap( QPixmap pix )
144 mySplash->setPixmap( pix );
145 int w = mySplash->sizeHint().width() + MARGIN_SIZE*2;
146 myPrgBar->setFixedWidth( w );
147 myLabel->setFixedWidth( w );
151 InquireServersGUI::~InquireServersGUI()
156 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
158 _argc = qApp->argc();
159 *_argv = qApp->argv();
162 //=================================================================================
163 // function : ClickOnCancel()
164 // purpose : cancel loading of SALOME
165 //=================================================================================
166 void InquireServersGUI::ClickOnCancel()
168 myThread->stop(); //it's necessary to stop asking servers
173 void InquireServersGUI::customEvent( QCustomEvent* pe )
177 case InquireEvent::ProgressEvent:
179 int* value = ( int* )(( InquireEvent*)pe)->data();
180 myPrgBar->setProgress( *value );
183 case InquireEvent::ProgressEventLabel:
185 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
186 myLabel->setText( *myString );
189 case InquireEvent::ProgressEventError:
191 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
192 QString appName = "SALOME Professional";
193 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
194 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
195 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
207 int InquireServersGUI::getExitStatus()
209 return myThread->getExitStatus();
212 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
213 : receiver(r), myExitStatus(0)
219 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
220 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
221 cenv = getenv( "CSF_RepeatServerRequest" );
223 int val = atoi( cenv );
227 //define delay time between two attempts
228 myDelay = 1000000; // 1 second
229 QString str = "Loading: ";
230 myMessages[0] = "Checking naming service...";
231 myMessages[1] = str + "SALOME_Registry_Server" + "...";
232 myMessages[2] = str + "SALOMEDS_Server" + "...";
233 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
234 myMessages[4] = str + "SALOME_Session_Server" + "...";
239 r->getArgs( _argc, &_argv);
241 // NRI : Temporary solution for SuperVisionContainer
242 for ( int i=1; i<=(_argc-1); i++) {
243 if (strcmp(_argv[i],"CPP")==0) {
244 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
247 if (strcmp(_argv[i],"PY")==0) {
248 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
251 if (strcmp(_argv[i],"SUPERV")==0) {
252 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
255 // if (strcmp(_argv[i],"GUI")==0) {
261 void InquireServersQThread::run()
263 while ( IsChecking && receiver )
265 for (int i=1; i<=8; i++)
267 if ( myMessages[i-1].isEmpty() )
272 myExitStatus = 0; //myExitStatus should be 0 because all servers exist and work
273 sleep( 1 ); // sleep( 1 second ) in order to see 100%. in other case it closes on 85%..
279 QString *message = new QString(myMessages[i-1]);
280 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
281 QThread::usleep(200000);
283 bool result = AskServer(i,&errMsg);
286 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
290 myExitStatus = 0; //myExitStatus should be 0 because all servers exist and work
291 sleep( 1 ); // sleep( 1 second ) in order to see 100%. in other case it closes on 85%..
297 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
305 qApp->exit( myExitStatus );
308 void InquireServersQThread::stop()
314 InquireServersQThread::~InquireServersQThread()
318 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
320 if ( iteration > myServersCount )
321 return true; // we did not launch server with number iteration, so checking for it is not neccessary
323 ASSERT(iteration<=myServersCount);
325 //will be set true if we get response from server
326 bool IsPassed = false;
327 QString errDescription;
334 //First checking - existence of Naming Service
336 for (int i = myRepeat; i ; i--)
338 for (i = myRepeat; i ; i--)
343 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
344 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
345 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
346 if (CORBA::is_nil(_root_context))
352 catch(CORBA::COMM_FAILURE&)
354 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
358 MESSAGE("Unknown Exception: unable to contact the naming service");
360 QThread::usleep(myDelay);
363 *errMessage = new QString("unable to contact the naming service");
366 //checking - existence of SALOME_Registry_Server
368 //checking - existence of SALOMEDS_Server
370 //checking - existence of SALOME_ModuleCatalog_Server
372 //checking - existence of SALOME_Session_Server
374 //checking - existence of SALOME_Container FactoryServer
376 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
378 //checking - existence of SALOME_Container SuperVisionContainer
381 IsPassed = pingServer(iteration, errDescription);
383 *errMessage = new QString(errDescription);
389 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
391 ASSERT(iteration<=myServersCount);
394 for (int i = myRepeat; i ; i--)
398 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
399 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
400 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
406 CORBA::Object_var obj = NS.Resolve("/Registry");
407 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
408 if (!CORBA::is_nil(registry))
410 MESSAGE("/Registry is found");
413 MESSAGE("Registry was activated");
420 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
421 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
422 if (!CORBA::is_nil(studyManager))
429 MESSAGE("/myStudyManager is found");
430 studyManager->ping();
432 MESSAGE("StudyManager was activated");
439 CORBA::Object_var obj = NS.Resolve("/Kernel/ModulCatalog");
440 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
441 if (!CORBA::is_nil(catalog))
443 MESSAGE("/Kernel/ModulCatalog is found");
446 MESSAGE("ModuleCatalog was activated");
453 CORBA::Object_var obj = NS.Resolve("/Kernel/Session");
454 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
455 if (!CORBA::is_nil(session))
457 MESSAGE("/Kernel/Session is found");
460 MESSAGE("SALOME_Session was activated");
467 string hostname = GetHostname();
468 string containerName = "/Containers/";
469 containerName += hostname;
470 containerName += "/FactoryServer";
472 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
473 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
474 if (!CORBA::is_nil(FScontainer))
478 MESSAGE("FactoryServer container was activated");
485 string hostname = GetHostname();
486 string containerName = "/Containers/";
487 containerName += hostname;
488 containerName += "/FactoryServerPy";
490 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
491 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
492 if (!CORBA::is_nil(FSPcontainer))
494 FSPcontainer->ping();
496 MESSAGE("FactoryServerPy container was activated");
503 string hostname = GetHostname();
504 string containerName = "/Containers/";
505 containerName += hostname;
506 containerName += "/SuperVisionContainer";
508 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
509 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
510 if (!CORBA::is_nil(SVcontainer))
515 MESSAGE("SuperVisionContainer container was activated");
522 catch (ServiceUnreachable&)
524 MESSAGE("Caught exception: Naming Service Unreachable");
525 errorDescr = "Caught exception: Naming Service Unreachable";
527 catch (CORBA::COMM_FAILURE&)
529 MESSAGE("Caught CORBA::SystemException CommFailure.");
530 errorDescr = "Caught CORBA::SystemException CommFailure";
532 catch (CORBA::SystemException&)
534 MESSAGE("Caught CORBA::SystemException.");
535 errorDescr = "Caught CORBA::SystemException";
537 catch (CORBA::Exception&)
539 MESSAGE("Caught CORBA::Exception.");
540 errorDescr = "Caught CORBA::Exception";
544 MESSAGE("Caught unknown exception.");
545 errorDescr = "Caught unknown exception";
547 QThread::usleep(myDelay);
555 serverName = "SALOME_Registry_Server is not loaded. ";
558 serverName = "SALOMEDS_Server is not loaded. ";
561 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
564 serverName = "SALOME_Session_Server is not loaded. ";
567 serverName = "SALOME_Container FactoryServer is not loaded. ";
570 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
573 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
576 errMessage = serverName + errorDescr;