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