Back to index

salome-gui  6.5.0
Session_Session_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : SALOME_Session_i.cxx
00023 //  Author : Paul RASCLE, EDF
00024 //  Module : SALOME
00025 
00026 #include "utilities.h"
00027 
00028 #include "Session_Session_i.hxx"
00029 
00030 #include "SALOME_NamingService.hxx"
00031 #include "SALOME_Event.h"
00032 
00033 #include "SUIT_Session.h"
00034 #include "SUIT_Desktop.h"
00035 #include "SUIT_Study.h"
00036 
00037 #include "Basics_Utils.hxx"
00038 
00039 #include <QMutex>
00040 #include <QWaitCondition>
00041 
00042 // Open CASCADE Includes
00043 #include <OSD_SharedLibrary.hxx>
00044 #include <OSD_Function.hxx>
00045 
00046 #ifdef WNT
00047 # include <process.h>
00048 #else
00049 #include <unistd.h>
00050 #endif
00051 
00055 SALOME_Session_i::SALOME_Session_i(int argc,
00056                                    char ** argv,
00057                                    CORBA::ORB_ptr orb,
00058                                    PortableServer::POA_ptr poa,
00059                                    QMutex* GUIMutex,
00060                                    QWaitCondition* GUILauncher)
00061 {
00062   _argc = argc ;
00063   _argv = argv ;
00064   _isGUI = FALSE ;
00065   _runningStudies= 0 ;
00066   _orb = CORBA::ORB::_duplicate(orb) ;
00067   _poa = PortableServer::POA::_duplicate(poa) ;
00068   _GUIMutex = GUIMutex;
00069   _GUILauncher = GUILauncher;
00070   _NS = new SALOME_NamingService(_orb);
00071   _isShuttingDown = false;
00072   //MESSAGE("constructor end");
00073 }
00074 
00078 Engines::EngineComponent_ptr SALOME_Session_i::GetComponent(const char* theLibraryName)
00079 {
00080   typedef Engines::EngineComponent_ptr TGetImpl(CORBA::ORB_ptr,
00081                                                 PortableServer::POA_ptr,
00082                                                 SALOME_NamingService*,QMutex*);
00083   OSD_SharedLibrary  aSharedLibrary(const_cast<char*>(theLibraryName));
00084   if(aSharedLibrary.DlOpen(OSD_RTLD_LAZY))
00085     if(OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
00086       return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
00087   return Engines::EngineComponent::_nil();
00088 }
00089 
00093 SALOME_Session_i::~SALOME_Session_i()
00094 {
00095   delete _NS;
00096   //MESSAGE("destructor end");
00097 }
00098 
00103 void SALOME_Session_i::NSregister()
00104 {
00105   CORBA::Object_var obref=_this();
00106   SALOME::Session_var pSession = SALOME::Session::_narrow(obref);
00107   try
00108     {
00109       _NS->Register(pSession, "/Kernel/Session");
00110     }
00111   catch (ServiceUnreachable&)
00112     {
00113       INFOS("Caught exception: Naming Service Unreachable");
00114       exit(1) ;
00115     }
00116   catch (...)
00117     {
00118       INFOS("Caught unknown exception from Naming Service");
00119     }
00120   //MESSAGE("Session registered in Naming Service");
00121 }
00122 
00126 void SALOME_Session_i::NSunregister()
00127 {
00128   try
00129     {
00130       _NS->Destroy_Name("/Kernel/Session");
00131     }
00132   catch (ServiceUnreachable&)
00133     {
00134       INFOS("Caught exception: Naming Service Unreachable");
00135     }
00136   catch (...)
00137     {
00138       INFOS("Caught unknown exception from Naming Service");
00139     }
00140 }
00141 
00146 void SALOME_Session_i::GetInterface()
00147 {
00148   _GUIMutex->lock();
00149   _GUIMutex->unlock();
00150   if ( !SUIT_Session::session() )
00151   {
00152     _GUILauncher->wakeAll();
00153     MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...");
00154   }
00155 }
00156 
00160 class CloseEvent : public SALOME_Event
00161 {
00162 public:
00163   virtual void Execute() {
00164     if ( SUIT_Session::session() )
00165       SUIT_Session::session()->closeSession( SUIT_Session::DONT_SAVE );
00166   }
00167 };
00168 
00172 void SALOME_Session_i::StopSession()
00173 {
00174   _GUIMutex->lock();
00175   _GUIMutex->unlock();
00176   if ( SUIT_Session::session() ) {
00177     ProcessVoidEvent( new CloseEvent() );
00178   }
00179 }
00180 
00182 void SALOME_Session_i::Shutdown()
00183 {
00184   _GUIMutex->lock();
00185   bool isBeingShuttingDown = _isShuttingDown;
00186   _isShuttingDown = true;
00187   _GUIMutex->unlock();
00188   if ( !isBeingShuttingDown ) {
00189     if ( SUIT_Session::session() ) {
00190       ProcessVoidEvent( new CloseEvent() );
00191     }
00192     else {
00193       _GUILauncher->wakeAll();
00194     }
00195   }
00196 }
00197 
00202 /*class QtLock
00203 {
00204 public:
00205   QtLock() { if ( qApp ) qApp->lock(); }
00206   ~QtLock() { if ( qApp ) qApp->unlock(); }
00207 };*/
00208 
00209 
00210 SALOME::StatSession SALOME_Session_i::GetStatSession()
00211 {
00212   // update Session state
00213   _GUIMutex->lock();
00214 
00215   _runningStudies = 0;
00216   {
00217     //QtLock lock;
00218     _isGUI = SUIT_Session::session();
00219     if ( _isGUI && SUIT_Session::session()->activeApplication() )
00220       _runningStudies = SUIT_Session::session()->activeApplication()->getNbStudies();
00221   }
00222 
00223   // getting stat info
00224   SALOME::StatSession_var myStats = new SALOME::StatSession ;
00225   if (_runningStudies)
00226     myStats->state = SALOME::running ;
00227   else if (_isShuttingDown)
00228     myStats->state = SALOME::shutdown ;
00229   else
00230     myStats->state = SALOME::asleep ;
00231   myStats->runningStudies = _runningStudies ;
00232   myStats->activeGUI = _isGUI ;
00233 
00234   _GUIMutex->unlock();
00235 
00236   return myStats._retn() ;
00237 }
00238 
00239 CORBA::Long SALOME_Session_i::GetActiveStudyId()
00240 {
00241   long aStudyId=-1;
00242   if ( SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
00243     if ( SUIT_Session::session()->activeApplication()->activeStudy() ) // mkr : IPAL12128
00244       aStudyId = SUIT_Session::session()->activeApplication()->activeStudy()->id();
00245   }
00246   return aStudyId;
00247 }
00248 
00249 CORBA::Long SALOME_Session_i::getPID() {
00250   return (CORBA::Long)
00251 #ifndef WNT
00252     getpid();
00253 #else
00254     _getpid();
00255 #endif
00256 }
00257 
00258 char* SALOME_Session_i::getHostname()
00259 {
00260   std::string aHostName = Kernel_Utils::GetHostname();
00261   return CORBA::string_dup( aHostName.data() );
00262 }
00263 
00264 bool SALOME_Session_i::restoreVisualState(CORBA::Long theSavePoint)
00265 {
00266   class TEvent: public SALOME_Event {
00267     int _savePoint;
00268   public:
00269     TEvent(int savePoint) { _savePoint = savePoint; }
00270     virtual void Execute() {
00271       SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
00272       if ( study ) {
00273         study->restoreState(_savePoint);
00274       }
00275     }
00276   };
00277   
00278   if(SUIT_Session::session() && SUIT_Session::session()->activeApplication() ) {
00279     SUIT_Study* study = SUIT_Session::session()->activeApplication()->activeStudy();
00280     if(!study) SUIT_Session::session()->activeApplication()->createEmptyStudy();      
00281     ProcessVoidEvent( new TEvent(theSavePoint) );
00282     return true;
00283   }
00284  
00285   return false;
00286 }
00287 
00288 void SALOME_Session_i::emitMessage(const char* theMessage)
00289 {
00290   class TEvent: public SALOME_Event {
00291   public:
00292     TEvent(const char * msg) {
00293       _msg = msg;
00294     }
00295     virtual void Execute() {
00296       SUIT_Session::session()->activeApplication()->desktop()->emitMessage(_msg);
00297     }
00298   private:
00299     const char* _msg;
00300   };
00301   if ( SUIT_Session::session() ) {
00302     if ( SUIT_Session::session()->activeApplication() ) {
00303       if ( SUIT_Session::session()->activeApplication()->desktop() ) {
00304         ProcessVoidEvent( new TEvent(theMessage) );
00305       }
00306     }
00307   }
00308 }
00309 
00310 void SALOME_Session_i::emitMessageOneWay(const char* theMessage)
00311 {
00312   emitMessage(theMessage);
00313 }