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"
63 #include "SUIT_ExceptionHandler.h"
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 )
85 QString salomeVersion()
87 QString path( ::getenv( "GUI_ROOT_DIR" ) );
88 if ( !path.isEmpty() )
89 path += QDir::separator();
90 path += QString( "bin/salome/VERSION" );
93 if ( !vf.open( IO_ReadOnly ) )
97 vf.readLine( line, 1024 );
100 if ( line.isEmpty() )
101 return QString::null;
103 while ( !line.isEmpty() && line.at( line.length() - 1 ) == QChar( '\n' ) )
104 line.remove( line.length() - 1, 1 );
107 int idx = line.findRev( ":" );
109 ver = line.mid( idx + 1 ).stripWhiteSpace();
114 PyObject* salome_shared_modules_module = 0;
116 void MessageOutput( QtMsgType type, const char* msg )
121 MESSAGE( "Debug: " << msg );
124 MESSAGE( "Warning: " << msg );
127 MESSAGE( "Fatal: " << msg );
133 static const char* pixmap_not_found_xpm[] = {
155 class SALOME_Session : public SUIT_Session
158 SALOME_Session() : SUIT_Session() {}
159 virtual ~SALOME_Session() {}
162 virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
164 SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
165 resMgr->setVersion( salomeVersion() );
166 resMgr->setCurrentFormat( "xml" );
167 resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
168 static QPixmap defaultPixmap( pixmap_not_found_xpm );
169 resMgr->setDefaultPixmap( defaultPixmap );
174 class SALOME_QApplication : public QApplication
177 SALOME_QApplication( int& argc, char** argv ) : QApplication( argc, argv ), myHandler ( 0 ) {}
179 virtual bool notify( QObject* receiver, QEvent* e )
181 return myHandler ? myHandler->handle( receiver, e ) :
182 QApplication::notify( receiver, e );
184 SUIT_ExceptionHandler* handler() const { return myHandler; }
185 void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
188 SUIT_ExceptionHandler* myHandler;
191 // class which calls SALOME::Session::GetInterface() from another thread
192 // to avoid mutual lock ( if called from the same thread as main()
193 class GetInterfaceThread : public QThread
196 GetInterfaceThread( SALOME::Session_var s ) : session ( s ) {}
200 if ( !CORBA::is_nil( session ) )
201 session->GetInterface();
203 printf( "\nFATAL ERROR: SALOME::Session object is nil! Can not display GUI\n\n" );
206 SALOME::Session_var session;
209 // returns true if 'str' is found in argv
210 bool isFound( const char* str, int argc, char** argv )
212 for ( int i = 1; i <= ( argc-1 ); i++ )
213 if ( !strcmp( argv[i], str ) )
218 // ---------------------------- MAIN -----------------------
219 int main( int argc, char **argv )
221 qInstallMsgHandler( MessageOutput );
223 // QApplication should be create before all other operations
224 // When uses QApplication::libraryPaths() ( example, QFile::encodeName() )
225 // qApp used for detection of the executable dir path.
226 SALOME_QApplication _qappl( argc, argv );
227 ASSERT( QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) ) );
229 QString path = QDir::convertSeparators( SUIT_Tools::addSlash( QString( ::getenv( "GUI_ROOT_DIR" ) ) ) + QString( "bin/salome" ) );
230 _qappl.addLibraryPath( path );
232 _qappl.setStyle( "salome" );
237 PortableServer::POA_var poa;
239 SUIT_Session* aGUISession = 0;
240 SALOME_NamingService* _NS = 0;
241 GetInterfaceThread* guiThread = 0;
242 SALOMETraceCollector* myThreadTrace = 0;
243 Session_ServerLauncher* myServerLauncher = 0;
247 // Python initialisation : only once
250 char* _argv[] = {""};
251 KERNEL_PYTHON::init_python( _argc,_argv );
252 PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
253 if ( !KERNEL_PYTHON::salome_shared_modules_module ) // import only once
254 KERNEL_PYTHON::salome_shared_modules_module = PyImport_ImportModule( "salome_shared_modules" );
255 if ( !KERNEL_PYTHON::salome_shared_modules_module )
257 INFOS( "salome_shared_modules_module == NULL" );
261 PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
263 // Create ORB, get RootPOA object, NamingService, etc.
264 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
265 ASSERT( SINGLETON_<ORB_INIT>::IsAlreadyExisting() );
267 orb = init( orbArgc, argv );
268 myThreadTrace = SALOMETraceCollector::instance( orb );
270 // Install SALOME thread event handler
271 SALOME_Event::GetSessionThread();
273 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
274 poa = PortableServer::POA::_narrow( obj );
276 PortableServer::POAManager_var pman = poa->the_POAManager();
278 INFOS( "pman->activate()" );
280 _NS = new SALOME_NamingService( orb );
284 catch ( SALOME_Exception& e ) {
285 INFOS( "run(): SALOME::SALOME_Exception is caught: "<<e.what() );
287 catch ( CORBA::SystemException& e ) {
288 INFOS( "Caught CORBA::SystemException." );
290 catch ( CORBA::Exception& e ) {
291 INFOS( "Caught CORBA::Exception." );
294 CORBA::TypeCode_var tc = tmp.type();
295 const char *p = tc->name();
296 INFOS ( "run(): CORBA exception of the kind : "<<p<< " is caught" );
298 catch ( exception& e ) {
299 INFOS( "run(): An exception has been caught: " <<e.what() );
302 INFOS( "Caught unknown exception." );
305 // CORBA Servant Launcher
307 QWaitCondition _ServerLaunch, _SessionStarted;
311 _GUIMutex.lock(); // to block Launch server thread until wait( mutex )
313 // Activate embedded CORBA servers: Registry, SALOMEDS, etc.
314 myServerLauncher = new Session_ServerLauncher( argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch, &_SessionStarted );
315 myServerLauncher->start();
317 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
319 // show splash screen if "SPLASH" parameter was passed ( default )
320 if ( isFound( "SPLASH", argc, argv ) )
322 // create temporary resource manager just to load splash icon
323 SUIT_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
324 resMgr.setVersion( salomeVersion() );
325 resMgr.setCurrentFormat( "xml" );
326 resMgr.loadLanguage( "SalomeApp", "en" );
328 // create splash object: widget ( splash with progress bar ) and "pinging" thread
329 InquireServersGUI splash;
330 splash.setPixmap( resMgr.loadPixmap( "SalomeApp", QObject::tr( "ABOUT" ) ) );
331 SUIT_Tools::centerWidget( &splash, _qappl.desktop() );
333 _qappl.setMainWidget( &splash );
334 QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) );
335 splash.show(); // display splash with running progress bar
336 _qappl.exec(); // wait untill splash closes ( progress runs till end or Cancel is pressed )
338 result = splash.getExitStatus(); // 1 is error
341 _SessionStarted.wait();
344 // call Session::GetInterface() if "GUI" parameter was passed ( default )
345 if ( !result && isFound( "GUI", argc, argv ) )
347 CORBA::Object_var obj = _NS->Resolve( "/Kernel/Session" );
348 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
349 ASSERT ( ! CORBA::is_nil( session ) );
351 INFOS( "Session activated, Launch IAPP..." );
352 guiThread = new GetInterfaceThread( session );
360 // Allow multiple activation/deactivation of GUI
363 MESSAGE( "waiting wakeAll()" );
364 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
365 // atomic operation lock - unlock on mutex
366 // unlock mutex: serverThread runs, calls _ServerLaunch->wakeAll()
367 // this thread wakes up, and lock mutex
371 // SUIT_Session creation
372 aGUISession = new SALOME_Session();
374 // Load SalomeApp dynamic library
375 INFOS( "creation SUIT_Application" );
376 SUIT_Application* aGUIApp = aGUISession->startApplication( "SalomeApp", 0, 0 );
379 _qappl.setHandler( aGUISession->handler() ); // after loading SalomeApp application
380 // aGUISession contains SalomeApp_ExceptionHandler
382 MESSAGE( "run(): starting the main event loop" );
383 result = _qappl.exec();
385 if ( result == SUIT_Session::FROM_GUI ) // desktop is closed by user from GUI
392 // Prepare _GUIMutex for a new GUI activation
397 if ( myServerLauncher )
398 myServerLauncher->KillAll(); // kill embedded servers
402 delete myServerLauncher;
404 delete myThreadTrace;