Salome HOME
Merge from branch CCRT_Port_V220.
[modules/kernel.git] / src / Session / SALOME_Session_Server.cxx
index ff9a6cf3c20d22bc4258a65411887e366fbc4047..a1106986d74ded6e6e48d8e4d768f2afd384fa64 100644 (file)
@@ -31,7 +31,6 @@
 #include "SALOME_NamingService.hxx"
 #include <iostream>
 #include <unistd.h>
-using namespace std;
 
 #include <qthread.h> 
 #include <qapplication.h>
@@ -46,21 +45,24 @@ using namespace std;
 #include "QAD_Config.h"
 #include "QAD_Tools.h"
 #include "QAD_ResourceMgr.h"
-#include "Utils_CatchSignals.h"
 #include "Utils_SALOME_Exception.hxx"
 #include "Utils_CorbaException.hxx"
 #include "SALOMEGUI_QtCatchCorbaException.hxx"
-
+#include "SALOME_Event.hxx"
 
 #include <SALOMEconfig.h>
 #include CORBA_SERVER_HEADER(SALOME_Session)
 #include CORBA_SERVER_HEADER(SALOMEDS)
 
 #include "utilities.h"
+#include "SALOMETraceCollector.hxx"
 
 #include "SALOME_Session_i.hxx"
 
 #include "Session_ServerLauncher.hxx"
+using namespace std;
+
+extern "C" int HandleSignals(QApplication *theQApplication);
 
 /*! - read arguments, define list of server to launch with their arguments.
  *  - wait for naming service
@@ -68,12 +70,6 @@ using namespace std;
  *  
  */
 
-// ----------------------------------------------------------------------------
-
-
-
-// ----------------------------------------------------------------------------
-
 //! CORBA server for SALOME Session
 /*!
  *  SALOME_Session Server launches a SALOME session servant.
@@ -86,15 +82,39 @@ using namespace std;
  *   - get session state
  */
 
+using namespace std;
+
+#ifdef _DEBUG_
+static int MYDEBUG = 0;
+#else
+static int MYDEBUG = 0;
+#endif
+
+void MessageOutput( QtMsgType type, const char *msg )
+{
+  switch ( type ) {
+  case QtDebugMsg:
+    MESSAGE( "Debug: " << msg );
+    break;
+  case QtWarningMsg:
+    MESSAGE( "Warning: " << msg );
+    break;
+  case QtFatalMsg:
+    MESSAGE( "Fatal: " << msg );
+    break;
+  }
+}
+
 int main(int argc, char **argv)
 {
+  SALOME_Event::GetSessionThread();
+  ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
+  int orbArgc = 1;
+  CORBA::ORB_var &orb = init( orbArgc , argv ) ;
+  SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
+  qInstallMsgHandler( MessageOutput );
   try
     {
-      ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
-      ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
-      int orbArgc = 1;
-      CORBA::ORB_var &orb = init( orbArgc , argv ) ;
-
       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
       PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
 
@@ -104,11 +124,7 @@ int main(int argc, char **argv)
 
       SALOME_NamingService *_NS = new SALOME_NamingService(orb);
 
-      Utils_CatchSignals aCatch;
-      aCatch.Activate();
-
       // CORBA Servant Launcher
-
       QMutex _GUIMutex ;
       QWaitCondition _ServerLaunch;
       _GUIMutex.lock();     // to block Launch server thread until wait(mutex)
@@ -117,34 +133,37 @@ int main(int argc, char **argv)
        = new Session_ServerLauncher(argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch);
       myServerLauncher->start();
 
-      MESSAGE("waiting wakeAll()");
-      _ServerLaunch.wait(&_GUIMutex); // to be reseased by Launch server thread when ready:
-      // atomic operation lock - unlock on mutex
-      // unlock mutex: serverThread runs, calls  _ServerLaunch->wakeAll()
-      // this thread wakes up, and lock mutex
+      QApplication *_qappl = 0;
+
+      // Allow multiple activation/deactivation of GUI
+      while ( 1 ) {
+       MESSAGE("waiting wakeAll()");
+       _ServerLaunch.wait(&_GUIMutex); // to be reseased by Launch server thread when ready:
+       // atomic operation lock - unlock on mutex
+       // unlock mutex: serverThread runs, calls  _ServerLaunch->wakeAll()
+       // this thread wakes up, and lock mutex
 
-      INFOS("Session activated, Launch IAPP...");
+       INFOS("Session activated, Launch IAPP...");
 
-      int qappArgc = 1;
-      QApplication *_qappl = new QApplication(qappArgc, argv );
-      INFOS("creation QApplication");
-      _GUIMutex.unlock();
+       int qappArgc = 1;
+       if ( !_qappl )
+         _qappl = new QApplication(qappArgc, argv );
 
-      QAD_ASSERT ( QObject::connect(_qappl, SIGNAL(lastWindowClosed()), _qappl, SLOT(quit()) ) );
-      SALOMEGUI_Application* _mw = new SALOMEGUI_Application ( "MDTV-Standard", "HDF", "hdf" );
-      INFOS("creation SALOMEGUI_Application");
-      
-      SCRUTE(_NS);
-      if ( !SALOMEGUI_Application::addToDesktop ( _mw, _NS ) )
-       {
+       INFOS("creation QApplication");
+       _GUIMutex.unlock();
+
+       QAD_ASSERT ( QObject::connect(_qappl, SIGNAL(lastWindowClosed()), _qappl, SLOT(quit()) ) );
+       SALOMEGUI_Application* _mw = new SALOMEGUI_Application ( "MDTV-Standard", "HDF", "hdf" );
+       INFOS("creation SALOMEGUI_Application");
+       
+       SCRUTE(_NS);
+       if ( !SALOMEGUI_Application::addToDesktop ( _mw, _NS ) ) {
          QAD_MessageBox::error1 ( 0,
                                   QObject::tr("ERR_ERROR"), 
                                   QObject::tr("ERR_APP_INITFAILED"),
                                   QObject::tr("BUT_OK") );
        }
-      else
-       {
-         
+       else {
          QFileInfo prgInfo(argv[0]);
          QDir prgDir(prgInfo.dirPath(true));
          QAD_CONFIG->setPrgDir(prgDir);        // CWD is program directory
@@ -154,77 +173,76 @@ int main(int argc, char **argv)
          
          _qappl->setPalette( QAD_Application::getPalette() ); 
 
-         //Utils_CatchSignals aCatch;
-         //aCatch.Activate();
-
          /* Run 'SALOMEGUI' application */
          QAD_Application::run();
 
          // T2.12 - catch exceptions thrown on attempts to modified a locked study
-         while (1) 
-           {
-             try 
-               {
-                 MESSAGE("run(): starting the main event loop");
-                 int _ret = _qappl->exec();
-                 break;
-               }
-             catch (SALOME::SALOME_Exception& e)
-               {
-                 QtCatchCorbaException(e);
-               }
-             catch(SALOMEDS::StudyBuilder::LockProtection&)
-               {
-                 INFOS("run(): An attempt to modify a locked study has not been handled by QAD_Operation");
-                 QApplication::restoreOverrideCursor();
-                 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
-                                         QObject::tr("WRN_WARNING"), 
-                                         QObject::tr("WRN_STUDY_LOCKED"),
-                                         QObject::tr("BUT_OK") );
-               }
-             catch (const CORBA::Exception& e)
-               {
-                 CORBA::Any tmp;
-                 tmp<<= e;
-                 CORBA::TypeCode_var tc = tmp.type();
-                 const char *p = tc->name();
-                 INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
+         while (1) {
+           try 
+             {
+               MESSAGE("run(): starting the main event loop");
+               int _ret = HandleSignals(_qappl);
+               break;
+             }
+           catch (SALOME::SALOME_Exception& e)
+             {
+               QtCatchCorbaException(e);
+             }
+           catch(SALOMEDS::StudyBuilder::LockProtection&)
+             {
+               INFOS("run(): An attempt to modify a locked study has not been handled by QAD_Operation");
+               QApplication::restoreOverrideCursor();
+               QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
+                                      QObject::tr("WRN_WARNING"), 
+                                      QObject::tr("WRN_STUDY_LOCKED"),
+                                      QObject::tr("BUT_OK") );
+             }
+           catch (const CORBA::Exception& e)
+             {
+               CORBA::Any tmp;
+               tmp<<= e;
+               CORBA::TypeCode_var tc = tmp.type();
+               const char *p = tc->name();
+               INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
 
-                 QApplication::restoreOverrideCursor();
-                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
-                                          QObject::tr("ERR_ERROR"), 
-                                          QObject::tr("ERR_APP_EXCEPTION")
-                                          + QObject::tr(" CORBA exception ") + QObject::tr(p),
-                                          QObject::tr("BUT_OK") );
-               }
-             catch(std::exception& e)
-               {
-                 INFOS("run(): An exception has been caught");
-                 QApplication::restoreOverrideCursor();
-                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
-                                          QObject::tr("ERR_ERROR"), 
-                                          QObject::tr("ERR_APP_EXCEPTION")+ " " +QObject::tr(e.what()),
-                                          QObject::tr("BUT_OK") );
-               }
-             catch(...)
-               {
-                 INFOS("run(): An exception has been caught");
-                 QApplication::restoreOverrideCursor();
-                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
-                                          QObject::tr("ERR_ERROR"), 
-                                          QObject::tr("ERR_APP_EXCEPTION"),
-                                          QObject::tr("BUT_OK") );
-               }
-           }
-         //aCatch.Deactivate();
-         QString confMsg = "Settings create $HOME/." 
-           + QObject::tr("MEN_APPNAME") + "/" + QObject::tr("MEN_APPNAME") + ".conf";
-         MESSAGE (confMsg );
+               QApplication::restoreOverrideCursor();
+               QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
+                                       QObject::tr("ERR_ERROR"), 
+                                       QObject::tr("ERR_APP_EXCEPTION")
+                                       + QObject::tr(" CORBA exception ") + QObject::tr(p),
+                                       QObject::tr("BUT_OK") );
+             }
+           catch(std::exception& e)
+             {
+               INFOS("run(): An exception has been caught " <<e.what() );
+               QApplication::restoreOverrideCursor();
+               QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
+                                       QObject::tr("ERR_ERROR"), 
+                                       QObject::tr("ERR_APP_EXCEPTION")+ "\n" +QObject::tr(e.what()),
+                                       QObject::tr("BUT_OK") );
+             }
+           catch(...)
+             {
+               INFOS("run(): An exception has been caught");
+               QApplication::restoreOverrideCursor();
+               QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
+                                       QObject::tr("ERR_ERROR"), 
+                                       QObject::tr("ERR_APP_EXCEPTION"),
+                                       QObject::tr("BUT_OK") );
+             }
+         }
+         QString confMsg = "Settings create " 
+           + QAD_CONFIG->getConfigDir().absPath() + "/" + QObject::tr("MEN_APPNAME") + ".conf";
+         MESSAGE (confMsg);
          QAD_CONFIG->createConfigFile(true);
        }
+
+       // Prepare _GUIMutex for a new GUI activation
+       _GUIMutex.lock(); 
+      }
+
       //orb->shutdown(0);
       myServerLauncher->KillAll();
-      aCatch.Deactivate();
     }
   catch (SALOME_Exception& e)
     {
@@ -252,5 +270,6 @@ int main(int argc, char **argv)
       INFOS("Caught unknown exception.");
     }
   MESSAGE("End of SALOME_Session_Server");
+  delete myThreadTrace;
   return 0 ;
 }