summaryrefslogtreecommitdiff
path: root/TAO/tao/GIOP_Server_Request.cpp
blob: f020aaf56c8bf427523de0a7605abffcb781e6a4 (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
// $Id$

// Implementation of the Dynamic Server Skeleton Interface  (for GIOP)

#include "tao/GIOP_Server_Request.h"

#include "tao/GIOP_Message_Acceptors.h"

#include "tao/CDR.h"
#include "tao/PortableServerC.h"
#include "tao/Environment.h"
#include "tao/NVList.h"
#include "tao/Principal.h"
#include "tao/ORB_Core.h"
#include "tao/ORB.h"
#include "tao/Timeprobe.h"
#include "tao/Any.h"
#include "tao/Marshal.h"
#include "tao/debug.h"
#include "tao/GIOP_Utils.h"
#include "tao/POA.h"

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

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

#if defined (ACE_ENABLE_TIMEPROBES)

static const char *TAO_Server_Request_Timeprobe_Description[] =
  {
    "GIOP_Server_Request::GIOP_Server_Request - start",
    "GIOP_Server_Request::GIOP_Server_Request - end",
  };

enum
  {
    // Timeprobe description table start key
    TAO_SERVER_REQUEST_START = 400,
    TAO_SERVER_REQUEST_END
  };

// Setup Timeprobes
ACE_TIMEPROBE_EVENT_DESCRIPTIONS (TAO_Server_Request_Timeprobe_Description,
                                  TAO_SERVER_REQUEST_START);

#endif /* ACE_ENABLE_TIMEPROBES */

TAO_GIOP_ServerRequest::
    TAO_GIOP_ServerRequest (TAO_Pluggable_Messaging *mesg_base,
                            TAO_InputCDR &input,
                            TAO_OutputCDR &output,
                            TAO_Transport *transport,
                            TAO_ORB_Core *orb_core)
      :mesg_base_ (mesg_base),
       incoming_ (&input),
       outgoing_ (&output),
       transport_(transport),
       response_expected_ (0),
       deferred_reply_ (0),
       sync_with_server_ (0),
       lazy_evaluation_ (0),

#if (TAO_HAS_MINIMUM_CORBA == 0)

       params_ (0),

#endif /* TAO_HAS_MINIMUM_CORBA */

       retval_ (0),
       exception_ (0),
       exception_type_ (TAO_GIOP_NO_EXCEPTION),
       orb_core_ (orb_core),
       service_info_ (),
       request_id_ (0),
       profile_ (orb_core),
       requesting_principal_ (0)
{
  ACE_FUNCTION_TIMEPROBE (TAO_SERVER_REQUEST_START);
  //no-op
}

// This constructor is used, by the locate request code

TAO_GIOP_ServerRequest::
    TAO_GIOP_ServerRequest (TAO_Pluggable_Messaging *mesg_base,
                            CORBA::ULong request_id,
                            CORBA::Boolean response_expected,
                            CORBA::Boolean deferred_reply,
                            TAO_ObjectKey &object_key,
                            const ACE_CString &operation,
                            TAO_OutputCDR &output,
                            TAO_Transport *transport,
                            TAO_ORB_Core *orb_core,
                            int &parse_error)
      : mesg_base_ (mesg_base),
        operation_ (operation),
        incoming_ (0),
        outgoing_ (&output),
        transport_ (transport),
        response_expected_ (response_expected),
        deferred_reply_ (deferred_reply),
        sync_with_server_ (0),
        lazy_evaluation_ (0),

#if (TAO_HAS_MINIMUM_CORBA == 0)

        params_ (0),

#endif /* TAO_HAS_MINIMUM_CORBA */

        retval_ (0),
        exception_ (0),
        exception_type_ (TAO_GIOP_NO_EXCEPTION),
        orb_core_ (orb_core),
        service_info_ (),
        request_id_ (request_id),
        profile_ (orb_core),
        requesting_principal_ (0)
{
  profile_.object_key (object_key);
  parse_error = 0;
}

TAO_GIOP_ServerRequest::~TAO_GIOP_ServerRequest (void)
{

#if (TAO_HAS_MINIMUM_CORBA == 0)

  if (this->params_)
    CORBA::release (this->params_);

#endif /* TAO_HAS_MINIMUM_CORBA */

  delete this->retval_;
  delete this->exception_;
}

CORBA::ORB_ptr
TAO_GIOP_ServerRequest::orb (void)
{
  return this->orb_core_->orb ();
}

#if (TAO_HAS_MINIMUM_CORBA == 0)

// Unmarshal in/inout params, and set up to marshal the appropriate
// inout/out/return values later on.

void
TAO_GIOP_ServerRequest::arguments (CORBA::NVList_ptr &list,
                                   CORBA::Environment &ACE_TRY_ENV)
{
  // Save params for later use when marshaling the reply.
  this->params_ = list;

  this->params_->_tao_incoming_cdr (*this->incoming_,
                                    CORBA::ARG_IN | CORBA::ARG_INOUT,
                                    this->lazy_evaluation_,
                                    ACE_TRY_ENV);
}

// Store the result value.  There's either an exception, or a result,
// but not both of them.  Results (and exceptions) can be reported
// only after the parameter list has been provided (maybe empty).

void
TAO_GIOP_ServerRequest::set_result (const CORBA::Any &value,
                                    CORBA::Environment &ACE_TRY_ENV)
{
  // setting a result when another result already exists or if an exception
  // exists is an error
  if (this->retval_ || this->exception_)
    ACE_THROW (CORBA::BAD_INV_ORDER ());

  ACE_NEW_THROW_EX (this->retval_,
                    CORBA::Any (value),
                    CORBA::NO_MEMORY ());
  ACE_CHECK;
}

// Store the exception value.

void
TAO_GIOP_ServerRequest::set_exception (const CORBA::Any &value,
                                       CORBA::Environment &ACE_TRY_ENV)
{
  if (this->retval_ || this->exception_)
    ACE_THROW (CORBA::BAD_INV_ORDER ());
  else
  {

#if (TAO_HAS_MINIMUM_CORBA == 0)

    const PortableServer::ForwardRequest *forward_request = 0;

    // If extraction of exception succeeded
    if ((value >>= forward_request) != 0)
      {
        this->forward_location_ = forward_request->forward_reference;
        this->exception_type_ = TAO_GIOP_LOCATION_FORWARD;
      }
    // Normal exception
    else
#endif /* TAO_HAS_MINIMUM_CORBA */
      {
        ACE_NEW_THROW_EX (this->exception_,
                          CORBA::Any (value),
                          CORBA::NO_MEMORY ());
        ACE_CHECK;

        this->exception_type_ = TAO_GIOP_USER_EXCEPTION;

        if (value.value ())
          {
            // @@ This cast is not safe, but we haven't implemented the >>=
            // and <<= operators for base exceptions (yet).
            CORBA_Exception* x = (CORBA_Exception*)value.value ();

            if (CORBA_SystemException::_downcast (x) != 0)
              {
                this->exception_type_ = TAO_GIOP_SYSTEM_EXCEPTION;
              }
          }
      }
   }
}

// this method will be utilized by the DSI servant to marshal outgoing
// parameters

void
TAO_GIOP_ServerRequest::dsi_marshal (CORBA::Environment &ACE_TRY_ENV)
{
  // NOTE: if "env" is set, it takes precedence over exceptions
  // reported using the mechanism of the ServerRequest.  Only system
  // exceptions are reported that way ...
  //
  // XXX Exception reporting is ambiguous; it can be cleaner than
  // this.  With both language-mapped and dynamic/explicit reporting
  // mechanisms, one of must be tested "first" ... so an exception
  // reported using the other mechanism could be "lost".  Perhaps only
  // the language mapped one should be used for system exceptions.


  // only if there wasn't any exception, we proceed
  if (this->exception_type_ == TAO_GIOP_NO_EXCEPTION &&
      CORBA::is_nil (this->forward_location_.in ()))
    {
      // ... then send any return value ...
      if (this->retval_)
        {
          CORBA::TypeCode_var tc = this->retval_->type ();
          if (this->retval_->any_owns_data ())
            {
              this->retval_->_tao_encode (*this->outgoing_,
                                          this->orb_core_,
                                          ACE_TRY_ENV);
              ACE_CHECK;
            }
          else
            {
              TAO_InputCDR cdr (this->retval_->_tao_get_cdr (),
                                this->retval_->_tao_byte_order ());
              (void) TAO_Marshal_Object::perform_append (tc.in (),
                                                         &cdr,
                                                         this->outgoing_,
                                                         ACE_TRY_ENV);
              ACE_CHECK;
            }
        }

      // ... Followed by "inout" and "out" parameters, left to right
      if (this->params_)
        {
          this->params_->_tao_encode (*this->outgoing_,
                                      this->orb_core_,
                                      CORBA::ARG_INOUT | CORBA::ARG_OUT,
                                      ACE_TRY_ENV);
          ACE_CHECK;
        }
    }
}

#endif /* TAO_HAS_MINIMUM_CORBA */

void
TAO_GIOP_ServerRequest::init_reply (CORBA::Environment &ACE_TRY_ENV)
{
  // Construct our reply generator
  TAO_Pluggable_Reply_Params reply_params;

  // We put all the info that we have in to this <reply_params> and
  // call the <write_reply_header> in the
  // pluggable_messaging_interface. One point to be noted however is
  // that, it was the pluggable_messaging classes who created us and
  // delegated us to do work on its behalf. But we would be calling
  // back. As we dont have a LOCK or any such things we can call
  // pluggable_messaging guys again. We would be on the same thread of
  // invocation. So *theoratically* there should not be a problem.
  reply_params.request_id_ = this->request_id_;

#if (TAO_HAS_MINIMUM_CORBA == 0)
  if (this->lazy_evaluation_ == 0 || this->params_ == 0)
    reply_params.params_ = 0;
  else
    reply_params.params_ = this->params_;
#endif /*TAO_HAS_MINIMUM_CORBA */

  // Pass in the service context list.  We are sending back what we
  // received in the Request.  (RTCORBA relies on it.  Check before
  // modifying...) marina
  reply_params.service_context_notowned (&this->service_info_);

  // Forward exception only.
  if (!CORBA::is_nil (this->forward_location_.in ()))
    {
      reply_params.reply_status_ = TAO_PLUGGABLE_MESSAGE_LOCATION_FORWARD;
    }

  // Any exception at all.
  else if (this->exception_ == 0)
    {
      reply_params.reply_status_ = TAO_PLUGGABLE_MESSAGE_NO_EXCEPTION;
    }
  else
    {
      reply_params.reply_status_ = this->exception_type_;
    }

  // Construct a REPLY header.
  this->mesg_base_->write_reply_header (*this->outgoing_,
                                        reply_params);

  // Finish the GIOP Reply header, then marshal the exception.
  if (reply_params.reply_status_ ==
      TAO_PLUGGABLE_MESSAGE_LOCATION_FORWARD)
    {
      // Marshall the forward location pointrr
      CORBA::Object_ptr object_ptr = this->forward_location_.in ();
      if ((*this->outgoing_ << object_ptr) == 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("TAO_GIOP_ServerRequest::marshal - ")
                      ACE_TEXT ("encoding forwarded objref failed\n")));
          return;
        }
    }
  else if (reply_params.reply_status_ !=
           TAO_PLUGGABLE_MESSAGE_NO_EXCEPTION)
    {
      // We are checking for NO exception. If there was a NO_EXCEPTION
      // type the IDL compiler would marshall the return values, the
      // out & inout parameters in to the stream <*this->output>

      // In this special case we only marshall the exception type
      CORBA::TypeCode_ptr except_tc = this->exception_->type ();

      // we use the any's ACE_Message_Block
      TAO_InputCDR cdr (this->exception_->_tao_get_cdr (),
                        this->exception_->_tao_byte_order ());
      (void) TAO_Marshal_Object::perform_append (except_tc,
                                                 &cdr,
                                                 this->outgoing_,
                                                 ACE_TRY_ENV);
    }
}

CORBA::Object_ptr
TAO_GIOP_ServerRequest::forward_location (void)
// get the forward_location
{
  return CORBA::Object::_duplicate (this->forward_location_.in ());
}

CORBA::ULong
TAO_GIOP_ServerRequest::exception_type (void)
// get the exception type
{
  return this->exception_type_;
}

void
TAO_GIOP_ServerRequest::send_no_exception_reply (void)
{
  // Construct our reply generator
  TAO_Pluggable_Reply_Params reply_params;
  reply_params.request_id_ = this->request_id_;

#if (TAO_HAS_MINIMUM_CORBA == 0)
  reply_params.params_ = 0;
#endif /*TAO_HAS_MINIMUM_CORBA*/

  // Change this to pass back the same thing we received, as well as
  // leave a comment why this is important!
  reply_params.svc_ctx_.length (0);

  // Pass in the service context list.  We are sending back what we
  // received in the Request.  (RTCORBA relies on it.  Check before
  // modifying...) marina
  reply_params.service_context_notowned (&this->service_info_);

  reply_params.reply_status_ = TAO_GIOP_NO_EXCEPTION;

  // Construct a REPLY header.
  this->mesg_base_->write_reply_header (*this->outgoing_,
                                        reply_params);

  // Send the message
  int result = this->mesg_base_->send_message (transport_,
                                               *this->outgoing_);

  if (result == -1)
    {
      if (TAO_debug_level > 0)
        {
          // No exception but some kind of error, yet a response
          // is required.
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("TAO: (%P|%t) %p: cannot send NO_EXCEPTION reply\n"),
                      ACE_TEXT ("TAO_GIOP_ServerRequest::send_no_exception_reply")));
        }
    }
}


void
TAO_GIOP_ServerRequest::tao_send_reply(void)
{


  int result = mesg_base_->send_message(transport_,
                                        *outgoing_);
  if (result == -1)
    {
      if (TAO_debug_level > 0)
        {
          // No exception but some kind of error, yet a response
          // is required.
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("TAO: (%P|%t) %p: cannot send reply\n"),
                      ACE_TEXT ("TAO_GIOP_ServerRequest::tao_send_reply")));

      }
    }
}

void
TAO_GIOP_ServerRequest::tao_send_reply_exception(CORBA::Exception& ex)
{
  int result = 0;
  if (response_expected_)
  {
    result = ACE_static_cast(TAO_GIOP_Message_Acceptors*,mesg_base_)->send_reply_exception (transport_,
                                               orb_core_,
                                               request_id_,
                                               &service_info(),
                                               &ex);
    if (result == -1)
    {
      if (TAO_debug_level > 0)
         ACE_ERROR ((LM_ERROR,
                     ACE_TEXT ("TAO: (%P|%t|%N|%l) %p: cannot send exception reply\n"),
                     ACE_TEXT ("tao_send_reply_exception()")));
         ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                              "TAO: ");
    }

  }
  else if (TAO_debug_level > 0)
  {
    // It is unfotunate that an exception (probably a system
    // exception) was thrown by the upcall code (even by the
    // user) when the client was not expecting a response.
    // However, in this case, we cannot close the connection
    // down, since it really isn't the client's fault.

    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("(%P|%t) exception thrown ")
                ACE_TEXT ("but client is not waiting a response\n")));
    ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "TAO: ");
   }

}

CORBA::Object_ptr
TAO_GIOP_ServerRequest::objref (CORBA_Environment &ACE_TRY_ENV)
{
  TAO_POA_Current_Impl *pci = TAO_TSS_RESOURCES::instance ()->poa_current_impl_;

  return pci->poa ()->id_to_reference (pci->object_id (), ACE_TRY_ENV);
}