1 // Copyright (C) 2003 CEA/DEN, EDF R&D
5 // File : InquireServersQThread.cxx
6 // Author : Vasily RUSYAEV
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"
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 InquireServersGUI::InquireServersGUI()
50 : QVBox(0, "SFA splash", Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder | WType_TopLevel | WStyle_StaysOnTop | WX11BypassWM )
53 myThread = new InquireServersQThread( this );
55 // 1. Polish the appearance
56 setMargin( MARGIN_SIZE );
57 setSpacing( SPACING_SIZE );
58 setFrameStyle( QFrame::Plain | QFrame::Box );
60 setMinimumSize( 200, 150 );
63 QFrame* frm = new QFrame( this );
64 frm->setFrameStyle( QFrame::Box | QFrame::Raised );
65 QHBoxLayout* frmLayout = new QHBoxLayout( frm );
66 frmLayout->setMargin( MARGIN_SIZE );
67 mySplash = new QLabel( frm, "splash" );
68 frmLayout->addWidget( mySplash );
71 //QPixmap pix = SUIT_ResourceMgr( "SalomeApp" ).loadPixmap( "SalomeApp", tr( "ABOUT" ) );
72 //splash->setPixmap( pix );
75 myPrgBar = new QProgressBar( this, "QProgressBar" );
76 myPrgBar->setFixedWidth( 180 );
77 //Sets the total number of steps .
78 myPrgBar->setTotalSteps ( myThread->getInquiredServers() );
79 myPrgBar->setProgress( 0 );
82 QWidget* aWgt1 = new QWidget( this );
83 QHBoxLayout* aHBoxLayout1 = new QHBoxLayout( aWgt1 );
84 myLabel = new QLabel( tr( "Loading:" ), aWgt1 );
85 myLabel->setFixedWidth( 180 );
86 myLabel->setAlignment( AlignLeft );
87 QFont theFont = myLabel->font();
88 theFont.setBold(true);
89 myLabel->setFont( theFont );
90 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
91 aHBoxLayout1->addWidget( myLabel );
92 aHBoxLayout1->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
95 QWidget* aWgt = new QWidget( this );
96 QHBoxLayout* aHBoxLayout = new QHBoxLayout( aWgt );
97 QPushButton* myCancelBtn = new QPushButton( tr( "Cancel" ), aWgt );
98 connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( ClickOnCancel() ) ) ;
99 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
100 aHBoxLayout->addWidget( myCancelBtn );
101 aHBoxLayout->addItem( new QSpacerItem( 0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
104 #if QT_VERSION >= 0x030005
105 QDesktopWidget *d = QApplication::desktop();
107 QWidget *d = QApplication::desktop();
109 //VRV: porting on Qt 3.0.5
111 int w = d->width(); // returns desktop width
112 int h = d->height(); // returns desktop height
113 QSize mySize = sizeHint (); // returns widget size
114 int Xc = ( w - mySize.width() ) / 2;
115 int Yc = ( h - mySize.height() ) / 2;
121 void InquireServersGUI::setPixmap( QPixmap pix )
123 if ( !pix.isNull() ) {
124 mySplash->setPixmap( pix );
125 myPrgBar->setFixedWidth( mySplash->sizeHint().width() );
126 myLabel->setFixedWidth( mySplash->sizeHint().width() );
130 InquireServersGUI::~InquireServersGUI()
135 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
137 _argc = qApp->argc();
138 *_argv = qApp->argv();
141 //=================================================================================
142 // function : ClickOnCancel()
143 // purpose : cancel loading of SALOME
144 //=================================================================================
145 void InquireServersGUI::ClickOnCancel()
147 //it's necessary to stop asking servers
150 //Also we should send QCloseEvent in order to close this widget (and remove from screen)
151 //QThread::postEvent ( this, new QCloseEvent() );
155 void InquireServersGUI::closeEvent ( QCloseEvent * pe)
157 //default implementation calls e->accept(), which hides this widget.
158 //See the QCloseEvent documentation for more details.
160 QApplication::flushX ();
161 QApplication::syncX ();
165 void InquireServersGUI::customEvent( QCustomEvent* pe )
169 case InquireEvent::ProgressEvent:
171 int* value = ( int* )(( InquireEvent*)pe)->data();
172 myPrgBar->setProgress( *value );
175 case InquireEvent::ProgressEventLabel:
177 QString* myString = ( QString* )(( InquireEvent*)pe)->data();
178 myLabel->setText( *myString );
181 case InquireEvent::ProgressEventError:
183 QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
184 QString appName = "SALOME Professional";
185 QString error = "An internal error occurred.\n"+ *myErrDesc + "\n";
186 QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
187 QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
199 int InquireServersGUI::getExitStatus()
201 return myThread->getExitStatus();
204 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
205 : receiver(r), myExitStatus(0)
211 //how many times we should repeat attempts to get response from all needed for launching SALOME servers
212 myRepeat = 30; // default value, user can change it by setting CSF_RepeatServerRequest env.variable
213 cenv = getenv( "CSF_RepeatServerRequest" );
215 int val = atoi( cenv );
219 //define delay time between two attempts
220 myDelay = 1000000; // 1 second
221 QString str = "Loading: ";
222 myMessages[0] = "Checking naming service...";
223 myMessages[1] = str + "SALOME_Registry_Server" + "...";
224 myMessages[2] = str + "SALOMEDS_Server" + "...";
225 myMessages[3] = str + "SALOME_ModuleCatalog_Server" + "...";
226 myMessages[4] = str + "SALOME_Session_Server" + "...";
231 r->getArgs( _argc, &_argv);
233 // NRI : Temporary solution for SuperVisionContainer
234 for ( int i=1; i<=(_argc-1); i++) {
235 if (strcmp(_argv[i],"CPP")==0) {
236 myMessages[5] = str + "SALOME_Container FactoryServer" + "...";
239 if (strcmp(_argv[i],"PY")==0) {
240 myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
243 if (strcmp(_argv[i],"SUPERV")==0) {
244 myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
247 // if (strcmp(_argv[i],"GUI")==0) {
253 void InquireServersQThread::run()
262 for (int i=1; i<=8; i++)
264 if ( myMessages[i-1].isEmpty() ) {
267 //myExitStatus should be 0 because all servers exist and work
269 //we should send QCloseEvent in order to close this widget (and remove from screen)
270 QThread::postEvent ( receiver , new QCloseEvent() );
274 QString *message = new QString(myMessages[i-1]);
275 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
276 QThread::usleep(200000);
278 bool result = AskServer(i,&errMsg);
281 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
285 //myExitStatus should be 0 because all servers exist and work
287 //we should send QCloseEvent in order to close this widget (and remove from screen)
288 QThread::postEvent ( receiver , new QCloseEvent() );
293 QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
294 //myExitStatus should be 1 because we didn't receive response from server
302 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
304 if ( iteration > myServersCount )
305 return true; // we did not launch server with number iteration, so checking for it is not neccessary
307 ASSERT(iteration<=myServersCount);
309 //will be set true if we get response from server
310 bool IsPassed = false;
311 QString errDescription;
315 //First checking - existence of Naming Service
316 for (int i = myRepeat; i ; i--)
320 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
321 CORBA::Object_var obj = orb->resolve_initial_references("NameService");
322 CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow(obj);
323 if (CORBA::is_nil(_root_context))
329 catch(CORBA::COMM_FAILURE&)
331 MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
335 MESSAGE("Unknown Exception: unable to contact the naming service");
337 QThread::usleep(myDelay);
340 *errMessage = new QString("unable to contact the naming service");
343 //checking - existence of SALOME_Registry_Server
345 //checking - existence of SALOMEDS_Server
347 //checking - existence of SALOME_ModuleCatalog_Server
349 //checking - existence of SALOME_Session_Server
351 //checking - existence of SALOME_Container FactoryServer
353 //checking - existence of SALOME_ContainerPy.py FactoryServerPy
355 //checking - existence of SALOME_Container SuperVisionContainer
358 IsPassed = pingServer(iteration, errDescription);
360 *errMessage = new QString(errDescription);
366 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
368 ASSERT(iteration<=myServersCount);
371 for (int i = myRepeat; i ; i--)
375 CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
376 SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
377 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
383 CORBA::Object_var obj = NS.Resolve("/Registry");
384 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
385 if (!CORBA::is_nil(registry))
387 MESSAGE("/Registry is found");
390 MESSAGE("Registry was activated");
397 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
398 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
399 if (!CORBA::is_nil(studyManager))
406 MESSAGE("/myStudyManager is found");
407 studyManager->ping();
409 MESSAGE("StudyManager was activated");
416 CORBA::Object_var obj = NS.Resolve("/Kernel/ModulCatalog");
417 SALOME_ModuleCatalog::ModuleCatalog_var catalog = SALOME_ModuleCatalog::ModuleCatalog::_narrow(obj) ;
418 if (!CORBA::is_nil(catalog))
420 MESSAGE("/Kernel/ModulCatalog is found");
423 MESSAGE("ModuleCatalog was activated");
430 CORBA::Object_var obj = NS.Resolve("/Kernel/Session");
431 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
432 if (!CORBA::is_nil(session))
434 MESSAGE("/Kernel/Session is found");
437 MESSAGE("SALOME_Session was activated");
444 string hostname = GetHostname();
445 string containerName = "/Containers/";
446 containerName += hostname;
447 containerName += "/FactoryServer";
449 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
450 Engines::Container_var FScontainer = Engines::Container::_narrow(obj) ;
451 if (!CORBA::is_nil(FScontainer))
455 MESSAGE("FactoryServer container was activated");
462 string hostname = GetHostname();
463 string containerName = "/Containers/";
464 containerName += hostname;
465 containerName += "/FactoryServerPy";
467 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
468 Engines::Container_var FSPcontainer = Engines::Container::_narrow(obj) ;
469 if (!CORBA::is_nil(FSPcontainer))
471 FSPcontainer->ping();
473 MESSAGE("FactoryServerPy container was activated");
480 string hostname = GetHostname();
481 string containerName = "/Containers/";
482 containerName += hostname;
483 containerName += "/SuperVisionContainer";
485 CORBA::Object_var obj = NS.Resolve(containerName.c_str());
486 Engines::Container_var SVcontainer = Engines::Container::_narrow(obj) ;
487 if (!CORBA::is_nil(SVcontainer))
492 MESSAGE("SuperVisionContainer container was activated");
499 catch (ServiceUnreachable&)
501 MESSAGE("Caught exception: Naming Service Unreachable");
502 errorDescr = "Caught exception: Naming Service Unreachable";
504 catch (CORBA::COMM_FAILURE&)
506 MESSAGE("Caught CORBA::SystemException CommFailure.");
507 errorDescr = "Caught CORBA::SystemException CommFailure";
509 catch (CORBA::SystemException&)
511 MESSAGE("Caught CORBA::SystemException.");
512 errorDescr = "Caught CORBA::SystemException";
514 catch (CORBA::Exception&)
516 MESSAGE("Caught CORBA::Exception.");
517 errorDescr = "Caught CORBA::Exception";
521 MESSAGE("Caught unknown exception.");
522 errorDescr = "Caught unknown exception";
524 QThread::usleep(myDelay);
532 serverName = "SALOME_Registry_Server is not loaded. ";
535 serverName = "SALOMEDS_Server is not loaded. ";
538 serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
541 serverName = "SALOME_Session_Server is not loaded. ";
544 serverName = "SALOME_Container FactoryServer is not loaded. ";
547 serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
550 serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
553 errMessage = serverName + errorDescr;