DIOP_Endpoint.cpp

Go to the documentation of this file.
00001 // This may look like C, but it's really -*- C++ -*-
00002 // DIOP_Endpoint.cpp,v 1.15 2006/05/16 12:35:39 jwillemsen Exp
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 /* __ACE_INLINE__ */
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 ()) // chars in host name
00126     + sizeof (':')                     // delimiter
00127     + ACE_OS::strlen ("65536")         // max port
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     // .. DCL
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   // The object_addr_ is initialized here, rather than at IOR decode
00207   // time for several reasons:
00208   //   1. A request on the object may never be invoked.
00209   //   2. The DNS setup may have changed dynamically.
00210   //   ...etc..
00211 
00212   // Double checked locking optimization.
00213   if (!this->object_addr_set_)
00214     {
00215       // We need to modify the object_addr_ in this method.  Do so
00216       // using a  non-const copy of the <this> pointer.
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               // If this call fails, it most likely due a hostname
00231               // lookup failure caused by a DNS misconfiguration.  If
00232               // a request is made to the object at the given host and
00233               // port, then a CORBA::TRANSIENT() exception should be
00234               // thrown.
00235 
00236               // Invalidate the ACE_INET_Addr.  This is used as a flag
00237               // to denote that ACE_INET_Addr initialization failed.
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 /* TAO_HAS_DIOP && TAO_HAS_DIOP != 0 */

Generated on Thu Nov 9 13:39:28 2006 for TAO_Strategies by doxygen 1.3.6