diff options
Diffstat (limited to 'TAO/tao/Strategies/SCIOP_Connector.cpp')
-rw-r--r-- | TAO/tao/Strategies/SCIOP_Connector.cpp | 192 |
1 files changed, 103 insertions, 89 deletions
diff --git a/TAO/tao/Strategies/SCIOP_Connector.cpp b/TAO/tao/Strategies/SCIOP_Connector.cpp index 2911873f0cd..e387d532ca2 100644 --- a/TAO/tao/Strategies/SCIOP_Connector.cpp +++ b/TAO/tao/Strategies/SCIOP_Connector.cpp @@ -14,7 +14,6 @@ #include "tao/Thread_Lane_Resources.h" #include "tao/Transport.h" #include "tao/Wait_Strategy.h" -#include "tao/Profile_Transport_Resolver.h" #include "ace/OS_NS_strings.h" #include "ace/Strategies_T.h" @@ -153,9 +152,9 @@ TAO_SCIOP_Connector::set_validate_endpoint (TAO_Endpoint *endpoint) } TAO_Transport * -TAO_SCIOP_Connector::make_connection (TAO::Profile_Transport_Resolver *r, +TAO_SCIOP_Connector::make_connection (TAO::Profile_Transport_Resolver *, TAO_Transport_Descriptor_Interface &desc, - ACE_Time_Value *timeout) + ACE_Time_Value *max_wait_time) { TAO_Endpoint *tao_endpoint = desc.endpoint (); @@ -164,7 +163,7 @@ TAO_SCIOP_Connector::make_connection (TAO::Profile_Transport_Resolver *r, while (tao_endpoint != 0) { TAO_SCIOP_Endpoint *sciop_endpoint = this->remote_endpoint (tao_endpoint); if (sciop_endpoint != 0) { - transport = make_connection_i (r, desc, timeout, sciop_endpoint); + transport = make_connection_i (desc, max_wait_time, sciop_endpoint); if (transport) { break; } @@ -177,37 +176,26 @@ TAO_SCIOP_Connector::make_connection (TAO::Profile_Transport_Resolver *r, TAO_Transport * -TAO_SCIOP_Connector::make_connection_i (TAO::Profile_Transport_Resolver *r, - TAO_Transport_Descriptor_Interface &desc, - ACE_Time_Value *timeout, +TAO_SCIOP_Connector::make_connection_i (TAO_Transport_Descriptor_Interface &desc, + ACE_Time_Value *max_wait_time, TAO_SCIOP_Endpoint *sciop_endpoint) { const ACE_INET_Addr &remote_address = sciop_endpoint->object_addr (); - if (TAO_debug_level > 2) - ACE_DEBUG ((LM_DEBUG, - "TAO (%P|%t) - SCIOP_Connector::make_connection_i, " - "to <%s:%d> which should %s\n", - ACE_TEXT_CHAR_TO_TCHAR(sciop_endpoint->host()), - sciop_endpoint->port(), - r->blocked () ? ACE_TEXT("block") : ACE_TEXT("nonblock"))); + if (TAO_debug_level > 2) { + ACE_DEBUG ((LM_DEBUG, + "TAO (%P|%t) - SCIOP_Connector::make_connection_i, " + "to <%s:%d>\n", + sciop_endpoint->host(), sciop_endpoint->port())); + } // Get the right synch options ACE_Synch_Options synch_options; - this->active_connect_strategy_->synch_options (timeout, + this->active_connect_strategy_->synch_options (max_wait_time, synch_options); - // If we don't need to block for a transport just set the timeout to - // be zero. - ACE_Time_Value tmp_zero (ACE_Time_Value::zero); - if (!r->blocked ()) - { - synch_options.timeout (ACE_Time_Value::zero); - timeout = &tmp_zero; - } - TAO_SCIOP_Connection_Handler *svc_handler = 0; // Connect. @@ -237,45 +225,93 @@ TAO_SCIOP_Connector::make_connection_i (TAO::Profile_Transport_Resolver *r, // another thread pick up the completion and potentially deletes the // handler before we get a chance to increment the reference count. - // Make sure that we always do a remove_reference - ACE_Event_Handler_var svc_handler_auto_ptr (svc_handler); + // No immediate result. Wait for completion. + if (result == -1 && errno == EWOULDBLOCK) + { + if (TAO_debug_level > 2) + ACE_DEBUG ((LM_DEBUG, + "TAO (%P|%t) - SCIOP_Connector::make_connection, " + "going to wait for connection completion on local" + "handle [%d]\n", + svc_handler->get_handle ())); + + // Wait for connection completion. + result = + this->active_connect_strategy_->wait (svc_handler, + max_wait_time); + + if (TAO_debug_level > 2) + { + ACE_DEBUG ((LM_DEBUG, + "TAO (%P|%t) - SCIOP_Connector::make_connection" + "wait done for handle[%d], result = %d\n", + svc_handler->get_handle (), result)); + } - TAO_Transport *transport = - svc_handler->transport (); + // There are three possibilities when wait() returns: (a) + // connection succeeded; (b) connection failed; (c) wait() + // failed because of some other error. It is easy to deal with + // (a) and (b). (c) is tricky since the connection is still + // pending and may get completed by some other thread. The + // following code deals with (c). - if (result == -1) - { - // No immediate result, wait for completion - if (errno == EWOULDBLOCK) + // Check if the handler has been closed. + int closed = + svc_handler->is_closed (); + + // In case of failures and close() has not be called. + if (result == -1 && + !closed) { - // Try to wait until connection completion. Incase we block, then we - // get a connected transport or not. In case of non block we get - // a connected or not connected transport - if (!this->wait_for_connection_completion (r, - transport, - timeout)) + // First, cancel from connector. + this->base_connector_.cancel (svc_handler); + + // Double check to make sure the handler has not been closed + // yet. This double check is required to ensure that the + // connection handler was not closed yet by some other + // thread since it was still registered with the connector. + // Once connector.cancel() has been processed, we are + // assured that the connector will no longer open/close this + // handler. + closed = + svc_handler->is_closed (); + + // If closed, there is nothing to do here. If not closed, + // it was either opened or is still pending. + if (!closed) { - if (TAO_debug_level > 2) - ACE_ERROR ((LM_ERROR, "TAO (%P|%t) - SCIOP_Connector::" - "make_connection_i, " - "wait for completion failed\n")); + // Check if the handler has been opened. + int open = + svc_handler->is_open (); + + // Some other thread was able to open the handler even + // though wait failed for this thread. + if (open) + // Overwrite <result>. + result = 0; + else + { + // Assert that it is still connecting. + ACE_ASSERT (svc_handler->is_connecting ()); + + // Force close the handler now. + svc_handler->close (); + } } - } - else - { - // Transport is not usable - transport = 0; - } + } } - // In case of errors transport is zero - if (transport == 0) + // Irrespective of success or failure, remove the extra #REFCOUNT#. + svc_handler->remove_reference (); + + // In case of errors. + if (result == -1) { // Give users a clue to the problem. if (TAO_debug_level) { ACE_DEBUG ((LM_ERROR, - "TAO (%P|%t) - SCIOP_Connector::make_connection_i, " + "TAO (%P|%t) - SCIOP_Connector::make_connection, " "connection to <%s:%d> failed (%p)\n", sciop_endpoint->host (), sciop_endpoint->port (), "errno")); @@ -288,12 +324,14 @@ TAO_SCIOP_Connector::make_connection_i (TAO::Profile_Transport_Resolver *r, // #REFCOUNT# is one. if (TAO_debug_level > 2) ACE_DEBUG ((LM_DEBUG, - "TAO (%P|%t) - SCIOP_Connector::make_connection_i, " - "new %s connection to <%s:%d> on Transport[%d]\n", - transport->is_connected() ? "connected" : "not connected", + "TAO (%P|%t) - SCIOP_Connector::make_connection, " + "new connection to <%s:%d> on Transport[%d]\n", sciop_endpoint->host (), sciop_endpoint->port (), svc_handler->peer ().get_handle ())); + TAO_Transport *transport = + svc_handler->transport (); + // Add the handler to Cache int retval = this->orb_core ()->lane_resources ().transport_cache ().cache_transport (&desc, @@ -308,31 +346,28 @@ TAO_SCIOP_Connector::make_connection_i (TAO::Profile_Transport_Resolver *r, if (TAO_debug_level > 0) { ACE_ERROR ((LM_ERROR, - "TAO (%P|%t) - SCIOP_Connector::make_connection_i, " + "TAO (%P|%t) - SCIOP_Connector::make_connection, " "could not add the new connection to cache\n")); } return 0; } - if (transport->is_connected () && - transport->wait_strategy ()->register_handler () != 0) + // Registration failures. + if (retval != 0) { - // Registration failures. - - // Purge from the connection cache, if we are not in the cache, this - // just does nothing. - (void) transport->purge_entry (); + // Purge from the connection cache. + transport->purge_entry (); // Close the handler. - (void) transport->close_connection (); + svc_handler->close (); if (TAO_debug_level > 0) - ACE_ERROR ((LM_ERROR, - "TAO (%P|%t) - SCIOP_Connector [%d]::make_connection_i, " - "could not register the transport " - "in the reactor.\n", - transport->id ())); + { + ACE_ERROR ((LM_ERROR, + "TAO (%P|%t) - SCIOP_Connector::make_connection, " + "could not register the new connection in the reactor\n")); + } return 0; } @@ -483,25 +518,4 @@ TAO_SCIOP_Connector::remote_endpoint (TAO_Endpoint *endpoint) return sciop_endpoint; } -int -TAO_SCIOP_Connector::cancel_svc_handler ( - TAO_Connection_Handler * svc_handler) -{ - TAO_SCIOP_Connection_Handler* handler= - dynamic_cast<TAO_SCIOP_Connection_Handler*>(svc_handler); - - if (handler) - { - // Cancel from the connector - this->base_connector_.cancel (handler); - - return 0; - } - else - { - return -1; - } -} - - #endif /* TAO_HAS_SCIOP == 1 */ |