summaryrefslogtreecommitdiff
path: root/TAO/tao/RTPortableServer/RT_Servant_Dispatcher.cpp
blob: c49c601952737bba50f8e8f93d062f8db8e0b192 (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
// -*- C++ -*-
#include "tao/RTPortableServer/RT_Servant_Dispatcher.h"

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

#include "tao/RTPortableServer/RT_POA.h"

#include "tao/ORB_Core.h"
#include "tao/ORB_Core_TSS_Resources.h"
#include "tao/TAO_Server_Request.h"
#include "tao/Transport.h"
#include "tao/Connection_Handler.h"
#include "tao/Service_Context.h"
#include "tao/Protocols_Hooks.h"
#include "tao/Network_Priority_Protocols_Hooks.h"
#include "tao/PortableServer/Network_Priority_Hook.h"
#include "tao/debug.h"
#include "tao/CDR.h"

#include "tao/RTCORBA/Thread_Pool.h"

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

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO_RT_Servant_Dispatcher::~TAO_RT_Servant_Dispatcher ()
{
}

void
TAO_RT_Servant_Dispatcher::pre_invoke_remote_request (
  TAO_Root_POA &poa,
  CORBA::Short servant_priority,
  TAO_ServerRequest &req,
  TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State &pre_invoke_state
  )
{
  TAO_Service_Context &request_service_context = req.request_service_context ();
  TAO_Service_Context &reply_service_context = req.reply_service_context ();

  TAO_Thread_Pool *thread_pool =
    static_cast <TAO_Thread_Pool *> (poa.thread_pool ());

  if (thread_pool != 0 &&
      thread_pool->with_lanes ())
    {
      //
      // We don't mess with the priority of threads in lanes.
      //

      if (TAO_debug_level > 0)
        {
          // Get the ORB_Core's TSS resources.
          TAO_ORB_Core_TSS_Resources *tss =
            poa.orb_core ().get_tss_resources ();

          /// Get the lane attribute in TSS.
          TAO_Thread_Lane *lane =
            static_cast<TAO_Thread_Lane *> (tss->lane_);

          ACE_ASSERT (lane->pool ().id () ==
                      thread_pool->id ());

          TAOLIB_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("Using thread pool with lane ")
                      ACE_TEXT ("(%P|%t|%d|%d): original thread ")
                      ACE_TEXT ("CORBA/native priority %d/%d not changed\n"),
                      lane->pool ().id (),
                      lane->id (),
                      lane->lane_priority (),
                      lane->native_priority ()));
        }

      return;
    }

  // Remember current thread's priority.
  TAO_Protocols_Hooks *tph = poa.orb_core ().get_protocols_hooks ();

  if (tph != 0)
    {
      const char *priority_model = 0;
      RTCORBA::Priority target_priority = TAO_INVALID_PRIORITY;

      // NOT_SPECIFIED PriorityModel processing.
      if (poa.priority_model () ==
          TAO::Portable_Server::Cached_Policies::NOT_SPECIFIED)
        {
          priority_model = "RTCORBA::NOT_SPECIFIED";
        }

      // CLIENT_PROPAGATED PriorityModel processing.
      else if (poa.priority_model () ==
          TAO::Portable_Server::Cached_Policies::CLIENT_PROPAGATED)
        {
          priority_model = "RTCORBA::CLIENT_PROPAGATED";

          // Attempt to extract client-propagated priority from the
          // ServiceContextList of the request.
          const IOP::ServiceContext *context = 0;

          if (request_service_context.get_context (IOP::RTCorbaPriority,
                                                   &context) == 1)
            {
              // Extract the target priority
              TAO_InputCDR cdr (reinterpret_cast
                                <const char*>
                                 (context->context_data.get_buffer ()),
                                context->context_data.length ());
              CORBA::Boolean byte_order;
              if (!(cdr >> ACE_InputCDR::to_boolean (byte_order)))
                throw ::CORBA::MARSHAL ();
              cdr.reset_byte_order (static_cast<int> (byte_order));

              if (!(cdr >> target_priority))
                throw ::CORBA::MARSHAL ();

              // Save the target priority in the response service
              // context to propagate back to the client as specified
              // by the RTCORBA specification.
              reply_service_context.set_context (*context);
            }
          else
            {
              // Use default priority if none came in the request.
              // (Request must have come from a non-RT ORB.)
              target_priority = poa.server_priority ();
            }
        }
      else
        // SERVER_DECLARED PriorityModel processing.
        {
          priority_model = "RTCORBA::SERVER_DECLARED";

          // Use the request associated with the servant.
          target_priority = servant_priority;
        }

      char thread_pool_id[BUFSIZ];
      if (TAO_debug_level > 0)
        {
          if (thread_pool == 0)
            ACE_OS::strcpy (thread_pool_id,
                            "default thread pool");
          else
            ACE_OS::sprintf (thread_pool_id,
                             "thread pool %d",
                             thread_pool->id ());
        }

      // Target priority is invalid.
      if (target_priority == TAO_INVALID_PRIORITY)
        {
          if (TAO_debug_level > 0)
            {
    // If we are in a multi-threaded configuration, print out the current
    // thread priority.
    #if defined (ACE_HAS_THREADS)

              if (tph->get_thread_CORBA_and_native_priority (
                    pre_invoke_state.original_CORBA_priority_,
                    pre_invoke_state.original_native_priority_) == -1)
                throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2,
                                                CORBA::COMPLETED_NO);

              TAOLIB_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t): %C processing using %C ")
                          ACE_TEXT ("original thread CORBA/native priority %d/%d ")
                          ACE_TEXT ("not changed\n"),
                          priority_model,
                          thread_pool_id,
                          pre_invoke_state.original_CORBA_priority_,
                          pre_invoke_state.original_native_priority_));

    // If we are in a single-threaded configuration, we cannot get the
    // current thread priority.  Therefore, print out a simpler message.
    #else /* ACE_HAS_THREADS */

              TAOLIB_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t): %C processing using %C ")
                          ACE_TEXT ("original thread CORBA/native priority ")
                          ACE_TEXT ("not changed\n"),
                          priority_model,
                          thread_pool_id));

    #endif /* ACE_HAS_THREADS */

            }
        }
      else
        {
          // Get the current thread's priority.

          if (tph->get_thread_CORBA_and_native_priority (
                pre_invoke_state.original_CORBA_priority_,
                pre_invoke_state.original_native_priority_) == -1)
            throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2,
                                            CORBA::COMPLETED_NO);

          // Priority needs to be temporarily changed for the
          // duration of request.
          if (target_priority != pre_invoke_state.original_CORBA_priority_)
            {
              if (tph->set_thread_CORBA_priority (target_priority) == -1)
                throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2,
                                                CORBA::COMPLETED_NO);

              pre_invoke_state.state_ =
                TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State::PRIORITY_RESET_REQUIRED;

              if (TAO_debug_level > 0)
                {
                  CORBA::Short native_priority = 0;
                  tph->get_thread_native_priority (native_priority);

                  TAOLIB_DEBUG ((LM_DEBUG,
                              ACE_TEXT ("%C processing using %C ")
                              ACE_TEXT ("(%P|%t): original thread CORBA/native priority %d/%d ")
                              ACE_TEXT ("temporarily changed to CORBA/native priority %d/%d\n"),
                              priority_model,
                              thread_pool_id,
                              pre_invoke_state.original_CORBA_priority_,
                              pre_invoke_state.original_native_priority_,
                              target_priority,
                              native_priority));
                }
            }
          // No change in priority required.
          else
            {
              if (TAO_debug_level > 0)
                {
                  TAOLIB_DEBUG ((LM_DEBUG,
                              ACE_TEXT ("%C processing using %C ")
                              ACE_TEXT ("(%P|%t): original thread CORBA/native priority %d/%d ")
                              ACE_TEXT ("is the same as the target priority\n"),
                              priority_model,
                              thread_pool_id,
                              pre_invoke_state.original_CORBA_priority_,
                              pre_invoke_state.original_native_priority_));
                }
            }
        }
    }

  TAO_Network_Priority_Protocols_Hooks *nph =
    poa.orb_core ().get_network_priority_protocols_hooks ();

  if (nph != 0)
    {
      poa.network_priority_hook ()-> set_dscp_codepoint (req, poa);
    }
  else if (tph != 0)
    {
      CORBA::Policy_var policy =
        poa.policies ().get_cached_policy (
          TAO_CACHED_POLICY_RT_SERVER_PROTOCOL);
      CORBA::Boolean set_server_network_priority =
        tph->set_server_network_priority (
          req.transport ()->tag (), policy.in ());
      TAO_Connection_Handler *connection_handler =
        req.transport ()->connection_handler ();
      connection_handler->set_dscp_codepoint (set_server_network_priority);
    }
}

void
TAO_RT_Servant_Dispatcher::pre_invoke_collocated_request (TAO_Root_POA &poa,
                                                          CORBA::Short servant_priority,
                                                          TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State &pre_invoke_state)
{
  TAO_Thread_Pool *thread_pool =
    static_cast <TAO_Thread_Pool *> (poa.thread_pool ());

  if (thread_pool == 0 || thread_pool->with_lanes ())
    {
      //
      // We don't mess with the priority of threads in lanes or for
      // the default thread pool.
      //
      return;
    }

  if (poa.priority_model () !=
      TAO::Portable_Server::Cached_Policies::SERVER_DECLARED ||
      servant_priority == TAO_INVALID_PRIORITY)
    {
      //
      // We either don't have server declared model or servant
      // priority is invalid.
      //
      return;
    }

  //
  // SERVER_DECLARED PriorityModel processing.
  //

  // Remember current thread's priority.
  TAO_Protocols_Hooks *tph = poa.orb_core ().get_protocols_hooks ();

  if (tph != 0)
    {
      if (tph->get_thread_CORBA_and_native_priority (
            pre_invoke_state.original_CORBA_priority_,
            pre_invoke_state.original_native_priority_) == -1)
        throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2, CORBA::COMPLETED_NO);

      // Change the priority of the current thread for the duration of
      // request.
      if (servant_priority != pre_invoke_state.original_CORBA_priority_)
        {
          if (tph->set_thread_CORBA_priority (servant_priority) == -1)
            throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2, CORBA::COMPLETED_NO);

          pre_invoke_state.state_ =
            TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State::PRIORITY_RESET_REQUIRED;
        }
    }
}

void
TAO_RT_Servant_Dispatcher::post_invoke (TAO_Root_POA &poa,
                                        TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State &pre_invoke_state)

{
  if (pre_invoke_state.state_ ==
      TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State::PRIORITY_RESET_REQUIRED)
    {
      pre_invoke_state.state_ =
        TAO::Portable_Server::Servant_Upcall::Pre_Invoke_State::NO_ACTION_REQUIRED;

      try
        {
          // Reset the priority of the current thread back to its original
          // value.
          TAO_Protocols_Hooks *tph = poa.orb_core ().get_protocols_hooks ();

          if (tph != 0)
            {
              if (tph->restore_thread_CORBA_and_native_priority (
                       pre_invoke_state.original_CORBA_priority_,
                       pre_invoke_state.original_native_priority_) == -1)
                throw ::CORBA::DATA_CONVERSION (CORBA::OMGVMCID | 2,
                                                CORBA::COMPLETED_NO);
            }
        }
      catch (const ::CORBA::Exception& ex)
        {
          // Eat up the exception.
          ex._tao_print_exception (
            "Exception caught: TAO - "
            "Priority_Model_Processing::"
            "~Priority_Model_Processing");
        }
    }
}

TAO_Root_POA *
TAO_RT_Servant_Dispatcher::create_Root_POA (const ACE_CString &name,
                                            PortableServer::POAManager_ptr poa_manager,
                                            const TAO_POA_Policy_Set &policies,
                                            ACE_Lock &lock,
                                            TAO_SYNCH_MUTEX &thread_lock,
                                            TAO_ORB_Core &orb_core,
                                            TAO_Object_Adapter *object_adapter)
{
  TAO_RT_POA *poa = 0;

  ACE_NEW_THROW_EX (poa,
                    TAO_RT_POA (name,
                                poa_manager,
                                policies,
                                0,
                                lock,
                                thread_lock,
                                orb_core,
                                object_adapter),
                    CORBA::NO_MEMORY ());

  return poa;
}

TAO_END_VERSIONED_NAMESPACE_DECL

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