Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / Strategies / SHMIOP_Endpoint.cpp
blobef5c6d8ab71570a42994cef71c172fa05a0d81c3
1 #include "tao/Strategies/SHMIOP_Endpoint.h"
3 #if defined (TAO_HAS_SHMIOP) && (TAO_HAS_SHMIOP != 0)
5 #include "tao/Strategies/SHMIOP_Connection_Handler.h"
6 #include "tao/debug.h"
7 #include "tao/ORB_Constants.h"
9 #include "ace/OS_NS_stdio.h"
11 #if !defined (__ACE_INLINE__)
12 # include "tao/Strategies/SHMIOP_Endpoint.inl"
13 #endif /* __ACE_INLINE__ */
15 #include "ace/os_include/os_netdb.h"
16 #include <cstring>
18 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
20 TAO_SHMIOP_Endpoint::TAO_SHMIOP_Endpoint (const ACE_MEM_Addr &addr,
21 int use_dotted_decimal_addresses)
22 : TAO_Endpoint (TAO_TAG_SHMEM_PROFILE)
23 , host_ ()
24 , port_ (0)
25 , object_addr_ (addr.get_remote_addr ())
26 , object_addr_set_ (0)
27 , next_ (0)
29 this->set (addr.get_remote_addr (), use_dotted_decimal_addresses);
32 TAO_SHMIOP_Endpoint::TAO_SHMIOP_Endpoint (const ACE_INET_Addr &addr,
33 int use_dotted_decimal_addresses)
34 : TAO_Endpoint (TAO_TAG_SHMEM_PROFILE)
35 , host_ ()
36 , port_ (0)
37 , object_addr_ (addr)
38 , object_addr_set_ (0)
39 , next_ (0)
41 this->set (addr, use_dotted_decimal_addresses);
44 TAO_SHMIOP_Endpoint::TAO_SHMIOP_Endpoint (const char *host,
45 CORBA::UShort port,
46 const ACE_INET_Addr &addr,
47 CORBA::Short priority)
48 : TAO_Endpoint (TAO_TAG_SHMEM_PROFILE, priority)
49 , host_ ()
50 , port_ (port)
51 , object_addr_ (addr)
52 , object_addr_set_ (0)
53 , next_ (0)
55 if (host != 0)
56 this->host_ = host;
59 TAO_SHMIOP_Endpoint::TAO_SHMIOP_Endpoint ()
60 : TAO_Endpoint (TAO_TAG_SHMEM_PROFILE)
61 , host_ ()
62 , port_ (0)
63 , object_addr_ ()
64 , object_addr_set_ (0)
65 , next_ (0)
69 TAO_SHMIOP_Endpoint::TAO_SHMIOP_Endpoint (const char *host,
70 CORBA::UShort port,
71 CORBA::Short priority)
72 : TAO_Endpoint (TAO_TAG_SHMEM_PROFILE)
73 , host_ ()
74 , port_ (port)
75 , object_addr_ ()
76 , object_addr_set_ (0)
77 , next_ (0)
79 if (host != 0)
80 this->host_ = host;
82 this->priority (priority);
85 TAO_SHMIOP_Endpoint::~TAO_SHMIOP_Endpoint ()
89 int
90 TAO_SHMIOP_Endpoint::set (const ACE_INET_Addr &addr,
91 int use_dotted_decimal_addresses)
93 char tmp_host[MAXHOSTNAMELEN + 1];
95 if (use_dotted_decimal_addresses
96 || addr.get_host_name (tmp_host, sizeof (tmp_host)) != 0)
98 if (use_dotted_decimal_addresses == 0 && TAO_debug_level > 5)
100 TAOLIB_DEBUG ((LM_DEBUG,
101 ACE_TEXT ("TAO (%P|%t) - SHMIOP_Endpoint::set, ")
102 ACE_TEXT ("- %p cannot determine hostname\n")));
105 const char *tmp = addr.get_host_addr ();
106 if (tmp == 0)
108 if (TAO_debug_level > 0)
109 TAOLIB_DEBUG ((LM_DEBUG,
110 ACE_TEXT ("TAO (%P|%t) - ")
111 ACE_TEXT ("SHMIOP_Endpoint::set ")
112 ACE_TEXT ("- %p\n"),
113 ACE_TEXT ("cannot determine hostname")));
114 return -1;
116 else
117 this->host_ = tmp;
119 else
120 this->host_ = CORBA::string_dup (tmp_host);
122 this->port_ = addr.get_port_number();
124 return 0;
128 TAO_SHMIOP_Endpoint::addr_to_string (char *buffer, size_t length)
130 size_t actual_len =
131 ACE_OS::strlen (this->host_.in ()) // chars in host name
132 + sizeof (':') // delimiter
133 + std::strlen ("65536") // max port
134 + sizeof ('\0');
136 if (length < actual_len)
137 return -1;
139 ACE_OS::sprintf (buffer, "%s:%d",
140 this->host_.in (), this->port_);
142 return 0;
145 const char *
146 TAO_SHMIOP_Endpoint::host (const char *h)
148 this->host_ = h;
150 return this->host_.in ();
153 TAO_Endpoint *
154 TAO_SHMIOP_Endpoint::next ()
156 return this->next_;
159 TAO_Endpoint *
160 TAO_SHMIOP_Endpoint::duplicate ()
162 TAO_SHMIOP_Endpoint *endpoint = 0;
163 ACE_NEW_RETURN (endpoint,
164 TAO_SHMIOP_Endpoint (this->host_.in (),
165 this->port_,
166 this->object_addr_,
167 this->priority ()),
170 return endpoint;
174 CORBA::Boolean
175 TAO_SHMIOP_Endpoint::is_equivalent (const TAO_Endpoint *other_endpoint)
177 const TAO_SHMIOP_Endpoint *endpoint = dynamic_cast <const TAO_SHMIOP_Endpoint *>
178 (other_endpoint);
180 if (endpoint == 0)
181 return 0;
183 return
184 this->port_ == endpoint->port_
185 && ACE_OS::strcmp (this->host_.in (), endpoint->host_.in ()) == 0;
188 CORBA::ULong
189 TAO_SHMIOP_Endpoint::hash ()
191 if (this->hash_val_ != 0)
192 return this->hash_val_;
195 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
196 guard,
197 this->addr_lookup_lock_,
198 this->hash_val_);
199 // .. DCL
200 if (this->hash_val_ != 0)
201 return this->hash_val_;
203 this->hash_val_ =
204 ACE::hash_pjw (this->host ()) + this->port ();
207 return this->hash_val_;
210 const ACE_INET_Addr &
211 TAO_SHMIOP_Endpoint::object_addr () const
213 // The object_addr_ is initialized here, rather than at IOR decode
214 // time for several reasons:
215 // 1. A request on the object may never be invoked.
216 // 2. The DNS setup may have changed dynamically.
217 // ...etc..
219 // Double checked locking optimization.
220 if (!this->object_addr_set_)
222 // We need to modify the object_addr_ in this method. Do so
223 // using a non-const copy of the <this> pointer.
224 TAO_SHMIOP_Endpoint *endpoint =
225 const_cast <TAO_SHMIOP_Endpoint *>(this);
227 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
228 guard,
229 endpoint->addr_lookup_lock_,
230 this->object_addr_ );
232 if (!this->object_addr_set_)
234 if (endpoint->object_addr_.set (this->port_,
235 this->host_.in ()) == -1)
237 // If this call fails, it most likely due a hostname
238 // lookup failure caused by a DNS misconfiguration. If
239 // a request is made to the object at the given host and
240 // port, then a CORBA::TRANSIENT() exception should be
241 // thrown.
243 // Invalidate the ACE_INET_Addr. This is used as a flag
244 // to denote that ACE_INET_Addr initialization failed.
245 endpoint->object_addr_.set_type (-1);
247 else
249 endpoint->object_addr_set_ = 1;
254 return this->object_addr_;
257 TAO_END_VERSIONED_NAMESPACE_DECL
259 #endif /* TAO_HAS_SHMIOP && TAO_HAS_SHMIOP != 0 */