Salome HOME
Fix compilation pb.
[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 WNT
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                 const client_infos &lesInfos = *(*im).second ;
63                 MESSAGE("Deletion") ; SCRUTE( lesInfos._name ) ;
64                 _reg.erase ( im ) ;
65         }
66         ASSERT(_reg.size()==0) ;
67         for (im=_fin.begin();im!=_fin.end(); im++)
68         {
69                 const client_infos &lesInfos = *(*im).second ;
70                 MESSAGE("Deletion") ; SCRUTE( lesInfos._name ) ;
71                 _fin.erase ( im ) ;
72         }
73         ASSERT(_fin.size()==0) ;
74         _Compteur = -1 ;
75         if ( _SessionName )
76         {
77 #ifndef WNT
78                 delete [] _SessionName ;
79 #else
80                 delete [] (char*)_SessionName ;
81 #endif
82                 _SessionName = 0 ;
83         }
84         END_OF("RegistryService::~RegistryService()") ;
85 }
86
87 /* ------------------------------*/
88 /* Contrats IDL                  */
89 /* ------------------------------*/
90
91 CORBA::ULong RegistryService::size ( void )
92 {
93         ASSERT(_SessionName) ;
94         ASSERT(strlen(_SessionName)>0) ;
95         return _reg.size() ;
96 }
97
98
99 CORBA::ULong RegistryService::add( const Registry::Infos & infos )
100 {
101         BEGIN_OF("RegistryService::add") ;
102         ASSERT(_SessionName) ;
103         ASSERT(strlen(_SessionName)>0) ;
104         client_infos *ptr_Component = new client_infos( infos ) ;
105         ASSERT(ptr_Component) ;
106         SCRUTE(ptr_Component->_name) ;
107
108         _Compteur++;
109         _reg[_Compteur]=ptr_Component;
110
111         END_OF("RegistryService::add") ;
112         return (CORBA::ULong)_Compteur ;
113 }
114
115 #ifndef WNT
116 void RegistryService::remove( const CORBA::ULong id)
117 #else
118 void RegistryService::remove( CORBA::ULong id)
119 #endif
120 {
121         BEGIN_OF("RegistryService::remove") ;
122         SCRUTE(id) ;
123         ASSERT(_SessionName) ;
124         ASSERT(strlen(_SessionName)>0) ;
125         
126         ASSERT(_reg.find(id)!=_reg.end()) 
127         _reg[id]->_status=TERMINATED;
128         _reg[id]->_ts_end = time(NULL) ;
129
130         _fin[id]=_reg[id];
131         SCRUTE(_fin.size()) ;
132
133         map<int,client_infos *>::iterator pos = _reg.find ( id ) ;
134          _reg.erase ( pos ) ;
135         SCRUTE(_reg.size()) ;
136         SCRUTE(_fin.size()) ;
137
138         END_OF("RegistryService::remove") ;
139         return ;
140 }
141
142
143 #ifndef WNT
144 void RegistryService::hello( const CORBA::ULong id )
145 #else
146 void RegistryService::hello( CORBA::ULong id )
147 #endif
148 {
149         BEGIN_OF("RegistryService::hello") ;
150         SCRUTE(id) ;
151         ASSERT(_SessionName) ;
152         ASSERT(strlen(_SessionName)>0) ;
153
154         ASSERT(_reg.find(id)!=_reg.end()) 
155         _reg[id]->_ts_hello = time(NULL) ;
156                 
157         END_OF("RegistryService::hello") ;
158         return ;
159 }
160
161
162 void RegistryService::end( void )
163 {
164         ASSERT(_SessionName) ;
165         ASSERT(strlen(_SessionName)>0) ;
166         BEGIN_OF( "RegistryService::end( void )" ) ;
167         exit( EXIT_SUCCESS ) ;
168 }
169
170
171 Registry::AllInfos* RegistryService::getall( void )
172 {
173         ASSERT(_SessionName) ;
174         ASSERT(strlen(_SessionName)>0) ;
175         return RegistryService::makeseq(_reg) ;
176 }
177
178 Registry::AllInfos* RegistryService::history( void )
179 {
180         ASSERT(_SessionName) ;
181         ASSERT(strlen(_SessionName)>0) ;
182         return RegistryService::makeseq(_fin) ;
183 }
184
185 Registry::AllInfos* RegistryService::makeseq(map<int,client_infos *> &mymap )
186 {
187         int i=0 ;
188
189         Registry::AllInfos *all = new Registry::AllInfos ;
190         ASSERT(all) ;
191         const int RegLength = mymap.size();
192         all->length(RegLength);
193
194         map<int,client_infos *>::iterator im;
195         for (im=mymap.begin();im!=mymap.end(); im++)
196         {
197
198                 Registry::Infos &infos = (*all)[i] ;
199                 const client_infos &lesInfos = *(*im).second ;
200                 infos.name      = CORBA::string_dup( lesInfos._name ) ;
201                 infos.pid       = lesInfos._pid ;
202                 infos.pwname    = lesInfos._pwname ;
203                 infos.machine   = CORBA::string_dup( lesInfos._machine ) ;
204                 infos.adip      = CORBA::string_dup( lesInfos._adip ) ;
205                 infos.uid       = lesInfos._uid ;
206                 infos.tc_start  = lesInfos._ts_start + lesInfos._difftime ;
207                 infos.tc_hello  = lesInfos._ts_hello + lesInfos._difftime ;
208                 infos.tc_end    = lesInfos._ts_end + lesInfos._difftime ;
209                 infos.difftime  = lesInfos._difftime ;
210                 infos.cdir      = CORBA::string_dup( lesInfos._cdir ) ;
211                 infos.status    = lesInfos._status ;
212
213                 i++;
214         }
215
216         return all ;
217 }
218
219
220 /* ------------------------------*/
221 /* Autres                        */
222 /* ------------------------------*/
223
224 RegistryService::client_infos::client_infos( const Registry::Infos &infos ):\
225                                                                         _ior(duplicate(infos.ior)),\
226                                                                         _name(duplicate(infos.name)),\
227                                                                         _pid(infos.pid),\
228                                                                         _machine(duplicate(infos.machine)),\
229                                                                         _adip(duplicate(infos.adip)),\
230                                                                         _uid(infos.uid),\
231                                                                         _pwname(duplicate(infos.pwname)),\
232                                                                         _ts_start(time(NULL)),\
233                                                                         _difftime(infos.tc_start - _ts_start),\
234                                                                         _cdir(duplicate(infos.cdir)),\
235                                                                         _ts_hello(_ts_start),\
236                                                                         _ts_end(0),\
237                                                                         _status(RUNNING)
238 {
239   //    SCRUTE(_ior) ;
240         ;
241 }
242
243 RegistryService::client_infos::~client_infos()
244 {
245         delete [] (char*)_ior ; (char*&)_ior = NULL  ;
246         delete [] (char*)_name ; (char*&)_name = NULL  ;
247         delete [] (char*)_machine ; (char*&)_machine = NULL ;
248         delete [] (char*)_pwname ; (char*&)_pwname = NULL ;
249         delete [] (char*)_adip ; (char*&)_adip = NULL ;
250         delete [] (char*)_cdir ; (char*&)_cdir = NULL ;
251 }
252
253 void RegistryService::SessionName( const char *sessionName )
254 {
255         ASSERT(sessionName) ;
256         ASSERT(strlen(sessionName)>0) ;
257         _SessionName = duplicate(sessionName) ;
258         return ;
259 }
260 void RegistryService::ping()
261 {
262 #ifndef WNT
263   MESSAGE(" RegistryService::ping() pid "<< getpid());
264 #else
265   MESSAGE(" RegistryService::ping() pid "<< _getpid());
266 #endif
267 }
268
269 CORBA::Long RegistryService::getPID()
270 {
271   return (CORBA::Long)getpid();
272 }