Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / TAO / tao / Strategies / DIOP_Connector.cpp
bloba82ab2ed3cd867161e157ea0a3ad6ff34738e07c
1 #include "tao/Strategies/DIOP_Connector.h"
3 #if defined (TAO_HAS_DIOP) && (TAO_HAS_DIOP != 0)
5 #include "ace/Connector.h"
7 #include "tao/debug.h"
8 #include "tao/ORB_Core.h"
9 #include "tao/SystemException.h"
10 #include "tao/Base_Transport_Property.h"
11 #include "tao/Protocols_Hooks.h"
12 #include "ace/OS_NS_strings.h"
13 #include "ace/OS_NS_string.h"
14 #include "tao/Thread_Lane_Resources.h"
15 #include "ace/os_include/os_netdb.h"
17 #include "tao/Strategies/DIOP_Profile.h"
18 #include <cstring>
20 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
22 TAO_DIOP_Connector::TAO_DIOP_Connector ()
23 : TAO_Connector (TAO_TAG_DIOP_PROFILE)
27 int
28 TAO_DIOP_Connector::open (TAO_ORB_Core *orb_core)
30 this->orb_core (orb_core);
32 // Create our connect strategy
33 if (this->create_connect_strategy () == -1)
34 return -1;
36 return 0;
39 int
40 TAO_DIOP_Connector::close ()
42 return 0;
45 int
46 TAO_DIOP_Connector::set_validate_endpoint (TAO_Endpoint *endpoint)
48 TAO_DIOP_Endpoint *diop_endpoint =
49 this->remote_endpoint (endpoint);
51 if (diop_endpoint == 0)
52 return -1;
54 const ACE_INET_Addr &remote_address =
55 diop_endpoint->object_addr ();
57 // Verify that the remote ACE_INET_Addr was initialized properly.
58 // Failure can occur if hostname lookup failed when initializing the
59 // remote ACE_INET_Addr.
60 #if defined (ACE_HAS_IPV6)
61 if (remote_address.get_type () != AF_INET &&
62 remote_address.get_type () != AF_INET6)
63 #else /* ACE_HAS_IPV6 */
64 if (remote_address.get_type () != AF_INET)
65 #endif /* !ACE_HAS_IPV6 */
67 if (TAO_debug_level > 0)
69 TAOLIB_DEBUG ((LM_DEBUG,
70 ACE_TEXT ("TAO (%P|%t) - DIOP connection failed.\n")
71 ACE_TEXT ("TAO (%P|%t) This is most likely ")
72 ACE_TEXT ("due to a hostname lookup ")
73 ACE_TEXT ("failure.\n")));
76 return -1;
79 return 0;
82 TAO_Transport *
83 TAO_DIOP_Connector::make_connection (TAO::Profile_Transport_Resolver *,
84 TAO_Transport_Descriptor_Interface &desc,
85 ACE_Time_Value * /*max_wait_time*/)
87 TAO_DIOP_Endpoint *diop_endpoint =
88 this->remote_endpoint (desc.endpoint ());
90 if (diop_endpoint == 0)
91 return 0;
93 const ACE_INET_Addr &remote_address =
94 diop_endpoint->object_addr ();
96 #if defined (ACE_HAS_IPV6) && !defined (ACE_HAS_IPV6_V6ONLY)
97 // Check if we need to invalidate accepted connections
98 // from IPv4 mapped IPv6 addresses
99 if (this->orb_core ()->orb_params ()->connect_ipv6_only () &&
100 remote_address.is_ipv4_mapped_ipv6 ())
102 if (TAO_debug_level > 0)
104 ACE_TCHAR remote_as_string[MAXHOSTNAMELEN + 16];
106 (void) remote_address.addr_to_string (remote_as_string,
107 sizeof remote_as_string);
109 TAOLIB_ERROR ((LM_ERROR,
110 ACE_TEXT ("TAO (%P|%t) - DIOP_Connection_Handler::open, ")
111 ACE_TEXT ("invalid connection to IPv4 mapped IPv6 interface <%s>!\n"),
112 remote_as_string));
114 return 0;
116 #endif /* ACE_HAS_IPV6 && ACE_HAS_IPV6_V6ONLY */
118 TAO_DIOP_Connection_Handler *svc_handler = 0;
120 ACE_NEW_RETURN (svc_handler,
121 TAO_DIOP_Connection_Handler (this->orb_core ()),
124 // Make sure that we always do a remove_reference
125 ACE_Event_Handler_var svc_handler_auto_ptr (svc_handler);
127 u_short port = 0;
128 const ACE_UINT32 ia_any = INADDR_ANY;
129 ACE_INET_Addr local_addr (port, ia_any);
131 #if defined (ACE_HAS_IPV6)
132 if (remote_address.get_type () == AF_INET6)
133 local_addr.set (port,
134 ACE_IPV6_ANY);
135 #endif /* ACE_HAS_IPV6 */
137 svc_handler->local_addr (local_addr);
138 svc_handler->addr (remote_address);
140 int retval = svc_handler->open (0);
142 // Failure to open a connection.
143 if (retval != 0)
145 svc_handler->close ();
147 if (TAO_debug_level > 0)
149 TAOLIB_ERROR ((LM_ERROR,
150 ACE_TEXT ("TAO (%P|%t) - DIOP_Connector::make_connection, ")
151 ACE_TEXT ("could not make a new connection\n")));
154 return 0;
157 if (TAO_debug_level > 2)
158 TAOLIB_DEBUG ((LM_DEBUG,
159 ACE_TEXT ("TAO (%P|%t) - DIOP_Connector::connect, ")
160 ACE_TEXT ("new connection on HANDLE %d\n"),
161 svc_handler->peer ().get_handle ()));
163 TAO_DIOP_Transport *transport =
164 dynamic_cast <TAO_DIOP_Transport *> (svc_handler->transport ());
166 // In case of errors transport is zero
167 if (transport == 0)
169 svc_handler->close ();
171 // Give users a clue to the problem.
172 if (TAO_debug_level > 3)
173 TAOLIB_DEBUG ((LM_ERROR,
174 ACE_TEXT ("TAO (%P|%t) - DIOP_Connector::make_connection, ")
175 ACE_TEXT ("connection to <%C:%u> failed (%p)\n"),
176 diop_endpoint->host (),
177 diop_endpoint->port (),
178 ACE_TEXT ("errno")));
180 return 0;
183 // Add the handler to Cache
184 retval =
185 this->orb_core ()->lane_resources ().transport_cache ().cache_transport (&desc,
186 transport);
188 // Failure in adding to cache.
189 if (retval == -1)
191 svc_handler->close ();
193 if (TAO_debug_level > 0)
195 TAOLIB_ERROR ((LM_ERROR,
196 ACE_TEXT ("TAO (%P|%t) - DIOP_Connector::make_connection, ")
197 ACE_TEXT ("could not add the new connection to cache\n")));
200 return 0;
203 svc_handler_auto_ptr.release ();
204 return transport;
207 TAO_Profile *
208 TAO_DIOP_Connector::create_profile (TAO_InputCDR& cdr)
210 TAO_Profile *pfile = 0;
211 ACE_NEW_RETURN (pfile,
212 TAO_DIOP_Profile (this->orb_core ()),
215 if (pfile->decode (cdr) == -1)
217 pfile->_decr_refcnt ();
218 pfile = 0;
221 return pfile;
224 TAO_Profile *
225 TAO_DIOP_Connector::make_profile ()
227 // The endpoint should be of the form:
228 // N.n@host:port/object_key
229 // or:
230 // host:port/object_key
232 TAO_Profile *profile = 0;
233 ACE_NEW_THROW_EX (profile,
234 TAO_DIOP_Profile (this->orb_core ()),
235 CORBA::NO_MEMORY (
236 CORBA::SystemException::_tao_minor_code (
237 TAO::VMCID,
238 ENOMEM),
239 CORBA::COMPLETED_NO));
241 return profile;
245 TAO_DIOP_Connector::check_prefix (const char *endpoint)
247 // Check for a valid string
248 if (!endpoint || !*endpoint)
249 return -1; // Failure
251 const char *protocol[] = { "diop", "dioploc" };
253 size_t const slot = std::strchr (endpoint, ':') - endpoint;
255 size_t const len0 = std::strlen (protocol[0]);
256 size_t const len1 = std::strlen (protocol[1]);
258 // Check for the proper prefix in the IOR. If the proper prefix
259 // isn't in the IOR then it is not an IOR we can use.
260 if (slot == len0
261 && ACE_OS::strncasecmp (endpoint, protocol[0], len0) == 0)
262 return 0;
263 else if (slot == len1
264 && ACE_OS::strncasecmp (endpoint, protocol[1], len1) == 0)
265 return 0;
267 return -1;
268 // Failure: not an DIOP IOR
269 // DO NOT throw an exception here.
272 char
273 TAO_DIOP_Connector::object_key_delimiter () const
275 return TAO_DIOP_Profile::object_key_delimiter_;
278 TAO_DIOP_Endpoint *
279 TAO_DIOP_Connector::remote_endpoint (TAO_Endpoint *endpoint)
281 if (endpoint->tag () != TAO_TAG_DIOP_PROFILE)
282 return 0;
284 return dynamic_cast<TAO_DIOP_Endpoint *> (endpoint);
288 TAO_DIOP_Connector::cancel_svc_handler (
289 TAO_Connection_Handler * /* svc_handler */)
291 return 0;
294 TAO_END_VERSIONED_NAMESPACE_DECL
296 #endif /* TAO_HAS_DIOP && TAO_HAS_DIOP != 0 */