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