summaryrefslogtreecommitdiff
path: root/TAO/tests/Cubit/TAO/DII_Cubit/clnt.cpp
blob: 5bb1e2dc9955a1f62e05841f6ae823345cf83738 (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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/tests/DII_Cubit
//
// = FILENAME
//    clnt.cpp
//
// = DESCRIPTION
//      This class implements a simple "cube" CORBA client for the Cubit
//      example using stubs generated by the TAO ORB IDL compiler.
//
// = AUTHORS
//      Andy Gokhale, Sumedh Mungee, and Sergio Flores-Gaitan
//
// ============================================================================
#include "ace/Profile_Timer.h"
#include "ace/Get_Opt.h"
#include "tao/corba.h"
#include "cubitC.h"
#include "clnt.h"

// Constructor.

Cubit_Client::Cubit_Client (void)
  : loop_count_ (250),
    exit_later_ (0),
    objref_ (CORBA::Object::_nil ()),
    cubit_ (Cubit::_nil ()),
    orb_ptr_ (0),
    call_count_ (0),
    error_count_ (0),
    cubit_key_ ("key00"),
    hostname_ (ACE_DEFAULT_SERVER_HOST),
    portnum_ (TAO_DEFAULT_SERVER_PORT)
{
}

// destructor

Cubit_Client::~Cubit_Client (void)
{
  // Free resources
  CORBA::release (this->cubit_);
  CORBA::release (this->objref_);
  CORBA::release (this->orb_ptr_);
}

// Simple function that returns the substraction of 117 from the
// parameter.

int
Cubit_Client::func (u_int i)
{
  return i - 117;
}

// Parses the command line arguments and returns an error status.

int
Cubit_Client::parse_args (void)
{
  ACE_Get_Opt opts (argc_, argv_, "dn:k:h:p:x");
  int c;
   
  while ((c = opts ()) != -1)
    switch (c) 
      {
      case 'd':  // debug flag
	TAO_debug_level++;
	break;
      case 'n':			// loop count
	loop_count_ = (u_int) ACE_OS::atoi (opts.optarg);
	break;
      case 'h':
        hostname_ = ACE_OS::strdup (opts.optarg);
        break;
      case 'p':
        portnum_ = ACE_OS::atoi (opts.optarg);
        break;
      case 'k':			// stringified objref
        cubit_key_ = ACE_OS::strdup (opts.optarg);
        break;
      case 'x':
	exit_later_++;
	break;
      case '?':
      default:
	ACE_ERROR_RETURN ((LM_ERROR,
			   "usage:  %s"
			   " [-d]"
			   " [-n loopcount]"
                           " [-k cubit-obj-ref-key]"
                           " [-h hostname]"
                           " [-p port]"
			   " [-x]"
			   "\n", argv_ [0]
			   ), -1);
      }
   
  return 0;  // Indicates successful parsing of command line
}

void
Cubit_Client::cube_union_stub (void)
{
  this->cubit_ = Cubit::_narrow (this->objref_);

  Cubit_oneof u, *r;
   
  this->call_count_++;
   
  u._disc = e_2nd;
  u.l = 3;
   
  // Cube a "union" ...
  r = this->cubit_->Cubit_cube_union (u, this->env_);

  if (this->env_.exception () != 0) 
    {
      this->env_.print_exception ("from cube_union");
      this->error_count_++;
    } 
  else 
    {
      dmsg ("cube union ...");
      u.l = u.l * u.l * u.l ;
      
      if (u.l != r->l) 
	{
	  ACE_DEBUG ((LM_DEBUG, "** cube_union ERROR\n"));
	  this->error_count_++;
	}
      
      delete r;
    }
   
  this->call_count_++;
   
  u._disc = e_5th;
#if 0
  u.cm.l = this->func (i);
  u.cm.s = this->func (i);
  u.cm.o = this->func (i);
#endif
   
  u.cm.l = 7;
  u.cm.s = 5;
  u.cm.o = 3;
   
  // Cube another "union" which uses the default arm ...
  r = this->cubit_->Cubit_cube_union (u, this->env_);

  if (this->env_.exception () != 0) 
    {
      this->env_.print_exception ("from cube_union");
      this->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_DEBUG ((LM_DEBUG, "** cube_union ERROR\n"));
	  this->error_count_++;
	}
      
      delete r;
    }
}

void
Cubit_Client::cube_union_dii (void)
{
  // Create the request ...
  CORBA::Request_ptr req;
   
  this->call_count_++;

  req = this->objref_->_request ((CORBA::String) "cube_union", this->env_);

  if (this->env_.exception () != 0) 
    {
      this->error_count_++;

      this->env_.print_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, this->env_);

  if (this->env_.exception () != 0) 
    {
      this->error_count_++;
      this->env_.print_exception ("cube_union_dii request arg add");
      CORBA::release (req);
      return;
    }
	
  req->result ()->value ()->replace (TC_Cubit_oneof, 0, CORBA::B_TRUE, this->env_);

  if (this->env_.exception () != 0) 
    {
      this->error_count_++;
      this->env_.print_exception ("cube_union_dii result type");
      CORBA::release (req);
      return;
    }

  // Make the invocation, verify the result.

  req->invoke ();

  if (req->env ()->exception () != 0) 
    {
      this->error_count_++;
      req->env ()->print_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) 
    {
      this->error_count_++;
      ACE_ERROR ((LM_ERROR, "cube_union_dii -- bad results\n"));
    }
  else
    dmsg ("cube_union_dii ... success!!");
   
  CORBA::release (req);
}

// Cube an octet

void
Cubit_Client::cube_octet (int i)
{
  CORBA::Octet arg_octet = this->func (i);

   // Cube an octet.
  CORBA::Octet ret_octet = this->cubit_->Cubit_cube_octet (arg_octet, this->env_);

  this->call_count_++;

  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("from cube_octet");
      this->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_DEBUG ((LM_DEBUG,
		    "** cube_octet (%d) ERROR (--> %d)\n",
		    (CORBA::Octet) this->func (i),
		    ret_octet));
	this->error_count_++;
      }
    }
}


// calculate the cube from a long

void
Cubit_Client::cube_long (int i)
{
  CORBA::Long arg_long = this->func (i);

  // Cube a long.
  CORBA::Long ret_long = this->cubit_->Cubit_cube_long (arg_long, this->env_);

  this->call_count_++;

  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("from cube_long");
      this->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_ERROR ((LM_ERROR,
		    "** cube_long (%ld) ERROR (--> %ld)\n",
		    (CORBA::Long) this->func (i),
		    ret_long));
	this->error_count_++;
      }
    }
}

// Cube a short.

void
Cubit_Client::cube_short (int i)
{
  CORBA::Short arg_short = this->func (i);
  // Cube a short.
  CORBA::Short ret_short = cubit_->Cubit_cube_short (arg_short, this->env_);

  this->call_count_++;

  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("from cube_short");
      this->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_ERROR ((LM_ERROR, "** cube_short (%d) ERROR (--> %d)\n",
		      (CORBA::Short) this->func (i),
		      ret_short));
	  this->error_count_++;
	}
    }
}

// Cube the numbers in a struct

void
Cubit_Client::cube_struct (int i)
{
  Cubit_Many arg_struct;
  Cubit_Many *ret_struct;

  this->call_count_++;

  arg_struct.l = this->func (i);
  arg_struct.s = this->func (i);
  arg_struct.o = this->func (i);

  // Cube a "struct" ...
  ret_struct = this->cubit_->Cubit_cube_struct (arg_struct, this->env_);

  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("from cube_struct");
      this->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_ERROR ((LM_ERROR, "** cube_struct ERROR\n"));
	  this->error_count_++;
	}
    }
  delete ret_struct;
}

int
Cubit_Client::init (int argc, char **argv)
{
  this->argc_ = argc;
  this->argv_ = argv;

  // Parse command line and verify parameters.
  if (this->parse_args () == -1)
    return -1;
  
  // construct the TAO ior for the Cubit object reference
  ACE_OS::sprintf (this->buf_, 
		   "iiop:1.0//%s:%d/%s", 
		   this->hostname_, 
		   this->portnum_, 
		   this->cubit_key_);

  // Retrieve the ORB.
  this->orb_ptr_ = CORBA::ORB_init (this->argc_,
                                    this->argv_,
                                    "internet",
                                    this->env_);

  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("ORB initialization");
      return -1;
    }

  this->objref_ = this->orb_ptr_->string_to_object ((CORBA::String) this->buf_, 
						    this->env_);

  if (this->env_.exception () != 0)
    {
      this->env_.print_exception ("string2object");
      return 1;
    }

  if (CORBA::is_nil (this->objref_) == CORBA::B_TRUE)
    ACE_ERROR_RETURN ((LM_ERROR,
		       "%s:  must identify non-null target objref\n",
		       this->argv_ [0]),
		      -1);

  // Narrow the CORBA::Object reference to the stub object, checking
  // the type along the way using _is_a.
  this->cubit_ = Cubit::_narrow (this->objref_);

  if (this->cubit_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
		       " (%P|%t) Unable to narrow object reference to a Cubit_ptr.\n"),
		      -1);

  return 0;
}

// Simple test for DII: call "cube_struct". 

void
Cubit_Client::cube_struct_dii (void)
{
  // Create the request ...

  CORBA::Request_ptr	req = 
    this->objref_->_request ((CORBA::String) "cube_struct", this->env_);

  if (this->env_.exception () != 0) 
    {
      this->env_.print_exception ("DII request create");
      return;
    }

  // ... 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, this->env_);

  if (this->env_.exception () != 0) 
    {
      this->env_.print_exception ("DII request arg add");
      CORBA::release (req);
      return;
    }
       
  req->result ()->value ()->replace (TC_Cubit_Many, 0, CORBA::B_TRUE, this->env_);

  if (this->env_.exception () != 0) 
    {
      this->env_.print_exception ("DII request result type");
      CORBA::release (req);
      return;
    }

  // Make the invocation, verify the result

  this->call_count_++;
  req->invoke ();

  if (req->env ()->exception () != 0) 
    {
      req->env ()->print_exception ("DII invoke");
      CORBA::release (req);
      return;
    }
       
  result = (Cubit_Many *) req->result ()->value ()->value ();
       
  if (result->o != 27 || result->l != 125 || result->s != -343)
    ACE_ERROR ((LM_ERROR, "DII cube_struct -- bad results\n"));
  else
    dmsg ("DII cube_struct ... success!!");
       
  CORBA::release (req);
}

// prints time statistics

void
Cubit_Client::print_stats (const char *call_name, ACE_Profile_Timer::ACE_Elapsed_Time &elapsed_time)
{
  double tmp;

  ACE_DEBUG ((LM_DEBUG,
	      "%s:\n",
	      call_name));
  
  if (this->call_count_ > 0  &&  this->error_count_ == 0)
    {
      tmp = 1000 / elapsed_time.real_time;

      elapsed_time.real_time *= ACE_ONE_SECOND_IN_MSECS;
      elapsed_time.user_time *= ACE_ONE_SECOND_IN_MSECS;
      elapsed_time.system_time *= ACE_ONE_SECOND_IN_MSECS;

      elapsed_time.real_time /= this->call_count_;
      elapsed_time.user_time /= this->call_count_;
      elapsed_time.system_time /= this->call_count_;

      tmp = 1000 / elapsed_time.real_time;

      ACE_DEBUG ((LM_DEBUG,
		  "\treal_time\t= %0.06f ms, \n\t"
		  "user_time\t= %0.06f ms, \n\t"
		  "system_time\t= %0.06f ms\n"
		  "\t%0.00f calls/second\n",
		  elapsed_time.real_time < 0.0? 0.0:elapsed_time.real_time,
		  elapsed_time.user_time < 0.0? 0.0:elapsed_time.user_time,
		  elapsed_time.system_time < 0.0? 0.0:elapsed_time.system_time,
		  tmp < 0.0? 0.0 : tmp));
    }
  else
    {
      ACE_ERROR ((LM_ERROR, "\tNo time stats printed.  Call count zero or error ocurred.\n"));
    }

  ACE_DEBUG ((LM_DEBUG,
	      "\t%d calls, %d errors\n",
	      this->call_count_,
	      this->error_count_));
}

// Execute client example code.

int
Cubit_Client::run (void)
{
  u_int i;

  ACE_Profile_Timer timer;
  ACE_Profile_Timer::ACE_Elapsed_Time elapsed_time;

  //  ACE_Time_Value before;

  // We start an ACE_Profile_Timer here...
  timer.start ();

  // Make the calls in a loop.
  for (i = 0; i < this->loop_count_; i++)
    {
      this->cube_short (i);
      this->cube_octet (i);
      this->cube_long (i);
      this->cube_struct (i);
    }

  // stop the timer.
  timer.stop ();

  timer.elapsed_time (elapsed_time);  
  // compute call average call time.
  this->print_stats ("cube average call", elapsed_time);

  // 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).
  this->call_count_ = 0;
  this->error_count_ = 0;

  timer.start ();
  // Make the calls in a loop.
  for (i = 0; i < this->loop_count_; i++)
    {
      this->cube_struct_dii ();
    }
  timer.stop ();

  timer.elapsed_time (elapsed_time);  
  // compute call average call time.
  this->print_stats ("cube_struct_dii call", elapsed_time);

  this->call_count_ = 0;
  this->error_count_ = 0;

  timer.start ();
  // Make the calls in a loop.
  for (i = 0; i < this->loop_count_; i++)
    {
      this->cube_union_stub ();
    }
  timer.stop ();

  timer.elapsed_time (elapsed_time);  
  // compute call average call time.
  this->print_stats ("cube_union_stub call", elapsed_time);

  this->call_count_ = 0;
  this->error_count_ = 0;

  timer.start ();
  // Make the calls in a loop.
  for (i = 0; i < this->loop_count_; i++)
    {
      this->cube_union_dii ();
    }
  timer.stop ();

  timer.elapsed_time (elapsed_time);  
  // compute call average call time.
  this->print_stats ("cube_union_dii call", elapsed_time);

  if (this->exit_later_)
    {
      this->cubit_->Cubit_please_exit (this->env_);
      dexc (this->env_, "server, please ACE_OS::exit");
    }

  return this->error_count_ == 0 ? 0 : 1;
}


// This function runs the test.

int
main (int argc, char **argv)
{
  Cubit_Client cubit_client;
 
  if (cubit_client.init (argc, argv) == -1)
    return 1;
  else
    return cubit_client.run ();
}