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