summaryrefslogtreecommitdiff
path: root/TAO/tao/GIOP_Message_Factory.cpp
blob: f0dff85ed2514eabefb0df4b03e8463afc8f2617 (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
//$Id$
// @(#)giop.cpp 1.10 95/09/21
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// GIOP:        Utility routines for sending, receiving GIOP messages
//
// Note that the Internet IOP is just the TCP-specific mapping of the
// General IOP.  Areas where other protocols may map differently
// include use of record streams (TCP has none), orderly disconnect
// (TCP has it), endpoint addressing (TCP uses host + port), security
// (Internet security should be leveraged by IIOP) and more.
//
// NOTE: There are a few places where this code knows that it's really
// talking IIOP instead of GIOP.  No rush to fix this so long as we
// are really not running atop multiple connection protocols.
//
// THREADING NOTE: currently, the connection manager eliminates tricky
// threading issues by providing this code with the same programming
// model both in threaded and unthreaded environments.  Since the GIOP
// APIs were all designed to be reentrant, this makes threading rather
// simple!
//
// That threading model is that the thread making (or handling) a call
// is given exclusive access to a connection for the duration of a
// call, so that no multiplexing or demultiplexing is needed.  That
// is, locking is at the "connection level" rather than "message
// level".

// The down side of this simple threading model is that utilization of
// system resources (mostly connections, but to some extent network
// I/O) in some kinds of environments can be inefficient.  However,
// simpler threading models are much easier to get properly debugged,
// and often perform better.  Also, such environments haven't been
// seen to be any kind of problem; the model can be changed later if
// needed, it's just an internal implementation detail.  Any portable
// ORB client is not allowed to rely on semantic implications of such
// a model.
//
// @@ there is lots of unverified I/O here.  In all cases, if an
// error is detected when marshaling or unmarshaling, it should be
// reported.

#include "tao/GIOP_Message_Factory.h"
#include "tao/GIOP_Utils.h"
#include "tao/Any.h"
#include "tao/ORB_Core.h"


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



//
// Client Side Message Factory Methods
// 

TAO_GIOP_Client_Message_Factory::TAO_GIOP_Client_Message_Factory (void)
{
}

TAO_GIOP_Client_Message_Factory::~TAO_GIOP_Client_Message_Factory (void)
{
}

int
TAO_GIOP_Client_Message_Factory::handle_input (TAO_Transport *transport,
                                               TAO_ORB_Core */*orb_core*/,
                                               TAO_Message_State_Factory &mesg_state,
                                               ACE_Time_Value *max_wait_time)
{
  TAO_GIOP_Message_State *state = 
    ACE_dynamic_cast (TAO_GIOP_Message_State *,
                      &mesg_state);

  if (state->header_received () == 0)
    {
      if (TAO_GIOP_Utils::read_bytes_input (transport,
                                            state->cdr,
                                            TAO_GIOP_HEADER_LEN,
                                            max_wait_time) == -1)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n"
                        "TAO_GIOP_Client_Message_Factory::handle_input"));
          return -1;
        }
      
      
      if (this->parse_magic_bytes (state->cdr) == -1)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n"
                        "TAO_GIOP_Client_Message_Factory::handle_input, parse_bytes")); 
          return -1;
        }
      
      // Read the rest of the stuff. That should be read by the
      // corresponding states
      if (this->parse_header (state) == -1)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n"
                          "TAO_GIOP_Client_Message_Factory::handle_input"));
          return -1;
        }
    }
  
  size_t missing_data =
    state->message_size - state->current_offset;
  

  ssize_t n = 
    TAO_GIOP_Utils::read_buffer (transport,
                                 state->cdr.rd_ptr () + state->current_offset,
                                 missing_data,
                                 max_wait_time);
  if (n == -1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - %p\n",
                    "TAO_GIOP_Client_Message_Factory::handle_input, read_buffer[1]"));
      return -1;
    }
  else if (n == 0)
    {
      if (errno == EWOULDBLOCK)
        return 0;
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - %p\n",
                    "TAO_GIOP_Client_Message::handle_input, read_buffer[2]"));
      return -1;
    }
  
  state->current_offset += n;
  
  if (state->current_offset == state->message_size)
    {
      if (TAO_debug_level >= 4)
        {
          size_t header_len = TAO_GIOP_HEADER_LEN;

          // Need to include GIOPlite too.
         
          char *buf = state->cdr.rd_ptr ();
          buf -= header_len;
          size_t msg_len = state->cdr.length () + header_len;
          TAO_GIOP_Utils::dump_msg ("recv",
                                    ACE_reinterpret_cast (u_char *,
                                                          buf),
                                    msg_len);
        }
    }

  return state->is_complete ();
}


CORBA::Boolean
TAO_GIOP_Client_Message_Factory::write_request_header (const IOP::ServiceContextList& /*svc_ctx*/,
                                                       CORBA::ULong request_id,
                                                       CORBA::Octet response_flags,
                                                       TAO_Target_Specification & /*spec*/,
                                                       const char */*opname*/,
                                                       TAO_OutputCDR &msg)
{
  // Adding only stuff that are common to all versions of GIOP. 
  // @@ Note: If at any stage we feel that this amount of granularity
  // for factorisation is not important we can dispense with it
  // anyway. 
  
  // First the request id
  msg << request_id;

  // Second the response flags
  switch (response_flags)
    {
      // We have to use magic numbers as the actual variables are
      // declared as const short. They cannot be used in switch case
      // statements. Probably what we can do is to add them as an
      // definitions in this class or in the parent and then use the
      // definitions. Hmm. Good idea.. But we will live with this for
      // the time being. 
    case 0: // SYNC_NONE
    case 1: // SYNC_WITH_TRANSPORT
    case 4: // This one corresponds to the TAO extension SYNC_FLUSH.
      // No response required.
      msg << CORBA::Any::from_octet (0);
        break;
    case 2: // SYNC_WITH_SERVER
      // Return before dispatching servant.
      msg << CORBA::Any::from_octet (1);
      break;
    case 3: // SYNC_WITH_TARGET
      // Return after dispatching servant.
      msg << CORBA::Any::from_octet (3);
      break;
      // Some cases for the DII are missing here. We can add that once
      // our IDL compiler starts supporting those stuff. This is
      // specific to GIOP 1.2. So some of the services would start
      // using this at some point of time and we will have them here
      // naturally out of a need.
    default:
      return 0;
    }
  
  return 1;
}



int                                                
TAO_GIOP_Client_Message_Factory::send_message (TAO_Transport *transport,
                                               TAO_OutputCDR &stream,
                                               ACE_Time_Value *max_wait_time,
                                               TAO_Stub *stub)
{
  // Get the header length
  size_t header_len = this->get_header_len ();

  // Get the message size offset
  size_t offset = this->get_message_size_offset ();
  
  return TAO_GIOP_Utils::send_message (transport,
                                       stream,
                                       header_len,
                                       offset,
                                       max_wait_time,
                                       stub);
}



//
// Server Side Message Factory Methods
// 


TAO_GIOP_Message_Acceptor::TAO_GIOP_Message_Acceptor (void)
{
  // Initialised...
  this->accept_states_ =  &(this->available_states_.giop_1_1_);
  
}

TAO_GIOP_Message_Acceptor::~TAO_GIOP_Message_Acceptor (void)
{
  //no-op
}
  
int
TAO_GIOP_Message_Acceptor::handle_input (TAO_Transport *transport,
                                         TAO_ORB_Core */*orb_core*/,
                                         TAO_Message_State_Factory &mesg_state,
                                         ACE_Time_Value *max_wait_time)
{
  TAO_GIOP_Message_State *state = 
    ACE_dynamic_cast (TAO_GIOP_Message_State *,
                      &mesg_state);

  if (state->header_received () == 0)
    {
      if (TAO_GIOP_Utils::read_bytes_input (transport,
                                            state->cdr,
                                            TAO_GIOP_HEADER_LEN,
                                            max_wait_time) == -1)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n"
                        "TAO_GIOP_Message_Acceptor::handle_input"));
          return -1;
        }
      
      if (this->parse_magic_bytes (state->cdr, 
                                   state) == -1)
        {
          this->accept_states_->send_error (transport);
          
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n"
                        "TAO_GIOP_Message_Acceptor::handle_input, parse_bytes")); 
          return -1;
        }
      
      // Read the rest of the stuff. That should be read by the
      // corresponding states
      if (this->accept_states_->parse_header (state) == -1)
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO (%P|%t) - %p\n"
                          "TAO_GIOP_Message_Acceptor::handle_input"));
          return -1;
        }
    }
  
  size_t missing_data =
    state->message_size - state->current_offset;
  
  ssize_t n = 
    TAO_GIOP_Utils::read_buffer (transport,
                                 state->cdr.rd_ptr () + state->current_offset,
                                 missing_data,
                                 max_wait_time);
  if (n == -1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - %p\n",
                    "TAO_GIOP_Message_Acceptor::handle_input, read_buffer[1]"));
      return -1;
    }
  else if (n == 0)
    {
      if (errno == EWOULDBLOCK)
        return 0;
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) - %p\n",
                    "TAO_GIOP_Message_Acceptor::handle_input, read_buffer[2]"));
      return -1;
    }
  
  state->current_offset += n;
  
  if (state->current_offset == state->message_size)
    {
      if (TAO_debug_level >= 4)
        {
          size_t header_len = TAO_GIOP_HEADER_LEN;

          // Need to include GIOPlite too.
         
          char *buf = state->cdr.rd_ptr ();
          buf -= header_len;
          size_t msg_len = state->cdr.length () + header_len;
          TAO_GIOP_Utils::dump_msg ("recv",
                                    ACE_reinterpret_cast (u_char *,
                                                          buf),
                                    msg_len);
        }
    }

  return state->is_complete ();
}


int
TAO_GIOP_Message_Acceptor::
process_connector_messages (TAO_Transport *transport, 
                            TAO_ORB_Core *orb_core,
                            TAO_InputCDR &input,
                            CORBA::Octet message_type)
{
  char repbuf[ACE_CDR::DEFAULT_BUFSIZE];
  
#if defined(ACE_HAS_PURIFY)
  (void) ACE_OS::memset (repbuf,
                         '\0',
                         sizeof repbuf);
#endif /* ACE_HAS_PURIFY */
  
  TAO_OutputCDR output (repbuf,
                        sizeof repbuf,
                        TAO_ENCAP_BYTE_ORDER,
                        orb_core->output_cdr_buffer_allocator (),
                        orb_core->output_cdr_dblock_allocator (),
                        orb_core->orb_params ()->cdr_memcpy_tradeoff (),
                        orb_core->to_iso8859 (),
                        orb_core->to_unicode ());

  switch (message_type)
    {
    case TAO_GIOP_REQUEST:
      // Should be taken care by the state specific invocations. They
      // could raise an exception or write things in the output CDR
      // stream 
      this->accept_states_->process_connector_request (transport,
                                                       orb_core,
                                                       input,
                                                       output);
      break;
    case TAO_GIOP_LOCATEREQUEST:
      this->accept_states_->process_connector_locate (transport,
                                                      orb_core,
                                                      input,
                                                      output);
      break;
    case TAO_GIOP_MESSAGERROR:
    case TAO_GIOP_REPLY:
    case TAO_GIOP_LOCATEREPLY:
    case TAO_GIOP_CLOSECONNECTION:
    default:
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) Illegal message received by server\n"));
      return this->accept_states_->send_error (transport);
      break;
    }

  return 0;
}

int 
TAO_GIOP_Message_Acceptor::parse_magic_bytes (TAO_InputCDR &input,
                                              TAO_GIOP_Message_State *state)
{
  // Grab the read pointer
  char *buf = input.rd_ptr ();
  
  // The values are hard-coded to support non-ASCII platforms.
  if (!(buf [0] == 0x47      // 'G'
        && buf [1] == 0x49   // 'I'
        && buf [2] == 0x4f   // 'O'
        && buf [3] == 0x50)) // 'P'
    {
      // Could be GIOPlite.. 
      //...
      
      // For the present...
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) bad header, magic word [%c%c%c%c]\n",
                    buf[0],
                    buf[1],
                    buf[2],
                    buf[3]));
      return -1;
    }
  
  state->giop_version.major = buf[TAO_GIOP_VERSION_MAJOR_OFFSET];
  state->giop_version.minor = buf[TAO_GIOP_VERSION_MINOR_OFFSET];

  // An extra variable?? Huh?? Can be removed??
  CORBA::Octet major = state->giop_version.major;
  CORBA::Octet minor = state->giop_version.minor;
  
  if ((this->available_states_.check_major (major) == -1) ||
      (this->available_states_.check_minor (minor) == -1)) 
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO (%P|%t) bad version <%d.%d>\n",
                    major, minor));
      return -1;
    }
  
  // Set the appropriate state 
  if (this->set_state (state) == -1)
    {
      if (TAO_debug_level > 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%P|%t|%N|%l) Failure <error> in set_state ()\n"),
                            -1);
        }
    }

  return 0;
}


int
TAO_GIOP_Message_Acceptor::set_state (TAO_GIOP_Message_State * /*state*/)
                                      
{
  // Logic for selecting from a registry set should be here. Let me go
  // first with  normal stuff.
  this->accept_states_ = &(this->available_states_.giop_1_1_);

  return 1;
}