]> SALOME platform Git repositories - modules/yacs.git/blob - src/Loader/InquireServersQThread.cxx
Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/yacs.git] / src / Loader / InquireServersQThread.cxx
1 //  Copyright (C) 2003  CEA/DEN, EDF R&D
2 //
3 //
4 //
5 //  File   : InquireServersQThread.cxx
6 //  Author : Vasily RUSYAEV
7 //  Module : SALOME
8 //  $Header$
9
10 #include "InquireServersQThread.h"
11
12 #include <qlabel.h>
13 #include <qpushbutton.h>
14 #include <qabstractlayout.h> 
15 #include <qlayout.h>
16 #include <qevent.h> 
17 #include <qfont.h> 
18 #include <qmessagebox.h> 
19 #include <qdir.h>
20 #include <qfileinfo.h>
21 #include <qstringlist.h>
22
23 //VRV: porting on Qt 3.0.5
24 #if QT_VERSION >= 0x030005
25 #include <qdesktopwidget.h> 
26 #endif
27 //VRV: porting on Qt 3.0.5
28
29 #include <qsize.h> 
30
31 #include <SALOMEconfig.h>
32
33 #include "Utils_ORB_INIT.hxx"
34 #include "Utils_SINGLETON.hxx"
35 #include "SALOME_NamingService.hxx"
36 #include "utilities.h"
37 #include "OpUtil.hxx"
38 using namespace std;
39
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)
45
46 #define MARGIN_SIZE  5
47 #define SPACING_SIZE 3
48
49 static QString findFile( QString filename );
50 static QString addSlash( const QString& path );
51
52 InquireServersGUI::InquireServersGUI()
53      : QVBox(0, "SFA splash", Qt::WDestructiveClose | Qt::WStyle_Customize | Qt::WStyle_NoBorder )
54 {
55   myGUI = false;
56   myThread = new InquireServersQThread(this);
57
58   // 1. Polish the appearance
59   setMargin( MARGIN_SIZE );
60   setSpacing( SPACING_SIZE );
61   setFrameStyle( QFrame::Plain | QFrame::Box );
62   setLineWidth( 2 );
63   setMinimumSize( 200, 150 );
64
65   // 2. Splash image
66   QFrame* frm = new QFrame( this );
67   frm->setFrameStyle( QFrame::Box | QFrame::Raised );
68   QHBoxLayout* frmLayout = new QHBoxLayout( frm );
69   frmLayout->setMargin( MARGIN_SIZE );
70   QLabel* splash = 0;
71   splash = new QLabel( frm, "splash" );
72   frmLayout->addWidget( splash );
73   // setting pixmap
74   QString path = findFile( "Application-Splash.png" );
75   splash->setPixmap( QPixmap( path )  );
76   
77   // 3. Progress bar
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 );
83
84   // 4. Info label
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 ) );
96
97   // 5. <Cancel> button
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 )  );
105
106   //Center widget
107 #if QT_VERSION >= 0x030005
108   QDesktopWidget *d = QApplication::desktop();
109 #else
110   QWidget *d = QApplication::desktop();
111 #endif
112 //VRV: porting on Qt 3.0.5
113
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;
119   move( Xc, Yc );
120
121   myThread->start();
122 }
123
124 InquireServersGUI::~InquireServersGUI()
125 {
126   delete myThread;
127 }
128
129 void InquireServersGUI::getArgs( int& _argc, char *** _argv)
130 {
131   _argc = qApp->argc();
132   *_argv = qApp->argv();
133 }
134
135 //=================================================================================
136 // function : ClickOnCancel()
137 // purpose  : cancel loading of SALOME
138 //=================================================================================
139 void InquireServersGUI::ClickOnCancel()
140 {
141   //it's necessary to stop asking servers
142   myThread->stop();
143   myGUI = false;
144   //Also we should send QCloseEvent in order to close this widget (and remove from screen) 
145   //QThread::postEvent ( this, new QCloseEvent() );
146   qApp->exit(1);
147 }
148
149 void InquireServersGUI::closeEvent ( QCloseEvent * pe)
150 {
151   //default implementation calls e->accept(), which hides this widget. 
152   //See the QCloseEvent documentation for more details.
153   pe->accept();
154   QApplication::flushX ();
155   QApplication::syncX ();
156   qApp->exit();
157 }
158
159 void InquireServersGUI::customEvent( QCustomEvent* pe )
160 {
161   switch( pe->type() )
162     {
163         case InquireEvent::ProgressEvent:
164         {
165             int* value = ( int* )(( InquireEvent*)pe)->data();
166             myPrgBar->setProgress( *value );
167             break;
168         }
169         case InquireEvent::ProgressEventLabel:
170         {
171             QString* myString = ( QString* )(( InquireEvent*)pe)->data();
172             myLabel->setText( *myString );
173             break;
174         }
175         case InquireEvent::ProgressEventError:
176         {
177             QString* myErrDesc = ( QString* )(( InquireEvent*)pe)->data();
178             QString  appName = "SALOME Professional";
179             QString  error = "An internal error occurred.\n"+ *myErrDesc + "\n";
180             QMessageBox myMsgBox(appName,error,QMessageBox::Critical,QMessageBox::Ok,QMessageBox::NoButton,
181                                         QMessageBox::NoButton,0,"MY",TRUE,WStyle_DialogBorder|WStyle_StaysOnTop);
182             myMsgBox.exec();
183             ClickOnCancel();
184             break;
185         }
186       default:
187         {
188           ;
189         }
190     }
191 }
192
193 int InquireServersGUI::getExitStatus()
194 {
195   myThread->getExitStatus();
196   return 0;
197 }
198
199 InquireServersQThread::InquireServersQThread( InquireServersGUI* r )
200      : receiver(r),  myExitStatus(0)
201 {
202   char* cenv;
203
204   IsChecking = true;
205   myServersCount = 5;
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" );
209   if ( cenv ) {
210     int val = atoi( cenv );
211     if ( val > 0 )
212       myRepeat = val;
213   }
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" + "...";
222   myMessages[5] = "";
223   myMessages[6] = "";
224   myMessages[7] = "";
225
226   r->getArgs( _argc, &_argv);
227
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" + "...";
232       myServersCount++;
233     }
234     if (strcmp(_argv[i],"PYTHON")==0) {
235       myMessages[6] = str + "SALOME_ContainerPy.py FactoryServerPy" + "...";
236       myServersCount++;
237     }
238     if (strcmp(_argv[i],"SUPERV")==0) {
239       myMessages[7] = str + "SALOME_Container SuperVisionContainer" + "...";
240       myServersCount++;
241     }
242     if (strcmp(_argv[i],"GUI")==0) {
243       r->withGUI(true);
244     }
245   }
246 }
247
248 void InquireServersQThread::run()
249 {
250 while (IsChecking)
251   {
252     for (int i=1; i<=8; i++)
253       {
254         if ( myMessages[i-1].isEmpty() ) {
255           if (i==8) {
256             IsChecking = false;
257             //myExitStatus should be 0 because all servers exist and work
258             myExitStatus = 0;
259             //we should send QCloseEvent in order to close this widget (and remove from screen) 
260             QThread::postEvent ( receiver , new QCloseEvent() );
261           } else
262             continue;
263         }
264         QString *message = new QString(myMessages[i-1]);
265         QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventLabel, message ) );
266         QThread::usleep(200000);
267         QString *errMsg;
268         bool result = AskServer(i,&errMsg);
269         if (result)
270           {
271             QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEvent, new int( i ) ) );
272             if (i==8)
273               {
274                 IsChecking = false;
275                 //myExitStatus should be 0 because all servers exist and work
276                 myExitStatus = 0;
277                 //we should send QCloseEvent in order to close this widget (and remove from screen) 
278                 QThread::postEvent ( receiver , new QCloseEvent() );
279               }
280           }
281         else
282           {
283             QThread::postEvent( receiver, new InquireEvent( ( QEvent::Type )InquireEvent::ProgressEventError, errMsg ) );
284             //myExitStatus should be 1 because we didn't receive response from server
285             myExitStatus = 1;
286             return;
287           }
288       }
289   }
290 }
291
292 bool InquireServersQThread::AskServer(int iteration, QString ** errMessage)
293 {
294   ASSERT(iteration<=myServersCount);
295   //will be set true if we get response from server
296   bool IsPassed = false;
297   QString errDescription;
298   switch (iteration)
299     {
300     case 1:
301       //First checking - existence of Naming Service
302       for (int i = myRepeat; i ; i--)
303         {
304           try
305             {
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))
310                 continue;
311               else
312                 IsPassed = true;
313               break;
314             }
315           catch(CORBA::COMM_FAILURE&)
316             {
317               MESSAGE("CORBA::COMM_FAILURE: unable to contact the naming service");
318             }
319           catch(...)
320             {
321               MESSAGE("Unknown Exception: unable to contact the naming service");
322             }
323           QThread::usleep(myDelay);
324         }
325       if (!IsPassed)
326         *errMessage = new QString("unable to contact the naming service");
327       break;
328     case 2:
329       //checking - existence of SALOME_Registry_Server
330     case 3:
331       //checking - existence of SALOMEDS_Server
332     case 4:
333       //checking - existence of SALOME_ModuleCatalog_Server
334     case 5:
335       //checking - existence of SALOME_Session_Server
336     case 6:
337       //checking - existence of SALOME_Container FactoryServer
338     case 7:
339       //checking - existence of SALOME_ContainerPy.py FactoryServerPy
340     case 8:
341       //checking - existence of SALOME_Container SuperVisionContainer
342
343
344       IsPassed = pingServer(iteration, errDescription);
345       if (!IsPassed)
346         *errMessage = new QString(errDescription);
347       break;
348     }
349 return IsPassed;
350 }
351
352 bool InquireServersQThread::pingServer(int iteration, QString& errMessage)
353 {
354   ASSERT(iteration<=myServersCount);
355   bool result = false;
356   QString errorDescr;
357   for (int i = myRepeat; i ; i--)
358     {
359       try
360         {
361           CORBA::ORB_var orb = CORBA::ORB_init(_argc,_argv) ;
362           SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
363           ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
364           NS.init_orb( orb ) ;
365           switch (iteration)
366             {
367             case 2:
368               {
369                 CORBA::Object_var obj = NS.Resolve("/Registry");
370                 Registry::Components_var registry = Registry::Components::_narrow(obj) ;
371                 if (!CORBA::is_nil(registry))
372                   {
373                     MESSAGE("/Registry is found");
374                     registry->ping();
375                     result = true;
376                     MESSAGE("Registry was activated");
377                     return result;
378                   }
379               }
380               break;
381             case 3:
382               {
383                 CORBA::Object_var obj = NS.Resolve("/myStudyManager");
384                 SALOMEDS::StudyManager_var studyManager = SALOMEDS::StudyManager::_narrow(obj) ;
385                 if (!CORBA::is_nil(studyManager))
386
387
388
389
390
391                   {
392                     MESSAGE("/myStudyManager is found");
393                     studyManager->ping();
394                     result = true;
395                     MESSAGE("StudyManager was activated");
396                     return result;
397                   }
398               }
399               break;
400             case 4:
401               {
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))
405                   {
406                     MESSAGE("/Kernel/ModulCatalog is found");
407                     catalog->ping();
408                     result = true;
409                     MESSAGE("ModuleCatalog was activated");
410                     return result;
411                   }
412               }
413               break;
414             case 5:
415               {
416                 CORBA::Object_var obj = NS.Resolve("Kernel/Session");
417                 SALOME::Session_var session = SALOME::Session::_narrow(obj) ;
418                 if (!CORBA::is_nil(session))
419                   {
420                     MESSAGE("/Kernel/Session is found");
421                     session->ping();
422                     result = true;
423                     MESSAGE("SALOME_Session was activated");
424                     return result;
425                   }
426               }
427               break;
428             case 6:
429               {
430                 string hostname = GetHostname();
431                 string containerName = "/Containers/";
432                 containerName += hostname;
433                 containerName += "/FactoryServer";
434
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))
438                   {
439                     FScontainer->ping();
440                     result = true;
441                     MESSAGE("FactoryServer container was activated");
442                     return result;
443                   }
444               }
445               break;
446             case 7:
447               {
448                 string hostname = GetHostname();
449                 string containerName = "/Containers/";
450                 containerName += hostname;
451                 containerName += "/FactoryServerPy";
452                 
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))
456                   {
457                     FSPcontainer->ping();
458                     result = true;
459                     MESSAGE("FactoryServerPy container was activated");
460                     return result;
461                   }
462               }
463               break;
464             case 8:
465               {
466                 string hostname = GetHostname();
467                 string containerName = "/Containers/";
468                 containerName += hostname;
469                 containerName += "/SuperVisionContainer";
470                 
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))
474                   {
475                     SVcontainer->ping();
476
477                     result = true;
478                     MESSAGE("SuperVisionContainer container was activated");
479                     return result;
480                   }
481               }
482               break;
483             }
484          }
485       catch (ServiceUnreachable&)
486         {
487           MESSAGE("Caught exception: Naming Service Unreachable");
488           errorDescr = "Caught exception: Naming Service Unreachable";
489         }
490       catch (CORBA::COMM_FAILURE&)
491         {
492           MESSAGE("Caught CORBA::SystemException CommFailure.");
493           errorDescr = "Caught CORBA::SystemException CommFailure";
494         }
495       catch (CORBA::SystemException&)
496         {
497           MESSAGE("Caught CORBA::SystemException.");
498           errorDescr = "Caught CORBA::SystemException";
499         }
500       catch (CORBA::Exception&)
501         {
502           MESSAGE("Caught CORBA::Exception.");
503           errorDescr = "Caught CORBA::Exception";
504         }
505       catch (...)
506         {
507           MESSAGE("Caught unknown exception.");
508           errorDescr = "Caught unknown exception";
509         }
510       QThread::usleep(myDelay);
511     }
512   if (!result)
513     {
514       QString serverName;
515       switch (iteration)
516         {
517         case 2:
518           serverName = "SALOME_Registry_Server is not loaded. ";
519           break;
520         case 3:
521           serverName = "SALOMEDS_Server is not loaded. ";
522           break;
523         case 4:
524           serverName = "SALOME_ModuleCatalog_Server is not loaded. ";
525           break;
526         case 5:
527           serverName = "SALOME_Session_Server is not loaded. ";
528           break;
529         case 6:
530           serverName = "SALOME_Container FactoryServer is not loaded. ";
531           break;
532         case 7:
533           serverName = "SALOME_ContainerPy.py FactoryServerPy is not loaded. ";
534           break;
535         case 8:
536           serverName = "SALOME_Container SuperVisionContainer is not loaded. ";
537           break;
538         }
539       errMessage = serverName + errorDescr;
540     }
541   return result;
542 }
543
544 static const char* SEPARATOR    = ":";
545
546 QString findFile( QString filename )
547 {
548   QString dir;
549   char* cenv;
550   
551   // Try ${KERNEL_ROOT_DIR}/share/salome/resources directory
552   cenv = getenv( "KERNEL_ROOT_DIR" );
553   if ( cenv ) {
554     dir.sprintf( "%s", cenv );
555     if ( !dir.isEmpty() ) {
556       dir = addSlash(dir) ;
557       dir = dir + "share" ;
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();
566     }
567   }
568   // Try CSF_ResourcesDefaults env.var directory ( or directory list )
569   cenv = getenv( "CSF_ResourcesDefaults" );
570   if ( cenv ) {
571     dir.sprintf( "%s", cenv );
572     if ( !dir.isEmpty() ) {
573       QStringList dirList = QStringList::split( SEPARATOR, dir, false ); // skip empty entries
574       for ( int i = 0; i < dirList.count(); i++ ) {
575         QFileInfo fileInfo( addSlash( dirList[ i ] ) + filename );
576         if ( fileInfo.isFile() && fileInfo.exists() )
577           return fileInfo.filePath();
578       }
579     }
580   }
581   // Try ${HOME}/.salome/resources directory
582   cenv = getenv( "HOME" );
583   if ( cenv ) {
584     dir.sprintf( "%s", cenv );
585     if ( !dir.isEmpty() ) {
586       dir = addSlash(dir) ;
587       dir = dir + ".salome" ;
588       dir = addSlash(dir) ;
589       dir = dir + "resources" ;
590       dir = addSlash(dir) ;
591       QFileInfo fileInfo( dir + filename );
592       if ( fileInfo.isFile() && fileInfo.exists() )
593         return fileInfo.filePath();
594     }
595   }
596   return filename;
597 }
598 QString addSlash( const QString& path )
599 {
600   if (!path.isNull()) {
601 #ifdef WNT
602     QChar slash ('\\');
603 #else
604     QChar slash ('/');
605 #endif
606     if ( path.at(path.length()-1) != slash )
607       return path + slash;
608   }
609   return path;
610 }