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 );
130 class SALOME_Session : public SUIT_Session
133 SALOME_Session() : SUIT_Session() {}
134 virtual ~SALOME_Session() {}
137 virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
139 SUIT_ResourceMgr* resMgr = new SUIT_ResourceMgr( appName, QString( "%1Config" ) );
140 resMgr->setVersion( salomeVersion() );
141 resMgr->setCurrentFormat( "xml" );
142 resMgr->setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
147 class SALOME_QApplication : public QApplication
150 SALOME_QApplication( int& argc, char** argv ) : QApplication( argc, argv ), myHandler ( 0 ) {}
152 virtual bool notify( QObject* receiver, QEvent* e )
154 return myHandler ? myHandler->handle( receiver, e ) :
155 QApplication::notify( receiver, e );
157 SUIT_ExceptionHandler* handler() const { return myHandler; }
158 void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
161 SUIT_ExceptionHandler* myHandler;
164 // class which calls SALOME::Session::GetInterface() from another thread
165 // to avoid mutual lock ( if called from the same thread as main()
166 class GetInterfaceThread : public QThread
169 GetInterfaceThread( SALOME::Session_var s ) : session ( s ) {}
173 if ( !CORBA::is_nil( session ) )
174 session->GetInterface();
176 printf( "\nFATAL ERROR: SALOME::Session object is nil! Can not display GUI\n\n" );
179 SALOME::Session_var session;
182 // returns true if 'str' is found in argv
183 bool isFound( const char* str, int argc, char** argv )
185 for ( int i = 1; i <= ( argc-1 ); i++ )
186 if ( !strcmp( argv[i], str ) )
191 // ---------------------------- MAIN -----------------------
192 int main( int argc, char **argv )
194 qInstallMsgHandler( MessageOutput );
196 // QApplication should be create before all other operations
197 // When uses QApplication::libraryPaths() ( example, QFile::encodeName() )
198 // qApp used for detection of the executable dir path.
199 SALOME_QApplication _qappl( argc, argv );
200 ASSERT( QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) ) );
202 QString path = QDir::convertSeparators( SUIT_Tools::addSlash( QString( ::getenv( "GUI_ROOT_DIR" ) ) ) + QString( "bin/salome" ) );
203 _qappl.addLibraryPath( path );
205 _qappl.setStyle( "salome" );
210 PortableServer::POA_var poa;
212 SUIT_Session* aGUISession = 0;
213 SALOME_NamingService* _NS = 0;
214 GetInterfaceThread* guiThread = 0;
215 SALOMETraceCollector* myThreadTrace = 0;
216 Session_ServerLauncher* myServerLauncher = 0;
220 // Python initialisation : only once
223 char* _argv[] = {""};
224 KERNEL_PYTHON::init_python( _argc,_argv );
225 PyEval_RestoreThread( KERNEL_PYTHON::_gtstate );
226 if ( !KERNEL_PYTHON::salome_shared_modules_module ) // import only once
227 KERNEL_PYTHON::salome_shared_modules_module = PyImport_ImportModule( "salome_shared_modules" );
228 if ( !KERNEL_PYTHON::salome_shared_modules_module )
230 INFOS( "salome_shared_modules_module == NULL" );
234 PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate );
236 // Create ORB, get RootPOA object, NamingService, etc.
237 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
238 ASSERT( SINGLETON_<ORB_INIT>::IsAlreadyExisting() );
240 orb = init( orbArgc, argv );
241 myThreadTrace = SALOMETraceCollector::instance( orb );
243 // Install SALOME thread event handler
244 SALOME_Event::GetSessionThread();
246 CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
247 poa = PortableServer::POA::_narrow( obj );
249 PortableServer::POAManager_var pman = poa->the_POAManager();
251 INFOS( "pman->activate()" );
253 _NS = new SALOME_NamingService( orb );
257 catch ( SALOME_Exception& e ) {
258 INFOS( "run(): SALOME::SALOME_Exception is caught: "<<e.what() );
260 catch ( CORBA::SystemException& e ) {
261 INFOS( "Caught CORBA::SystemException." );
263 catch ( CORBA::Exception& e ) {
264 INFOS( "Caught CORBA::Exception." );
267 CORBA::TypeCode_var tc = tmp.type();
268 const char *p = tc->name();
269 INFOS ( "run(): CORBA exception of the kind : "<<p<< " is caught" );
271 catch ( exception& e ) {
272 INFOS( "run(): An exception has been caught: " <<e.what() );
275 INFOS( "Caught unknown exception." );
278 // CORBA Servant Launcher
280 QWaitCondition _ServerLaunch, _SessionStarted;
284 _GUIMutex.lock(); // to block Launch server thread until wait( mutex )
286 // Activate embedded CORBA servers: Registry, SALOMEDS, etc.
287 myServerLauncher = new Session_ServerLauncher( argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch, &_SessionStarted );
288 myServerLauncher->start();
290 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
292 // show splash screen if "SPLASH" parameter was passed ( default )
293 if ( isFound( "SPLASH", argc, argv ) )
295 // create temporary resource manager just to load splash icon
296 SUIT_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
297 resMgr.setVersion( salomeVersion() );
298 resMgr.setCurrentFormat( "xml" );
299 resMgr.loadLanguage( "SalomeApp", "en" );
301 // create splash object: widget ( splash with progress bar ) and "pinging" thread
302 InquireServersGUI splash;
303 splash.setPixmap( resMgr.loadPixmap( "SalomeApp", QObject::tr( "ABOUT" ) ) );
304 SUIT_Tools::centerWidget( &splash, _qappl.desktop() );
306 _qappl.setMainWidget( &splash );
307 QObject::connect( &_qappl, SIGNAL( lastWindowClosed() ), &_qappl, SLOT( quit() ) );
308 splash.show(); // display splash with running progress bar
309 _qappl.exec(); // wait untill splash closes ( progress runs till end or Cancel is pressed )
311 result = splash.getExitStatus(); // 1 is error
314 _SessionStarted.wait();
317 // call Session::GetInterface() if "GUI" parameter was passed ( default )
318 if ( !result && isFound( "GUI", argc, argv ) )
320 CORBA::Object_var obj = _NS->Resolve( "/Kernel/Session" );
321 SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
322 ASSERT ( ! CORBA::is_nil( session ) );
324 INFOS( "Session activated, Launch IAPP..." );
325 guiThread = new GetInterfaceThread( session );
333 // Allow multiple activation/deactivation of GUI
336 MESSAGE( "waiting wakeAll()" );
337 _ServerLaunch.wait( &_GUIMutex ); // to be reseased by Launch server thread when ready:
338 // atomic operation lock - unlock on mutex
339 // unlock mutex: serverThread runs, calls _ServerLaunch->wakeAll()
340 // this thread wakes up, and lock mutex
344 // SUIT_Session creation
345 aGUISession = new SALOME_Session();
347 // Load SalomeApp dynamic library
348 INFOS( "creation SUIT_Application" );
349 SUIT_Application* aGUIApp = aGUISession->startApplication( "SalomeApp", 0, 0 );
352 _qappl.setHandler( aGUISession->handler() ); // after loading SalomeApp application
353 // aGUISession contains SalomeApp_ExceptionHandler
355 MESSAGE( "run(): starting the main event loop" );
356 result = _qappl.exec();
358 if ( result == SUIT_Session::FROM_GUI ) // desktop is closed by user from GUI
365 // Prepare _GUIMutex for a new GUI activation
370 if ( myServerLauncher )
371 myServerLauncher->KillAll(); // kill embedded servers
375 delete myServerLauncher;
377 delete myThreadTrace;