00001
00002
00003
00004
00005 #include "tao/Strategies/DIOP_Endpoint.h"
00006
00007 #if defined (TAO_HAS_DIOP) && (TAO_HAS_DIOP != 0)
00008
00009 #include "tao/Strategies/DIOP_Connection_Handler.h"
00010 #include "tao/debug.h"
00011 #include "tao/ORB_Constants.h"
00012
00013 #include "ace/OS_NS_stdio.h"
00014 #include "ace/OS_NS_string.h"
00015
00016
00017 ACE_RCSID (Strategies,
00018 DIOP_Endpoint,
00019 "DIOP_Endpoint.cpp,v 1.15 2006/05/16 12:35:39 jwillemsen Exp")
00020
00021
00022 #if !defined (__ACE_INLINE__)
00023 # include "tao/Strategies/DIOP_Endpoint.i"
00024 #endif
00025
00026 #include "ace/os_include/os_netdb.h"
00027
00028 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00029
00030 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const ACE_INET_Addr &addr,
00031 int use_dotted_decimal_addresses)
00032
00033 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE)
00034 , host_ ()
00035 , port_ (0)
00036 , object_addr_ (addr)
00037 , object_addr_set_ (0)
00038 , next_ (0)
00039 {
00040 this->set (addr, use_dotted_decimal_addresses);
00041 }
00042
00043 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const char *host,
00044 CORBA::UShort port,
00045 const ACE_INET_Addr &addr,
00046 CORBA::Short priority)
00047 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE,
00048 priority)
00049 , host_ ()
00050 , port_ (port)
00051 , object_addr_ (addr)
00052 , object_addr_set_ (0)
00053 , next_ (0)
00054 {
00055 if (host != 0)
00056 this->host_ = host;
00057 }
00058
00059 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (void)
00060 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE),
00061 host_ (),
00062 port_ (0),
00063 object_addr_ (),
00064 object_addr_set_ (0),
00065 next_ (0)
00066 {
00067 }
00068
00069 TAO_DIOP_Endpoint::TAO_DIOP_Endpoint (const char *host,
00070 CORBA::UShort port,
00071 CORBA::Short priority)
00072 : TAO_Endpoint (TAO_TAG_DIOP_PROFILE),
00073 host_ (),
00074 port_ (port),
00075 object_addr_ (),
00076 object_addr_set_ (0),
00077 next_ (0)
00078 {
00079 if (host != 0)
00080 this->host_ = host;
00081
00082 this->priority (priority);
00083 }
00084
00085 TAO_DIOP_Endpoint::~TAO_DIOP_Endpoint (void)
00086 {
00087
00088 }
00089
00090 int
00091 TAO_DIOP_Endpoint::set (const ACE_INET_Addr &addr,
00092 int use_dotted_decimal_addresses)
00093 {
00094 char tmp_host[MAXHOSTNAMELEN + 1];
00095
00096 if (use_dotted_decimal_addresses
00097 || addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
00098 {
00099 const char *tmp = addr.get_host_addr ();
00100 if (tmp == 0)
00101 {
00102 if (TAO_debug_level > 0)
00103 ACE_DEBUG ((LM_DEBUG,
00104 ACE_TEXT ("\n\nTAO (%P|%t) ")
00105 ACE_TEXT ("DIOP_Endpoint::set ")
00106 ACE_TEXT ("- %p\n\n"),
00107 ACE_TEXT ("cannot determine hostname")));
00108 return -1;
00109 }
00110 else
00111 this->host_ = tmp;
00112 }
00113 else
00114 this->host_ = CORBA::string_dup (tmp_host);
00115
00116 this->port_ = addr.get_port_number();
00117
00118 return 0;
00119 }
00120
00121 int
00122 TAO_DIOP_Endpoint::addr_to_string (char *buffer, size_t length)
00123 {
00124 size_t const actual_len =
00125 ACE_OS::strlen (this->host_.in ())
00126 + sizeof (':')
00127 + ACE_OS::strlen ("65536")
00128 + sizeof ('\0');
00129
00130 if (length < actual_len)
00131 return -1;
00132
00133 ACE_OS::sprintf (buffer, "%s:%d",
00134 this->host_.in (), this->port_);
00135
00136 return 0;
00137 }
00138
00139 const char *
00140 TAO_DIOP_Endpoint::host (const char *h)
00141 {
00142 this->host_ = h;
00143
00144 return this->host_.in ();
00145 }
00146
00147 TAO_Endpoint *
00148 TAO_DIOP_Endpoint::next (void)
00149 {
00150 return this->next_;
00151 }
00152
00153 TAO_Endpoint *
00154 TAO_DIOP_Endpoint::duplicate (void)
00155 {
00156 TAO_DIOP_Endpoint *endpoint = 0;
00157
00158 ACE_NEW_RETURN (endpoint,
00159 TAO_DIOP_Endpoint (this->host_.in (),
00160 this->port_,
00161 this->object_addr_,
00162 this->priority ()),
00163 0);
00164
00165 return endpoint;
00166 }
00167
00168 CORBA::Boolean
00169 TAO_DIOP_Endpoint::is_equivalent (const TAO_Endpoint *other_endpoint)
00170 {
00171 TAO_Endpoint *endpt = const_cast<TAO_Endpoint *> (other_endpoint);
00172
00173 TAO_DIOP_Endpoint *endpoint = dynamic_cast<TAO_DIOP_Endpoint *> (endpt);
00174 if (endpoint == 0)
00175 return 0;
00176
00177 return (this->port () == endpoint->port ()
00178 && ACE_OS::strcmp(this->host (), endpoint->host()) == 0);
00179 }
00180
00181 CORBA::ULong
00182 TAO_DIOP_Endpoint::hash (void)
00183 {
00184 if (this->hash_val_ != 0)
00185 return this->hash_val_;
00186
00187 {
00188 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00189 guard,
00190 this->addr_lookup_lock_,
00191 this->hash_val_);
00192
00193 if (this->hash_val_ != 0)
00194 return this->hash_val_;
00195
00196 this->hash_val_ =
00197 ACE::hash_pjw (this->host ()) + this->port ();
00198 }
00199
00200 return this->hash_val_;
00201 }
00202
00203 const ACE_INET_Addr &
00204 TAO_DIOP_Endpoint::object_addr (void) const
00205 {
00206
00207
00208
00209
00210
00211
00212
00213 if (!this->object_addr_set_)
00214 {
00215
00216
00217 TAO_DIOP_Endpoint *endpoint =
00218 const_cast<TAO_DIOP_Endpoint *> (this);
00219
00220 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
00221 guard,
00222 endpoint->addr_lookup_lock_,
00223 this->object_addr_ );
00224
00225 if (!this->object_addr_set_)
00226 {
00227 if (endpoint->object_addr_.set (this->port_,
00228 this->host_.in ()) == -1)
00229 {
00230
00231
00232
00233
00234
00235
00236
00237
00238 endpoint->object_addr_.set_type (-1);
00239 }
00240 else
00241 {
00242 endpoint->object_addr_set_ = 1;
00243 }
00244 }
00245 }
00246 return this->object_addr_;
00247 }
00248
00249 TAO_END_VERSIONED_NAMESPACE_DECL
00250
00251 #endif