summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Notify/Basic/IdAssignment.cpp
blob: 6d96f4389236b25d638615934f46121b501f4600 (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
// $Id$

#include "ace/Arg_Shifter.h"
#include "ace/Get_Opt.h"
#include "tao/debug.h"
#include "orbsvcs/orbsvcs/CosNamingC.h"
#include "orbsvcs/orbsvcs/CosNotifyCommC.h"
#include "IdAssignment.h"

ACE_RCSID (Notify_Tests, 
           IdAssignment, 
           "$Id$")

IdAssignment::IdAssignment (void)
  : iter_ (3),
    ec_count_ (3),
    consumer_admin_count_ (3),
    supplier_admin_count_ (3)
{
}

IdAssignment::~IdAssignment (void)
{
}

int
IdAssignment::parse_args (int argc,
                          char *argv[])
{
    ACE_Arg_Shifter arg_shifter (argc, argv);

    const char *current_arg = 0;

    while (arg_shifter.is_anything_left ())
    {
      if ((current_arg = arg_shifter.get_the_parameter ("-iter")))
        {
          this->iter_ = ACE_OS::atoi (current_arg);
          // The number of times to repeat the test.
          arg_shifter.consume_arg ();
        }
      if ((current_arg = arg_shifter.get_the_parameter ("-ec_count")))
        {
          this->ec_count_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      if ((current_arg = arg_shifter.get_the_parameter ("-ca_count")))
        {
          this->consumer_admin_count_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      if ((current_arg = arg_shifter.get_the_parameter ("-sa_count")))
        {
          this->supplier_admin_count_ = ACE_OS::atoi (current_arg);
          arg_shifter.consume_arg ();
        }
      else if (arg_shifter.cur_arg_strncasecmp ("-?") == 0)
        {
          ACE_DEBUG((LM_DEBUG,
                     "usage: %s "
                     "-iter <count>",
                     "-ec_count <count>",
                     "-ca_count <count>",
                     "-sa_count <count>\n",
                     argv[0],
                     argv[0]));

          arg_shifter.consume_arg ();

          return -1;
        }
      else
        {
          arg_shifter.ignore_arg ();
        }
    }

  return 0;
}

void
IdAssignment::init (int argc,
                    char *argv[]
                    ACE_ENV_ARG_DECL)
{
  CORBA::ORB_var orb = CORBA::ORB_init (argc,
                                        argv,
                                        ""
                                        ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::Object_var rootObj =
    orb->resolve_initial_references ("NameService"
                                     ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (rootObj.in ()))
    {
      ACE_ERROR ((LM_ERROR,
                  " (%P|%t) Unable to resolve naming service !\n"));
      return;

    }
  CosNaming::NamingContext_var rootNC =
    CosNaming::NamingContext::_narrow (rootObj.in ()
                                       ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  CosNaming::Name name (1);
  name.length (1);
  name[0].id = CORBA::string_dup ("NotifyEventChannelFactory");

  CORBA::Object_var obj = rootNC->resolve (name
                                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (obj.in ()))
    {
      ACE_ERROR ((LM_ERROR,
                  " (%P|%t) Unable to locate Notify_Service \n"));
      return;
    }

  this->notify_factory_ =
    CosNotifyChannelAdmin::EventChannelFactory::_narrow (
        obj.in()
        ACE_ENV_ARG_PARAMETER
      );
  ACE_CHECK;

}

CosNotifyChannelAdmin::ChannelID
IdAssignment::create_ec (ACE_ENV_SINGLE_ARG_DECL)
{
  CosNotifyChannelAdmin::ChannelID id;
  CosNotification::QoSProperties initial_qos;
  CosNotification::AdminProperties initial_admin;
  CosNotifyChannelAdmin::EventChannel_var ec =
    this->notify_factory_->create_channel (initial_qos,
                                           initial_admin,
                                           id
                                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  return id;
}


void
IdAssignment::destroy_ec(CosNotifyChannelAdmin::ChannelID id
                          ACE_ENV_ARG_DECL)
{
  CosNotifyChannelAdmin::EventChannel_var ec =
    this->notify_factory_->get_event_channel (id
                                              ACE_ENV_ARG_PARAMETER);

  ACE_CHECK;

  if (CORBA::is_nil (ec.in()))
    {
      ACE_ERROR((LM_ERROR,
                 " (%P|%t) Unable to find event channel\n"));
      return;
    }

   ec->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
   ACE_CHECK;
}

CosNotifyChannelAdmin::AdminID
IdAssignment::create_supplier_admin (CosNotifyChannelAdmin::ChannelID channel_id
                                     ACE_ENV_ARG_DECL)
{
  CosNotifyChannelAdmin::AdminID adminid;
  CosNotifyChannelAdmin::InterFilterGroupOperator ifgop =
    CosNotifyChannelAdmin::OR_OP;

  CosNotifyChannelAdmin::EventChannel_var ec =
    this->notify_factory_->get_event_channel (channel_id
                                              ACE_ENV_ARG_PARAMETER);

  ACE_CHECK_RETURN (0);

  if (CORBA::is_nil (ec.in ()))
    {
      ACE_ERROR((LM_ERROR,
                 " (%P|%t) Unable to find event channel\n"));
      return 0;
    }

  CosNotifyChannelAdmin::SupplierAdmin_var supplier_admin =
    ec->new_for_suppliers (ifgop,
                           adminid
                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  if (CORBA::is_nil (supplier_admin.in ()))
    ACE_ERROR_RETURN ((LM_ERROR,
                       " (%P|%t) Unable to create supplier admin\n"),0);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG,
                "created supplier admin\n"));

  return adminid;
}

CosNotifyChannelAdmin::AdminID
IdAssignment::create_consumer_admin (CosNotifyChannelAdmin::ChannelID channel_id
                                     ACE_ENV_ARG_DECL)
{
  CosNotifyChannelAdmin::AdminID adminid;
  CosNotifyChannelAdmin::InterFilterGroupOperator ifgop =
    CosNotifyChannelAdmin::OR_OP;

  CosNotifyChannelAdmin::EventChannel_var ec =
    this->notify_factory_->get_event_channel (channel_id
                                              ACE_ENV_ARG_PARAMETER);

  ACE_CHECK_RETURN (0);

  if (CORBA::is_nil (ec.in ()))
    {
      ACE_ERROR((LM_ERROR,
                 " (%P|%t) Unable to find event channel\n"));
      return 0;
    }

  CosNotifyChannelAdmin::ConsumerAdmin_var consumer_admin =
    ec->new_for_consumers (ifgop,
                           adminid
                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (0);

  if (CORBA::is_nil (consumer_admin.in ()))
    ACE_ERROR_RETURN ((LM_ERROR,
                       " (%P|%t) Unable to create consumer admin\n"),0);

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG,
                "created consumer admin\n"));

  return adminid;
}


void
IdAssignment::destroy_consumer_admin (
    CosNotifyChannelAdmin::ChannelID channel_id,
    CosNotifyChannelAdmin::AdminID admin_id
    ACE_ENV_ARG_DECL
  )
{
  CosNotifyChannelAdmin::EventChannel_var ec =
    this->notify_factory_->get_event_channel (channel_id
                                              ACE_ENV_ARG_PARAMETER);

  ACE_CHECK;

  if (CORBA::is_nil (ec.in ()))
    {
      ACE_ERROR((LM_ERROR,
                 " (%P|%t) Unable to find event channel\n"));
      return;
    }

  CosNotifyChannelAdmin::ConsumerAdmin_var consumer_admin =
    ec->get_consumeradmin (admin_id
                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (consumer_admin.in()))
    {
      ACE_ERROR ((LM_ERROR,
                  " (%P|%t) Unable to get consumer admin\n"));
    }

  consumer_admin->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG,
                "destroyed consumer admin\n"));
}


void
IdAssignment::destroy_supplier_admin (
    CosNotifyChannelAdmin::ChannelID channel_id,
    CosNotifyChannelAdmin::AdminID admin_id
    ACE_ENV_ARG_DECL
  )
{
  CosNotifyChannelAdmin::EventChannel_var ec =
    this->notify_factory_->get_event_channel (channel_id
                                              ACE_ENV_ARG_PARAMETER);

  ACE_CHECK;

  if (CORBA::is_nil (ec.in ()))
    {
      ACE_ERROR ((LM_ERROR,
                  " (%P|%t) Unable to find event channel\n"));
    }

  CosNotifyChannelAdmin::SupplierAdmin_var supplier_admin =
    ec->get_supplieradmin (admin_id
                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (supplier_admin.in ()))
    ACE_ERROR ((LM_ERROR,
                " (%P|%t) Unable to get supplier admin\n"));

  supplier_admin->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (TAO_debug_level)
    ACE_DEBUG ((LM_DEBUG,
                "destroyed supplier admin\n"));
}

void
IdAssignment::run_test(ACE_ENV_SINGLE_ARG_DECL)
{
  CosNotifyChannelAdmin::ChannelID* ec_id = 0;
  ACE_NEW (ec_id,
           CosNotifyChannelAdmin::ChannelID [this->ec_count_]);

  CosNotifyChannelAdmin::AdminID* consumer_admin_id = 0;
  ACE_NEW (consumer_admin_id,
           CosNotifyChannelAdmin::AdminID [this->consumer_admin_count_]);

  CosNotifyChannelAdmin::AdminID* supplier_admin_id = 0;
  ACE_NEW (supplier_admin_id,
           CosNotifyChannelAdmin::AdminID [this->supplier_admin_count_]);

  // *******************************************************************

  int i;

  for (i = 0; i < this->iter_; ++i)
    {
      int ec_count;

      for (ec_count = 0; ec_count < this->ec_count_; ++ec_count)
        {
          ec_id[ec_count] = this->create_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
          ACE_CHECK;

          // Connect <consumer_admin_count_> number of consumers
          // to the current ec.
          for (int cons_count = 0;
               cons_count < this->consumer_admin_count_;
               ++cons_count)
            {
              consumer_admin_id [cons_count] =
                this->create_consumer_admin (ec_id [ec_count]
                                             ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }

          // Connect <supplier_admin_count_> number of suppliers
          // to the current ec.
          for (int supp_count = 0;
               supp_count < this->supplier_admin_count_;
               ++supp_count)
            {
              supplier_admin_id [supp_count] =
                this->create_supplier_admin (ec_id [ec_count]
                                             ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }
        }

      // Destroy the ec, the admins should destroy too.
      for (ec_count = 0; ec_count < this->ec_count_; ++ec_count)
        {
          this->destroy_ec (ec_id[ec_count]
                            ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }

    } // for


  //******************************************************************************
  // Repeat, but this time destroy the admins explicity.
  for (i = 0; i < this->iter_; ++i)
    {
      int ec_count;

      for (ec_count = 0; ec_count < this->ec_count_; ++ec_count)
        {
          ec_id[ec_count] = this->create_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
          ACE_CHECK;

          int cons_count, supp_count;
          // Connect <consumer_admin_count_> number of consumers
          // to the current ec.
          for (cons_count = 0;
               cons_count < this->consumer_admin_count_;
               ++cons_count)
            {
              consumer_admin_id[cons_count] =
                this->create_consumer_admin (ec_id[ec_count]
                                             ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }

          // Connect <supplier_admin_count_> number of suppliers
          // to the current ec.
          for (supp_count = 0; supp_count < this->supplier_admin_count_; ++supp_count)
            {
              supplier_admin_id[supp_count] =
                this->create_supplier_admin (ec_id[ec_count]
                                             ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }

          // Destroy the admins.

          // Destroy consumer admins.
          for (cons_count = 0;
               cons_count < this->consumer_admin_count_;
               ++cons_count)
            {
              this->destroy_consumer_admin (ec_id[ec_count],
                                            consumer_admin_id[cons_count]
                                            ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }

          // Destroy supplier admins
          for (supp_count = 0;
               supp_count < this->supplier_admin_count_;
               ++supp_count)
            {
              this->destroy_supplier_admin (ec_id[ec_count],
                                            supplier_admin_id[supp_count]
                                            ACE_ENV_ARG_PARAMETER);
              ACE_CHECK;
            }

        }

      // Destroy the ec,
      for (ec_count = 0; ec_count < this->ec_count_; ++ec_count)
        {
          this->destroy_ec (ec_id[ec_count]
                            ACE_ENV_ARG_PARAMETER);
          ACE_CHECK;
        }

    } // for

  //********************************************************************************
}

int main (int argc, char* argv[])
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      IdAssignment test;

      test.parse_args (argc,
                       argv);

      test.init (argc,
                 argv
                 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      test.run_test (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Caught exception:");
      return 1;
    }
  ACE_ENDTRY;

  ACE_DEBUG ((LM_DEBUG,
              "IdAssignment test suceeded\n"));
  return 0;
}