summaryrefslogtreecommitdiff
path: root/TAO/tao/RTCORBA/RT_Policy_Protocols_Hooks.cpp
blob: 1fb509afb336ef017a6db0850dd65df6a756e0ee (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
#include "tao/RTCORBA/RT_Policy_Protocols_Hooks.h"

#if defined (TAO_HAS_CORBA_MESSAGING) && TAO_HAS_CORBA_MESSAGING != 0

#include "tao/RTCORBA/RT_Policy_i.h"
#include "tao/RTCORBA/Priority_Mapping_Manager.h"
#include "tao/RTCORBA/RT_Stub.h"

#include "tao/Stub.h"
#include "tao/ORB_Core.h"
#include "tao/MProfile.h"
#include "tao/Service_Context.h"
#include "tao/Acceptor_Registry.h"
#include "tao/Thread_Lane_Resources.h"
#include "tao/Protocol_Factory.h"
#include "tao/Transport_Acceptor.h"
#include "tao/Transport_Connector.h"
#include "tao/Policy_Set.h"
#include "tao/debug.h"
#include "tao/CDR.h"
#include "tao/Client_Network_Priority_Policy.h"

#include "ace/Dynamic_Service.h"
#include "ace/OS_NS_string.h"

ACE_RCSID (RTCORBA,
           RT_Policy_Protocols_Hooks,
           "$Id$")

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_RT_Policy_Protocols_Hooks::TAO_RT_Policy_Protocols_Hooks (void)
  : orb_core_ (0)
  , current_ ()
{
}


TAO_RT_Policy_Protocols_Hooks::~TAO_RT_Policy_Protocols_Hooks (void)
{
}

void
TAO_RT_Policy_Protocols_Hooks::init_hooks (TAO_ORB_Core *orb_core
                                           ACE_ENV_ARG_DECL)
{
  this->orb_core_ = orb_core;

  // Save a reference to the priority mapping manager.
  CORBA::Object_var obj =
    orb_core->object_ref_table ().resolve_initial_reference (
      TAO_OBJID_PRIORITYMAPPINGMANAGER);

  this->mapping_manager_ =
    TAO_Priority_Mapping_Manager::_narrow (obj.in ()
                                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  // Save a reference to the priority mapping manager.
  CORBA::Object_var object =
    orb_core->object_ref_table ().resolve_initial_reference (
      TAO_OBJID_NETWORKPRIORITYMAPPINGMANAGER);

  this->network_mapping_manager_ =
    TAO_Network_Priority_Mapping_Manager::_narrow (object.in ()
                                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  object =
    this->orb_core_->object_ref_table ().resolve_initial_reference (
      TAO_OBJID_RTCURRENT);

  this->current_ =
    RTCORBA::Current::_narrow (object.in ()
                               ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

}

RTCORBA::ProtocolProperties_ptr
TAO_RT_Policy_Protocols_Hooks::server_protocol_properties (
   IOP::ProfileId protocol_tag,
   CORBA::Policy_ptr policy ACE_ENV_ARG_DECL)
{
  if (CORBA::is_nil (policy))
    return 0;

  RTCORBA::ServerProtocolPolicy_var server_protocol_policy =
    RTCORBA::ServerProtocolPolicy::_narrow (policy
                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  if (CORBA::is_nil (server_protocol_policy.in ()))
    return 0;

  TAO_ServerProtocolPolicy *server_protocols =
    dynamic_cast<TAO_ServerProtocolPolicy *> (server_protocol_policy.in ());

  if (server_protocols == 0)
    return 0;

  // TAO_ServerProtocolPolicy
  RTCORBA::ProtocolList &protocols =
    server_protocols->protocols_rep ();

  for (CORBA::ULong j = 0; j < protocols.length (); ++j)
    {
      if (protocols[j].protocol_type == protocol_tag)
        {
          return RTCORBA::ProtocolProperties::_duplicate (protocols[j].transport_protocol_properties.in ());
        }
    }

  return 0;
}

RTCORBA::ProtocolProperties_ptr
TAO_RT_Policy_Protocols_Hooks::client_protocol_properties_at_object_level (
   IOP::ProfileId protocol_tag, TAO_Stub *stub ACE_ENV_ARG_DECL)
{
  CORBA::Policy_var policy =
    stub->get_cached_policy (TAO_CACHED_POLICY_RT_CLIENT_PROTOCOL
                             ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  return this->client_protocol_properties (protocol_tag,
                                           policy.in ()
                                           ACE_ENV_ARG_PARAMETER);
}


RTCORBA::ProtocolProperties_ptr
TAO_RT_Policy_Protocols_Hooks::client_protocol_properties (
  IOP::ProfileId protocol_tag,
  CORBA::Policy_ptr policy
  ACE_ENV_ARG_DECL)
{
  if (CORBA::is_nil (policy))
    return 0;

  RTCORBA::ClientProtocolPolicy_var client_protocol_policy =
    RTCORBA::ClientProtocolPolicy::_narrow (policy
                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  if (CORBA::is_nil (client_protocol_policy.in ()))
    return 0;

  TAO_ClientProtocolPolicy *client_protocols =
    dynamic_cast<TAO_ClientProtocolPolicy *> (client_protocol_policy.in ());

  if (client_protocols == 0)
    return 0;

  // TAO_ClientProtocolPolicy
  RTCORBA::ProtocolList &protocols =
    client_protocols->protocols_rep ();

  for (CORBA::ULong j = 0; j < protocols.length (); ++j)
    {
      if (protocols[j].protocol_type == protocol_tag)
        {
          return RTCORBA::ProtocolProperties::_duplicate (protocols[j].transport_protocol_properties.in ());
        }
    }

  return 0;
}

CORBA::Boolean
TAO_RT_Policy_Protocols_Hooks::set_network_priority (
   IOP::ProfileId protocol_tag,
   RTCORBA::ProtocolProperties_ptr protocol_properties
   ACE_ENV_ARG_DECL)
{
  if (CORBA::is_nil (protocol_properties))
      return false;

  if (protocol_tag == IOP::TAG_INTERNET_IOP)
    {
      RTCORBA::TCPProtocolProperties_var tcp_protocol_properties =
        RTCORBA::TCPProtocolProperties::_narrow (protocol_properties
                                                 ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (false);

      return tcp_protocol_properties->enable_network_priority ();
    }

  if (protocol_tag == TAO_TAG_DIOP_PROFILE)
    {
      RTCORBA::UserDatagramProtocolProperties_var udp_protocol_properties =
        RTCORBA::UserDatagramProtocolProperties::_narrow (protocol_properties
                                                          ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (false);

      return udp_protocol_properties->enable_network_priority ();
    }

  if (protocol_tag == TAO_TAG_SCIOP_PROFILE)
    {
      RTCORBA::StreamControlProtocolProperties_var sctp_protocol_properties =
        RTCORBA::StreamControlProtocolProperties::_narrow (protocol_properties
                                                           ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (false);

      return sctp_protocol_properties->enable_network_priority ();
    }

  return false;
}

CORBA::Boolean
TAO_RT_Policy_Protocols_Hooks::set_client_network_priority (
  IOP::ProfileId protocol_tag,
  TAO_Stub *stub
  ACE_ENV_ARG_DECL)
{
  if (protocol_tag != IOP::TAG_INTERNET_IOP &&
      protocol_tag != TAO_TAG_DIOP_PROFILE &&
      protocol_tag != TAO_TAG_SCIOP_PROFILE)
    return false;

  RTCORBA::ProtocolProperties_var protocol_properties =
    this->client_protocol_properties_at_object_level (protocol_tag,
                                                      stub
                                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (false);

  return this->set_network_priority (protocol_tag,
                                     protocol_properties.in ()
                                     ACE_ENV_ARG_PARAMETER);
}

CORBA::Boolean
TAO_RT_Policy_Protocols_Hooks::set_server_network_priority (
  IOP::ProfileId protocol_tag,
  CORBA::Policy *policy
  ACE_ENV_ARG_DECL)
{
  if (protocol_tag != IOP::TAG_INTERNET_IOP &&
      protocol_tag != TAO_TAG_DIOP_PROFILE &&
      protocol_tag != TAO_TAG_SCIOP_PROFILE)
    return false;

  RTCORBA::ProtocolProperties_var protocol_properties =
    this->server_protocol_properties (protocol_tag,
                                      policy
                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (false);

  return this->set_network_priority (protocol_tag,
                                     protocol_properties.in ()
                                     ACE_ENV_ARG_PARAMETER);
}

void
TAO_RT_Policy_Protocols_Hooks::set_dscp_codepoint (CORBA::Long &dscp_codepoint
                                            ACE_ENV_ARG_DECL_NOT_USED)
{
}

CORBA::Long
TAO_RT_Policy_Protocols_Hooks::get_dscp_codepoint (void)
{
  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG,
                "TAO_RT_Protocols_Hooks::get_dscp_codepoint\n"));

  CORBA::Long codepoint = 0;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Make several invocation, changing the priority of this thread
      // for each.

      RTCORBA::NetworkPriorityMapping *pm =
        this->network_mapping_manager_->mapping ();

      const CORBA::Short priority =
        this->current_->the_priority (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (pm->to_network (priority, codepoint) == 0)
        {
          if (TAO_debug_level > 0)
            {
              ACE_ERROR ((LM_ERROR,
                          "Cannot convert corba priority %d "
                          "to network priority\n",
                          priority));
            }

          return -1;
        }
    }
  ACE_CATCHANY
    {
      if (TAO_debug_level > 0)
        {
          ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                               "TAO_RT_Protocols_Hooks::get_dscp_codepoint");
        }

      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return codepoint;
}

void
TAO_RT_Policy_Protocols_Hooks::rt_service_context (
    TAO_Stub *stub,
    TAO_Service_Context &service_context,
    CORBA::Boolean restart
    ACE_ENV_ARG_DECL
  )
{
  // If the restart flag is true, then this call for a
  // reinvocation. We need not prepare the Service Context List once
  // again. We can use the already existing one.
  if (!restart)
    {
      TAO_RT_Stub *rt_stub =
        dynamic_cast<TAO_RT_Stub *> (stub);

      CORBA::Policy_var priority_model_policy =
        rt_stub->get_cached_policy (TAO_CACHED_POLICY_PRIORITY_MODEL
                                    ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      if (!CORBA::is_nil (priority_model_policy.in ()))
        {
          CORBA::Short client_priority;

          int status =
            this->get_thread_CORBA_priority (client_priority
                                             ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;

          if (status == -1)
            ACE_THROW (CORBA::DATA_CONVERSION (1, CORBA::COMPLETED_NO));

          this->add_rt_service_context_hook (service_context,
                                             priority_model_policy.in (),
                                             client_priority
                                             ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }
      else
        {
          // The Object does not contain PriorityModel policy in its IOR.
          // We must be talking to a non-RT ORB.  Do nothing.
        }
    }
}

void
TAO_RT_Policy_Protocols_Hooks::add_rt_service_context_hook (
    TAO_Service_Context &service_context,
    CORBA::Policy *model_policy,
    CORBA::Short &client_priority
    ACE_ENV_ARG_DECL
  )
{

  RTCORBA::PriorityModelPolicy_var model_policy_ptr =
    RTCORBA::PriorityModelPolicy::_narrow (model_policy
                                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  TAO_PriorityModelPolicy *priority_model =
    static_cast<TAO_PriorityModelPolicy *> (model_policy_ptr.in ());

  if (priority_model->get_priority_model () == RTCORBA::CLIENT_PROPAGATED)
    {
      // Encapsulate the priority of the current thread into
      // a service context.
      TAO_OutputCDR cdr;
      if ((cdr << ACE_OutputCDR::from_boolean (TAO_ENCAP_BYTE_ORDER) == 0)
          || (cdr << client_priority) == 0)
        {
          ACE_THROW (CORBA::MARSHAL ());
        }

      service_context.set_context (IOP::RTCorbaPriority, cdr);
    }
}

void
TAO_RT_Policy_Protocols_Hooks::get_selector_hook (
    CORBA::Policy *model_policy,
    CORBA::Boolean &is_client_propagated,
    CORBA::Short &server_priority
  )
{

  RTCORBA::PriorityModelPolicy_var model_policy_ptr =
    RTCORBA::PriorityModelPolicy::_narrow (model_policy);

  TAO_PriorityModelPolicy *priority_model_policy =
    static_cast<TAO_PriorityModelPolicy *> (model_policy_ptr.in ());

  if (priority_model_policy->get_priority_model ()
        == RTCORBA::CLIENT_PROPAGATED)
    {
      is_client_propagated = 1;
    }

  if (!is_client_propagated)
    {
      server_priority = priority_model_policy->server_priority ();
    }

  return;
}

void
TAO_RT_Policy_Protocols_Hooks::get_selector_bands_policy_hook (
    CORBA::Policy *bands_policy,
    CORBA::Short priority,
    CORBA::Short &min_priority,
    CORBA::Short &max_priority,
    int &in_range
  )
{
  RTCORBA::PriorityBandedConnectionPolicy_var bands_policy_ptr =
    RTCORBA::PriorityBandedConnectionPolicy::_narrow (bands_policy);

  TAO_PriorityBandedConnectionPolicy *priority_bands_policy =
    static_cast<TAO_PriorityBandedConnectionPolicy *> (bands_policy_ptr.in ());

  // Find the band with the range covering our target priority.
  RTCORBA::PriorityBands &bands =
    priority_bands_policy->priority_bands_rep ();

  for (CORBA::ULong i = 0; i < bands.length (); ++i)
    {
      if (bands[i].low <= priority && bands[i].high >= priority)
        {
          min_priority = bands[i].low;
          max_priority = bands[i].high;

          in_range = 1;
          break;
        }
    }
}

TAO_END_VERSIONED_NAMESPACE_DECL

ACE_STATIC_SVC_DEFINE (TAO_RT_Policy_Protocols_Hooks,
                       ACE_TEXT ("RT_Policy_Protocols_Hooks"),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (TAO_RT_Policy_Protocols_Hooks),
                       ACE_Service_Type::DELETE_THIS
                       | ACE_Service_Type::DELETE_OBJ,
                       0)
ACE_FACTORY_DEFINE (TAO_RTCORBA, TAO_RT_Policy_Protocols_Hooks)

#endif /* TAO_HAS_CORBA_MESSAGING && TAO_HAS_CORBA_MESSAGING != 0 */