summaryrefslogtreecommitdiff
path: root/TAO/tao/Invocation_Endpoint_Selectors.cpp
blob: c5132e72b8ab3c968f4fbb63e725b7dd457d2ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// $Id$

#include "tao/Invocation_Endpoint_Selectors.h"

#if !defined (__ACE_INLINE__)
#include "tao/Invocation_Endpoint_Selectors.i"
#endif /* __ACE_INLINE__ */

#include "tao/Invocation.h"
#include "tao/Client_Priority_Policy.h"
#include "tao/Stub.h"
#include "tao/Profile.h"
#include "tao/Endpoint.h"
#include "tao/RT_Invocation_Endpoint_Selectors.h"

ACE_RCSID(tao, Invocation_Endpoint_Selectors, "$Id$")

// ****************************************************************

void
TAO_Endpoint_Selector_Factory::get_selector (TAO_GIOP_Invocation
                                             *invocation,
                                             CORBA::Environment &ACE_TRY_ENV)
{
#if (TAO_HAS_RT_CORBA == 1)
  // Obtains correct selector if TAO::Client_Priority_Policy is
  // enabled.
  this->check_client_priority_policy (invocation,
                                      ACE_TRY_ENV);
  ACE_CHECK;

  // CASE 1: TAO::Client_Priority_Policy is enabled.
  if (invocation->endpoint_selector_ != 0)
    return;

  // Initialize selection state with all RTCORBA policies affecting
  // endpoint selection.
  TAO_Endpoint_Selection_State &state =
    invocation->endpoint_selection_state_;

  state.priority_model_policy_ =
    invocation->stub_->exposed_priority_model ();
  state.private_connection_ =
    invocation->stub_->private_connection ();

  this->init_client_protocol (invocation, ACE_TRY_ENV);
  ACE_CHECK;
  this->init_bands (invocation, ACE_TRY_ENV);
  ACE_CHECK;

  //
  // Look at RTCORBA policies to decide on appropriate selector.
  //

  // CASE 2: No PriorityModelPolicy set.
  if (state.priority_model_policy_ == 0)
    {
     // Bands without priority model do not make sense.
      if (state.bands_policy_ != 0)
        {
          if (invocation->inconsistent_policies_.ptr ())
            {
              invocation->inconsistent_policies_->length (1);
              invocation->inconsistent_policies_[0u] =
                CORBA::Policy::_duplicate (state.bands_policy_);
            }
          ACE_THROW (CORBA::INV_POLICY ());
        }

      if (state.client_protocol_policy_ == 0)
        invocation->endpoint_selector_ =
          invocation->orb_core_->default_endpoint_selector ();
      else
        {
          invocation->endpoint_selector_ =
            invocation->orb_core_->protocol_endpoint_selector ();
        }
      return;
    }

  CORBA::Boolean is_client_propagated = 0;

  // @@ This is to be used in case 3:
  CORBA::Short server_priority = 0;

  invocation->orb_core_->get_protocols_hooks () ->get_selector_hook (
                                               state.priority_model_policy_,
                                               is_client_propagated,
                                               server_priority);

  if (is_client_propagated)
    {
      // Get client priority.
      if (invocation->orb_core_->get_protocols_hooks ()
          ->get_thread_priority (invocation->orb_core_,
                                 state.client_priority_,
                                 ACE_TRY_ENV) 
          == -1)
        ACE_THROW (CORBA::DATA_CONVERSION (1,
                                           CORBA::COMPLETED_NO));
    }

  // CASE 3: PriorityBandedConnection Policy is set.
  if (state.bands_policy_ != 0)
    {
      // Figure out target priority.
      CORBA::Short p;
      if (is_client_propagated)
        p = state.client_priority_;
      else
        p = server_priority;

      int in_range = 0;

      invocation->orb_core_->get_protocols_hooks
        ()->get_selector_bands_policy_hook (state.bands_policy_,
                                            state.min_priority_,
                                            state.max_priority_,
                                            p,
                                            in_range);

      // If priority doesn't fall into any of the bands
      if (!in_range)
        {
          if (invocation->inconsistent_policies_.ptr ())
            {
              invocation->inconsistent_policies_->length (2);
              invocation->inconsistent_policies_[0u] =
                CORBA::Policy::_duplicate (state.bands_policy_);
              invocation->inconsistent_policies_[1u] =
                CORBA::Policy::_duplicate (state.priority_model_policy_);
            }
          ACE_THROW (CORBA::INV_POLICY ());
        }

      // Matching band found.  Instantiate appropriate selector.
      if (state.client_protocol_policy_ == 0)
        invocation->endpoint_selector_ =
          invocation->orb_core_->bands_endpoint_selector ();
      else
          invocation->endpoint_selector_ =
            invocation->orb_core_->bands_protocol_selector ();

      return;
    }

  // CASE 4: CLIENT_PROPAGATED priority model, no bands.
  if (is_client_propagated)
    {
      if (state.client_protocol_policy_ == 0)
        invocation->endpoint_selector_ =
          invocation->orb_core_->priority_endpoint_selector ();
      else
        invocation->endpoint_selector_ =
          invocation->orb_core_->priority_protocol_selector ();
    }
  else
    {
      // CASE 5: SERVER_DECLARED priority model, no bands.
      if (state.client_protocol_policy_ == 0)
        invocation->endpoint_selector_ =
          invocation->orb_core_->default_endpoint_selector ();
      else
        invocation->endpoint_selector_ =
          invocation->orb_core_->protocol_endpoint_selector ();
    }
#else /* TAO_HAS_RT_CORBA == 1 */
  
  ACE_UNUSED_ARG (ACE_TRY_ENV);
  
  invocation->endpoint_selector_ =
    invocation->orb_core_->default_endpoint_selector ();

#endif /* TAO_HAS_RT_CORBA == 1 */
}

#if (TAO_HAS_RT_CORBA == 1)

void
TAO_Endpoint_Selector_Factory::
check_client_priority_policy (TAO_GIOP_Invocation *invocation,
                              CORBA::Environment &ACE_TRY_ENV)
{
#if (TAO_HAS_CLIENT_PRIORITY_POLICY == 1)

  TAO_Client_Priority_Policy *policy =
    invocation->stub_->client_priority ();

  // Automatically release the policy
  CORBA::Object_var auto_release = policy;

  if (policy == 0)
    invocation->endpoint_selector_ =
      invocation->orb_core_->default_endpoint_selector ();

  else
    {
      // Policy is set.
      TAO::PrioritySpecification priority_spec =
        policy->priority_specification (ACE_TRY_ENV);
      ACE_CHECK;

      TAO::PrioritySelectionMode mode = priority_spec.mode;

      if (mode == TAO::USE_NO_PRIORITY)
      // Don't care about priority - use default selector.
        invocation->endpoint_selector_ =
              invocation->orb_core_->default_endpoint_selector ();
      else
        // Care about priority - use client priority policy selector.
        {
          invocation->endpoint_selector_ =
            invocation->orb_core_->client_priority_policy_selector ();

          // Initialize endpoint selection state.
          if (mode == TAO::USE_PRIORITY_RANGE)
            {
              invocation->endpoint_selection_state_.min_priority_ =
                priority_spec.min_priority;
              invocation->endpoint_selection_state_.max_priority_ =
                priority_spec.max_priority;
            }
          else
            // mode == TAO::USE_THREAD_PRIORITY
            {
              CORBA::Short priority;
              if (invocation->orb_core_->get_protocols_hooks
                  ()->get_thread_priority (invocation->orb_core_,
                                           priority,
                                           ACE_TRY_ENV) 
                  == -1)
                ACE_THROW (CORBA::DATA_CONVERSION (1,
                                                   CORBA::COMPLETED_NO));

              invocation->endpoint_selection_state_.min_priority_ =
                priority;
              invocation->endpoint_selection_state_.max_priority_ =
                priority;
            }
        }
    }

#else

      ACE_UNUSED_ARG (invocation);
      ACE_UNUSED_ARG (ACE_TRY_ENV);

#endif /* TAO_HAS_CLIENT_PRIORITY_POLICY == 1 */
}

void
TAO_Endpoint_Selector_Factory::
init_client_protocol (TAO_GIOP_Invocation *invocation,
                      CORBA::Environment &ACE_TRY_ENV)
{
  ACE_TRY
    {
      invocation->endpoint_selection_state_.client_protocol_policy_ =
        invocation->stub_->effective_client_protocol (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCH (CORBA::INV_POLICY, ex)
    {
      if (invocation->inconsistent_policies_.ptr ())
        {
          invocation->inconsistent_policies_->length (1);
          invocation->inconsistent_policies_[0u] =
            invocation->stub_->client_protocol ();
        }
      ACE_RE_THROW;
    }
  ACE_ENDTRY;
  ACE_CHECK;
}

void
TAO_Endpoint_Selector_Factory::
init_bands (TAO_GIOP_Invocation *invocation,
            CORBA::Environment &ACE_TRY_ENV)
{
  ACE_TRY
    {
      invocation->endpoint_selection_state_.bands_policy_ =
        invocation->stub_->effective_priority_banded_connection (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCH (CORBA::INV_POLICY, ex)
    {
      if (invocation->inconsistent_policies_.ptr ())
        {
          invocation->inconsistent_policies_->length (1);
          invocation->inconsistent_policies_[0u] =
            invocation->stub_->priority_banded_connection ();
        }
      ACE_RE_THROW;
    }
  ACE_ENDTRY;
  ACE_CHECK;
}

TAO_Endpoint_Selection_State::~TAO_Endpoint_Selection_State (void)
{
  CORBA::release (this->priority_model_policy_);
  CORBA::release (this->client_protocol_policy_);
  CORBA::release (this->bands_policy_);
  CORBA::release (this->private_connection_);
}

#endif /* TAO_HAS_RT_CORBA == 1 */

// ****************************************************************

TAO_Invocation_Endpoint_Selector::~TAO_Invocation_Endpoint_Selector (void)
{
}

// ****************************************************************

TAO_Default_Endpoint_Selector::~TAO_Default_Endpoint_Selector (void)
{
}

void
TAO_Default_Endpoint_Selector::select_endpoint (TAO_GIOP_Invocation
                                                *invocation,
                                                CORBA::Environment &ACE_TRY_ENV)
{
  invocation->profile_ = invocation->stub_->profile_in_use ();
  invocation->endpoint_ = invocation->profile_->endpoint ();

  if (invocation->endpoint_ == 0)
    {
      // Unknown protocol - move onto the next profile.
      this->next (invocation, ACE_TRY_ENV);
      ACE_CHECK;
      this->select_endpoint (invocation, ACE_TRY_ENV);
    }
}

void
TAO_Default_Endpoint_Selector::next (TAO_GIOP_Invocation
                                     *invocation,
                                     CORBA::Environment &ACE_TRY_ENV)
{
  if (invocation->stub_->next_profile_retry () == 0)
    ACE_THROW (CORBA::TRANSIENT (
                                 CORBA_SystemException::_tao_minor_code (
                                      TAO_INVOCATION_CONNECT_MINOR_CODE,
                                      errno),
                                 CORBA::COMPLETED_NO));
}

void
TAO_Default_Endpoint_Selector::forward (TAO_GIOP_Invocation
                                        *invocation,
                                        const TAO_MProfile &mprofile,
                                        CORBA::Environment &ACE_TRY_ENV)
{
  invocation->stub_->add_forward_profiles (mprofile);
  // This has to be and is thread safe.
  // TAO_Stub::add_forward_profiles() already makes a copy of the
  // MProfile, so do not make a copy here.


  // We may not need to do this since TAO_GIOP_Invocations
  // get created on a per-call basis. For now we'll play it safe.
  if (invocation->stub_->next_profile () == 0)
    ACE_THROW (CORBA::TRANSIENT (
                                 CORBA_SystemException::_tao_minor_code (
                                       TAO_INVOCATION_LOCATION_FORWARD_MINOR_CODE,
                                       errno),
                                 CORBA::COMPLETED_NO));
}

void
TAO_Default_Endpoint_Selector::success (TAO_GIOP_Invocation *invocation)
{
  invocation->stub_->set_valid_profile ();
}

void
TAO_Default_Endpoint_Selector::close_connection (TAO_GIOP_Invocation *invocation)
{
  // Get rid of any forwarding profiles and reset
  // the profile list to point to the first profile!
  // FRED For now we will not deal with recursive forwards!
  invocation->stub_->reset_profiles ();
}