1 // SALOME Session : implementation of Session.idl
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_Session_Server.cxx
25 // Author : Paul RASCLE, EDF
29 #include "Utils_ORB_INIT.hxx"
30 #include "Utils_SINGLETON.hxx"
31 #include "SALOME_NamingService.hxx"
36 #include <qapplication.h>
38 #include <qwaitcondition.h>
40 #include "SALOMEGUI_Application.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"
53 #include <SALOMEconfig.h>
54 #include CORBA_SERVER_HEADER(SALOME_Session)
55 #include CORBA_SERVER_HEADER(SALOMEDS)
57 #include "utilities.h"
58 #include "SALOMETraceCollector.hxx"
60 #include "SALOME_Session_i.hxx"
62 #include "Session_ServerLauncher.hxx"
65 extern "C" int HandleSignals(QApplication *theQApplication);
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
73 //! CORBA server for SALOME Session
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.
79 * Main services offered by the servant are:
81 * - stop Session (must be idle)
88 static int MYDEBUG = 0;
90 static int MYDEBUG = 0;
93 void MessageOutput( QtMsgType type, const char *msg )
97 MESSAGE( "Debug: " << msg );
100 MESSAGE( "Warning: " << msg );
103 MESSAGE( "Fatal: " << msg );
108 int main(int argc, char **argv)
110 SALOME_Event::GetSessionThread();
111 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
113 CORBA::ORB_var &orb = init( orbArgc , argv ) ;
114 SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
115 qInstallMsgHandler( MessageOutput );
118 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
119 PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
121 PortableServer::POAManager_var pman = poa->the_POAManager() ;
123 INFOS("pman->activate()");
125 SALOME_NamingService *_NS = new SALOME_NamingService(orb);
127 // CORBA Servant Launcher
129 QWaitCondition _ServerLaunch;
130 _GUIMutex.lock(); // to block Launch server thread until wait(mutex)
132 Session_ServerLauncher* myServerLauncher
133 = new Session_ServerLauncher(argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch);
134 myServerLauncher->start();
136 QApplication *_qappl = 0;
138 // Allow multiple activation/deactivation of GUI
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
146 INFOS("Session activated, Launch IAPP...");
150 _qappl = new QApplication(qappArgc, argv );
152 INFOS("creation QApplication");
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");
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") );
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
174 _qappl->setPalette( QAD_Application::getPalette() );
176 /* Run 'SALOMEGUI' application */
177 QAD_Application::run();
179 // T2.12 - catch exceptions thrown on attempts to modified a locked study
183 MESSAGE("run(): starting the main event loop");
184 int _ret = HandleSignals(_qappl);
187 catch (SALOME::SALOME_Exception& e)
189 QtCatchCorbaException(e);
191 catch(SALOMEDS::StudyBuilder::LockProtection&)
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") );
200 catch (const CORBA::Exception& 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");
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") );
215 catch(std::exception& e)
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") );
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") );
234 QString confMsg = "Settings create "
235 + QAD_CONFIG->getConfigDir().absPath() + "/" + QObject::tr("MEN_APPNAME") + ".conf";
237 QAD_CONFIG->createConfigFile(true);
240 // Prepare _GUIMutex for a new GUI activation
245 myServerLauncher->KillAll();
247 catch (SALOME_Exception& e)
249 INFOS("run(): SALOME::SALOME_Exception is caught: "<<e.what());
251 catch (CORBA::SystemException& e)
253 INFOS("Caught CORBA::SystemException.");
255 catch (CORBA::Exception& e)
257 INFOS("Caught CORBA::Exception.");
260 CORBA::TypeCode_var tc = tmp.type();
261 const char *p = tc->name();
262 INFOS ("run(): CORBA exception of the kind : "<<p<< " is caught");
264 catch(std::exception& e)
266 INFOS("run(): An exception has been caught: " <<e.what());
270 INFOS("Caught unknown exception.");
272 MESSAGE("End of SALOME_Session_Server");
273 delete myThreadTrace;