Salome HOME
PR: new python function getShortHostName() in Utils_Identity.py, based on socket...
[modules/yacs.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 #include "utilities.h"
30
31 #include "SALOME_Session_i.hxx"
32 #include "SALOME_NamingService.hxx"
33 #include "SALOME_Event.hxx"
34
35 #include "QAD_Application.h"
36 #include "QAD_Desktop.h"
37 #include <qapplication.h>
38
39 // Open CASCADE Includes
40 #include <OSD_SharedLibrary.hxx>
41 #include <OSD_LoadMode.hxx>
42 #include <OSD_Function.hxx>
43 using namespace std;
44
45 //=============================================================================
46 /*! SALOME_Session_i
47  *  constructor
48  */ 
49 //=============================================================================
50
51 SALOME_Session_i::SALOME_Session_i(int argc, 
52                                    char ** argv, 
53                                    CORBA::ORB_ptr orb, 
54                                    PortableServer::POA_ptr poa, 
55                                    QMutex* GUIMutex,
56                                    QWaitCondition* GUILauncher)
57 {
58   _argc = argc ;
59   _argv = argv ;
60   _isGUI = FALSE ;
61   _runningStudies= 0 ;
62   _orb = CORBA::ORB::_duplicate(orb) ;
63   _poa = PortableServer::POA::_duplicate(poa) ;
64   _GUIMutex = GUIMutex;
65   _GUILauncher = GUILauncher;
66   //MESSAGE("constructor end");
67 }
68
69 //=============================================================================
70 /*! GetVisuComponent
71  *  returns Visu component
72  */ 
73 //=============================================================================
74
75 Engines::Component_ptr SALOME_Session_i::GetVisuComponent()
76 {
77   //MESSAGE("SALOME_Session_i::GetVisuGen");
78   typedef Engines::Component_ptr TGetImpl(CORBA::ORB_ptr,
79                                          PortableServer::POA_ptr,
80                                          SALOME_NamingService*,QMutex*);
81   OSD_SharedLibrary  aSharedLibrary("libVISUEngineImpl.so");
82   if(aSharedLibrary.DlOpen(OSD_RTLD_LAZY))
83     if(OSD_Function anOSDFun = aSharedLibrary.DlSymb("GetImpl"))
84       return ((TGetImpl (*)) anOSDFun)(_orb,_poa,_NS,_GUIMutex);
85   return Engines::Component::_nil();
86 }
87
88 //=============================================================================
89 /*! ~SALOME_Session_i
90  *  destructor
91  */ 
92 //=============================================================================
93
94 SALOME_Session_i::~SALOME_Session_i()
95 {
96   //MESSAGE("destructor end"); 
97 }
98
99 //=============================================================================
100 /*! NSregister
101  *  tries to find the Corba Naming Service and to register the session,
102  *  gives naming service interface to _IAPPThread
103  */ 
104 //=============================================================================
105
106 void SALOME_Session_i::NSregister()
107 {
108   SALOME::Session_ptr pSession = SALOME::Session::_narrow(_this());
109   try
110     {
111       _NS = new SALOME_NamingService(_orb);
112       _NS->Register(pSession, "/Kernel/Session");
113     }
114   catch (ServiceUnreachable&)
115     {
116       INFOS("Caught exception: Naming Service Unreachable");
117       exit(1) ;
118     }
119   catch (...)
120     {
121       INFOS("Caught unknown exception from Naming Service");
122     }
123   //MESSAGE("Session registered in Naming Service"); 
124 }
125
126 //=============================================================================
127 /*! GetInterface
128  *  Launches the GUI if there is none.
129  *  The Corba method is oneway (corba client does'nt wait for GUI completion)
130  */ 
131 //=============================================================================
132
133 void SALOME_Session_i::GetInterface()
134 {
135   if( !QAD_Application::getDesktop() ) {
136     _GUILauncher->wakeAll();
137     MESSAGE("SALOME_Session_i::GetInterface() called, starting GUI...")
138   }
139 }
140
141 //=============================================================================
142 /*! StopSession
143  *  Kills the session if there are no active studies nore GUI
144  */ 
145 //=============================================================================
146 class CloseEvent : public SALOME_Event
147 {
148 public:
149   virtual void Execute() {
150     if ( QAD_Application::getDesktop() )
151       QAD_Application::getDesktop()->closeDesktop( true );
152   }
153 };
154
155 void SALOME_Session_i::StopSession()
156 {
157   ProcessVoidEvent( new CloseEvent() );
158 }
159  
160 //=============================================================================
161 /*! StatSession
162  *  Send a SALOME::StatSession structure (see idl) to the client
163  *  (number of running studies and presence of GUI)
164  */ 
165 //=============================================================================
166
167 class QtLock
168 {
169 public:
170   QtLock() { if ( qApp ) qApp->lock(); }
171   ~QtLock() { if ( qApp ) qApp->unlock(); }
172 };
173
174
175 SALOME::StatSession SALOME_Session_i::GetStatSession()
176 {
177   // update Session state
178   _GUIMutex->lock();    
179
180   _runningStudies = 0;
181   {
182     QtLock lock;
183     _isGUI = QAD_Application::getDesktop();
184     if ( _isGUI && QAD_Application::getDesktop()->getActiveApp() )
185       _runningStudies = QAD_Application::getDesktop()->getActiveApp()->getStudies().count();
186   }
187
188   _GUIMutex->unlock();
189
190   // getting stat info
191   SALOME::StatSession_var myStats = new SALOME::StatSession ;
192   if (_runningStudies)
193     myStats->state = SALOME::running ;
194   else
195     myStats->state = SALOME::asleep ;
196   myStats->runningStudies = _runningStudies ;
197   myStats->activeGUI = _isGUI ;
198   return myStats._retn() ;
199 }
200
201 CORBA::Long SALOME_Session_i::GetActiveStudyId()
202 {
203   long aStudyId=-1;
204   if( QAD_Application::getDesktop() && QAD_Application::getDesktop()->getActiveStudy()) {
205     aStudyId = QAD_Application::getDesktop()->getActiveStudy()->getStudyId();
206   }
207   return aStudyId;
208 }