Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/kernel.git] / src / Session / SALOME_Session_i.cxx
1 //  SALOME Session : implementation of Session.idl
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Session_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "utilities.h"
31
32 #include "SALOME_Session_i.hxx"
33 #include "SALOME_NamingService.hxx"
34 #include "SALOME_Session_QThread.hxx"
35
36 #include "QAD_Application.h"
37 #include "QAD_Desktop.h"
38 #include <qapplication.h>
39
40 // Open CASCADE Includes
41 #include <OSD_SharedLibrary.hxx>
42 #include <OSD_LoadMode.hxx>
43 #include <OSD_Function.hxx>
44
45 //=============================================================================
46 /*! SALOME_Session_i
47  *  constructor
48  */ 
49 //=============================================================================
50
51 SALOME_Session_i::SALOME_Session_i(int argc, char ** argv, CORBA::ORB_ptr orb, PortableServer::POA_ptr poa)
52 {
53   _argc = argc ;
54   _argv = argv ;
55   _IAPPThread = new SALOME_Session_QThread(_argc, _argv) ;
56   _isGUI = FALSE ;
57   _runningStudies= 0 ;
58   _orb = CORBA::ORB::_duplicate(orb) ;
59   _poa = PortableServer::POA::_duplicate(poa) ;
60   MESSAGE("constructor end");
61 }
62   
63 Engines::Component_ptr SALOME_Session_i::GetVisuGen(){
64   typedef Engines::Component_ptr VisuGen(CORBA::ORB_ptr, PortableServer::POA_ptr,
65                                          SALOME_NamingService*, QMutex*);
66   MESSAGE("SALOME_Session_i::GetVisuGen");
67   OSD_SharedLibrary  visuSharedLibrary("libVisuEngine.so");
68   if(visuSharedLibrary.DlOpen(OSD_RTLD_LAZY))
69     if(OSD_Function osdFun = visuSharedLibrary.DlSymb("GetVisuGen"))
70       return ((VisuGen (*)) osdFun)(_orb,_poa,_NS,&_GUIMutex);
71   return Engines::Component::_nil();
72 }
73
74 Engines::Component_ptr SALOME_Session_i::GetVisuComponent() {
75   MESSAGE("SALOME_Session_i::GetVisuGen");
76   typedef Engines::Component_ptr VisuGen(CORBA::ORB_ptr,PortableServer::POA_ptr,QMutex*);
77   OSD_SharedLibrary  visuSharedLibrary("libVISUEngine.so");
78   if(visuSharedLibrary.DlOpen(OSD_RTLD_LAZY))
79     if(OSD_Function osdFun = visuSharedLibrary.DlSymb("GetVisuGen"))
80       return ((VisuGen (*)) osdFun)(_orb,_poa,&_GUIMutex);
81   return Engines::Component::_nil();
82 }
83
84 //=============================================================================
85 /*! ~SALOME_Session_i
86  *  destructor
87  */ 
88 //=============================================================================
89
90 SALOME_Session_i::~SALOME_Session_i()
91 {
92   MESSAGE("destructor end"); 
93 }
94
95 //=============================================================================
96 /*! NSregister
97  *  tries to find the Corba Naming Service and to register the session,
98  *  gives naming service interface to _IAPPThread
99  */ 
100 //=============================================================================
101
102 void SALOME_Session_i::NSregister()
103 {
104   SALOME::Session_ptr pSession = SALOME::Session::_narrow(_this());
105   try
106     {
107       _NS = new SALOME_NamingService(_orb);
108       _NS->Register(pSession, "/Kernel/Session");
109       _IAPPThread->setNamingService(_NS);
110     }
111   catch (ServiceUnreachable&)
112     {
113       INFOS("Caught exception: Naming Service Unreachable");
114       exit(1) ;
115     }
116   catch (...)
117     {
118       INFOS("Caught unknown exception from Naming Service");
119     }
120   MESSAGE("NSregister end"); 
121 }
122
123 //=============================================================================
124 /*! GetInterface
125  *  Launches the GUI if there is none.
126  *  The Corba method is oneway (corba client does'nt wait for GUI completion)
127  */ 
128 //=============================================================================
129
130 void SALOME_Session_i::GetInterface()
131 {
132   _GUIMutex.lock() ;       // get access to boolean _isGUI
133   _isGUI = _IAPPThread->running();
134   if(!_isGUI){
135     _isGUI = TRUE ; 
136     _IAPPThread->start() ;
137   }
138   _GUIMutex.unlock() ; // release access to boolean _isGUI 
139 }
140
141 //=============================================================================
142 /*! StopSession
143  *  Kills the session if there are no active studies nore GUI
144  */ 
145 //=============================================================================
146
147 void SALOME_Session_i::StopSession()
148 {
149   qApp->lock();
150   QAD_Application::getDesktop()->closeDesktop( true );
151   qApp->unlock();
152 /*
153   _GUIMutex.lock();         // get access to boolean _isGUI
154   if ((! _isGUI) && (! _runningStudies))
155     {
156       MESSAGE("Ask for Session Kill, OK");
157       exit(0);
158     }
159   else
160     {
161       _GUIMutex.unlock() ;  // release access to boolean _isGUI
162       MESSAGE("Ask for Session Kill, NOK");
163       if (_isGUI) throw SALOME::Session::GUIActive();   
164       if (_runningStudies) throw SALOME::Session::RunningStudies();
165     }
166 */
167 }
168  
169 //=============================================================================
170 /*! StatSession
171  *  Send a SALOME::StatSession structure (see idl) to the client
172  *  (number of running studies and presence of GUI)
173  */ 
174 //=============================================================================
175
176 SALOME::StatSession SALOME_Session_i::GetStatSession()
177 {
178   // update Session state
179   _GUIMutex.lock();    
180   _isGUI = _IAPPThread->running();
181   _runningStudies = 0;
182   if (_isGUI) {
183     qApp->lock();
184     if ( QAD_Application::getDesktop() && QAD_Application::getDesktop()->getActiveApp() )
185       _runningStudies = QAD_Application::getDesktop()->getActiveApp()->getStudies().count();
186     qApp->unlock();
187   }
188   _GUIMutex.unlock();
189   // getting stat info
190   SALOME::StatSession_var myStats = new SALOME::StatSession ;
191   if (_runningStudies)
192     myStats->state = SALOME::running ;
193   else
194     myStats->state = SALOME::asleep ;
195   myStats->runningStudies = _runningStudies ;
196   myStats->activeGUI = _isGUI ;
197   return myStats._retn() ;
198 }
199