Salome HOME
DCQ: prepare V2.0.0
[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 using namespace std;
35
36 #include <qthread.h> 
37 #include <qapplication.h>
38 #include <qlabel.h>
39 #include <qwaitcondition.h>
40
41 #include "SALOMEGUI_Application.h"
42 #include "QAD.h"
43 #include "QAD_MessageBox.h"
44 #include "QAD_Application.h"
45 #include "QAD_Settings.h"
46 #include "QAD_Config.h"
47 #include "QAD_Tools.h"
48 #include "QAD_ResourceMgr.h"
49 #include "Utils_CatchSignals.h"
50 #include "Utils_SALOME_Exception.hxx"
51 #include "Utils_CorbaException.hxx"
52 #include "SALOMEGUI_QtCatchCorbaException.hxx"
53
54
55 #include <SALOMEconfig.h>
56 #include CORBA_SERVER_HEADER(SALOME_Session)
57 #include CORBA_SERVER_HEADER(SALOMEDS)
58
59 #include "utilities.h"
60
61 #include "SALOME_Session_i.hxx"
62
63 #include "Session_ServerLauncher.hxx"
64
65 /*! - read arguments, define list of server to launch with their arguments.
66  *  - wait for naming service
67  *  - create and run a thread for launch of all servers
68  *  
69  */
70
71 // ----------------------------------------------------------------------------
72
73
74
75 // ----------------------------------------------------------------------------
76
77 //! CORBA server for SALOME Session
78 /*!
79  *  SALOME_Session Server launches a SALOME session servant.
80  *  The servant registers to the Naming Service.
81  *  See SALOME_Session.idl for interface specification.
82  *
83  *  Main services offered by the servant are:
84  *   - launch GUI
85  *   - stop Session (must be idle)
86  *   - get session state
87  */
88
89 int main(int argc, char **argv)
90 {
91   try
92     {
93       ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
94       ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
95       int orbArgc = 1;
96       CORBA::ORB_var &orb = init( orbArgc , argv ) ;
97
98       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
99       PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
100
101       PortableServer::POAManager_var pman = poa->the_POAManager() ;
102       pman->activate() ;
103       INFOS("pman->activate()");
104
105       SALOME_NamingService *_NS = new SALOME_NamingService(orb);
106
107       Utils_CatchSignals aCatch;
108       aCatch.Activate();
109
110       // CORBA Servant Launcher
111
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       MESSAGE("waiting wakeAll()");
121       _ServerLaunch.wait(&_GUIMutex); // to be reseased by Launch server thread when ready:
122       // atomic operation lock - unlock on mutex
123       // unlock mutex: serverThread runs, calls  _ServerLaunch->wakeAll()
124       // this thread wakes up, and lock mutex
125
126       INFOS("Session activated, Launch IAPP...");
127
128       int qappArgc = 1;
129       QApplication *_qappl = new QApplication(qappArgc, argv );
130       INFOS("creation QApplication");
131       _GUIMutex.unlock();
132
133       QAD_ASSERT ( QObject::connect(_qappl, SIGNAL(lastWindowClosed()), _qappl, SLOT(quit()) ) );
134       SALOMEGUI_Application* _mw = new SALOMEGUI_Application ( "MDTV-Standard", "HDF", "hdf" );
135       INFOS("creation SALOMEGUI_Application");
136       
137       SCRUTE(_NS);
138       if ( !SALOMEGUI_Application::addToDesktop ( _mw, _NS ) )
139         {
140           QAD_MessageBox::error1 ( 0,
141                                    QObject::tr("ERR_ERROR"), 
142                                    QObject::tr("ERR_APP_INITFAILED"),
143                                    QObject::tr("BUT_OK") );
144         }
145       else
146         {
147           
148           QFileInfo prgInfo(argv[0]);
149           QDir prgDir(prgInfo.dirPath(true));
150           QAD_CONFIG->setPrgDir(prgDir);        // CWD is program directory
151           QAD_CONFIG->createConfigFile(false);  // Create config file
152                                                 // ~/.tr(MEN_APPNAME)/tr(MEN_APPNAME).conf if there's none
153           QAD_CONFIG->readConfigFile();         // Read config file
154           
155           _qappl->setPalette( QAD_Application::getPalette() ); 
156
157           //Utils_CatchSignals aCatch;
158           //aCatch.Activate();
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             {
166               try 
167                 {
168                   MESSAGE("run(): starting the main event loop");
169                   int _ret = _qappl->exec();
170                   break;
171                 }
172               catch (SALOME::SALOME_Exception& e)
173                 {
174                   QtCatchCorbaException(e);
175                 }
176               catch(SALOMEDS::StudyBuilder::LockProtection&)
177                 {
178                   INFOS("run(): An attempt to modify a locked study has not been handled by QAD_Operation");
179                   QApplication::restoreOverrideCursor();
180                   QAD_MessageBox::warn1 ( (QWidget*)QAD_Application::getDesktop(),
181                                           QObject::tr("WRN_WARNING"), 
182                                           QObject::tr("WRN_STUDY_LOCKED"),
183                                           QObject::tr("BUT_OK") );
184                 }
185               catch (const CORBA::Exception& e)
186                 {
187                   CORBA::Any tmp;
188                   tmp<<= e;
189                   CORBA::TypeCode_var tc = tmp.type();
190                   const char *p = tc->name();
191                   INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
192
193                   QApplication::restoreOverrideCursor();
194                   QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
195                                            QObject::tr("ERR_ERROR"), 
196                                            QObject::tr("ERR_APP_EXCEPTION")
197                                            + QObject::tr(" CORBA exception ") + QObject::tr(p),
198                                            QObject::tr("BUT_OK") );
199                 }
200               catch(std::exception& e)
201                 {
202                   INFOS("run(): An exception has been caught");
203                   QApplication::restoreOverrideCursor();
204                   QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
205                                            QObject::tr("ERR_ERROR"), 
206                                            QObject::tr("ERR_APP_EXCEPTION")+ " " +QObject::tr(e.what()),
207                                            QObject::tr("BUT_OK") );
208                 }
209               catch(...)
210                 {
211                   INFOS("run(): An exception has been caught");
212                   QApplication::restoreOverrideCursor();
213                   QAD_MessageBox::error1 ( (QWidget*)QAD_Application::getDesktop(),
214                                            QObject::tr("ERR_ERROR"), 
215                                            QObject::tr("ERR_APP_EXCEPTION"),
216                                            QObject::tr("BUT_OK") );
217                 }
218             }
219           //aCatch.Deactivate();
220           QString confMsg = "Settings create " 
221             + QAD_CONFIG->getConfigDir().absPath() + "/" + QObject::tr("MEN_APPNAME") + ".conf";
222           MESSAGE (confMsg);
223           QAD_CONFIG->createConfigFile(true);
224         }
225       //orb->shutdown(0);
226       myServerLauncher->KillAll();
227       aCatch.Deactivate();
228     }
229   catch (SALOME_Exception& e)
230     {
231       INFOS("run(): SALOME::SALOME_Exception is caught: "<<e.what());
232     }
233   catch (CORBA::SystemException& e)
234     {
235       INFOS("Caught CORBA::SystemException.");
236     }
237   catch (CORBA::Exception& e)
238     {
239       INFOS("Caught CORBA::Exception.");
240       CORBA::Any tmp;
241       tmp<<= e;
242       CORBA::TypeCode_var tc = tmp.type();
243       const char *p = tc->name();
244       INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
245     }
246   catch(std::exception& e)
247     {
248       INFOS("run(): An exception has been caught: " <<e.what());
249     }
250   catch (...)
251     {
252       INFOS("Caught unknown exception.");
253     }
254   MESSAGE("End of SALOME_Session_Server");
255   return 0 ;
256 }