summaryrefslogtreecommitdiff
path: root/TAO/tao/Synch_Invocation.cpp
blob: f802e989359753c2546a617a9d29ae8661f53461 (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
//$Id$
#include "Synch_Invocation.h"
#include "Profile_Transport_Resolver.h"
#include "Synch_Reply_Dispatcher.h"
#include "Transport.h"
#include "Stub.h"
#include "Bind_Dispatcher_Guard.h"
#include "operation_details.h"
#include "Pluggable_Messaging.h"
#include "Wait_Strategy.h"
#include "Messaging_SyncScopeC.h"
#include "TAOC.h"

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

namespace TAO
{
  Synch_Twoway_Invocation::Synch_Twoway_Invocation (Profile_Transport_Resolver &resolver,
                                                    TAO_Operation_Details &detail)
    : Remote_Invocation (resolver, detail)
  {

  }

  Invocation_Status
  Synch_Twoway_Invocation::communicate (Argument **args,
                                        int args_number
                                        ACE_ENV_ARG_DECL)
  {
    TAO_Synch_Reply_Dispatcher rd (this->resolver_.stub ()->orb_core (),
                                   this->detail_.reply_service_info ());

    // Register a reply dispatcher for this invocation. Use the
    // preallocated reply dispatcher.
    TAO_Bind_Dispatcher_Guard dispatch_guard (this->detail_.request_id (),
                                              &rd,
                                              this->resolver_.transport ()->tms ());

    if (dispatch_guard.status () != 0)
      {
        // @@ What is the right way to handle this error?
        // this->close_connection ();

        ACE_THROW_RETURN (CORBA::INTERNAL (TAO_DEFAULT_MINOR_CODE,
                                           CORBA::COMPLETED_NO),
                          TAO_INVOKE_FAILURE);
      }

    TAO_Target_Specification tspec;
    this->init_target_spec (tspec ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);


    TAO_OutputCDR &cdr =
      this->resolver_.transport ()->messaging_object ()->out_stream ();

    this->write_header (tspec,
                        cdr
                        ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);

    this->marshal_data (args,
                        args_number,
                        cdr
                        ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);


    this->send_message (TAO_Transport::TAO_TWOWAY_REQUEST,
                        cdr
                        ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);

    // @@ In all MT environments, there's a cancellation point lurking
    // here; need to investigate.  Client threads would frequently be
    // canceled sometime during recv_request ... the correct action to
    // take on being canceled is to issue a CancelRequest message to the
    // server and then imediately let other client-side cancellation
    // handlers do their jobs.
    //
    // In C++, that basically means to unwind the stack using almost
    // normal procedures: all destructors should fire, and some "catch"
    // blocks should probably be able to handle things like releasing
    // pointers. (Without unwinding the C++ stack, resources that must
    // be freed by thread cancellation won't be freed, and the process
    // won't continue to function correctly.)  The tricky part is that
    // according to POSIX, all C stack frames must also have their
    // (explicitly coded) handlers called.  We assume a POSIX.1c/C/C++
    // environment.

    this->resolver_.transport ()->wait_strategy ()->wait (0,
                                                          rd);

    return this->check_reply_status (rd,
                                     args,
                                     args_number
                                     ACE_ENV_ARG_PARAMETER);
  }


  Invocation_Status
  Synch_Twoway_Invocation::check_reply_status (TAO_Synch_Reply_Dispatcher &rd,
                                               Argument **args,
                                               int args_number
                                               ACE_ENV_ARG_DECL)
  {
    // Grab the reply CDR
    TAO_InputCDR &cdr =
      rd.reply_cdr ();

    // Set the translators
    this->resolver_.transport ()->assign_translators (&cdr, 0);

    int i = 0;

    // At this point it can be assumed that the GIOP/whatever protocol
    // header and the reply header are already handled.  Further it
    // can be assumed that the reply body contains the details
    // required for further processing. All the other details should
    // have been handled in the reply dispatcher/protocol framework.
    switch (rd.reply_status ())
      {
      case TAO_PLUGGABLE_MESSAGE_NO_EXCEPTION:
        for (i = 0; i != args_number; ++i)
          {
            if (!((*args[i]).demarshal (rd.reply_cdr ())))
              ACE_THROW_RETURN (CORBA::MARSHAL (),
                                TAO_INVOKE_FAILURE);
          }
        break;
      case TAO_PLUGGABLE_MESSAGE_LOCATION_FORWARD:
        // Handle the forwarding and return so the stub restarts the
        // request!
        return this->location_forward (cdr
                                       ACE_ENV_ARG_PARAMETER);
      case TAO_PLUGGABLE_MESSAGE_USER_EXCEPTION:
        return this->handle_user_exception (cdr
                                            ACE_ENV_ARG_PARAMETER);
      case TAO_PLUGGABLE_MESSAGE_SYSTEM_EXCEPTION:
        // return this->handle_system_exception (ACE_ENV_SINGLE_ARG_PARAMETER)
        break;
#if 0
        {
        // @@ Add the location macros for this exceptions...

        CORBA::String_var type_id;

        if ((this->inp_stream () >> type_id.inout ()) == 0)
          {
            // Could not demarshal the exception id, raise an local
            // CORBA::MARSHAL
            ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE,
                                              CORBA::COMPLETED_MAYBE),
                              TAO_INVOKE_OK);
          }

        CORBA::ULong minor = 0;
        CORBA::ULong completion = 0;

        if ((this->inp_stream () >> minor) == 0
            || (this->inp_stream () >> completion) == 0)
          {
            ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE,
                                              CORBA::COMPLETED_MAYBE),
                              TAO_INVOKE_OK);
          }

        CORBA::SystemException *ex =
          TAO_Exceptions::create_system_exception (type_id.in ()
                                                   ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (TAO_INVOKE_OK);

        if (ex == 0)
          {
            // @@ We should raise a CORBA::NO_MEMORY, but we ran out
            //    of memory already. We need a pre-allocated, TSS,
            //    CORBA::NO_MEMORY instance
            ACE_NEW_RETURN (ex,
                            CORBA::UNKNOWN,
                            TAO_INVOKE_EXCEPTION);
          }

        ex->minor (minor);
        ex->completed (CORBA::CompletionStatus (completion));

#if defined (TAO_HAS_EXCEPTIONS)
        // Without this, the call to create_system_exception() above
        // causes a memory leak. On platforms without native exceptions,
        // the CORBA::Environment class manages the memory.
        auto_ptr<CORBA::SystemException> safety (ex);
#endif

        // Raise the exception.
        ACE_ENV_RAISE (ex);

        return TAO_INVOKE_OK;
      }
      // NOTREACHED.


    case TAO_PLUGGABLE_MESSAGE_NEEDS_ADDRESSING_MODE:
      {
        // We have received an exception with a request to change the
        // addressing mode. First let us read the mode that the
        // server/agent asks for.
        CORBA::Short addr_mode = 0;
        if (this->inp_stream ().read_short (addr_mode) == 0)
          {
            // Could not demarshal the addressing disposition, raise an local
            // CORBA::MARSHAL
            ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE,
                                              CORBA::COMPLETED_MAYBE),
                              TAO_INVOKE_OK);
          }

        // Now set this addressing mode in the profile, so that
        // the next invocation need not go through this.
        this->profile_->addressing_mode (addr_mode
                                         ACE_ENV_ARG_PARAMETER);

        // Now restart the invocation
        return TAO_INVOKE_RESTART;
      }
#endif

    }
    return TAO_INVOKE_SUCCESS;
  }

  Invocation_Status
  Synch_Twoway_Invocation::location_forward (TAO_InputCDR &inp_stream
                                             ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CORBA::SystemException))
  {
    CORBA::Object_var forward_reference;

    if ((inp_stream >> forward_reference.out ()) == 0)
      {
        ACE_THROW_RETURN (CORBA::MARSHAL (),
                          TAO_INVOKE_FAILURE);
      }

    // The object pointer has to be changed to a TAO_Stub pointer
    // in order to obtain the profiles.
    TAO_Stub *stubobj = forward_reference->_stubobj ();

    if (stubobj == 0)
      ACE_THROW_RETURN (CORBA::INTERNAL (),
                        TAO_INVOKE_FAILURE);

    // Reset the profile in the stubs
    this->resolver_.stub ()->add_forward_profiles (stubobj->base_profiles ());

    // Is this check needed?
    /*
    if (this->resolver_.stub ()->next_profile () == 0)
      ACE_THROW (CORBA::TRANSIENT (
        CORBA::SystemException::_tao_minor_code (
          TAO_INVOCATION_LOCATION_FORWARD_MINOR_CODE,
          errno),
        CORBA::COMPLETED_NO));
    */
    return TAO_INVOKE_RESTART;
  }

  Invocation_Status
  Synch_Twoway_Invocation::handle_user_exception (TAO_InputCDR &cdr
                                                  ACE_ENV_ARG_DECL)
    ACE_THROW_SPEC ((CORBA::SystemException))
  {
    // Pull the exception from the stream.
    CORBA::String_var buf;

    if ((cdr >> buf.inout ()) == 0)
      {
        // Could not demarshal the exception id, raise an local
        // CORBA::MARSHAL
        ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE,
                                          CORBA::COMPLETED_MAYBE),
                          TAO_INVOKE_FAILURE);
      }

    CORBA::Exception *exception =
      this->detail_.corba_exception (buf.in ()
                                     ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);

    exception->_tao_decode (cdr
                            ACE_ENV_ARG_PARAMETER);
    ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);

    if (TAO_debug_level > 5)
      {
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("TAO: (%P|%t) Synch_Twoway_Invocation::handle_user_exception - ")
                    ACE_TEXT ("raising exception %s\n"),
                    buf.in ()));
      }

    // @@ Think about a better way to raise the exception here,
    //    maybe we need some more macros?
#if defined (TAO_HAS_EXCEPTIONS)
    // If we have native exceptions, we must manage the memory allocated
    // by the call above to alloc(). Otherwise the Environment class
    // manages the memory.
    auto_ptr<CORBA::Exception> safety (exception);

    // Direct throw because we don't have the ACE_TRY_ENV.
    exception->_raise ();
#else
    // We can not use ACE_THROW here.
    ACE_TRY_ENV.exception (exception);
#endif

    return TAO_INVOKE_SUCCESS;
  }

/*================================================================================*/

  Synch_Oneway_Invocation::Synch_Oneway_Invocation (Profile_Transport_Resolver &r,
                                                    TAO_Operation_Details &d)
    : Synch_Twoway_Invocation (r, d)
  {
  }

  Invocation_Status
  Synch_Oneway_Invocation::communicate (Argument **args,
                                        int args_number
                                        ACE_ENV_ARG_DECL)
  {
    const CORBA::Octet response_flags =
      this->detail_.response_flags ();

    if (response_flags == CORBA::Octet (Messaging::SYNC_NONE)
        || response_flags == CORBA::Octet (TAO::SYNC_EAGER_BUFFERING)
        || response_flags == CORBA::Octet (TAO::SYNC_DELAYED_BUFFERING))
      {
        TAO_Target_Specification tspec;
        this->init_target_spec (tspec ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);

        TAO_OutputCDR &cdr =
          this->resolver_.transport ()->messaging_object ()->out_stream ();

        this->write_header (tspec,
                            cdr
                            ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);

        this->marshal_data (args,
                            args_number,
                            cdr
                            ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);


        this->send_message (TAO_Transport::TAO_ONEWAY_REQUEST,
                            cdr
                            ACE_ENV_ARG_PARAMETER);
        ACE_CHECK_RETURN (TAO_INVOKE_FAILURE);
      }
    else
      {
        return Synch_Twoway_Invocation::communicate (args,
                                                     args_number
                                                     ACE_ENV_ARG_PARAMETER);
      }

    return TAO_INVOKE_SUCCESS;
  }


}