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