00001
00002
00003 #include "tao/Strategies/DIOP_Connection_Handler.h"
00004
00005 #if defined (TAO_HAS_DIOP) && (TAO_HAS_DIOP != 0)
00006
00007 #include "tao/Timeprobe.h"
00008 #include "tao/debug.h"
00009 #include "tao/ORB_Core.h"
00010 #include "tao/ORB.h"
00011 #include "tao/CDR.h"
00012 #include "tao/Server_Strategy_Factory.h"
00013 #include "tao/Transport_Cache_Manager.h"
00014 #include "tao/Thread_Lane_Resources.h"
00015 #include "tao/Base_Transport_Property.h"
00016 #include "tao/Protocols_Hooks.h"
00017 #include "tao/Resume_Handle.h"
00018
00019 #include "tao/Strategies/DIOP_Transport.h"
00020 #include "tao/Strategies/DIOP_Endpoint.h"
00021
00022 #include "ace/os_include/netinet/os_tcp.h"
00023 #include "ace/os_include/os_netdb.h"
00024
00025 ACE_RCSID(tao, DIOP_Connect, "$Id: DIOP_Connection_Handler.cpp 85817 2009-06-26 20:44:58Z mitza $")
00026
00027
00028 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00029
00030 TAO_DIOP_Connection_Handler::TAO_DIOP_Connection_Handler (ACE_Thread_Manager *t)
00031 : TAO_DIOP_SVC_HANDLER (t, 0 , 0),
00032 TAO_Connection_Handler (0),
00033 dscp_codepoint_ (IPDSFIELD_DSCP_DEFAULT << 2)
00034 {
00035
00036
00037
00038
00039
00040 ACE_ASSERT (0);
00041 }
00042
00043 TAO_DIOP_Connection_Handler::TAO_DIOP_Connection_Handler (TAO_ORB_Core *orb_core)
00044 : TAO_DIOP_SVC_HANDLER (orb_core->thr_mgr (), 0, 0),
00045 TAO_Connection_Handler (orb_core),
00046 dscp_codepoint_ (IPDSFIELD_DSCP_DEFAULT << 2)
00047 {
00048 TAO_DIOP_Transport* specific_transport = 0;
00049 ACE_NEW (specific_transport,
00050 TAO_DIOP_Transport (this, orb_core));
00051
00052
00053 this->transport (specific_transport);
00054 }
00055
00056 TAO_DIOP_Connection_Handler::~TAO_DIOP_Connection_Handler (void)
00057 {
00058 delete this->transport ();
00059 int const result =
00060 this->release_os_resources ();
00061
00062 if (result == -1 && TAO_debug_level)
00063 {
00064 ACE_ERROR ((LM_ERROR,
00065 ACE_TEXT ("TAO (%P|%t) - DIOP_Connection_Handler::")
00066 ACE_TEXT ("~DIOP_Connection_Handler, ")
00067 ACE_TEXT ("release_os_resources() failed %m\n")));
00068 }
00069 }
00070
00071
00072 const ACE_INET_Addr &
00073 TAO_DIOP_Connection_Handler::addr (void)
00074 {
00075 return this->addr_;
00076 }
00077
00078 void
00079 TAO_DIOP_Connection_Handler::addr (const ACE_INET_Addr &addr)
00080 {
00081 this->addr_ = addr;
00082 }
00083
00084 const ACE_INET_Addr &
00085 TAO_DIOP_Connection_Handler::local_addr (void)
00086 {
00087 return this->local_addr_;
00088 }
00089
00090 void
00091 TAO_DIOP_Connection_Handler::local_addr (const ACE_INET_Addr &addr)
00092 {
00093 this->local_addr_ = addr;
00094 }
00095
00096
00097 int
00098 TAO_DIOP_Connection_Handler::open_handler (void *v)
00099 {
00100 return this->open (v);
00101 }
00102
00103 int
00104 TAO_DIOP_Connection_Handler::open (void*)
00105 {
00106 TAO_DIOP_Protocol_Properties protocol_properties;
00107
00108
00109 protocol_properties.send_buffer_size_ =
00110 this->orb_core ()->orb_params ()->sock_sndbuf_size ();
00111 protocol_properties.recv_buffer_size_ =
00112 this->orb_core ()->orb_params ()->sock_rcvbuf_size ();
00113 protocol_properties.hop_limit_ =
00114 this->orb_core ()->orb_params ()->ip_hoplimit ();
00115
00116 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
00117
00118 if (tph != 0)
00119 {
00120 try
00121 {
00122 if (this->transport ()->opened_as () == TAO::TAO_CLIENT_ROLE)
00123 {
00124 tph->client_protocol_properties_at_orb_level (protocol_properties);
00125 }
00126 else
00127 {
00128 tph->server_protocol_properties_at_orb_level (protocol_properties);
00129 }
00130 }
00131 catch (const ::CORBA::Exception&)
00132 {
00133 return -1;
00134 }
00135 }
00136
00137 this->peer ().open (this->local_addr_);
00138
00139 if (this->set_socket_option (this->peer (),
00140 protocol_properties.send_buffer_size_,
00141 protocol_properties.recv_buffer_size_) == -1)
00142 return -1;
00143
00144 if (protocol_properties.hop_limit_ >= 0)
00145 {
00146 int result = 0;
00147 #if defined (ACE_HAS_IPV6)
00148 if (this->local_addr_.get_type () == AF_INET6)
00149 {
00150 #if defined (ACE_WIN32)
00151 DWORD hop_limit =
00152 static_cast<DWORD> (protocol_properties.hop_limit_);
00153 #else
00154 int hop_limit =
00155 static_cast<int> (protocol_properties.hop_limit_);
00156 #endif
00157 result = this->peer ().set_option (
00158 IPPROTO_IPV6,
00159 IPV6_UNICAST_HOPS,
00160 (void *) &hop_limit,
00161 sizeof (hop_limit));
00162 }
00163 else
00164 #endif
00165 {
00166 #if defined (ACE_WIN32)
00167 DWORD hop_limit =
00168 static_cast<DWORD> (protocol_properties.hop_limit_);
00169 #else
00170 int hop_limit =
00171 static_cast<int> (protocol_properties.hop_limit_);
00172 #endif
00173 result = this->peer ().set_option (
00174 IPPROTO_IP,
00175 IP_TTL,
00176 (void *) &hop_limit,
00177 sizeof (hop_limit));
00178 }
00179
00180 if (result != 0)
00181 {
00182 if (TAO_debug_level)
00183 {
00184 ACE_ERROR ((LM_ERROR,
00185 ACE_TEXT("TAO (%P|%t) - DIOP_Connection_Handler::open, ")
00186 ACE_TEXT("couldn't set hop limit\n\n")));
00187 }
00188 return -1;
00189 }
00190 }
00191
00192 if (TAO_debug_level > 5)
00193 {
00194 ACE_DEBUG ((LM_DEBUG,
00195 ACE_TEXT("TAO (%P|%t) - DIOP_Connection_Handler::open, ")
00196 ACE_TEXT("listening on: <%C:%u>\n"),
00197 this->local_addr_.get_host_name (),
00198 this->local_addr_.get_port_number ()));
00199 }
00200
00201
00202
00203
00204 if (!this->transport ()->post_open ((size_t) this->peer ().get_handle ()))
00205 return -1;
00206
00207 this->state_changed (TAO_LF_Event::LFS_SUCCESS,
00208 this->orb_core ()->leader_follower ());
00209
00210 return 0;
00211 }
00212
00213 int
00214 TAO_DIOP_Connection_Handler::open_server (void)
00215 {
00216 TAO_DIOP_Protocol_Properties protocol_properties;
00217
00218
00219 protocol_properties.send_buffer_size_ =
00220 this->orb_core ()->orb_params ()->sock_sndbuf_size ();
00221 protocol_properties.recv_buffer_size_ =
00222 this->orb_core ()->orb_params ()->sock_rcvbuf_size ();
00223
00224 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
00225
00226 if (tph != 0)
00227 {
00228 try
00229 {
00230 if (this->transport ()->opened_as () == TAO::TAO_CLIENT_ROLE)
00231 {
00232 tph->client_protocol_properties_at_orb_level (protocol_properties);
00233 }
00234 else
00235 {
00236 tph->server_protocol_properties_at_orb_level (protocol_properties);
00237 }
00238 }
00239 catch (const ::CORBA::Exception&)
00240 {
00241 return -1;
00242 }
00243 }
00244
00245 this->peer ().open (this->local_addr_);
00246
00247 if (this->set_socket_option (this->peer (),
00248 protocol_properties.send_buffer_size_,
00249 protocol_properties.recv_buffer_size_) == -1)
00250 return -1;
00251
00252 if (TAO_debug_level > 5)
00253 {
00254 ACE_DEBUG ((LM_DEBUG,
00255 ACE_TEXT("TAO (%P|%t) - DIOP_Connection_Handler::open_server, ")
00256 ACE_TEXT("listening on %C:%d\n"),
00257 this->local_addr_.get_host_name (),
00258 this->local_addr_.get_port_number ()
00259 ));
00260 }
00261
00262 this->transport ()->id ((size_t) this->peer ().get_handle ());
00263
00264 return 0;
00265 }
00266
00267 int
00268 TAO_DIOP_Connection_Handler::resume_handler (void)
00269 {
00270 return ACE_Event_Handler::ACE_APPLICATION_RESUMES_HANDLER;
00271 }
00272
00273 int
00274 TAO_DIOP_Connection_Handler::close_connection (void)
00275 {
00276 return this->close_connection_eh (this);
00277 }
00278
00279 int
00280 TAO_DIOP_Connection_Handler::handle_input (ACE_HANDLE h)
00281 {
00282 return this->handle_input_eh (h, this);
00283 }
00284
00285 int
00286 TAO_DIOP_Connection_Handler::handle_output (ACE_HANDLE handle)
00287 {
00288 int const result =
00289 this->handle_output_eh (handle, this);
00290
00291 if (result == -1)
00292 {
00293 this->close_connection ();
00294 return 0;
00295 }
00296
00297 return result;
00298 }
00299
00300 int
00301 TAO_DIOP_Connection_Handler::handle_timeout (const ACE_Time_Value &,
00302 const void *)
00303 {
00304
00305
00306
00307 return 0;
00308 }
00309
00310 int
00311 TAO_DIOP_Connection_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
00312 {
00313
00314
00315
00316
00317
00318 return 0;
00319 }
00320
00321 int
00322 TAO_DIOP_Connection_Handler::close (u_long flags)
00323 {
00324 return this->close_handler (flags);
00325 }
00326
00327 int
00328 TAO_DIOP_Connection_Handler::release_os_resources (void)
00329 {
00330 return this->peer ().close ();
00331 }
00332
00333 int
00334 TAO_DIOP_Connection_Handler::add_transport_to_cache (void)
00335 {
00336 ACE_INET_Addr addr;
00337
00338
00339
00340
00341
00342
00343
00344 TAO_DIOP_Endpoint endpoint (
00345 addr,
00346 this->orb_core ()->orb_params ()->cache_incoming_by_dotted_decimal_address ());
00347
00348
00349 TAO_Base_Transport_Property prop (&endpoint);
00350
00351
00352 return this->orb_core ()->lane_resources ()
00353 .transport_cache ().cache_transport (&prop, this->transport ());
00354 }
00355
00356 int
00357 TAO_DIOP_Connection_Handler::set_tos (int tos)
00358 {
00359 if (tos != this->dscp_codepoint_)
00360 {
00361 int result = 0;
00362 #if defined (ACE_HAS_IPV6)
00363 ACE_INET_Addr local_addr;
00364 if (this->peer ().get_local_addr (local_addr) == -1)
00365 return -1;
00366 else if (local_addr.get_type () == AF_INET6)
00367 # if !defined (IPV6_TCLASS)
00368
00369
00370 {
00371 if (TAO_debug_level)
00372 {
00373 ACE_DEBUG ((LM_DEBUG,
00374 "TAO (%P|%t) - DIOP_Connection_Handler::"
00375 "set_dscp_codepoint -> IPV6_TCLASS not supported yet\n"));
00376 }
00377 return 0;
00378 }
00379 # else
00380 result = this->peer ().set_option (IPPROTO_IPV6,
00381 IPV6_TCLASS,
00382 (int *) &tos ,
00383 (int) sizeof (tos));
00384 else
00385 # endif
00386 #endif
00387 result = this->peer ().set_option (IPPROTO_IP,
00388 IP_TOS,
00389 (int *) &tos ,
00390 (int) sizeof (tos));
00391
00392 if (TAO_debug_level)
00393 {
00394 ACE_DEBUG ((LM_DEBUG,
00395 "TAO (%P|%t) - DIOP_Connection_Handler::"
00396 "set_dscp_codepoint, dscp: %x; result: %d; %C\n",
00397 tos,
00398 result,
00399 result == -1 ? "try running as superuser" : ""));
00400 }
00401
00402
00403 if (result == 0)
00404 this->dscp_codepoint_ = tos;
00405 }
00406 return 0;
00407 }
00408
00409 int
00410 TAO_DIOP_Connection_Handler::set_dscp_codepoint (CORBA::Long dscp)
00411 {
00412 int tos = IPDSFIELD_DSCP_DEFAULT << 2;
00413 tos = (int)(dscp) << 2;
00414 this->set_tos (tos);
00415 return 0;
00416 }
00417
00418 int
00419 TAO_DIOP_Connection_Handler::set_dscp_codepoint (CORBA::Boolean set_network_priority)
00420 {
00421 int tos = IPDSFIELD_DSCP_DEFAULT << 2;
00422
00423 if (set_network_priority)
00424 {
00425 TAO_Protocols_Hooks *tph = this->orb_core ()->get_protocols_hooks ();
00426
00427 if (tph != 0)
00428 {
00429 CORBA::Long codepoint = tph->get_dscp_codepoint ();
00430
00431 tos = (int)(codepoint) << 2;
00432 this->set_tos (tos);
00433 }
00434 }
00435 return 0;
00436 }
00437
00438 int
00439 TAO_DIOP_Connection_Handler::handle_write_ready (const ACE_Time_Value *t)
00440 {
00441 return ACE::handle_write_ready (this->peer ().get_handle (), t);
00442 }
00443
00444 TAO_END_VERSIONED_NAMESPACE_DECL
00445
00446 #endif