Salome HOME
Fix compilation pb.
[modules/kernel.git] / src / Registry / RegistryConnexion.cxx
1 //  SALOME Registry : Registry server implementation
2 //
3 //  Copyright (C) 2003  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 //
23 //
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 using namespace std;
43
44
45 Registry::Components_var Connexion( int argc , char **argv , const char *ptrSessionName ) throw( CommException )
46 {
47         Registry::Components_var varComponents = 0 ;
48         ASSERT(ptrSessionName) ;
49         ASSERT(strlen(ptrSessionName)>0) ;
50         const char *registryName = "Registry" ;
51
52         try
53         {
54                 ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
55                 ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
56                 CORBA::ORB_var &orb = init( argc , argv ) ;
57         
58                 SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
59                 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
60                 naming.init_orb( orb ) ;
61
62                 // Recuperation de la reference de l'objet
63                 CORBA::Object_var object = naming.Resolve( registryName ) ;
64                 if(CORBA::is_nil(object)) throw CommException( "unable to find the RegistryService" ) ;
65
66                 // Specialisation de l'objet generique
67                 varComponents = Registry::Components::_narrow( object ) ;
68                 ASSERT(! CORBA::is_nil(varComponents)) ;
69         }
70         catch( ... )
71         {
72                 throw CommException ( "NamingService Connexion Error" ) ;
73         }
74         return varComponents ;
75 }
76
77
78 RegistryConnexion::RegistryConnexion( int argc , char **argv , const char *ior , const char *ptrSessionName, const char *componentName ): \
79         _Ior(duplicate(ior)), _VarComponents( Connexion(argc,argv,ptrSessionName) ), _SessionName(ptrSessionName),_Name(""), _Id(0)
80 {
81         this->add( componentName ) ;
82 }
83
84
85 RegistryConnexion::RegistryConnexion( void ): _Ior(0), _VarComponents( 0 ), _Name(""), _Id(0)
86 {
87         INTERRUPTION(EXIT_FAILURE) ;
88 }
89
90
91 RegistryConnexion::~RegistryConnexion()
92 {
93         BEGIN_OF("RegistryConnexion::~RegistryConnexion()" ) ;
94         if( _Id != 0 )
95         {
96                 ASSERT(_SessionName!="" ) ;
97                 ASSERT(_Name!="" ) ;
98                 _VarComponents->remove( _Id ) ;
99         }
100         _Id   = 0 ;
101 #ifndef WNT
102         delete [] _Ior;
103 #else
104         delete [] (char*) _Ior;
105 #endif
106         _Ior = 0;
107         _SessionName = "";
108         _Name = "" ;
109         END_OF("RegistryConnexion::~RegistryConnexion()" ) ;
110 }
111
112
113 void RegistryConnexion::add( const char *aName )
114 {
115         ASSERT(_SessionName!="" ) ;
116         ASSERT(_Name=="" ) ;
117         ASSERT(_Id==0 ) ;
118         ASSERT(aName) ;
119         SCRUTE(aName) ;
120         ASSERT(strlen( aName )>0) ;
121
122         const Identity lesInfos( aName ) ;
123         Registry::Infos infos ;
124                 infos.name      = CORBA::string_dup( lesInfos.name() ) ;
125                 infos.pid       = lesInfos.pid() ;
126                 infos.machine   = CORBA::string_dup( lesInfos.host_char() ) ;
127                 infos.adip      = CORBA::string_dup( lesInfos.adip() ) ;
128                 infos.uid       = (long)lesInfos.uid() ;
129                 infos.pwname    = CORBA::string_dup( lesInfos.pwname() ) ;
130                 infos.tc_start  = lesInfos.start() ;
131                 infos.tc_hello  = 0 ;
132                 infos.tc_end    = 0 ;
133                 infos.difftime  = 0 ;
134                 infos.cdir      = CORBA::string_dup( lesInfos.rep() ) ;
135                 infos.status    = -1 ;
136                 infos.ior       = CORBA::string_dup(_Ior);
137
138         ASSERT(!CORBA::is_nil(this->_VarComponents)) ;
139
140         CORBA::ULong id = _VarComponents->add( infos ) ;
141
142         SCRUTE(aName) ; SCRUTE(id) ;
143         ASSERT(id) ;
144
145         _Id   = id ;
146         _Name = aName ;
147
148         return ;
149 }
150
151
152 void RegistryConnexion::remove( void )
153 {
154         ASSERT(_Id>0) ;
155         ASSERT(!CORBA::is_nil(this->_VarComponents)) ;
156         _VarComponents->remove( _Id ) ;
157
158         _Name = "" ;
159         _Id   = 0 ;
160
161         return ;
162 }