Merge pull request #2218 from jwillemsen/jwi-pthreadsigmask
[ACE_TAO.git] / TAO / tao / Strategies / Optimized_Connection_Endpoint_Selector.cpp
blobe90d111ee9734ab516c957512c3be39fbbeedec9
1 // -*- C++ -*-
2 #include "tao/Strategies/Optimized_Connection_Endpoint_Selector.h"
4 #include "tao/debug.h"
5 #include "tao/Stub.h"
6 #include "tao/Profile.h"
7 #include "tao/Endpoint.h"
8 #include "tao/Base_Transport_Property.h"
9 #include "tao/ORB_Core.h"
10 #include "tao/Transport.h"
11 #include "tao/Profile_Transport_Resolver.h"
12 #include "tao/SystemException.h"
14 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
16 // ****************************************************************
18 ACE_Time_Value TAO_Optimized_Connection_Endpoint_Selector::timeout_;
20 TAO_Optimized_Connection_Endpoint_Selector::
21 TAO_Optimized_Connection_Endpoint_Selector (const ACE_Time_Value &tv)
23 TAO_Optimized_Connection_Endpoint_Selector::timeout_ = tv;
24 if (TAO_debug_level)
26 TAOLIB_DEBUG ((LM_DEBUG,
27 ACE_TEXT ("TAO(%P|%t) Optimized Connection Enpoint Selector: ")
28 ACE_TEXT ("Initializing timeout hook tv = %d sec, %d usec\n"),
29 tv.sec(), tv.usec()));
31 if (tv > ACE_Time_Value::zero)
33 TAO_ORB_Core::connection_timeout_hook
34 (TAO_Optimized_Connection_Endpoint_Selector::hook);
38 TAO_Optimized_Connection_Endpoint_Selector::
39 ~TAO_Optimized_Connection_Endpoint_Selector ()
44 void
45 TAO_Optimized_Connection_Endpoint_Selector::hook (TAO_ORB_Core *,
46 TAO_Stub *,
47 bool &has_timeout,
48 ACE_Time_Value &tv)
50 has_timeout =
51 TAO_Optimized_Connection_Endpoint_Selector::
52 timeout_ > ACE_Time_Value::zero;
53 if (has_timeout)
54 tv = TAO_Optimized_Connection_Endpoint_Selector::timeout_;
57 int
58 TAO_Optimized_Connection_Endpoint_Selector::check_profile (TAO_Profile *p,
59 TAO::Profile_Transport_Resolver *r)
61 TAO_Endpoint *effective_endpoint = 0;
63 r->profile(p);
64 effective_endpoint = p->endpoint ();
65 size_t endpoint_count = p->endpoint_count();
66 for (size_t i = 0; i < endpoint_count; ++i)
68 TAO_Base_Transport_Property desc (effective_endpoint);
69 if (r->find_transport(&desc))
70 return 1;
71 // Go to the next endpoint in this profile
72 effective_endpoint = effective_endpoint->next();
74 return 0;
77 void
78 TAO_Optimized_Connection_Endpoint_Selector::select_endpoint
79 (TAO::Profile_Transport_Resolver *r,
80 ACE_Time_Value *max_wait_time)
82 TAO_Stub *stub = r->stub();
83 TAO_Profile *p = stub->profile_in_use();
85 // first, look for the endpoints for the current profile in use.
86 // if that is available then go for it.
88 if (this->check_profile (p, r) != 0)
89 return;
91 // next, look for any other profiles. If the stub has any forward profiles,
92 // use those, otherwise look at the base profiles. This separation is
93 // necessary to avoid re-using a corbaloc or other previously forwarded
94 // profile.
96 const TAO_MProfile *profiles = stub->forward_profiles();
97 if (profiles != 0)
99 for (CORBA::ULong count = 0; count < profiles->profile_count(); count++)
101 p = const_cast<TAO_Profile *>(profiles->get_profile(count));
102 if (this->check_profile (p, r) != 0)
104 if (stub->profile_in_use() != p)
106 // thread-safe way to coerse stub to this profile.
107 stub->reset_profiles();
108 while (stub->profile_in_use() != p)
109 if (stub->next_profile_retry() == 0)
110 break;
112 return;
116 else
120 p = stub->profile_in_use();
121 if (this->check_profile(p, r) != 0)
122 return;
124 while (stub->next_profile_retry () != 0);
128 // at this point, we do not have an existing transport, so we must
129 // reset the profile list and try establishing connections via the
130 // connector(s).
134 r->profile (r->stub ()->profile_in_use ());
136 // Check whether we need to do a blocked wait or we have a
137 // non-blocked wait and we support that. If this is not the
138 // case we can't use this profile so try the next.
139 if (r->blocked_connect () ||
140 (!r->blocked_connect () && r->profile ()->supports_non_blocking_oneways ()))
142 const size_t endpoint_count = r->profile ()->endpoint_count ();
144 TAO_Endpoint *ep = r->profile ()->endpoint ();
146 for (size_t i = 0; i < endpoint_count; ++i)
148 TAO_Base_Transport_Property desc (ep);
149 const bool retval = r->try_connect (&desc, max_wait_time);
151 // Check if the connect has completed.
152 if (retval)
153 return;
155 // Go to the next endpoint in this profile.
156 ep = ep->next ();
160 while (r->stub ()->next_profile_retry () != 0);
162 // If we get here, we completely failed to find an endpoint
163 // that we know how to use. We used to throw an exception
164 // but that would prevent any request interception points
165 // being called. They may know how to fix the problem so
166 // we wait to throw the exception in
167 // Synch_Twoway_Invocation::remote_twoway and
168 // Synch_Oneway_Invocation::remote_oneway instead.
171 TAO_END_VERSIONED_NAMESPACE_DECL