summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/Forwarding/server.cpp
blob: 5473a396bb01c4f0383b392c5fec444e186fa00e (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
// $Id$

// ========================================================================
// = LIBRARY
//    TAO/tests/POA/Forwarding
//
// = FILENAME
//    server.cpp
//
// = DESCRIPTION
//
// = AUTHOR
//    Irfan Pyarali and Michael Kircher
// ========================================================================

#include "tao/corba.h"
#include "ace/Read_Buffer.h"
#include "MyFooServant.h"

ACE_RCSID(Forwarding, server, "$Id$")

static char *first_foo_forward_to_IOR_ = 0;

static char *second_foo_forward_to_IOR_ = 0;

static FILE *first_foo_ior_output_file_;

static FILE *second_foo_ior_output_file_;

static MyFirstFooServant *myFirstFooServant_ptr;
static MyFooServantLocator *myFooServantLocator_ptr;


static int
read_ior (char *filename,
          const u_int foo_number)
{
  // Open the file for reading.
  ACE_HANDLE f_handle_ = ACE_OS::open (filename,0);
  
  if (f_handle_ == ACE_INVALID_HANDLE)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Unable to open %s for writing: %p\n",
                       filename),
                       -1);

  ACE_Read_Buffer ior_buffer (f_handle_);

  if (foo_number == 1)
    {
      first_foo_forward_to_IOR_ = ior_buffer.read ();

      if (first_foo_forward_to_IOR_ == 0)
	ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to allocate memory to read ior: %p\n"),
			  -1);  
    }
  else if (foo_number == 2)
    {
      second_foo_forward_to_IOR_ = ior_buffer.read ();

      if (second_foo_forward_to_IOR_ == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Unable to allocate memory to read ior: %p\n"),
                          -1);
    }
  
  if (foo_number == 1)
    ACE_DEBUG ((LM_DEBUG,
                "POA approach: Read ior: %s\n",
                first_foo_forward_to_IOR_));  
  else if (foo_number == 2)
    ACE_DEBUG ((LM_DEBUG,
                "Locator approach: Read ior: %s\n",
                second_foo_forward_to_IOR_));  
  return 0;
}


static int
parse_args (int argc, char **argv)
{
  ACE_Get_Opt get_opts (argc, argv, "f:g:k:l:o:O:p:");
  int c;
  int result; 
  
  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'f': // read the IOR from the file.
        result = read_ior (get_opts.optarg,1);
        if (result < 0)
          ACE_ERROR_RETURN ((LM_ERROR,
			     "Unable to read ior from %s : %p\n",
			     get_opts.optarg),
			    -1);
        break;
      case 'g': // read the IOR from the file.
        result = read_ior (get_opts.optarg,2);
        if (result < 0)
          ACE_ERROR_RETURN ((LM_ERROR,
			     "Unable to read ior from %s : %p\n",
			     get_opts.optarg),
			    -1);
        break;
      case 'k':
        first_foo_forward_to_IOR_ = get_opts.optarg;
        break;
      case 'l':
        second_foo_forward_to_IOR_ = get_opts.optarg;
        break;
      case 'o': // output the IOR to a file.
        first_foo_ior_output_file_ = ACE_OS::fopen (get_opts.optarg, "w");
        if (first_foo_ior_output_file_ == 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Unable to open %s for writing: %p\n",
                             get_opts.optarg), -1);
        break; 
      case 'p': // output the IOR to a file.
        second_foo_ior_output_file_ = ACE_OS::fopen (get_opts.optarg, "w");
        if (second_foo_ior_output_file_ == 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Unable to open %s for writing: %p\n",
                             get_opts.optarg), -1);
        break; 
      case 'O': 
        break;
      case '?':  
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "\nusage:  %s \n"
                           "-f forward_to_IOR_file (first foo)\n"
                           "-g forward_to_IOR_file (second foo)\n"
                           "-k forward_to_IOR (first foo)\n"
                           "-l forward_to_IOR (second foo)\n"
                           "-o file_for_IOR (first foo)\n"
                           "-p file_for_IOR (second foo)\n"
                           "\n",
                           argv [0]),
			  -1);
      }
  
  // Indicates successful parsing of command line.
  return 0;
}

static void 
get_forward_reference (char *IOR,
                       CORBA::Object_var &forward_location_var,
                       CORBA::ORB_ptr orb_ptr,
                       CORBA::Environment &env)
{
  if (IOR != 0)
    {
      forward_location_var = orb_ptr->string_to_object (IOR, env);
            
      if (env.exception () != 0)
        {
          env.print_exception ("ORB::string_to_object");
          return;
        }
      
      if (CORBA::is_nil (forward_location_var.in ()))
        ACE_DEBUG ((LM_DEBUG,
                    "Error: Forward_to location is wrong\n"));
    }
}


// Documentation !!!!  first_POA will contain an object which will
// use the POA directly to do forwarding

int setup_first_poa (PortableServer::POA_ptr root_poa_ptr,
                     PortableServer::POAManager_ptr poa_manager_ptr, 
                     PortableServer::POA_var &first_poa_var)
{
  CORBA::Environment env;

  // Policies for the childPOA to be created.
  CORBA::PolicyList policies (2); 
  policies.length (2);

  // The next two policies are common to both
  
  // Id Assignment Policy
  policies[0] = root_poa_ptr->create_id_assignment_policy (PortableServer::USER_ID, env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_id_assignment_policy");
      return -1;
    }
  
  // Lifespan policy
  policies[1] = root_poa_ptr->create_lifespan_policy (PortableServer::PERSISTENT, env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_lifespan_policy");
      return -1;
    }

    
  ACE_CString name = "firstPOA";
    
  // Create firstPOA as the child of RootPOA with the above policies
  // firstPOA will use SERVANT_ACTIVATOR because of RETAIN policy.
  first_poa_var = root_poa_ptr->create_POA (name.c_str (),
                                            poa_manager_ptr,
                                            policies,
                                            env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_POA");
      return -1;
    }

  return 0;
}


// second_POA will contain an object which will use the
// Servant_Locator to do forwarding

int setup_second_poa (PortableServer::POA_ptr root_poa,
                      PortableServer::POAManager_ptr poa_manager_ptr, 
                      PortableServer::POA_var &second_poa_var)
{
  CORBA::Environment env;

  // Policies for the childPOA to be created.
  CORBA::PolicyList policies (4); 
  policies.length (4);

  // The next two policies are common to both
  
  // Id Assignment Policy
  policies[0] = root_poa->create_id_assignment_policy (PortableServer::USER_ID, env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_id_assignment_policy");
      return -1;
    }
  
  // Lifespan policy
  policies[1] = root_poa->create_lifespan_policy (PortableServer::PERSISTENT, env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_lifespan_policy");
      return -1;
    }
        
  // Tell the POA to use a servant manager
  policies[2] =
  root_poa->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER, env);
  if (env.exception () != 0)
    {
	    env.print_exception ("PortableServer::POA::create_request_processing_policy");
	    return -1;
    }
    
  // Servant Retention Policy -> Use a locator
  policies[3] =
  root_poa->create_servant_retention_policy (PortableServer::NON_RETAIN, env);
  if (env.exception () != 0)
    {
	    env.print_exception ("PortableServer::POA::create_servant_retention_policy");
	    return -1; 
    }
    
  ACE_CString name = "secondPOA";
    
  // Create secondPOA as child of RootPOA with the above policies
  // secondPOA will use a SERVANT_LOCATOR because of NON_RETAIN
  // policy.
  second_poa_var = root_poa->create_POA (name.c_str (),
	                      			       poa_manager_ptr,
				                             policies,
				                             env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_POA");
      return -1;
    }
  
  // Creation of childPOAs is over. Destroy the Policy objects.
  for (CORBA::ULong i = 0;
       i < policies.length () && env.exception () == 0;
       ++i)
    {
      CORBA::Policy_ptr policy = policies[i];
      policy->destroy (env);
    }

  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::destroy");
      return -1;
    }
  return 0;
}

int create_first_servant (PortableServer::POA_ptr first_poa_ptr,
                          CORBA::ORB_ptr orb_ptr)
{
  CORBA::Environment env;

  // Get the forward_to reference to feed it into object
  // implementations
  CORBA::Object_var first_foo_forward_to_var;

  get_forward_reference (first_foo_forward_to_IOR_,
                         first_foo_forward_to_var,
                         orb_ptr,
                         env);

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

  ACE_NEW_RETURN (myFirstFooServant_ptr, 
                  MyFirstFooServant (orb_ptr,
                                     first_poa_ptr, 
                                     27,
                                     first_foo_forward_to_var.in ()),
                  -1);

  // Create ObjectId and use that ObjectId to activate the
  // first_foo_impl object.
  PortableServer::ObjectId_var first_foo_oid_var =
    PortableServer::string_to_ObjectId ("firstFoo");

  first_poa_ptr->activate_object_with_id (first_foo_oid_var.in (),
                                          myFirstFooServant_ptr,
                                          env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::activate_object_with_id");
      return -1;
    }
  
  // Get Object reference for first_foo_impl object.
  Foo_var first_foo_var = myFirstFooServant_ptr->_this (env);

  if (env.exception () != 0)
    {
      env.print_exception ("POA_Foo::_this");
      return -1;
    }
  
  // Stringyfy the object reference and print it out.
  CORBA::String_var first_foo_ior_var =
    orb_ptr->object_to_string (first_foo_var.in (), env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB::object_to_string");
      return -1;
    }
  
  ACE_DEBUG ((LM_DEBUG,
              "POA approach: Own IOR: %s\n",
              first_foo_ior_var.in ()));
  
  if (first_foo_ior_output_file_)
    {
      ACE_OS::fprintf (first_foo_ior_output_file_,
                       "%s",
                       first_foo_ior_var.in ());
      ACE_OS::fclose (first_foo_ior_output_file_);
      
      ACE_DEBUG ((LM_DEBUG, "POA approach: Wrote IOR to a file.\n"));
    }
  return 0;
}


int 
create_second_servant (PortableServer::POA_ptr second_poa_ptr,
                       CORBA::ORB_ptr orb_ptr)
{
  CORBA::Environment env;
  
  CORBA::Object_var second_foo_forward_to_var;
  get_forward_reference (second_foo_forward_to_IOR_,
                         second_foo_forward_to_var,
                         orb_ptr,
                         env);
  if (env.exception () != 0)
    {
      env.print_exception ("get_forward_reference");
      return -1;
    }

  // instantiate the servant locator and set it for the second child
  // POA The locator gets to know where to forward to
  
  ACE_NEW_RETURN (myFooServantLocator_ptr, 
                  MyFooServantLocator (orb_ptr, 
                                       second_foo_forward_to_var.in ()),
                  -1);
  PortableServer::ServantLocator_var servant_locator_var = 
    myFooServantLocator_ptr->_this (env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POAManager::_this");
      return -1;
    }
  
  // Set MyFooServantLocator object as the servant Manager of
  // secondPOA.

  second_poa_ptr->set_servant_manager (servant_locator_var.in (), env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POAManager::set_servant_manager");
      return -1;
    }
  
  // Create the second MyFooServant
  //            ======
  
  // Try to create a reference with user created ID in second_poa
  // which uses MyFooServantLocator.
  
  PortableServer::ObjectId_var second_foo_oid_var =
    PortableServer::string_to_ObjectId ("secondFoo");
  
  CORBA::Object_var second_foo_var =
    second_poa_ptr->create_reference_with_id (second_foo_oid_var.in (),
                                          "IDL:Foo:1.0", env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::create_reference_with_id");
      return -1;
    }
  
  // Invoke object_to_string on the references created in firstPOA and
  // secondPOA.

  CORBA::String_var second_foo_ior_var = 
    orb_ptr->object_to_string (second_foo_var.in (), env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB::object_to_string");
      return -1;
    }
  
  ACE_DEBUG ((LM_DEBUG,
              "Locator approach: Own IOR: %s\n",
              second_foo_ior_var.in ()));
  
  
  if (second_foo_ior_output_file_)
    {
      ACE_OS::fprintf (second_foo_ior_output_file_,
                       "%s",
                       second_foo_ior_var.in ());
      ACE_OS::fclose (second_foo_ior_output_file_);

      ACE_DEBUG ((LM_DEBUG, "Locator approach: Wrote IOR to a file.\n"));
    }
  
  return 0;
}

int
main (int argc, char **argv)
{
  // @@ Michael, this function is WAY too long!  Can you please break
  // it up into a number of subfunctions and put them into a class or
  // something?!  It's impossible to tell what's going on here!
  int result = parse_args (argc, argv);

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

  CORBA::Environment env;
  
  // Initialize the ORB first.
  CORBA::ORB_var orb_var = CORBA::ORB_init (argc, argv, 0, env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("CORBA::ORB_init");
      return -1;
    }

  // Obtain the RootPOA.
  CORBA::Object_var obj_var = orb_var->resolve_initial_references ("RootPOA");
  
  // Get the POA_var object from Object_var.
  PortableServer::POA_var root_poa_var =
    PortableServer::POA::_narrow (obj_var.in (), env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::_narrow");
      return -1;
    }
  
  // Get the POAManager of the RootPOA.
  PortableServer::POAManager_var poa_manager_var =
    root_poa_var->the_POAManager (env);
  
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::the_POAManager");
      return -1;
    }
  
  PortableServer::POA_var first_poa_var;
  PortableServer::POA_var second_poa_var;

  if (setup_first_poa (root_poa_var.in(), poa_manager_var.in(), first_poa_var) == -1)
    return -1; 
  if (setup_second_poa (root_poa_var.in(), poa_manager_var.in(), second_poa_var) == -1)
    return -1;  
    
  // Create the first MyFooServant
  //            =====
  if (create_first_servant (first_poa_var.in(), orb_var.in()) == -1)
    return -1;

  if (create_second_servant (second_poa_var.in(), orb_var.in()) == -1)
    return -1;    
  
  poa_manager_var->activate (env);
  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POAManager::activate");
      return -1;
    }
  
  if (orb_var->run () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%p\n",
                       "CORBA::ORB::run"),
                      -1);

  
  // Destroy RootPOA. (Also destroys childPOA)
  root_poa_var->destroy (CORBA::B_TRUE,
                         CORBA::B_TRUE,
                         env);
  delete myFirstFooServant_ptr;
  delete myFooServantLocator_ptr;

  if (env.exception () != 0)
    {
      env.print_exception ("PortableServer::POA::destroy");
      return -1;
    }

  
  return 0;
}