summaryrefslogtreecommitdiff
path: root/TAO/IIOP/test/clnt.cpp
blob: f0249489889a6513dff03db54f2ebd4e24de6778 (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// @(#)clnt.cpp	1.2 05/14/97
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// TEST:	Simple "cube" client, calling hand-crafted stubs.
//
// Modified version of Cubit Example written by Sun Microsystems Inc.
// Modified by: Brian Mendel

#include <ace/Get_Opt.h>
//#include <crtdbg.h>

#if	unix
#	include	<unistd.h>
#	include	<sys/time.h>

#elif defined (VXWORKS)
#	include	<unistd.h>
#	include	<time.h>

#else	// windows

#endif	// unix

#include	"cubitC.h"

#include	<corba/debug.h>


#if !defined (_WIN32)
extern char 	*optarg;	// missing on some platforms
#endif

inline int func (unsigned i) { return i - 117; }

extern void
print_exception (const CORBA_Exception *, const char *, FILE *f=stdout);


//
// forward declarations
//
static void cube_union_stub(unsigned, unsigned&, unsigned&,
                            CORBA_Object_ptr, CORBA_Environment &);

static void cube_union_dii(unsigned &, unsigned &,
                           CORBA_Object_ptr, CORBA_Environment &);

// Global variables
const char* TAO_arg_ior = 0;
unsigned           loop_count = 1;
int                exit_later = 0;

// = TITLE
//     Parses the command line arguments and returns an error status
//
// = DESCRIPTION
//     This method parses the command line arguments
int parse_args(int argc, char *argv[])
{
   ACE_Get_Opt opts (argc, argv, "dn:O:x");
   int			c;
   
   while ((c = opts ()) != -1)
      switch (c) {
 
      case 'd':  // debug flag
         TAO_debug_level++;
         continue;
   
      case 'n':			// loop count
         loop_count = (unsigned) ACE_OS::atoi (opts.optarg);
         continue;
   
      case 'O':			// stringified objref
         {
           TAO_arg_ior = ACE_OS::strdup(opts.optarg);
         }
         continue;
   
      case 'x':
         exit_later++;
         continue;
   
      case '?':
      default:
         ACE_OS::fprintf (stderr, "usage:  %s"
            " [-d]"
            " [-n loopcount]"
            " [-O objref]"
            " [-x]"
            "\n", argv [0]
            );
         return 1;
      }
   
   return 0;  // Indicates successful parsing of command line
}

int
main (int    argc, char   *argv[])
{
  CORBA_ORB_ptr orb_ptr;
  CORBA_Object_ptr objref = CORBA_Object::_nil();
  CORBA_Environment env;

  orb_ptr = CORBA_ORB_init(argc, argv, "internet", env);
  if (env.exception() != 0)
    {
      print_exception(env.exception(), "ORB initialization");
      return 1;
    }

  //
  // Parse command line and verify parameters.
  //
  parse_args(argc, argv);

  if (TAO_arg_ior == 0)
    ACE_ERROR_RETURN((LM_ERROR, "%s: must specify an object reference using -O <ior>\n", argv[0]), 1);

  objref = orb_ptr->string_to_object ((CORBA_String)TAO_arg_ior, env);

  ACE_OS::free((void*)TAO_arg_ior);
  TAO_arg_ior = 0;

  if (env.exception () != 0)
    {
      print_exception (env.exception (), "string2object");
      return 1;
    }

  if (CORBA_is_nil (objref) == CORBA_B_TRUE)
    ACE_ERROR_RETURN((LM_ERROR, "%s:  must identify non-null target objref\n", argv [0]), 1);

  // Narrow the CORBA_Object reference to the stub object, checking
  // the type along the way using _is_a
  Cubit_ptr aCubit = Cubit::_narrow(objref);
  if (aCubit == 0)
    ACE_ERROR_RETURN((LM_ERROR, "(%P|%t) Unable to narrow object reference to a Cubit_ptr.\n"), 1);

  //
  // Make the calls in a loop.
  //
  unsigned i;
  unsigned call_count, error_count;

  call_count = 0;
  error_count = 0;

  ACE_Time_Value before, after;

  before = ACE_OS::gettimeofday();

  for (i = 0; i < loop_count; i++)
    {
      //
      // Cube an octet.
      //
      CORBA_Octet	arg_octet, ret_octet;
       
      call_count++;
      ret_octet = aCubit->Cubit_cube_octet (arg_octet = func (i), env);
      if (env.exception () != 0)
        {
          print_exception (env.exception (), "from cube_octet");
          error_count++;
        }
      else
        {
          dmsg2 ("cube octet:  %d --> %d\n", arg_octet, ret_octet);
          arg_octet = arg_octet * arg_octet * arg_octet;
          if (arg_octet != ret_octet) {
            ACE_OS::printf ("** cube_octet(%d) ERROR (--> %d)\n",
                            (CORBA_Octet) func (i), ret_octet);
            error_count++;
          }
        }

      //
      // Cube a short.
      //
      CORBA_Short	arg_short, ret_short;
       
      call_count++;
      ret_short = aCubit->Cubit_cube_short (arg_short = func (i), env);
      if (env.exception () != 0)
        {
          print_exception (env.exception (), "from cube_short");
          error_count++;
        }
      else
        {
          dmsg2 ("cube short:  %d --> %d\n", arg_short, ret_short);
          arg_short = arg_short * arg_short * arg_short;
          if (arg_short != ret_short)
            {
              ACE_OS::printf ("** cube_short(%d) ERROR (--> %d)\n",
                              (CORBA_Short) func (i), ret_short);
              error_count++;
            }
        }

      //
      // Cube a long.
      //
      CORBA_Long	arg_long, ret_long;
       
      call_count++;
      ret_long = aCubit->Cubit_cube_long (arg_long = func (i), env);
      if (env.exception () != 0)
        {
          print_exception (env.exception (), "from cube_long");
          error_count++;
        }
      else
        {
          dmsg2 ("cube long:  %d --> %d\n", arg_long, ret_long);
          arg_long = arg_long * arg_long * arg_long;
          if (arg_long != ret_long) {
            ACE_OS::printf ("** cube_long(%ld) ERROR (--> %ld)\n",
                            (CORBA_Long) func (i), ret_long);
            error_count++;
          }
        }

      //
      // Cube a "struct" ...
      //
      Cubit_Many	arg_struct, *ret_struct;
       
      call_count++;
       
      arg_struct.l = func (i);
      arg_struct.s = func (i);
      arg_struct.o = func (i);
       
      ret_struct = aCubit->Cubit_cube_struct (arg_struct, env);
      if (env.exception () != 0)
        {
          print_exception (env.exception (), "from cube_struct");
          error_count++;
        }
      else
        {
          dmsg ("cube struct ...");
          arg_struct.l = arg_struct.l * arg_struct.l * arg_struct.l;
          arg_struct.s = arg_struct.s * arg_struct.s * arg_struct.s;
          arg_struct.o = arg_struct.o * arg_struct.o * arg_struct.o;
          
          if (arg_struct.l != ret_struct->l
              || arg_struct.s != ret_struct->s
              || arg_struct.o != ret_struct->o)
            {
              ACE_OS::printf ("** cube_struct ERROR\n");
              error_count++;
            }
          delete ret_struct;
        }
       
    }
    
  after = ACE_OS::gettimeofday();
    
  if (call_count > 0) 
    {
      if (error_count == 0)
        {
          ACE_Time_Value diff = after - before;
          unsigned long	us = diff.sec() * 1000 * 1000 + diff.usec();
          
          us /= call_count;
          
          if (us > 0)
            ACE_OS::printf ("cube average call ACE_OS::time\t= %ld.%.03ldms, \t"
                            "%ld calls/second\n",
                            us / 1000, us % 1000,
                            1000000L / us);
        }

      ACE_OS::printf ("%d calls, %d errors\n", call_count, error_count);
    }

  //
  // Simple test for DII:  call "cube_struct".  (It's not timed
  // since the copious mallocation of DII would bias numbers against
  // typical stub-based calls.)
  //
  do {
    //
    // Create the request ...
    //
    CORBA_Request_ptr	req;
       
    req = objref->_request ((const CORBA_String) "cube_struct", env);
    if (env.exception () != 0) {
      print_exception (env.exception (), "DII request create");
      break;
    }

    //
    // ... initialise the argument list and result ...
    //
    Cubit_Many	arg, *result;
       
    arg.o = 3; arg.l = 5; arg.s = -7;
       
    CORBA_Any	tmp_arg (TC_Cubit_Many, &arg, CORBA_B_FALSE);
       
    req->arguments ()->add_value (0, tmp_arg, CORBA_ARG_IN, env);
    if (env.exception () != 0) {
      print_exception (env.exception (), "DII request arg add");
      CORBA_release (req);
      break;
    }
       
    req->result ()->value ()
      ->replace (TC_Cubit_Many, 0, CORBA_B_TRUE, env);
    if (env.exception () != 0) {
      print_exception (env.exception (), "DII request result type");
      CORBA_release (req);
      break;
    }

    //
    // Make the invocation, verify the result
    //
    req->invoke ();
    if (req->env ()->exception () != 0) {
      print_exception (req->env ()->exception (), "DII invoke");
      CORBA_release (req);
      break;
    }
       
    result = (Cubit_Many *) req->result ()->value ()->value ();
       
    if (result->o != 27 || result->l != 125 || result->s != -343)
      ACE_OS::fprintf (stderr, "DII cube_struct -- bad results\n");
    else
      dmsg ("DII cube_struct ... success!!");
       
    CORBA_release (req);
       
  } while (0);

  //
  // Two more tests, using the "cube_union" function
  //
  cube_union_dii(call_count, error_count, objref, env);
  if (env.exception () != 0)
    error_count++;
    
  cube_union_stub(i, call_count, error_count, objref, env);
  if (env.exception () != 0)
    error_count++;
    
  if (exit_later) {
    aCubit->Cubit_please_exit (env);
    dexc (env, "server, please ACE_OS::exit");
  }
    
  CORBA_release (objref);
    
  return (error_count == 0) ? 0 : 1;
}


static void
cube_union_stub(unsigned          i,
                unsigned          &call_count,
                unsigned          &error_count,
                CORBA_Object_ptr  objref,
                CORBA_Environment &env)
{
   Cubit_ptr aCubit = Cubit::_narrow(objref);
   //
   // Cube a "union" ...
   //
   Cubit_oneof    u, *r;
   
   call_count++;
   
   u._disc = e_2nd;
   u.l = 3;
   
   r = aCubit->Cubit_cube_union (u, env);
   if (env.exception () != 0) {
      print_exception (env.exception (), "from cube_union");
      error_count++;
   } else {
      dmsg ("cube union ...");
      u.l = u.l  * u.l * u.l ;
      
      if (u.l != r->l) {
         ACE_OS::printf ("** cube_union ERROR\n");
         error_count++;
      }
      
      delete r;
   }
   
   //
   // Cube another "union" which uses the default arm ...
   //
   call_count++;
   
   u._disc = e_5th;
   u.cm.l = func (i);
   u.cm.s = func (i);
   u.cm.o = func (i);
   
   u.cm.l = 7;
   u.cm.s = 5;
   u.cm.o = 3;
   
   r = aCubit->Cubit_cube_union (u, env);
   if (env.exception () != 0) {
      print_exception (env.exception (), "from cube_union");
      error_count++;
   } else {
      dmsg ("cube union ...");
      u.cm.l = u.cm.l * u.cm.l * u.cm.l;
      u.cm.s = u.cm.s * u.cm.s * u.cm.s;
      u.cm.o = u.cm.o * u.cm.o * u.cm.o;
      
      if (u.cm.l != r->cm.l
         || u.cm.s != r->cm.s
         || u.cm.o != r->cm.o) {
         ACE_OS::printf ("** cube_union ERROR\n");
         error_count++;
      }
      
      delete r;
   }
}


static void
cube_union_dii (unsigned          &call_count,
                unsigned          &error_count,
                CORBA_Object_ptr  objref,
                CORBA_Environment &env)
{
   //
   // Create the request ...
   //
   CORBA_Request_ptr	req;
   
   call_count++;

   req = objref->_request ((const CORBA_String) "cube_union", env);
   if (env.exception () != 0) {
      error_count++;
      
      print_exception (env.exception (), "cube_union_dii request create");
      return;
   }

   //
   // ... initialise the argument list and result ...
   //
   Cubit_oneof	u, *r;
   
   u._disc = e_3rd;
   u.cm.l = 5;
   u.cm.s = -7;
   u.cm.o = 3;

   CORBA_Any	tmp_arg (TC_Cubit_oneof, &u, CORBA_B_FALSE);
   
   req->arguments ()->add_value (0, tmp_arg, CORBA_ARG_IN, env);
   if (env.exception () != 0) {
      error_count++;
      print_exception (env.exception (), "cube_union_dii request arg add");
      CORBA_release (req);
      return;
   }
	
   req->result ()->value ()->replace (TC_Cubit_oneof, 0, CORBA_B_TRUE, env);
   if (env.exception () != 0) {
      error_count++;
      print_exception (env.exception (), "cube_union_dii result type");
      CORBA_release (req);
      return;
   }

   //
   // Make the invocation, verify the result
   //
   req->invoke ();
   if (req->env ()->exception () != 0) {
      error_count++;
      print_exception (req->env ()->exception (),"cube_union_dii invoke");
      CORBA_release (req);
      return;
   }

   r = (Cubit_oneof *) req->result ()->value ()->value ();
   
   if (r->cm.o != 27 || r->cm.l != 125 || r->cm.s != -343) {
      error_count++;
      ACE_OS::fprintf (stderr, "cube_union_dii -- bad results\n");
   }
   else
      dmsg ("cube_union_dii ... success!!");
   
   CORBA_release (req);
}