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