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