]> SALOME platform Git repositories - modules/kernel.git/blob - src/Session/SALOME_Session_Server.cxx
Salome HOME
PR: mergefrom_PRAS_br3_06Nov04
[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 "LocalTraceCollector.hxx"
59
60 #include "SALOME_Session_i.hxx"
61
62 #include "Session_ServerLauncher.hxx"
63
64 extern "C" int HandleSignals(QApplication *theQApplication);
65
66 /*! - read arguments, define list of server to launch with their arguments.
67  *  - wait for naming service
68  *  - create and run a thread for launch of all servers
69  *  
70  */
71
72 //! CORBA server for SALOME Session
73 /*!
74  *  SALOME_Session Server launches a SALOME session servant.
75  *  The servant registers to the Naming Service.
76  *  See SALOME_Session.idl for interface specification.
77  *
78  *  Main services offered by the servant are:
79  *   - launch GUI
80  *   - stop Session (must be idle)
81  *   - get session state
82  */
83
84 using namespace std;
85
86 #ifdef _DEBUG_
87 static int MYDEBUG = 0;
88 #else
89 static int MYDEBUG = 0;
90 #endif
91
92 int main(int argc, char **argv)
93 {
94   SALOME_Event::GetSessionThread();
95   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
96   int orbArgc = 1;
97   CORBA::ORB_var &orb = init( orbArgc , argv ) ;
98   LocalTraceCollector *myThreadTrace = LocalTraceCollector::instance(orb);
99   try
100     {
101       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
102       PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
103
104       PortableServer::POAManager_var pman = poa->the_POAManager() ;
105       pman->activate() ;
106       INFOS("pman->activate()");
107
108       SALOME_NamingService *_NS = new SALOME_NamingService(orb);
109
110       // CORBA Servant Launcher
111       QMutex _GUIMutex ;
112       QWaitCondition _ServerLaunch;
113       _GUIMutex.lock();     // to block Launch server thread until wait(mutex)
114
115       Session_ServerLauncher* myServerLauncher
116         = new Session_ServerLauncher(argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch);
117       myServerLauncher->start();
118
119       QApplication *_qappl = 0;
120
121       // Allow multiple activation/deactivation of GUI
122       while ( 1 ) {
123         MESSAGE("waiting wakeAll()");
124         _ServerLaunch.wait(&_GUIMutex); // to be reseased by Launch server thread when ready:
125         // atomic operation lock - unlock on mutex
126         // unlock mutex: serverThread runs, calls  _ServerLaunch->wakeAll()
127         // this thread wakes up, and lock mutex
128
129         INFOS("Session activated, Launch IAPP...");
130
131         int qappArgc = 1;
132         if ( !_qappl )
133           _qappl = new QApplication(qappArgc, argv );
134
135         INFOS("creation QApplication");
136         _GUIMutex.unlock();
137
138         QAD_ASSERT ( QObject::connect(_qappl, SIGNAL(lastWindowClosed()), _qappl, SLOT(quit()) ) );
139         SALOMEGUI_Application* _mw = new SALOMEGUI_Application ( "MDTV-Standard", "HDF", "hdf" );
140         INFOS("creation SALOMEGUI_Application");
141         
142         SCRUTE(_NS);
143         if ( !SALOMEGUI_Application::addToDesktop ( _mw, _NS ) ) {
144           QAD_MessageBox::error1 ( 0,
145                                    QObject::tr("ERR_ERROR"), 
146                                    QObject::tr("ERR_APP_INITFAILED"),
147                                    QObject::tr("BUT_OK") );
148         }
149         else {
150           QFileInfo prgInfo(argv[0]);
151           QDir prgDir(prgInfo.dirPath(true));
152           QAD_CONFIG->setPrgDir(prgDir);        // CWD is program directory
153           QAD_CONFIG->createConfigFile(false);  // Create config file
154                                                 // ~/.tr(MEN_APPNAME)/tr(MEN_APPNAME).conf if there's none
155           QAD_CONFIG->readConfigFile();         // Read config file
156           
157           _qappl->setPalette( QAD_Application::getPalette() ); 
158
159           /* Run 'SALOMEGUI' application */
160           QAD_Application::run();
161
162           // T2.12 - catch exceptions thrown on attempts to modified a locked study
163           while (1) {
164             try 
165               {
166                 MESSAGE("run(): starting the main event loop");
167                 int _ret = HandleSignals(_qappl);
168                 break;
169               }
170             catch (SALOME::SALOME_Exception& e)
171               {
172                 QtCatchCorbaException(e);
173               }
174             catch(SALOMEDS::StudyBuilder::LockProtection&)
175               {
176                 INFOS("run(): An attempt to modify a locked study has not been handled by QAD_Operation");
177                 QApplication::restoreOverrideCursor();
178                 QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
179                                        QObject::tr("WRN_WARNING"), 
180                                        QObject::tr("WRN_STUDY_LOCKED"),
181                                        QObject::tr("BUT_OK") );
182               }
183             catch (const CORBA::Exception& e)
184               {
185                 CORBA::Any tmp;
186                 tmp<<= e;
187                 CORBA::TypeCode_var tc = tmp.type();
188                 const char *p = tc->name();
189                 INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
190
191                 QApplication::restoreOverrideCursor();
192                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
193                                         QObject::tr("ERR_ERROR"), 
194                                         QObject::tr("ERR_APP_EXCEPTION")
195                                         + QObject::tr(" CORBA exception ") + QObject::tr(p),
196                                         QObject::tr("BUT_OK") );
197               }
198             catch(std::exception& e)
199               {
200                 INFOS("run(): An exception has been caught");
201                 QApplication::restoreOverrideCursor();
202                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
203                                         QObject::tr("ERR_ERROR"), 
204                                         QObject::tr("ERR_APP_EXCEPTION")+ "\n" +QObject::tr(e.what()),
205                                         QObject::tr("BUT_OK") );
206               }
207             catch(...)
208               {
209                 INFOS("run(): An exception has been caught");
210                 QApplication::restoreOverrideCursor();
211                 QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
212                                         QObject::tr("ERR_ERROR"), 
213                                         QObject::tr("ERR_APP_EXCEPTION"),
214                                         QObject::tr("BUT_OK") );
215               }
216           }
217           QString confMsg = "Settings create " 
218             + QAD_CONFIG->getConfigDir().absPath() + "/" + QObject::tr("MEN_APPNAME") + ".conf";
219           MESSAGE (confMsg);
220           QAD_CONFIG->createConfigFile(true);
221         }
222
223         // Prepare _GUIMutex for a new GUI activation
224         _GUIMutex.lock(); 
225       }
226
227       //orb->shutdown(0);
228       myServerLauncher->KillAll();
229     }
230   catch (SALOME_Exception& e)
231     {
232       INFOS("run(): SALOME::SALOME_Exception is caught: "<<e.what());
233     }
234   catch (CORBA::SystemException& e)
235     {
236       INFOS("Caught CORBA::SystemException.");
237     }
238   catch (CORBA::Exception& e)
239     {
240       INFOS("Caught CORBA::Exception.");
241       CORBA::Any tmp;
242       tmp<<= e;
243       CORBA::TypeCode_var tc = tmp.type();
244       const char *p = tc->name();
245       INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
246     }
247   catch(std::exception& e)
248     {
249       INFOS("run(): An exception has been caught: " <<e.what());
250     }
251   catch (...)
252     {
253       INFOS("Caught unknown exception.");
254     }
255   MESSAGE("End of SALOME_Session_Server");
256   delete myThreadTrace;
257   return 0 ;
258 }