1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SALOME_Session_i.cxx
23 // Author : Paul RASCLE, EDF
26 #include "utilities.h"
28 #include "Session_Session_i.hxx"
30 #include "SALOME_NamingService.hxx"
31 #include "SALOME_Event.h"
32 #include "SalomeApp_Engine_i.h"
33 #include "SUIT_Session.h"
34 #include "SUIT_Desktop.h"
35 #include "SUIT_Study.h"
37 #include "Basics_Utils.hxx"
40 #include <QWaitCondition>
42 // Open CASCADE Includes
43 #include <OSD_SharedLibrary.hxx>
44 #include <OSD_Function.hxx>
55 SALOME_Session_i::SALOME_Session_i(int argc,
58 PortableServer::POA_ptr poa,
60 QWaitCondition* GUILauncher)
66 _orb = CORBA::ORB::_duplicate(orb) ;
67 _poa = PortableServer::POA::_duplicate(poa) ;
69 _GUILauncher = GUILauncher;
70 _NS = new SALOME_NamingService(_orb);
71 _isShuttingDown = false;
72 //MESSAGE("constructor end");
76 returns Visu component
78 Engines::EngineComponent_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
80 typedef Engines::EngineComponent_ptr TGetImpl(CORBA::ORB_ptr,
81 PortableServer::POA_ptr,
82 SALOME_NamingService*,QMutex*);
83 OSD_SharedLibrary aSharedLibrary(const_cast<char*>(theLibraryName));
84 if (aSharedLibrary.DlOpen(OSD_RTLD_LAZY)) {
85 if (OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
86 return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
88 CORBA::Object_var obj = SalomeApp_Engine_i::EngineForComponent(theLibraryName, true);
89 if (!CORBA::is_nil(obj)){
90 Engines::EngineComponent_var anEngine = Engines::EngineComponent::_narrow(obj);
91 return anEngine._retn();
93 return Engines::EngineComponent::_nil();
99 SALOME_Session_i::~SALOME_Session_i()
102 //MESSAGE("destructor end");
106 tries to find the Corba Naming Service and to register the session,
107 gives naming service interface to _IAPPThread
109 void SALOME_Session_i::NSregister()
111 CORBA::Object_var obref=_this();
112 SALOME::Session_var pSession = SALOME::Session::_narrow(obref);
115 _NS->Register(pSession, "/Kernel/Session");
117 catch (ServiceUnreachable&)
119 INFOS("Caught exception: Naming Service Unreachable");
124 INFOS("Caught unknown exception from Naming Service");
126 //MESSAGE("Session registered in Naming Service");
130 Unregister session server from CORBA Naming Service
132 void SALOME_Session_i::NSunregister()
136 _NS->Destroy_Name("/Kernel/Session");
138 catch (ServiceUnreachable&)
140 INFOS("Caught exception: Naming Service Unreachable");
144 INFOS("Caught unknown exception from Naming Service");
149 Launches the GUI if there is none.
150 The Corba method is oneway (corba client does'nt wait for GUI completion)
152 void SALOME_Session_i::GetInterface()
156 if ( !SUIT_Session::session() )
158 _GUILauncher->wakeAll();
159 MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...");
164 Kills the session if there are no active studies nore GUI
166 class CloseEvent : public SALOME_Event
169 virtual void Execute() {
170 if ( SUIT_Session::session() )
171 SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE );
176 Stop session (close all GUI windows)
178 void SALOME_Session_i::StopSession()
182 if ( SUIT_Session::session() ) {
183 ProcessVoidEvent( new CloseEvent() );
188 void SALOME_Session_i::Shutdown()
191 bool isBeingShuttingDown = _isShuttingDown;
192 _isShuttingDown = true;
194 if ( !isBeingShuttingDown ) {
195 if ( SUIT_Session::session() ) {
196 ProcessVoidEvent( new CloseEvent() );
199 _GUILauncher->wakeAll();
205 Send a SALOME::StatSession structure (see idl) to the client
206 (number of running studies and presence of GUI)
211 QtLock() { if ( qApp ) qApp->lock(); }
212 ~QtLock() { if ( qApp ) qApp->unlock(); }
216 SALOME::StatSession SALOME_Session_i::GetStatSession()
218 // update Session state
224 _isGUI = SUIT_Session::session();
225 if ( _isGUI && SUIT_Session::session()->activeApplication() )
226 _runningStudies = SUIT_Session::session()->activeApplication()->getNbStudies();
230 SALOME::StatSession_var myStats = new SALOME::StatSession ;
232 myStats->state = SALOME::running ;
233 else if (_isShuttingDown)
234 myStats->state = SALOME::shutdown ;
236 myStats->state = SALOME::asleep ;
237 myStats->runningStudies = _runningStudies ;
238 myStats->activeGUI = _isGUI ;
242 return myStats._retn() ;
245 CORBA::Long SALOME_Session_i::GetActiveStudyId()
248 if ( SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
249 if ( SUIT_Session::session()->activeApplication()->activeStudy() ) // mkr : IPAL12128
250 aStudyId = SUIT_Session::session()->activeApplication()->activeStudy()->id();
255 CORBA::Long SALOME_Session_i::getPID() {
264 char* SALOME_Session_i::getHostname()
266 std::string aHostName = Kernel_Utils::GetHostname();
267 return CORBA::string_dup( aHostName.data() );
270 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
272 class TEvent: public SALOME_Event {
275 TEvent(int savePoint) { _savePoint = savePoint; }
276 virtual void Execute() {
277 SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
279 study->restoreState(_savePoint);
284 if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
285 SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
286 if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();
287 ProcessVoidEvent( new TEvent(theSavePoint) );
294 void SALOME_Session_i::emitMessage(const char* theMessage)
296 class TEvent: public SALOME_Event {
298 TEvent(const char * msg) {
301 virtual void Execute() {
302 SUIT_Session::session()->activeApplication()->desktop()->emitMessage(_msg);
307 if ( SUIT_Session::session() ) {
308 if ( SUIT_Session::session()->activeApplication() ) {
309 if ( SUIT_Session::session()->activeApplication()->desktop() ) {
310 ProcessVoidEvent( new TEvent(theMessage) );
313 MESSAGE("try to emit message '"<<theMessage<<"' but there is no desktop");
317 MESSAGE("try to emit message '"<<theMessage<<"' but there is no application");
321 MESSAGE("try to emit message '"<<theMessage<<"' but there is no session");
325 void SALOME_Session_i::emitMessageOneWay(const char* theMessage)
327 emitMessage(theMessage);