Salome HOME
Test EXPORTS definition with target name as suggested by cmake
[modules/kernel.git] / src / Registry / RegistryService.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME Registry : Registry server implementation
23 //  File   : RegistryService.cxx
24 //  Author : Pascale NOYRET - Antoine YESSAYAN, EDF
25 //  Module : SALOME
26 //  $Header$
27 //
28 # include "RegistryService.hxx"
29 # include "OpUtil.hxx"
30 # include "utilities.h"
31
32 extern "C"
33 {
34 # include <time.h>
35 }
36
37 #ifndef WIN32
38 #include <unistd.h>
39 #else
40 #include <process.h>
41 #endif
42 using namespace std;
43
44 /* ------------------------------*/
45 /* Constructeurs et Destructeurs */
46 /* ------------------------------*/
47
48 RegistryService::RegistryService( void ) : _SessionName(0), _Compteur(0)
49 {
50         MESSAGE("Passage dans RegistryService::RegistryService()") ;
51         _orb = CORBA::ORB::_nil();
52 }
53
54
55 RegistryService::~RegistryService()
56 {
57         BEGIN_OF("RegistryService::~RegistryService()") ;
58         map<int,client_infos *>::iterator im;
59         for (im=_reg.begin();im!=_reg.end(); im++)
60         {
61 #if defined(_DEBUG_) || defined(_DEBUG)
62                 const client_infos &lesInfos = *(*im).second ;
63                 MESSAGE("Deletion") ; SCRUTE( lesInfos._name ) ;
64 #endif
65                 _reg.erase ( im ) ;
66         }
67         ASSERT(_reg.size()==0) ;
68         for (im=_fin.begin();im!=_fin.end(); im++)
69         {
70 #if defined(_DEBUG_) || defined(_DEBUG)
71                 const client_infos &lesInfos = *(*im).second ;
72                 MESSAGE("Deletion") ; SCRUTE( lesInfos._name ) ;
73 #endif
74                 _fin.erase ( im ) ;
75         }
76         ASSERT(_fin.size()==0) ;
77         _Compteur = -1 ;
78         if ( _SessionName )
79         {
80 #ifndef WIN32
81                 delete [] _SessionName ;
82 #else
83                 delete [] (char*)_SessionName ;
84 #endif
85                 _SessionName = 0 ;
86         }
87         END_OF("RegistryService::~RegistryService()") ;
88 }
89
90 /* ------------------------------*/
91 /* Contrats IDL                  */
92 /* ------------------------------*/
93
94 CORBA::ULong RegistryService::size ( void )
95 {
96         ASSERT(_SessionName) ;
97         ASSERT(strlen(_SessionName)>0) ;
98         return _reg.size() ;
99 }
100
101
102 CORBA::ULong RegistryService::add( const Registry::Infos & infos )
103 {
104         BEGIN_OF("RegistryService::add") ;
105         ASSERT(_SessionName) ;
106         ASSERT(strlen(_SessionName)>0) ;
107         client_infos *ptr_Component = new client_infos( infos ) ;
108         ASSERT(ptr_Component) ;
109         SCRUTE(ptr_Component->_name) ;
110
111         _Compteur++;
112         _reg[_Compteur]=ptr_Component;
113
114         END_OF("RegistryService::add") ;
115         return (CORBA::ULong)_Compteur ;
116 }
117
118 #ifndef WIN32
119 void RegistryService::remove( const CORBA::ULong id)
120 #else
121 void RegistryService::remove( CORBA::ULong id)
122 #endif
123 {
124         BEGIN_OF("RegistryService::remove") ;
125         SCRUTE(id) ;
126         ASSERT(_SessionName) ;
127         ASSERT(strlen(_SessionName)>0) ;
128         
129         ASSERT(_reg.find(id)!=_reg.end()) 
130         _reg[id]->_status=TERMINATED;
131         _reg[id]->_ts_end = time(NULL) ;
132
133         _fin[id]=_reg[id];
134         SCRUTE(_fin.size()) ;
135
136         map<int,client_infos *>::iterator pos = _reg.find ( id ) ;
137          _reg.erase ( pos ) ;
138         SCRUTE(_reg.size()) ;
139         SCRUTE(_fin.size()) ;
140
141         END_OF("RegistryService::remove") ;
142         return ;
143 }
144
145
146 #ifndef WIN32
147 void RegistryService::hello( const CORBA::ULong id )
148 #else
149 void RegistryService::hello( CORBA::ULong id )
150 #endif
151 {
152         BEGIN_OF("RegistryService::hello") ;
153         SCRUTE(id) ;
154         ASSERT(_SessionName) ;
155         ASSERT(strlen(_SessionName)>0) ;
156
157         ASSERT(_reg.find(id)!=_reg.end()) 
158         _reg[id]->_ts_hello = time(NULL) ;
159                 
160         END_OF("RegistryService::hello") ;
161         return ;
162 }
163
164
165 void RegistryService::end( void )
166 {
167         ASSERT(_SessionName) ;
168         ASSERT(strlen(_SessionName)>0) ;
169         BEGIN_OF( "RegistryService::end( void )" ) ;
170         exit( EXIT_SUCCESS ) ;
171 }
172
173
174 Registry::AllInfos* RegistryService::getall( void )
175 {
176         ASSERT(_SessionName) ;
177         ASSERT(strlen(_SessionName)>0) ;
178         return RegistryService::makeseq(_reg) ;
179 }
180
181 Registry::AllInfos* RegistryService::history( void )
182 {
183         ASSERT(_SessionName) ;
184         ASSERT(strlen(_SessionName)>0) ;
185         return RegistryService::makeseq(_fin) ;
186 }
187
188 Registry::AllInfos* RegistryService::makeseq(map<int,client_infos *> &mymap )
189 {
190         int i=0 ;
191
192         Registry::AllInfos *all = new Registry::AllInfos ;
193         ASSERT(all) ;
194         const int RegLength = mymap.size();
195         all->length(RegLength);
196
197         map<int,client_infos *>::iterator im;
198         for (im=mymap.begin();im!=mymap.end(); im++)
199         {
200
201                 Registry::Infos &infos = (*all)[i] ;
202                 const client_infos &lesInfos = *(*im).second ;
203                 infos.name      = CORBA::string_dup( lesInfos._name ) ;
204                 infos.pid       = lesInfos._pid ;
205                 infos.pwname    = lesInfos._pwname ;
206                 infos.machine   = CORBA::string_dup( lesInfos._machine ) ;
207                 infos.adip      = CORBA::string_dup( lesInfos._adip ) ;
208                 infos.uid       = lesInfos._uid ;
209                 infos.tc_start  = lesInfos._ts_start + lesInfos._difftime ;
210                 infos.tc_hello  = lesInfos._ts_hello + lesInfos._difftime ;
211                 infos.tc_end    = lesInfos._ts_end + lesInfos._difftime ;
212                 infos.difftime  = lesInfos._difftime ;
213                 infos.cdir      = CORBA::string_dup( lesInfos._cdir ) ;
214                 infos.status    = lesInfos._status ;
215
216                 i++;
217         }
218
219         return all ;
220 }
221
222
223 /* ------------------------------*/
224 /* Autres                        */
225 /* ------------------------------*/
226
227 RegistryService::client_infos::client_infos( const Registry::Infos &infos ):\
228                                                                         _ior(duplicate(infos.ior)),\
229                                                                         _name(duplicate(infos.name)),\
230                                                                         _pid(infos.pid),\
231                                                                         _machine(duplicate(infos.machine)),\
232                                                                         _adip(duplicate(infos.adip)),\
233                                                                         _uid(infos.uid),\
234                                                                         _pwname(duplicate(infos.pwname)),\
235                                                                         _ts_start(time(NULL)),\
236                                                                         _difftime(infos.tc_start - _ts_start),\
237                                                                         _cdir(duplicate(infos.cdir)),\
238                                                                         _ts_hello(_ts_start),\
239                                                                         _ts_end(0),\
240                                                                         _status(RUNNING)
241 {
242   //    SCRUTE(_ior) ;
243         ;
244 }
245
246 RegistryService::client_infos::~client_infos()
247 {
248         delete [] (char*)_ior ; (char*&)_ior = NULL  ;
249         delete [] (char*)_name ; (char*&)_name = NULL  ;
250         delete [] (char*)_machine ; (char*&)_machine = NULL ;
251         delete [] (char*)_pwname ; (char*&)_pwname = NULL ;
252         delete [] (char*)_adip ; (char*&)_adip = NULL ;
253         delete [] (char*)_cdir ; (char*&)_cdir = NULL ;
254 }
255
256 void RegistryService::SessionName( const char *sessionName )
257 {
258         ASSERT(sessionName) ;
259         ASSERT(strlen(sessionName)>0) ;
260         _SessionName = duplicate(sessionName) ;
261         return ;
262 }
263 void RegistryService::ping()
264 {
265 #ifndef WIN32
266   MESSAGE(" RegistryService::ping() pid "<< getpid());
267 #else
268   MESSAGE(" RegistryService::ping() pid "<< _getpid());
269 #endif
270 }
271
272 CORBA::Long RegistryService::getPID()
273 {
274   return (CORBA::Long)getpid();
275 }