Salome HOME
NRI : Check if a component-username is already associated with a component-name.
[modules/kernel.git] / src / Registry / RegistryConnexion.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : RegistryConnexion.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 "Utils_ORB_INIT.hxx"
13 # include "RegistryConnexion.hxx"
14 # include "SALOME_NamingService.hxx"
15 # include "Utils_Identity.hxx"
16 # include "Utils_SINGLETON.hxx"
17 # include "Utils_CommException.hxx"
18 # include "OpUtil.hxx"
19 # include "utilities.h"
20
21 extern "C"
22 {
23 # include <stdio.h>
24 }
25
26
27 Registry::Components_var Connexion( int argc , char **argv , const char *ptrSessionName ) throw( CommException )
28 {
29         Registry::Components_var varComponents = 0 ;
30         ASSERT(ptrSessionName) ;
31         ASSERT(strlen(ptrSessionName)>0) ;
32         const char *registryName = "Registry" ;
33
34         try
35         {
36                 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
37                 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
38                 CORBA::ORB_var &orb = init( argc , argv ) ;
39         
40                 SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
41                 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
42                 naming.init_orb( orb ) ;
43
44                 // Recuperation de la reference de l'objet
45                 CORBA::Object_var object = naming.Resolve( registryName ) ;
46                 if(CORBA::is_nil(object)) throw CommException( "unable to find the RegistryService" ) ;
47
48                 // Specialisation de l'objet generique
49                 varComponents = Registry::Components::_narrow( object ) ;
50                 ASSERT(! CORBA::is_nil(varComponents)) ;
51         }
52         catch( ... )
53         {
54                 throw CommException ( "NamingService Connexion Error" ) ;
55         }
56         return varComponents ;
57 }
58
59
60 RegistryConnexion::RegistryConnexion( int argc , char **argv , const char *ior , const char *ptrSessionName, const char *componentName ): \
61         _Ior(duplicate(ior)), _VarComponents( Connexion(argc,argv,ptrSessionName) ), _SessionName(ptrSessionName),_Name(""), _Id(0)
62 {
63         this->add( componentName ) ;
64 }
65
66
67 RegistryConnexion::RegistryConnexion( void ): _Ior(0), _VarComponents( 0 ), _Name(""), _Id(0)
68 {
69         INTERRUPTION(EXIT_FAILURE) ;
70 }
71
72
73 RegistryConnexion::~RegistryConnexion()
74 {
75         BEGIN_OF("RegistryConnexion::~RegistryConnexion()" ) ;
76         if( _Id != 0 )
77         {
78                 ASSERT(_SessionName!="" ) ;
79                 ASSERT(_Name!="" ) ;
80                 _VarComponents->remove( _Id ) ;
81         }
82         _Id   = 0 ;
83         delete [] _Ior;
84         _Ior = 0;
85         _SessionName = "";
86         _Name = "" ;
87         END_OF("RegistryConnexion::~RegistryConnexion()" ) ;
88 }
89
90
91 void RegistryConnexion::add( const char *aName )
92 {
93         ASSERT(_SessionName!="" ) ;
94         ASSERT(_Name=="" ) ;
95         ASSERT(_Id==0 ) ;
96         ASSERT(aName) ;
97         SCRUTE(aName) ;
98         ASSERT(strlen( aName )>0) ;
99
100         const Identity lesInfos( aName ) ;
101         Registry::Infos infos ;
102                 infos.name      = CORBA::string_dup( lesInfos.name() ) ;
103                 infos.pid       = lesInfos.pid() ;
104                 infos.machine   = CORBA::string_dup( lesInfos.host_char() ) ;
105                 infos.adip      = CORBA::string_dup( lesInfos.adip() ) ;
106                 infos.uid       = lesInfos.uid() ;
107                 infos.pwname    = CORBA::string_dup( lesInfos.pwname() ) ;
108                 infos.tc_start  = lesInfos.start() ;
109                 infos.tc_hello  = 0 ;
110                 infos.tc_end    = 0 ;
111                 infos.difftime  = 0 ;
112                 infos.cdir      = CORBA::string_dup( lesInfos.rep() ) ;
113                 infos.status    = -1 ;
114                 infos.ior       = CORBA::string_dup(_Ior);
115
116         ASSERT(!CORBA::is_nil(this->_VarComponents)) ;
117
118         CORBA::ULong id = _VarComponents->add( infos ) ;
119
120         SCRUTE(aName) ; SCRUTE(id) ;
121         ASSERT(id) ;
122
123         _Id   = id ;
124         _Name = aName ;
125
126         return ;
127 }
128
129
130 void RegistryConnexion::remove( void )
131 {
132         ASSERT(_Id>0) ;
133         ASSERT(!CORBA::is_nil(this->_VarComponents)) ;
134         _VarComponents->remove( _Id ) ;
135
136         _Name = "" ;
137         _Id   = 0 ;
138
139         return ;
140 }