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