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