summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Bug_2248_Regression/client.cpp
blob: 2dc93f7276566bf4a65933798ad1572ba58c8ae4 (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
// $Id$
#include "client.h"
#include "Client_i.h"
#include "testC.h"
#include "ace/Get_Opt.h"
#include "ace/Read_Buffer.h"
#include "tao/IORManipulation/IORManip_Loader.h"
#include "tao/PortableServer/PortableServer.h"
#include "orbsvcs/FaultTolerance/FT_Service_Activate.h"
#include "orbsvcs/FaultTolerance/FT_IOGR_Property.h"
#include "ace/OS_NS_stdio.h"

// Files which have the IOR
const char *first_ior = 0;
const char *first_key = 0;
const char *second_ior = 0;
const char *second_key = 0;
const char *first_iogr_file = 0;
const char *second_iogr_file = 0;

// Objects
CORBA::Object_var object_primary = 0;
CORBA::Object_var object_secondary = 0;

// Reference to the IOR manipulator

int
parse_args (int argc, char* argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "a:k:b:l:g:h:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'a':
        first_ior = get_opts.opt_arg ();
        break;
      case 'k':
	first_key = get_opts.opt_arg ();
	break;
      case 'b':
        second_ior = get_opts.opt_arg ();
        break;
      case 'l':
	second_key = get_opts.opt_arg ();
	break;
      case 'g':
        first_iogr_file = get_opts.opt_arg ();
        break;
      case 'h':
        second_iogr_file = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-a <iorfile1> -k <key1> "
                           "-b <iorfile2> -l <key2> "
                           "-g <iogr v10 output file> "
                           "-h <iogr v11 output file> "
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}


int
main (int argc,
      char *argv[])
{

  Manager manager;

  try
    {
      // Initilaize the ORB, POA etc.
      manager.init (argc,
                    argv);

      // the command line arguments
      if (parse_args (argc, argv) == -1)
        return -1;

      // Merge the different IORS
      manager.make_merged_iors ();

      // Write IOR to file
      if (manager.write_to_files ())
	      return -1;

      // Client, who is going to use the merged IOR
      // Construct that with the managers ORB
      Client_i client_imp (manager.orb ());
      return client_imp.init ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Caught");
      return -1;
    }

  return 0;
}

Manager::Manager (void)
{
  //no-op
}

void
Manager::init (int& argc,
               char**& argv)
{
  this->orb_ = CORBA::ORB_init (argc,
                                argv,
                                0);

  // Obtain the RootPOA.
  CORBA::Object_var obj_var =
    this->orb_->resolve_initial_references ("RootPOA");

  // Get the POA_var object from Object_var.
  PortableServer::POA_var root_poa_var =
    PortableServer::POA::_narrow (obj_var.in ());

  // Get the POAManager of the RootPOA.
  PortableServer::POAManager_var poa_manager_var =
    root_poa_var->the_POAManager ();

  poa_manager_var->activate ();
}

int
Manager::make_merged_iors (void)
{
  // Get an object reference for the ORBs IORManipultion object!
  CORBA::Object_var IORM =
	  this->orb_->resolve_initial_references (
		  TAO_OBJID_IORMANIPULATION, 0);

  if (CORBA::is_nil (IORM.in()))
	  return -1;

  TAO_IOP::TAO_IOR_Manipulation_var iorm =
	  TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM.in());

  if (CORBA::is_nil (iorm.in()))
	  return -1;

  CORBA::Object_var first = orb_->string_to_object (first_ior);

  if (CORBA::is_nil (first.in()))
	  return -1;

  // Create the list
  TAO_IOP::TAO_IOR_Manipulation::IORList iors (1);
  iors.length(1);
  iors [0] = first;

  merged_set_10_ = iorm->merge_iors (iors);

  if (CORBA::is_nil (merged_set_10_.in()))
    {
      return -1;
    }

  int result = set_properties (iorm.in(), merged_set_10_.in(),
			    first.in(), 10);

  if (-1 == result)
    {
      return -1;
    }

  first = orb_->string_to_object (first_ior);
  if (CORBA::is_nil (first.in()))
    {
      return -1;
    }

  CORBA::Object_var second = orb_->string_to_object (second_ior);

  if (CORBA::is_nil (second.in()))
    {
      return -1;
    }

  // Create the list

  iors.length(2);
  iors [0] = first;
  iors [1] = second;

  merged_set_11_ = iorm->merge_iors (iors);

  if (CORBA::is_nil (merged_set_11_.in()))
    {
      return -1;
    }

  result = set_properties (iorm.in(), merged_set_11_.in(),
			    first.in(), 11);

  if (-1 == result)
    {
      return -1;
    }

  return 0;
}

int
Manager::set_properties (TAO_IOP::TAO_IOR_Manipulation_ptr iorm,
			 CORBA::Object_ptr merged_set,
			 CORBA::Object_ptr primary,
			 CORBA::ULong version)
{
  FT::TagFTGroupTaggedComponent ft_tag_component;

  // Property values

  // Major and Minor revision numbers
  ft_tag_component.component_version.major = (CORBA::Octet) 1;
  ft_tag_component.component_version.minor = (CORBA::Octet) 0;

  // Domain id
  const char *id = "iogr_testing";
  ft_tag_component.group_domain_id = id;

  // Object group id
  ft_tag_component.object_group_id = 10;

  // Version
  ft_tag_component.object_group_ref_version = version;

  // Construct the IOGR Property class
  TAO_FT_IOGR_Property iogr_prop (ft_tag_component);

  // Set the property
  CORBA::Boolean retval = iorm->set_primary (&iogr_prop,
                                             primary,
					     merged_set);

  // Set the primary
  // See we are setting the second ior as the primary
  if (retval != 0)
    {
      retval = iorm->set_property (&iogr_prop,
                                    merged_set);
    }

  return 0;
}

int
Manager::run (void)
{
  try
    {
      this->orb_->run ();
    }
  catch (const CORBA::Exception& ex)
    {
      ACE_ERROR_RETURN ((LM_DEBUG,
                         "Error in run \n"),
                        -1);
    }

  return 0;
}

int Manager::write_to_files ()
{
  if (write_to_file (merged_set_10_.in(), first_iogr_file) ||
      write_to_file (merged_set_11_.in(), second_iogr_file))
    {
      return -1;
    }
  return 0;
}

int
Manager::write_to_file (CORBA::Object_ptr ior, const char* ior_output_file)
{
  CORBA::String_var iorref =
    this->orb_->object_to_string (ior);

  if (ior_output_file != 0)
    {
      FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s",
                           ior_output_file),
                          1);
      ACE_OS::fprintf (output_file, "%s", iorref.in ());
      ACE_OS::fclose (output_file);
    }

  return 0;
}

CORBA::ORB_ptr
Manager::orb (void)
{
  return this->orb_.in ();
}

Client_i::Client_i (CORBA::ORB_ptr orb)
  :orb_ (CORBA::ORB::_duplicate (orb))
{
}

int run_remote_test (Simple_Server_ptr server, const char* execute_key)
{
  char expected[1024], received[1024];
  if (execute_key)
    {
      ACE_OS::sprintf (expected,
		      "remote_call() completed by %s", execute_key);
    }
  else
    {
      ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
    }

  try
    {
      // Make a remote call
      CORBA::String_var s =
        server->remote_call ();

      if (!s.in())
        {
	  ACE_OS::strcpy (received, "(null)");
        }
      else
        {
	  ACE_OS::strcpy (received, s.in());
        }
    }
  catch (const CORBA::TRANSIENT& te)
    {
      ACE_OS::strcpy (received, "CORBA::TRANSIENT");
    }
  catch (const CORBA::COMM_FAILURE& cf)
    {
      ACE_OS::strcpy (received, "CORBA::COMM_FAILURE");
    }

  ACE_DEBUG ((LM_DEBUG,
	      ACE_TEXT("CLIENT> REMOTE   expected: '%s'"),
		       expected));
  if (ACE_OS::strcmp (expected, received))
    {
      ACE_DEBUG ((LM_DEBUG,
		  ACE_TEXT ("... FAIL\n"
			    "CLIENT>          received: '%s'\n"),
		  received));
      return -1;
    }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
  return 0;
}

int run_abort_test (Simple_Server_ptr server,
		    const char* request_key, const char* execute_key)
{
  char expected[1024], received[1024];
  if (execute_key)
    {
      ACE_OS::sprintf (expected,
		       "abort() completed by %s, still_alive=1",
		        execute_key);
    }
  else
    {
      ACE_OS::strcpy (expected, "CORBA::TRANSIENT");
    }

  try
    {
      CORBA::String_var s = server->abort (request_key);

      if (!s.in())
        {
	  ACE_OS::strcpy (received, "(null)");
        }
      else
        {
	  ACE_OS::strcpy (received, s.in());
        }
    }
  catch (const CORBA::TRANSIENT& te)
    {
      ACE_OS::strcpy (received, "CORBA::TRANSIENT");
    }
  catch (const CORBA::COMM_FAILURE& cf)
    {
      ACE_OS::strcpy (received, "CORBA::COMM_FAILURE");
    }

  ACE_DEBUG ((LM_DEBUG,
	      ACE_TEXT("CLIENT> ABORT    expected: '%s'"),
		       expected));

  if (ACE_OS::strcmp (expected, received))
  {
    ACE_DEBUG ((LM_DEBUG,
	        ACE_TEXT ("... FAIL\n"
			  "CLIENT>          received: '%s'\n"),
	        received));
    return -1;
  }

  ACE_DEBUG ((LM_DEBUG, ACE_TEXT("... OK\n")));
  return 0;
}

int run_test (Simple_Server_ptr server)
{
  int rv = run_abort_test (server, first_key, second_key);

  if (rv)
    {
      return rv;
    }

  rv = run_remote_test (server, second_key);

  if (rv)
    {
      return rv;
    }

  rv = run_abort_test (server, second_key, 0);

  return rv;
}

int
Client_i::init (void)
{
  char iogrstr[1024];
  ACE_OS::sprintf (iogrstr, "file://%s", first_iogr_file);

  int argc = 0;
  char **argv = 0;
  this->orb_ = CORBA::ORB_init (argc,
			        argv,
			        0);

  CORBA::Object_var object =
	  this->orb_->string_to_object (iogrstr);


  // Combined IOR stuff
  Simple_Server_var server =
	  Simple_Server::_narrow (object.in ());

  if (CORBA::is_nil (server.in ()))
  {
	  ACE_ERROR ((LM_ERROR,
		      "Object reference <%s> is nil\n",
		      iogrstr));
  }

  // run tests
  int result = run_test (server.in());

  return result;
}