]> SALOME platform Git repositories - modules/kernel.git/blob - src/Session/SALOME_Session_Server.cxx
Salome HOME
PR: merge from tag mergeto_trunk_17Jan05
[modules/kernel.git] / src / Session / SALOME_Session_Server.cxx
1 //  SALOME Session : implementation of Session.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Session_Server.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include "Utils_ORB_INIT.hxx"
30 #include "Utils_SINGLETON.hxx"
31 #include "SALOME_NamingService.hxx"
32 #include <iostream>
33 #include <unistd.h>
34
35 #include <qthread.h> 
36 #include <qapplication.h>
37 #include <qlabel.h>
38 #include <qwaitcondition.h>
39
40 #include "SALOMEGUI_Application.h"
41 #include "QAD.h"
42 #include "QAD_MessageBox.h"
43 #include "QAD_Application.h"
44 #include "QAD_Settings.h"
45 #include "QAD_Config.h"
46 #include "QAD_Tools.h"
47 #include "QAD_ResourceMgr.h"
48 #include "Utils_SALOME_Exception.hxx"
49 #include "Utils_CorbaException.hxx"
50 #include "SALOMEGUI_QtCatchCorbaException.hxx"
51 #include "SALOME_Event.hxx"
52
53 #include <SALOMEconfig.h>
54 #include CORBA_SERVER_HEADER(SALOME_Session)
55 #include CORBA_SERVER_HEADER(SALOMEDS)
56
57 #include "utilities.h"
58 #include "SALOMETraceCollector.hxx"
59
60 #include "SALOME_Session_i.hxx"
61
62 #include "Session_ServerLauncher.hxx"
63 using namespace std;
64
65 extern "C" int HandleSignals(QApplication *theQApplication);
66
67 /*! - read arguments, define list of server to launch with their arguments.
68  *  - wait for naming service
69  *  - create and run a thread for launch of all servers
70  *  
71  */
72
73 //! CORBA server for SALOME Session
74 /*!
75  *  SALOME_Session Server launches a SALOME session servant.
76  *  The servant registers to the Naming Service.
77  *  See SALOME_Session.idl for interface specification.
78  *
79  *  Main services offered by the servant are:
80  *   - launch GUI
81  *   - stop Session (must be idle)
82  *   - get session state
83  */
84
85 using namespace std;
86
87 #ifdef _DEBUG_
88 static int MYDEBUG = 0;
89 #else
90 static int MYDEBUG = 0;
91 #endif
92
93 void MessageOutput( QtMsgType type, const char *msg )
94 {
95   switch ( type ) {
96   case QtDebugMsg:
97     MESSAGE( "Debug: " << msg );
98     break;
99   case QtWarningMsg:
100     MESSAGE( "Warning: " << msg );
101     break;
102   case QtFatalMsg:
103     MESSAGE( "Fatal: " << msg );
104     break;
105   }
106 }
107
108 int main(int argc, char **argv)
109 {
110   SALOME_Event::GetSessionThread();
111   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
112   int orbArgc = 1;
113   CORBA::ORB_var &orb = init( orbArgc , argv ) ;
114   SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
115   qInstallMsgHandler( MessageOutput );
116   try
117     {
118       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
119       PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
120
121       PortableServer::POAManager_var pman = poa->the_POAManager() ;
122       pman->activate() ;
123       INFOS("pman->activate()");
124
125       SALOME_NamingService *_NS = new SALOME_NamingService(orb);
126
127       // CORBA Servant Launcher
128       QMutex _GUIMutex ;
129       QWaitCondition _ServerLaunch;
130       _GUIMutex.lock();     // to block Launch server thread until wait(mutex)
131
132       Session_ServerLauncher* myServerLauncher
133         = new Session_ServerLauncher(argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch);
134       myServerLauncher->start();
135
136       QApplication *_qappl = 0;
137
138       // Allow multiple activation/deactivation of GUI
139       while ( 1 ) {
140         MESSAGE("waiting wakeAll()");
141         _ServerLaunch.wait(&_GUIMutex); // to be reseased by Launch server thread when ready:
142         // atomic operation lock - unlock on mutex
143         // unlock mutex: serverThread runs, calls  _ServerLaunch->wakeAll()
144         // this thread wakes up, and lock mutex
145
146         INFOS("Session activated, Launch IAPP...");
147
148         int qappArgc = 1;
149         if ( !_qappl )
150           _qappl = new QApplication(qappArgc, argv );
151
152         INFOS("creation QApplication");
153         _GUIMutex.unlock();
154
155         QAD_ASSERT ( QObject::connect(_qappl, SIGNAL(lastWindowClosed()), _qappl, SLOT(quit()) ) );
156         SALOMEGUI_Application* _mw = new SALOMEGUI_Application ( "MDTV-Standard", "HDF", "hdf" );
157         INFOS("creation SALOMEGUI_Application");
158         
159         SCRUTE(_NS);
160         if ( !SALOMEGUI_Application::addToDesktop ( _mw, _NS ) ) {
161           QAD_MessageBox::error1 ( 0,
162                                    QObject::tr("ERR_ERROR"), 
163                                    QObject::tr("ERR_APP_INITFAILED"),
164                                    QObject::tr("BUT_OK") );
165         }
166         else {
167           QFileInfo prgInfo(argv[0]);
168           QDir prgDir(prgInfo.dirPath(true));
169           QAD_CONFIG->setPrgDir(prgDir);        // CWD is program directory
170           QAD_CONFIG->createConfigFile(false);  // Create config file
171                                                 // ~/.tr(MEN_APPNAME)/tr(MEN_APPNAME).conf if there's none
172           QAD_CONFIG->readConfigFile();         // Read config file
173           
174           _qappl->setPalette( QAD_Application::getPalette() ); 
175
176           /* Run 'SALOMEGUI' application */
177           QAD_Application::run();
178
179           // T2.12 - catch exceptions thrown on attempts to modified a locked study
180           while (1) {
181             try 
182               {
183                 MESSAGE("run(): starting the main event loop");
184                 int _ret = HandleSignals(_qappl);
185                 break;
186               }
187             catch (SALOME::SALOME_Exception& e)
188               {
189                 QtCatchCorbaException(e);
190               }
191             catch(SALOMEDS::StudyBuilder::LockProtection&)
192               {
193                 INFOS("run(): An attempt to modify a locked study has not been handled by QAD_Operation");
194                 QApplication::restoreOverrideCursor();
195                 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
196                                        QObject::tr("WRN_WARNING"), 
197                                        QObject::tr("WRN_STUDY_LOCKED"),
198                                        QObject::tr("BUT_OK") );
199               }
200             catch (const CORBA::Exception& e)
201               {
202                 CORBA::Any tmp;
203                 tmp<<= e;
204                 CORBA::TypeCode_var tc = tmp.type();
205                 const char *p = tc->name();
206                 INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
207
208                 QApplication::restoreOverrideCursor();
209                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
210                                         QObject::tr("ERR_ERROR"), 
211                                         QObject::tr("ERR_APP_EXCEPTION")
212                                         + QObject::tr(" CORBA exception ") + QObject::tr(p),
213                                         QObject::tr("BUT_OK") );
214               }
215             catch(std::exception& e)
216               {
217                 INFOS("run(): An exception has been caught " <<e.what() );
218                 QApplication::restoreOverrideCursor();
219                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
220                                         QObject::tr("ERR_ERROR"), 
221                                         QObject::tr("ERR_APP_EXCEPTION")+ "\n" +QObject::tr(e.what()),
222                                         QObject::tr("BUT_OK") );
223               }
224             catch(...)
225               {
226                 INFOS("run(): An exception has been caught");
227                 QApplication::restoreOverrideCursor();
228                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
229                                         QObject::tr("ERR_ERROR"), 
230                                         QObject::tr("ERR_APP_EXCEPTION"),
231                                         QObject::tr("BUT_OK") );
232               }
233           }
234           QString confMsg = "Settings create " 
235             + QAD_CONFIG->getConfigDir().absPath() + "/" + QObject::tr("MEN_APPNAME") + ".conf";
236           MESSAGE (confMsg);
237           QAD_CONFIG->createConfigFile(true);
238         }
239
240         // Prepare _GUIMutex for a new GUI activation
241         _GUIMutex.lock(); 
242       }
243
244       //orb->shutdown(0);
245       myServerLauncher->KillAll();
246     }
247   catch (SALOME_Exception& e)
248     {
249       INFOS("run(): SALOME::SALOME_Exception is caught: "<<e.what());
250     }
251   catch (CORBA::SystemException& e)
252     {
253       INFOS("Caught CORBA::SystemException.");
254     }
255   catch (CORBA::Exception& e)
256     {
257       INFOS("Caught CORBA::Exception.");
258       CORBA::Any tmp;
259       tmp<<= e;
260       CORBA::TypeCode_var tc = tmp.type();
261       const char *p = tc->name();
262       INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
263     }
264   catch(std::exception& e)
265     {
266       INFOS("run(): An exception has been caught: " <<e.what());
267     }
268   catch (...)
269     {
270       INFOS("Caught unknown exception.");
271     }
272   MESSAGE("End of SALOME_Session_Server");
273   delete myThreadTrace;
274   return 0 ;
275 }