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