summaryrefslogtreecommitdiff
path: root/TAO/tao/RTCORBA/RT_Invocation_Endpoint_Selectors.cpp
blob: 70070e94104451b33c9f97e7cc3b0983816a0f06 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// $Id$

#include "RT_Invocation_Endpoint_Selectors.h"

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

#include "tao/Invocation.h"
#include "RT_Policy_i.h"
#include "tao/Stub.h"
#include "tao/Profile.h"
#include "tao/Endpoint.h"
#include "RT_Stub.h"
#include "Private_Transport_Descriptor.h"
#include "tao/Base_Transport_Property.h"
#include "RT_Endpoint_Utils.h"
#include "RT_Protocols_Hooks.h"

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

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

TAO_Priority_Endpoint_Selector::~TAO_Priority_Endpoint_Selector (void)
{
}

void
TAO_Priority_Endpoint_Selector::select_endpoint (TAO_GIOP_Invocation *invocation,
                                                 CORBA::Environment &ACE_TRY_ENV)
{
  TAO_RT_Stub *rt_stub = ACE_dynamic_cast (TAO_RT_Stub *,
                                           invocation->stub ());
  CORBA::Short client_priority;

  // Get client priority.
  TAO_Protocols_Hooks *tph = invocation->orb_core ()->get_protocols_hooks (ACE_TRY_ENV);
  ACE_CHECK;

  if (tph->get_thread_CORBA_priority (client_priority,
                                      ACE_TRY_ENV)
      == -1)
    ACE_THROW (CORBA::DATA_CONVERSION (1,
               CORBA::COMPLETED_NO));

  do
    {
      // Obtain profile.
      invocation->profile (invocation->stub ()->profile_in_use ());

      // Select an endpoint from the profile.

      if (invocation->profile ()->endpoint_count () == 0)
        {
          // Unknown protocol - move onto the next profile.
          continue;
        }

      else if (invocation->profile ()->endpoint_count () == 1)
        {
          // Profile contains just one endpoint.  This happens when:
          //    a) we are talking to a nonTAO server (which doesn't have
          //       the concept of multiple endpoints per profile)
          //    or
          //    b) we have TAO server with a non-lane threadpool, in which
          //    case there is only one acceptor
          // In both cases we should use the endpoint regardless of its priority.
          invocation->endpoint (invocation->profile ()->endpoint ());
        }
      else
        {
          // Profiles contains more than one endpoint.  Find one with the
          // right priority.

          TAO_Endpoint *endpoint = 0;
          for (TAO_Endpoint *endp = invocation->profile ()->endpoint ();
               endp != 0;
               endp = endp->next ())
            {
              if (endp->priority () == client_priority)
                {
                  endpoint = endp;
                  break;
                }
            }

          if (endpoint != 0)
            {
              // Found an Endpoint with correct priority.
              invocation->endpoint (endpoint);
            }
          else
            {
              // The profile didn't contain an endpoint with matching priority.
              // There are two possibilities:
              //   a) the server is a multi-homed host and this is the
              //      only reason for profile containing more than 1
              //      endpoint, i.e., case a) above.
              //   b) we have misconfiguration - threadpool with lanes on
              //      the server, and client priority not matching any of
              //      the lanes.
              if (this->is_multihomed (invocation->profile ()->endpoint ()))
                {
                  // case a
                  invocation->endpoint (invocation->profile ()->endpoint ());
                }
              else
                // case b
                {
                  if (invocation->inconsistent_policies ().ptr ())
                    {
                      CORBA::Policy_var priority_model_policy = rt_stub->exposed_priority_model (ACE_TRY_ENV);
                      ACE_CHECK;

                      invocation->inconsistent_policies ()->length (1);
                      invocation->inconsistent_policies ()[0u] =
                        CORBA::Policy::_duplicate (priority_model_policy.in ());
                    }
                  ACE_THROW (CORBA::INV_POLICY ());
                }
            }
        }

      // Try to perform the invocation using this endpoint.
      int status;
      if (rt_stub->private_connection ())
        {
          TAO_Private_Transport_Descriptor private_desc (invocation->endpoint (),
                                                         ACE_reinterpret_cast (long, invocation->stub ()));

          status = invocation->perform_call (private_desc, ACE_TRY_ENV);
          ACE_CHECK;
        }
      else
        {
          TAO_Base_Transport_Property default_desc (invocation->endpoint ());

          status = invocation->perform_call (default_desc, ACE_TRY_ENV);
          ACE_CHECK;
        }

      // Check if the invocation has completed.
      if (status == 1)
        return;
    }
  while (invocation->stub ()->next_profile_retry () != 0);

  // If we get here, we completely failed to find an endpoint selector
  // that we know how to use, so throw an exception.
  ACE_THROW (CORBA::TRANSIENT (TAO_OMG_VMCID | 2,
                               CORBA::COMPLETED_NO));

}

int
TAO_Priority_Endpoint_Selector::is_multihomed (TAO_Endpoint *endpoint)
{
  for (TAO_Endpoint *endp = endpoint;
       endp != 0;
       endp = endp->next ())
    {
      if (endp->next () != 0
          && endp->priority () != endp->next ()->priority ())
        return 0;
    }
  return 1;
}

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

TAO_Bands_Endpoint_Selector::~TAO_Bands_Endpoint_Selector (void)
{
}

void
TAO_Bands_Endpoint_Selector::select_endpoint (TAO_GIOP_Invocation *invocation,
                                              CORBA::Environment &ACE_TRY_ENV)
{
  TAO_RT_Stub *rt_stub = ACE_dynamic_cast (TAO_RT_Stub *,
                                           invocation->stub ());
  TAO_Protocols_Hooks *protocol_hooks =
    invocation->orb_core ()->get_protocols_hooks (ACE_TRY_ENV);
  ACE_CHECK;

  CORBA::Policy_var bands_policy = TAO_RT_Endpoint_Utils::priority_bands_policy (invocation,
                                                                              ACE_TRY_ENV);
  ACE_CHECK;

  // Figure out target priority.
  CORBA::Policy_var priority_model_policy =
    rt_stub->exposed_priority_model (ACE_TRY_ENV);
  ACE_CHECK;

  CORBA::Short server_priority = 0;
  CORBA::Boolean is_client_propagated = 0;
  protocol_hooks->get_selector_hook (
                                     priority_model_policy.in (),  // input
                                     is_client_propagated, // side effect
                                     server_priority); // side effect


  CORBA::Short p;
  if (is_client_propagated)
    {
      // Get Client priority.

      int status =
        protocol_hooks->get_thread_CORBA_priority (p, // side effect
                                                   ACE_TRY_ENV);
      ACE_CHECK;
      if (status == -1)
        {
          ACE_THROW (CORBA::DATA_CONVERSION (1,
                     CORBA::COMPLETED_NO));
        }
    }
  else
    p = server_priority;

  int in_range = 0;

  CORBA::Short min_priority;
  CORBA::Short max_priority;

  protocol_hooks->get_selector_bands_policy_hook (bands_policy.in (),
                                                  min_priority,
                                                  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 (bands_policy.in ());
          invocation->inconsistent_policies ()[1u] =
            CORBA::Policy::_duplicate (priority_model_policy.in ());
        }
      ACE_THROW (CORBA::INV_POLICY ());
    }

  do
    {
      // Obtain profile.
      invocation->profile (invocation->stub ()->profile_in_use ());

      // Select an endpoint from the profile.

      if (invocation->profile ()->endpoint_count () == 0)
        {
          // Unknown protocol - move onto the next profile.
          continue;
        }

      else if (invocation->profile ()->endpoint_count () == 1)
        {
          // Profile contains just one endpoint.  This happens when:
          //    a) we are talking to a nonTAO server (which doesn't have
          //       the concept of multiple endpoints per profile)
          //    or
          //    b) we have TAO server with a non-lane threadpool, in which
          //    case there is only one acceptor
          // In both cases we should use the endpoint regardless of its priority.
          invocation->endpoint (invocation->profile ()->endpoint ());
        }
      else
        {
          // Profiles contains more than one endpoint.  Find one with the
          // right priority.

          TAO_Endpoint *endpoint = 0;
          for (TAO_Endpoint *endp = invocation->profile ()->endpoint ();
               endp != 0;
               endp = endp->next ())
            {
              if (endp->priority () <= max_priority
                  && endp->priority () >= min_priority)
                {
                  endpoint = endp;
                  break;
                }
           }

          if (endpoint != 0)
            {
              // Found an Endpoint with correct priority.
              invocation->endpoint (endpoint);
            }
          else
            {
              if (invocation->inconsistent_policies ().ptr ())
                {
                  invocation->inconsistent_policies ()->length (1);
                  invocation->inconsistent_policies ()[0u] =
                    CORBA::Policy::_duplicate (bands_policy.in ());
                }
          ACE_THROW (CORBA::INV_POLICY ());
            }
        }

      // Try to perform the invocation using this endpoint.
      int status;
      if (rt_stub->private_connection ())
        {
          TAO_Private_Transport_Descriptor private_desc (invocation->endpoint (),
                                                         ACE_reinterpret_cast (long, invocation->stub ()));

          status = invocation->perform_call (private_desc, ACE_TRY_ENV);
          ACE_CHECK;
        }
      else
        {
          TAO_Base_Transport_Property default_desc (invocation->endpoint ());

          status = invocation->perform_call (default_desc, ACE_TRY_ENV);
          ACE_CHECK;
        }

      // Check if the invocation has completed.
      if (status == 1)
        return;
    }
  while (invocation->stub ()->next_profile_retry () != 0);

  // If we get here, we completely failed to find an endpoint selector
  // that we know how to use, so throw an exception.
  ACE_THROW (CORBA::TRANSIENT (TAO_OMG_VMCID | 2,
                               CORBA::COMPLETED_NO));


}

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

TAO_Protocol_Endpoint_Selector::~TAO_Protocol_Endpoint_Selector (void)
{
}

void
TAO_Protocol_Endpoint_Selector::select_endpoint (TAO_GIOP_Invocation
                                                 *invocation,
                                                 CORBA::Environment &ACE_TRY_ENV)
{
  /// Narrow down to the right policy.
  CORBA::Policy_var cp =
    TAO_RT_Endpoint_Utils::client_protocol_policy (invocation, ACE_TRY_ENV);
  ACE_CHECK;

  RTCORBA::ClientProtocolPolicy_var cp_policy =
    RTCORBA::ClientProtocolPolicy::_narrow (cp.in (), ACE_TRY_ENV);
  ACE_CHECK;

  /// Cast to TAO_ClientProtocolPolicy
  TAO_ClientProtocolPolicy *client_protocol_policy =
    ACE_static_cast (TAO_ClientProtocolPolicy *,
                     cp_policy.in ());

  /// Get the ProtocolList
  RTCORBA::ProtocolList & protocols =
    client_protocol_policy->protocols_rep ();

  TAO_RT_Stub *rt_stub = ACE_dynamic_cast (TAO_RT_Stub *,
                                           invocation->stub ());

  CORBA::Boolean valid_endpoint_found = 0;

  for (CORBA::ULong protocol_index = 0;
       protocol_index < protocols.length ();
       protocol_index++)
    {
      // Find the profiles that match the current protocol.
      TAO_Profile *profile = 0;
      TAO_MProfile& mprofile = invocation->stub ()->base_profiles ();

      for (TAO_PHandle i = 0;
           i < mprofile.profile_count ();
           ++i)
        {
          profile = mprofile.get_profile (i);
          if (profile->tag ()
              == protocols[protocol_index].protocol_type)
            {
              // Save away the profile and endpoint that we found in the
              // invocation.
              invocation->profile (profile);
              invocation->endpoint (profile->endpoint());
              valid_endpoint_found = 1;

              // Try to perform the invocation using this endpoint.
              int status;
              if (rt_stub->private_connection ())
                {
                  TAO_Private_Transport_Descriptor private_desc (invocation->endpoint (),
                                                                 ACE_reinterpret_cast (long, invocation->stub ()));

                  status = invocation->perform_call (private_desc, ACE_TRY_ENV);
                  ACE_CHECK;
                }
              else
                {
                  TAO_Base_Transport_Property default_desc (invocation->endpoint ());

                  status = invocation->perform_call (default_desc, ACE_TRY_ENV);
                  ACE_CHECK;
                }

              // Check if the invocation has completed.
              if (status == 1)
                return;
            }
        }
    }

  // We have tried all the protocols specified in the client
  // protocol policy with no success.  Throw exception.

  // Figure out proper exception.
  if (!valid_endpoint_found)
    {
      if (invocation->inconsistent_policies ().ptr ())
        {
          invocation->inconsistent_policies ()->length (1);
          invocation->inconsistent_policies ()[0u] =
            CORBA::Policy::_duplicate (client_protocol_policy);
        }
      ACE_THROW (CORBA::INV_POLICY ());
    }

    // If we get here, we completely failed to find an endpoint selector
  // that we know how to use, so throw an exception.
  ACE_THROW (CORBA::TRANSIENT (TAO_OMG_VMCID | 2,
                               CORBA::COMPLETED_NO));


}

void
TAO_Protocol_Endpoint_Selector::next (TAO_GIOP_Invocation
                                     *,
                                     CORBA::Environment &)
{
  ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("You Are not Suppesed to call: TAO_Protocol_Endpoint_Selector::next!!!\n")));
  //  invocation->profile_index ()++;
  // If we ran out of profiles to try - this will be detected and
  // exception thrown once <endpoint> is called.
}

void
TAO_Protocol_Endpoint_Selector::forward (TAO_GIOP_Invocation
                                        *invocation,
                                         const TAO_MProfile& /* mprofile */,
                                        CORBA::Environment &ACE_TRY_ENV)
{
  // Location forwarding is currently not supported when using
  // RTCORBA::ClientProtocolPolicy.  With the current profile
  // management scheme being broken, it would be a pain to support here.
  // @@ Add support for location forwarding once profile management is
  // redesigned.

  // Currently location forward replies are ignored.  When we receive
  // a location forward reply, we act as if the profile we used
  // didn't work, and we try the next one.  Should we throw not
  // supported exception instead?
  this->next (invocation, ACE_TRY_ENV);
}

void
TAO_Protocol_Endpoint_Selector::success (TAO_GIOP_Invocation *invocation)
{
  invocation->stub ()->set_valid_profile ();
}

void
TAO_Protocol_Endpoint_Selector::close_connection (TAO_GIOP_Invocation *)
{
}

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

TAO_Priority_Protocol_Selector::~TAO_Priority_Protocol_Selector (void)
{
}

int
TAO_Priority_Protocol_Selector::is_multihomed (TAO_Endpoint *endpoint)
{
  for (TAO_Endpoint *endp = endpoint;
       endp != 0;
       endp = endp->next ())
    {
      if (endp->next () != 0
          && endp->priority () != endp->next ()->priority ())
        return 0;
    }
  return 1;
}

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

TAO_Bands_Protocol_Selector::~TAO_Bands_Protocol_Selector (void)
{
}