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
43 #include <qapplication.h>
44 #include <qwaitcondition.h>
46 #include "Utils_SALOME_Exception.hxx"
47 #include "Utils_CorbaException.hxx"
48 #include "SALOME_Event.hxx"
50 #include <SALOMEconfig.h>
51 #include CORBA_SERVER_HEADER(SALOME_Session)
52 #include CORBA_SERVER_HEADER(SALOMEDS)
54 #include <utilities.h>
55 #include "Session_Session_i.hxx"
56 #include "Session_ServerLauncher.hxx"
58 #include "SUIT_Tools.h"
59 #include "SUIT_Session.h"
60 #include "SUIT_Application.h"
61 #include "SUIT_MessageBox.h"
62 #include "SUIT_ResourceMgr.h"
64 #include "SUIT_ExceptionHandler.h"
66 extern "C" int HandleSignals( QApplication *theQApplication );
68 /*! - read arguments, define list of server to launch with their arguments.
69 * - wait for naming service
70 * - create and run a thread for launch of all servers
74 //! CORBA server for SALOME Session
76 * SALOME_Session Server launches a SALOME session servant.
77 * The servant registers to the Naming Service.
78 * See SALOME_Session.idl for interface specification.
80 * Main services offered by the servant are:
82 * - stop Session ( must be idle )
86 PyObject* salome_shared_modules_module = 0;
88 void MessageOutput( QtMsgType type, const char* msg )
93 MESSAGE( "Debug: " << msg );
96 MESSAGE( "Warning: " << msg );
99 MESSAGE( "Fatal: " << msg );
105 static const char* pixmap_not_found_xpm[] = {
127 QString salomeVersion()
129 QString path( ::getenv( "GUI_ROOT_DIR" ) );
130 if ( !path.isEmpty() )
131 path += QDir::separator();
132 path += QString( "bin/salome/VERSION" );
135 if ( !vf.open( IO_ReadOnly ) )
136 return QString::null;
139 vf.readLine( line, 1024 );
142 if ( line.isEmpty() )
143 return QString::null;
145 while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
146 line.remove( line.length() - 1, 1 );
149 int idx = line.findRev( ":" );
151 ver = line.mid( idx + 1 ).stripWhiteSpace();
156 class SALOME_ResourceMgr : public SUIT_ResourceMgr
159 SALOME_ResourceMgr( const QString& app, const QString& resVarTemplate ) : SUIT_ResourceMgr( app, resVarTemplate )
161 setCurrentFormat( "xml" );
162 setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
163 setDefaultPixmap( QPixmap( pixmap_not_found_xpm ) );
165 static void initResourceMgr()
167 if ( myExtAppName.isNull() || myExtAppVersion.isNull() ) {
168 SALOME_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
169 resMgr.loadLanguage( "SalomeApp", "en" );
171 myExtAppName = QObject::tr( "APP_NAME" ).stripWhiteSpace();
172 if ( myExtAppName == "APP_NAME" || myExtAppName.lower() == "salome" )
173 myExtAppName = "SalomeApp";
174 myExtAppVersion = QObject::tr( "APP_VERSION" );
175 if ( myExtAppVersion == "APP_VERSION" ) {
176 if ( myExtAppName != "SalomeApp" )
177 myExtAppVersion = "";
178 else myExtAppVersion = salomeVersion();
182 QString version() const { return myExtAppVersion; }
185 QString userFileName( const QString& appName ) const
187 if ( version().isNull() ) return "";
188 return SUIT_ResourceMgr::userFileName( myExtAppName );
192 static QString myExtAppName;
193 static QString myExtAppVersion;
196 QString SALOME_ResourceMgr::myExtAppName = QString::null;
197 QString SALOME_ResourceMgr::myExtAppVersion = QString::null;
199 class SALOME_Session : public SUIT_Session
202 SALOME_Session() : SUIT_Session() {}
203 virtual ~SALOME_Session() {}
206 virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
208 SALOME_ResourceMgr::initResourceMgr();
209 SALOME_ResourceMgr* resMgr = new SALOME_ResourceMgr( appName, QString( "%1Config" ) );
214 class SALOME_QApplication : public QApplication
217 SALOME_QApplication( int& argc, char** argv ) : QApplication( argc, argv ), myHandler ( 0 ) {}
219 virtual bool notify( QObject* receiver, QEvent* e )
221 return myHandler ? myHandler->handle( receiver, e ) :
222 QApplication::notify( receiver, e );
224 SUIT_ExceptionHandler* handler() const { return myHandler; }
225 void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
228 SUIT_ExceptionHandler* myHandler;
231 // class which calls SALOME::Session::GetInterface() from another thread
232 // to avoid mutual lock ( if called from the same thread as main()
233 class GetInterfaceThread : public QThread
236 GetInterfaceThread( SALOME::Session_var s ) : session ( s ) {}
240 if ( !CORBA::is_nil( session ) )
241 session->GetInterface();
243 printf( "\nFATAL ERROR: SALOME::Session object is nil! Can not display GUI\n\n" );
246 SALOME::Session_var session;
249 // returns true if 'str' is found in argv
250 bool isFound( const char* str, int argc, char** argv )
252 for ( int i = 1; i <= ( argc-1 ); i++ )
253 if ( !strcmp( argv[i], str ) )
258 // ---------------------------- MAIN -----------------------
259 int main( int argc, char **argv )
261 qInstallMsgHandler( MessageOutput );
263 // QApplication should be create before all other operations
264 // When uses QApplication::libraryPaths() ( example, QFile::encodeName() )
265 // qApp used for detection of the executable dir path.
266 SALOME_QApplication _qappl( argc, argv );
267 ASSERT( QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) ) );
269 QString path = QDir::convertSeparators( SUIT_Tools::addSlash( QString( ::getenv( "GUI_ROOT_DIR" ) ) ) + QString( "bin/salome" ) );
270 _qappl.addLibraryPath( path );
272 _qappl.setStyle( "salome" );
277 PortableServer::POA_var poa;
279 SUIT_Session* aGUISession = 0;
280 SALOME_NamingService* _NS = 0;
281 GetInterfaceThread* guiThread = 0;
282 Session_ServerLauncher* myServerLauncher = 0;
286 // Python initialisation : only once
289 char* _argv[] = {""};
290 KERNEL_PYTHON::init_python( _argc,_argv );
291 PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
292 if ( !KERNEL_PYTHON::salome_shared_modules_module ) // import only once
293 KERNEL_PYTHON::salome_shared_modules_module = PyImport_ImportModule( "salome_shared_modules" );
294 if ( !KERNEL_PYTHON::salome_shared_modules_module )
296 INFOS( "salome_shared_modules_module == NULL" );
300 PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
302 // Create ORB, get RootPOA object, NamingService, etc.
303 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
304 ASSERT( SINGLETON_<ORB_INIT>::IsAlreadyExisting() );
306 orb = init( orbArgc, argv );
308 // Install SALOME thread event handler
309 SALOME_Event::GetSessionThread();
311 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
312 poa = PortableServer::POA::_narrow( obj );
314 PortableServer::POAManager_var pman = poa->the_POAManager();
316 INFOS( "pman->activate()" );
318 _NS = new SALOME_NamingService( orb );
322 catch ( SALOME_Exception& e ) {
323 INFOS( "run(): SALOME::SALOME_Exception is caught: "<<e.what() );
325 catch ( CORBA::SystemException& e ) {
326 INFOS( "Caught CORBA::SystemException." );
328 catch ( CORBA::Exception& e ) {
329 INFOS( "Caught CORBA::Exception." );
332 CORBA::TypeCode_var tc = tmp.type();
333 const char *p = tc->name();
334 INFOS ( "run(): CORBA exception of the kind : "<<p<< " is caught" );
336 catch ( exception& e ) {
337 INFOS( "run(): An exception has been caught: " <<e.what() );
340 INFOS( "Caught unknown exception." );
343 // CORBA Servant Launcher
345 QWaitCondition _ServerLaunch, _SessionStarted;
349 _GUIMutex.lock(); // to block Launch server thread until wait( mutex )
351 // Activate embedded CORBA servers: Registry, SALOMEDS, etc.
352 myServerLauncher = new Session_ServerLauncher( argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch, &_SessionStarted );
353 myServerLauncher->start();
355 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
357 // show splash screen if "SPLASH" parameter was passed ( default )
358 if ( isFound( "SPLASH", argc, argv ) )
360 // create temporary resource manager just to load splash icon
361 SALOME_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
362 resMgr.loadLanguage( "SalomeApp", "en" );
364 // create splash object: widget ( splash with progress bar ) and "pinging" thread
365 InquireServersGUI splash;
366 splash.setPixmap( resMgr.loadPixmap( "SalomeApp", QObject::tr( "ABOUT_SPLASH" ) ) );
367 SUIT_Tools::centerWidget( &splash, _qappl.desktop() );
369 _qappl.setMainWidget( &splash );
370 QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) );
371 splash.show(); // display splash with running progress bar
372 _qappl.exec(); // wait untill splash closes ( progress runs till end or Cancel is pressed )
374 result = splash.getExitStatus(); // 1 is error
377 _SessionStarted.wait();
380 // call Session::GetInterface() if "GUI" parameter was passed ( default )
381 if ( !result && isFound( "GUI", argc, argv ) )
383 CORBA::Object_var obj = _NS->Resolve( "/Kernel/Session" );
384 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
385 ASSERT ( ! CORBA::is_nil( session ) );
387 INFOS( "Session activated, Launch IAPP..." );
388 guiThread = new GetInterfaceThread( session );
396 // Allow multiple activation/deactivation of GUI
399 MESSAGE( "waiting wakeAll()" );
400 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
401 // atomic operation lock - unlock on mutex
402 // unlock mutex: serverThread runs, calls _ServerLaunch->wakeAll()
403 // this thread wakes up, and lock mutex
407 // SUIT_Session creation
408 aGUISession = new SALOME_Session();
410 // Load SalomeApp dynamic library
411 INFOS( "creation SUIT_Application" );
412 SUIT_Application* aGUIApp = aGUISession->startApplication( "SalomeApp", 0, 0 );
415 _qappl.setHandler( aGUISession->handler() ); // after loading SalomeApp application
416 // aGUISession contains SalomeApp_ExceptionHandler
418 MESSAGE( "run(): starting the main event loop" );
419 result = _qappl.exec();
421 if ( result == SUIT_Session::FROM_GUI ) // desktop is closed by user from GUI
428 // Prepare _GUIMutex for a new GUI activation
433 if ( myServerLauncher )
434 myServerLauncher->KillAll(); // kill embedded servers
438 delete myServerLauncher;
441 LocalTraceBufferPool *bp1 = LocalTraceBufferPool::instance();
442 LocalTraceBufferPool::deleteInstance(bp1);