Optimized_Connection_Endpoint_Selector.cpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 #include "tao/Strategies/Optimized_Connection_Endpoint_Selector.h"
00004 
00005 #include "tao/debug.h"
00006 #include "tao/Stub.h"
00007 #include "tao/Profile.h"
00008 #include "tao/Endpoint.h"
00009 #include "tao/Base_Transport_Property.h"
00010 #include "tao/ORB_Core.h"
00011 #include "tao/Transport.h"
00012 #include "tao/Profile_Transport_Resolver.h"
00013 #include "tao/SystemException.h"
00014 
00015 ACE_RCSID (tao,
00016            Optimized_Connection_Endpoint_Selector,
00017            "$Id: Optimized_Connection_Endpoint_Selector.cpp 76995 2007-02-11 12:51:42Z johnnyw $")
00018 
00019 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00020 
00021 // ****************************************************************
00022 
00023 ACE_Time_Value TAO_Optimized_Connection_Endpoint_Selector::timeout_;
00024 
00025 TAO_Optimized_Connection_Endpoint_Selector::
00026 TAO_Optimized_Connection_Endpoint_Selector (const ACE_Time_Value &tv)
00027 {
00028   TAO_Optimized_Connection_Endpoint_Selector::timeout_ = tv;
00029   if (TAO_debug_level)
00030     {
00031       ACE_DEBUG ((LM_DEBUG,
00032                   ACE_TEXT ("TAO(%P|%t) Optimized Connection Enpoint Selector: ")
00033                   ACE_TEXT ("Initializing timeout hook tv = %d sec, %d usec\n"),
00034               tv.sec(), tv.usec()));
00035     }
00036   if (tv > ACE_Time_Value::zero)
00037     {
00038       TAO_ORB_Core::connection_timeout_hook
00039         (TAO_Optimized_Connection_Endpoint_Selector::hook);
00040     }
00041 }
00042 
00043 TAO_Optimized_Connection_Endpoint_Selector::
00044 ~TAO_Optimized_Connection_Endpoint_Selector (void)
00045 {
00046 }
00047 
00048 
00049 void
00050 TAO_Optimized_Connection_Endpoint_Selector::hook (TAO_ORB_Core *,
00051                                                   TAO_Stub *,
00052                                                   bool &has_timeout,
00053                                                   ACE_Time_Value &tv)
00054 {
00055   has_timeout =
00056     TAO_Optimized_Connection_Endpoint_Selector::
00057     timeout_ > ACE_Time_Value::zero;
00058   if (has_timeout)
00059     tv = TAO_Optimized_Connection_Endpoint_Selector::timeout_;
00060 }
00061 
00062 int
00063 TAO_Optimized_Connection_Endpoint_Selector::check_profile (TAO_Profile *p,
00064                                                            TAO::Profile_Transport_Resolver *r)
00065 {
00066   TAO_Endpoint *effective_endpoint = 0;
00067 
00068   r->profile(p);
00069   effective_endpoint = p->endpoint ();
00070   size_t endpoint_count = p->endpoint_count();
00071   for (size_t i = 0; i < endpoint_count; ++i)
00072     {
00073       TAO_Base_Transport_Property desc (effective_endpoint);
00074       if (r->find_transport(&desc))
00075         return 1;
00076       // Go to the next endpoint in this profile
00077       effective_endpoint = effective_endpoint->next();
00078     }
00079   return 0;
00080 }
00081 
00082 void
00083 TAO_Optimized_Connection_Endpoint_Selector::select_endpoint
00084   (TAO::Profile_Transport_Resolver *r,
00085    ACE_Time_Value *max_wait_time)
00086 {
00087   TAO_Stub *stub = r->stub();
00088   TAO_Profile *p = stub->profile_in_use();
00089 
00090   // first, look for the endpoints for the current profile in use.
00091   // if that is available then go for it.
00092 
00093   if (this->check_profile (p, r) != 0)
00094     return;
00095 
00096   // next, look for any other profiles. If the stub has any forward profiles,
00097   // use those, otherwise look at the base profiles. This separation is
00098   // necessary to avoid re-using a corbaloc or other previously forwarded
00099   // profile.
00100 
00101   const TAO_MProfile *profiles = stub->forward_profiles();
00102   if (profiles != 0)
00103     {
00104       for (CORBA::ULong count = 0; count <  profiles->profile_count(); count++)
00105         {
00106           p = const_cast<TAO_Profile *>(profiles->get_profile(count));
00107           if (this->check_profile (p, r) != 0)
00108             {
00109               if (stub->profile_in_use() != p)
00110                 {
00111                   // thread-safe way to coerse stub to this profile.
00112                   stub->reset_profiles();
00113                   while (stub->profile_in_use() != p)
00114                     if (stub->next_profile_retry() == 0)
00115                       break;
00116                 }
00117               return;
00118             }
00119         }
00120     }
00121   else
00122     {
00123       do
00124         {
00125           p = stub->profile_in_use();
00126           if (this->check_profile(p, r) != 0)
00127             return;
00128         }
00129       while (stub->next_profile_retry () != 0);
00130     }
00131 
00132 
00133 
00134   // at this point, we do not have an existing transport, so we must
00135   // reset the profile list and try establishing connections via the
00136   // connector(s).
00137 
00138   do
00139     {
00140       r->profile (r->stub ()->profile_in_use ());
00141 
00142       // Check whether we need to do a blocked wait or we have a
00143       // non-blocked wait and we support that.  If this is not the
00144       // case we can't use this profile so try the next.
00145       if (r->blocked_connect () ||
00146          (!r->blocked_connect () && r->profile ()->supports_non_blocking_oneways ()))
00147         {
00148           const size_t endpoint_count = r->profile ()->endpoint_count ();
00149 
00150           TAO_Endpoint *ep = r->profile ()->endpoint ();
00151 
00152           for (size_t i = 0; i < endpoint_count; ++i)
00153             {
00154               TAO_Base_Transport_Property desc (ep);
00155               const bool retval = r->try_connect (&desc, max_wait_time);
00156 
00157               // Check if the connect has completed.
00158               if (retval)
00159                 return;
00160 
00161               // Go to the next endpoint in this profile.
00162               ep = ep->next ();
00163             }
00164         }
00165     }
00166   while (r->stub ()->next_profile_retry () != 0);
00167 
00168   // If we get here, we completely failed to find an endpoint selector
00169   // that we know how to use, so throw an exception.
00170   throw ::CORBA::TRANSIENT (CORBA::OMGVMCID | 2, CORBA::COMPLETED_NO);
00171 }
00172 
00173 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:47:18 2010 for TAO_Strategies by  doxygen 1.4.7