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
28 #include <Container_init_python.hxx>
29 #include "Utils_ORB_INIT.hxx"
30 #include "Utils_SINGLETON.hxx"
31 #include "SALOME_NamingService.hxx"
32 #include "SALOMETraceCollector.hxx"
34 #include "InquireServersQThread.h" // splash
41 #include <qapplication.h>
42 #include <qwaitcondition.h>
44 #include "Utils_SALOME_Exception.hxx"
45 #include "Utils_CorbaException.hxx"
46 #include "SALOME_Event.hxx"
48 #include <SALOMEconfig.h>
49 #include CORBA_SERVER_HEADER(SALOME_Session)
50 #include CORBA_SERVER_HEADER(SALOMEDS)
52 #include <utilities.h>
53 #include "Session_Session_i.hxx"
54 #include "Session_ServerLauncher.hxx"
56 #include "SUIT_Tools.h"
57 #include "SUIT_Session.h"
58 #include "SUIT_Application.h"
59 #include "SUIT_MessageBox.h"
61 #include "SUIT_ExceptionHandler.h"
63 extern "C" int HandleSignals( QApplication *theQApplication );
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
71 //! CORBA server for SALOME Session
73 * SALOME_Session Server launches a SALOME session servant.
74 * The servant registers to the Naming Service.
75 * See SALOME_Session.idl for interface specification.
77 * Main services offered by the servant are:
79 * - stop Session ( must be idle )
83 QString salomeVersion()
85 QString path( ::getenv( "GUI_ROOT_DIR" ) );
86 if ( !path.isEmpty() )
87 path += QDir::separator();
88 path += QString( "bin/salome/VERSION" );
91 if ( !vf.open( IO_ReadOnly ) )
95 vf.readLine( line, 1024 );
101 while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
102 line.remove( line.length() - 1, 1 );
105 int idx = line.findRev( ":" );
107 ver = line.mid( idx + 1 ).stripWhiteSpace();
112 PyObject* salome_shared_modules_module = 0;
114 void MessageOutput( QtMsgType type, const char* msg )
119 MESSAGE( "Debug: " << msg );
122 MESSAGE( "Warning: " << msg );
125 MESSAGE( "Fatal: " << msg );
131 static const char* pixmap_not_found_xpm[] = {
153 class SALOME_Session : public SUIT_Session
156 SALOME_Session() : SUIT_Session() {}
157 virtual ~SALOME_Session() {}
160 virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
162 SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
163 resMgr->setVersion( salomeVersion() );
164 resMgr->setCurrentFormat( "xml" );
165 resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
166 static QPixmap defaultPixmap( pixmap_not_found_xpm );
167 resMgr->setDefaultPixmap( defaultPixmap );
172 class SALOME_QApplication : public QApplication
175 SALOME_QApplication( int& argc, char** argv ) : QApplication( argc, argv ), myHandler ( 0 ) {}
177 virtual bool notify( QObject* receiver, QEvent* e )
179 return myHandler ? myHandler->handle( receiver, e ) :
180 QApplication::notify( receiver, e );
182 SUIT_ExceptionHandler* handler() const { return myHandler; }
183 void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
186 SUIT_ExceptionHandler* myHandler;
189 // class which calls SALOME::Session::GetInterface() from another thread
190 // to avoid mutual lock ( if called from the same thread as main()
191 class GetInterfaceThread : public QThread
194 GetInterfaceThread( SALOME::Session_var s ) : session ( s ) {}
198 if ( !CORBA::is_nil( session ) )
199 session->GetInterface();
201 printf( "\nFATAL ERROR: SALOME::Session object is nil! Can not display GUI\n\n" );
204 SALOME::Session_var session;
207 // returns true if 'str' is found in argv
208 bool isFound( const char* str, int argc, char** argv )
210 for ( int i = 1; i <= ( argc-1 ); i++ )
211 if ( !strcmp( argv[i], str ) )
216 // ---------------------------- MAIN -----------------------
217 int main( int argc, char **argv )
219 qInstallMsgHandler( MessageOutput );
221 // QApplication should be create before all other operations
222 // When uses QApplication::libraryPaths() ( example, QFile::encodeName() )
223 // qApp used for detection of the executable dir path.
224 SALOME_QApplication _qappl( argc, argv );
225 ASSERT( QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) ) );
227 QString path = QDir::convertSeparators( SUIT_Tools::addSlash( QString( ::getenv( "GUI_ROOT_DIR" ) ) ) + QString( "bin/salome" ) );
228 _qappl.addLibraryPath( path );
230 _qappl.setStyle( "salome" );
235 PortableServer::POA_var poa;
237 SUIT_Session* aGUISession = 0;
238 SALOME_NamingService* _NS = 0;
239 GetInterfaceThread* guiThread = 0;
240 SALOMETraceCollector* myThreadTrace = 0;
241 Session_ServerLauncher* myServerLauncher = 0;
245 // Python initialisation : only once
248 char* _argv[] = {""};
249 KERNEL_PYTHON::init_python( _argc,_argv );
250 PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
251 if ( !KERNEL_PYTHON::salome_shared_modules_module ) // import only once
252 KERNEL_PYTHON::salome_shared_modules_module = PyImport_ImportModule( "salome_shared_modules" );
253 if ( !KERNEL_PYTHON::salome_shared_modules_module )
255 INFOS( "salome_shared_modules_module == NULL" );
259 PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
261 // Create ORB, get RootPOA object, NamingService, etc.
262 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
263 ASSERT( SINGLETON_<ORB_INIT>::IsAlreadyExisting() );
265 orb = init( orbArgc, argv );
266 myThreadTrace = SALOMETraceCollector::instance( orb );
268 // Install SALOME thread event handler
269 SALOME_Event::GetSessionThread();
271 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
272 poa = PortableServer::POA::_narrow( obj );
274 PortableServer::POAManager_var pman = poa->the_POAManager();
276 INFOS( "pman->activate()" );
278 _NS = new SALOME_NamingService( orb );
282 catch ( SALOME_Exception& e ) {
283 INFOS( "run(): SALOME::SALOME_Exception is caught: "<<e.what() );
285 catch ( CORBA::SystemException& e ) {
286 INFOS( "Caught CORBA::SystemException." );
288 catch ( CORBA::Exception& e ) {
289 INFOS( "Caught CORBA::Exception." );
292 CORBA::TypeCode_var tc = tmp.type();
293 const char *p = tc->name();
294 INFOS ( "run(): CORBA exception of the kind : "<<p<< " is caught" );
296 catch ( exception& e ) {
297 INFOS( "run(): An exception has been caught: " <<e.what() );
300 INFOS( "Caught unknown exception." );
303 // CORBA Servant Launcher
305 QWaitCondition _ServerLaunch, _SessionStarted;
309 _GUIMutex.lock(); // to block Launch server thread until wait( mutex )
311 // Activate embedded CORBA servers: Registry, SALOMEDS, etc.
312 myServerLauncher = new Session_ServerLauncher( argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch, &_SessionStarted );
313 myServerLauncher->start();
315 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
317 // show splash screen if "SPLASH" parameter was passed ( default )
318 if ( isFound( "SPLASH", argc, argv ) )
320 // create temporary resource manager just to load splash icon
321 SUIT_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
322 resMgr.setVersion( salomeVersion() );
323 resMgr.setCurrentFormat( "xml" );
324 resMgr.loadLanguage( "SalomeApp", "en" );
326 // create splash object: widget ( splash with progress bar ) and "pinging" thread
327 InquireServersGUI splash;
328 splash.setPixmap( resMgr.loadPixmap( "SalomeApp", QObject::tr( "ABOUT" ) ) );
329 SUIT_Tools::centerWidget( &splash, _qappl.desktop() );
331 _qappl.setMainWidget( &splash );
332 QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) );
333 splash.show(); // display splash with running progress bar
334 _qappl.exec(); // wait untill splash closes ( progress runs till end or Cancel is pressed )
336 result = splash.getExitStatus(); // 1 is error
339 _SessionStarted.wait();
342 // call Session::GetInterface() if "GUI" parameter was passed ( default )
343 if ( !result && isFound( "GUI", argc, argv ) )
345 CORBA::Object_var obj = _NS->Resolve( "/Kernel/Session" );
346 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
347 ASSERT ( ! CORBA::is_nil( session ) );
349 INFOS( "Session activated, Launch IAPP..." );
350 guiThread = new GetInterfaceThread( session );
358 // Allow multiple activation/deactivation of GUI
361 MESSAGE( "waiting wakeAll()" );
362 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
363 // atomic operation lock - unlock on mutex
364 // unlock mutex: serverThread runs, calls _ServerLaunch->wakeAll()
365 // this thread wakes up, and lock mutex
369 // SUIT_Session creation
370 aGUISession = new SALOME_Session();
372 // Load SalomeApp dynamic library
373 INFOS( "creation SUIT_Application" );
374 SUIT_Application* aGUIApp = aGUISession->startApplication( "SalomeApp", 0, 0 );
377 _qappl.setHandler( aGUISession->handler() ); // after loading SalomeApp application
378 // aGUISession contains SalomeApp_ExceptionHandler
380 MESSAGE( "run(): starting the main event loop" );
381 result = _qappl.exec();
383 if ( result == SUIT_Session::FROM_GUI ) // desktop is closed by user from GUI
390 // Prepare _GUIMutex for a new GUI activation
395 if ( myServerLauncher )
396 myServerLauncher->KillAll(); // kill embedded servers
400 delete myServerLauncher;
402 delete myThreadTrace;