summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/CosEvent_Service/CosEvent_Service.cpp
blob: 92300783d506420dd2f90e809883372c064bef53 (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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// $Id$

#include "ace/Get_Opt.h"
#include "CosEvent_Service.h"

// @@ Pradeep: somehow the user should be able to specify if the EC is
// to be instantiated locally or if you want to use a remote EC.
// Please check the real-time Event_Service and how they manage
// optionally create a scheduling service.

CosEvent_Service::CosEvent_Service (void)
  : service_name ("CosEventService"),
    rt_service_name ("EventService"),
    schedule_name_ ("ScheduleService"),
    scheduler_ (RtecScheduler::Scheduler::_nil ()),
    rtec_ (RtecEventChannelAdmin::EventChannel::_nil ()),
    cos_ec_ (CosEventChannelAdmin::EventChannel::_nil ()),
    global_scheduler_ (0),
    remote_Rtec_ (1),
    eventTypeIds_ (0),
    eventSourceIds_ (0),
    source_type_pairs_ (0)
{
  // No-Op.
}

CosEvent_Service::~CosEvent_Service (void)
{
  // No-Op.
}

int
CosEvent_Service::init_ORB  (int argc, char *argv [])
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->orb_ = CORBA::ORB_init (argc,
                                    argv,
                                    "",
                                    ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::Object_var poa_object  =
        this->orb_->resolve_initial_references("RootPOA",
                                               ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                          -1);

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in (),
                                      ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (ACE_TRY_ENV);

      ACE_TRY_CHECK;

      poa_manager->activate (ACE_TRY_ENV);

      ACE_TRY_CHECK;

      return 0;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in CosEvent_Service::init_ORB");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
CosEvent_Service::parse_args (int argc, char *argv [])
{
  ACE_Get_Opt get_opt (argc, argv, "r:n:s:e:o:p:l");
  int opt;

  while ((opt = get_opt ()) != EOF)
    {
      switch (opt)
        {
        case 'n':
          this->service_name = get_opt.optarg;
          break;

        case 'r':
          this->rt_service_name = get_opt.optarg;
          break;

        case 's':
          if (ACE_OS::strcasecmp (get_opt.optarg, "global") == 0)
            {
              this->global_scheduler_ = 1;
            }
          else if (ACE_OS::strcasecmp (get_opt.optarg, "local") == 0)
            {
              this->global_scheduler_ = 0;
            }
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          "Unknown scheduling type <%s> "
                          "defaulting to local\n",
                          get_opt.optarg));
              this->global_scheduler_ = 0;
              return -1;
            }
          break;

        case 'l':
          this->remote_Rtec_ = 0;
          break;

        case 'e':
          this->eventTypeIds_ =  get_opt.optarg;
          break;

        case 'o':
          this->eventSourceIds_ = get_opt.optarg;
          break;

        case 'p':
          this->source_type_pairs_ = get_opt.optarg;
          break;

        case '?':
        default:
          ACE_DEBUG ((LM_DEBUG,
                      "Usage: %s "
                      " -n <COS Event Service name>"
                      " -r <RealTime Event Service name>"
                      " -s <global|local>"
                      " -l" // creates the RtEC locally.
                      " -e [\"EventType_1, EventType_2...\"] for ConsumerQOS."
                      " -o [\"EventSourceID_1, [EventSourceID_2...\"] for ConsumerQOS."
                      " -p [\"Source, Event\" pair] for SupplierQOS."
                      " \n",
                      argv[0]));
          return -1;
        }
    }

  return 0;
}

int
CosEvent_Service::init_NamingService (void)
{
  // Initialization of the naming service.
  if (this->naming_client_.init (this->orb_.in ()) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) Unable to initialize "
                       "the TAO_Naming_Client. \n"),
                      -1);
  return 0;
}

int
CosEvent_Service::get_Rtec_viaNamingService (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      CosNaming::Name rt_ref_name (1); // name of the RT Event service.
      rt_ref_name.length (1);
      rt_ref_name[0].id =
      CORBA::string_dup (this->rt_service_name);

      CORBA::Object_var rtEC_obj =
       this->naming_client_->resolve (rt_ref_name,
				      ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // The CORBA::Object_var object is downcast to
      // RtecEventChannelAdmin::EventChannel
      // using the <_narrow> method.
      this->rtec_ =
        RtecEventChannelAdmin::EventChannel::_narrow (rtEC_obj.in (),
                                                      ACE_TRY_ENV);
      ACE_TRY_CHECK;

      return 0;
  }
  ACE_CATCHANY
   {
     ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                          "Exception in CosEvent_Service::get_Rtec_viaNamingService\n");
     return -1;
   }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
CosEvent_Service::start_Scheduler (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      if (this->global_scheduler_ == 0)
        {
          this->scheduler_ =
            this->scheduler_impl_._this (ACE_TRY_ENV);
          ACE_TRY_CHECK;

          CORBA::String_var str =
            this->orb_->object_to_string (this->scheduler_.in (),
                                          ACE_TRY_ENV);
          ACE_TRY_CHECK;

          ACE_DEBUG ((LM_DEBUG,
                      "CosEvent_Service: The (local) scheduler IOR is <%s>\n",
                      str.in ()));

          if (ACE_Scheduler_Factory::server (this->scheduler_.in ()) == -1)
            return -1;
        }
      else // get the global scheduler
        {
          CosNaming::Name schedule_name (1);
          schedule_name.length (1);
          schedule_name[0].id = CORBA::string_dup (this->schedule_name_);

          CORBA::Object_var sched_obj =
            this->naming_client_->resolve (schedule_name,
                                           ACE_TRY_ENV);
          ACE_TRY_CHECK;

          // The CORBA::Object_var object is downcast to
          // RtecScheduler::Scheduler using the <_narrow> method.
          this->scheduler_ =
            RtecScheduler::Scheduler::_narrow (sched_obj.in (),
                                               ACE_TRY_ENV);
          ACE_TRY_CHECK;

          if (ACE_Scheduler_Factory::server (this->scheduler_.in ()) == -1)
            return -1;
        }

      ACE_Scheduler_Factory::use_config (this->naming_client_.get_context());
      return 0;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in CosEvent_Service::start_Scheduler\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}
int
CosEvent_Service::create_local_RtecService (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      ACE_NEW_RETURN (this->ec_impl_,
                      ACE_EventChannel(0,
                                       ACE_DEFAULT_EVENT_CHANNEL_TYPE,
                                       &module_factory_), -1);

      this->rtec_ = this->ec_impl_->_this (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::String_var str = this->orb_->object_to_string (this->rtec_.in (),
                                                            ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "CosEvent_Service: The RTEC IOR is <%s>\n",
                  str.in ()));

      this->ec_impl_->activate ();

      return 0;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in CosEvent_Service::create_local_RtecService\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

void
CosEvent_Service::init_SupplierQOS (RtecScheduler::handle_t supp_handle)
{
  // @@ Pradeep: It is very important that you make the type of
  // events generated by the CosEC an option.
  // I know this is not very well documented, but the type should
  // be >= ACE_ES_EVENT_UNDEFINED = 16
  // Something else: please make the EventSourceID for the
  // supplier also an option...

  char c = ' '; // space
  char *tok = 0;

 // if nothing was specified on the command line use defaults..
  if (this->source_type_pairs_ == 0)
    this->supplier_qos_.insert (1,
                                ACE_ES_EVENT_ANY,
                                supp_handle,
                                1);
  else // parse the event types..
    {
      tok = ACE_OS::strtok (this->source_type_pairs_, &c);
      if (tok == 0) // error
        {
          ACE_DEBUG ((LM_DEBUG, "error parsing source,event pairs for SupplierQOS, defaulting to source id = 1, eventid = ACE_ES_EVENT_ANY"));

          this->supplier_qos_.insert (1,
                                      ACE_ES_EVENT_ANY,
                                      supp_handle,
                                      1);
        }
      else
        // we just use 1 source-type pair in the event channel.
        // so scan for the 1st pair only.
        {
          int source_val = 0, type_val = 0;
          source_val = ACE_OS::atoi (tok);

          tok = ACE_OS::strtok (0, &c);

          if (tok != 0)
            type_val = ACE_OS::atoi (tok);

          ACE_DEBUG ((LM_DEBUG, "supplier_qos::insert (%d, %d)\n",
                      source_val, type_val));

          // Setup the QOS params..
          this->supplier_qos_.insert (source_val,
                                      type_val,
                                      supp_handle,
                                      1);
        }
    }
}

void
CosEvent_Service::init_ConsumerQOS (RtecScheduler::handle_t cons_handle)
{
  // @@ Pradeep: ditto here, make the set of sources (and/or type)
  // a parameter, and make sure the user can specify multiple of
  // them (just call insert_source() or insert_type() in the
  // parse_args routine).

  char c = ' '; // space
  char *tok = 0;

  this->consumer_qos_.start_disjunction_group ();

  // insert the event ids first..

  // if nothing was specified on the command line use defaults..
  if (this->eventTypeIds_ == 0)
    {
      //this->consumer_qos_.insert_type (ACE_ES_EVENT_ANY, // default
      //                             cons_handle);
      // @@ if i uncomment this line then the Rtec displays the message
      // "Consumer tried to register for allevents!  This is not implemented."
      // whenever a consumer tries to register with it.
    }
  else // parse the event types..
    {
      tok = ACE_OS::strtok ( this->eventTypeIds_, &c);
      if (tok == 0) // error
        {
          ACE_DEBUG ((LM_DEBUG, "error parsing eventIds for ConsumerQOS, defaulting to 1"));
          this->consumer_qos_.insert_type (ACE_ES_EVENT_ANY,
                                           cons_handle);
        }
      else
        do
          {
            int type_val = ACE_OS::atoi (tok);
            ACE_DEBUG ((LM_DEBUG, "consumer_qos::insert_type (%d)\n",
                        type_val));
            this->consumer_qos_.insert_type (type_val,
                                             cons_handle);
            tok = ACE_OS::strtok (0, &c);
          }
        while (tok != 0);
    }

  // repeat for source ids..

  // if nothing was specified on the command line use defaults..
  if (this->eventSourceIds_ == 0)
    this->consumer_qos_.insert_source (1, // default = 1
                                       cons_handle);
  else // parse the event types..
    {
      tok = ACE_OS::strtok (this->eventSourceIds_, &c);
      if (tok == 0) // error
        {
          ACE_DEBUG ((LM_DEBUG, "error parsing sourceIds for ConsumerQOS, defaulting to 1"));
          this->consumer_qos_.insert_source (1, // default = 1
                                             cons_handle);
        }
      else
        do
          {
            int source_val = ACE_OS::atoi (tok);
            ACE_DEBUG ((LM_DEBUG, "consumer_qos::insert_source (%d)\n",
                        source_val));
            this->consumer_qos_.insert_type (source_val,
                                             cons_handle);
            tok = ACE_OS::strtok (0, &c);
          }
        while (tok != 0);
    }
}

int
CosEvent_Service::create_CosEC (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      RtecScheduler::handle_t supp_handle =
        this->scheduler_->create ("supplier",
                                  ACE_TRY_ENV);

      ACE_TRY_CHECK;

      this->init_SupplierQOS (supp_handle);

      RtecScheduler::handle_t cons_handle =
        this->scheduler_->create ("consumer",
                                  ACE_TRY_ENV);
      ACE_TRY_CHECK;

      this->init_ConsumerQOS (cons_handle);

      const RtecEventChannelAdmin::ConsumerQOS &consumerqos =
        this->consumer_qos_.get_ConsumerQOS ();

      const RtecEventChannelAdmin::SupplierQOS &supplierqos =
        this->supplier_qos_.get_SupplierQOS ();

      if (this->ec_i_.init (consumerqos,
                            supplierqos,
                            this->rtec_,
                            ACE_TRY_ENV) != 0)
        return -1;

      ACE_TRY_CHECK;

      this->cos_ec_ = this->ec_i_._this (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::String_var str = this->orb_->object_to_string (this->cos_ec_,
                                                            ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "CosEvent_Service: The COSEC IOR is <%s>\n",
                  str.in ()));
      return 0;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in CosEvent_Service::create_CosEC");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
CosEvent_Service::register_CosEC (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
   {
      // Name the object.
      CosNaming::Name ec_obj_name (1);
      ec_obj_name.length (1);
      ec_obj_name[0].id =
	CORBA::string_dup (this->service_name);

      // Now, attach the object name to the context.
      this->naming_client_->rebind (ec_obj_name,
                                    this->cos_ec_,
                                    ACE_TRY_ENV);
      ACE_TRY_CHECK;
      return 0;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in CosEvent_Service::register_CosEC.\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

int
CosEvent_Service::startup (int argc, char *argv[])
{
  // initalize the ORB.
  if (this->init_ORB (argc, argv) == -1)
    return -1;

  // The ORB is initialized before <parse_args> because the ACE_Channel
  // <destroy> method assumes an initialized ORB.

  // check command line args.
  if (this->parse_args (argc, argv) == -1)
    return -1;

  // initialize the Naming Client.
  if (this->init_NamingService () == -1)
    return -1;

  // start the scheduler
  if (this->start_Scheduler ())
    {
      // scheduler startup failed..cleanup
      this->shutdown ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         "(%P|%t) Failed to start the scheduler\n"),
                        -1);
    }

  // see if the user wants a local Rtec..
  if (this->remote_Rtec_ == 0 && this->create_local_RtecService () == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Failed to create a local RtEC.\n"),
                        -1);
    }
  else
    // get hold of the Rtec Service via the Naming Service.
    if (this->get_Rtec_viaNamingService () == -1)
      {
        ACE_DEBUG ((LM_ERROR, "Could not locate the Real Time Event service <%s> via the Naming Service, trying to create a local copy..\n", this->rt_service_name));

        // Rtec was not active.. try creating a local copy.
        if (this->create_local_RtecService () == -1)
          ACE_ERROR_RETURN ((LM_ERROR,
                             " (%P|%t) Failed to create a local RtEC.\n"),
                            -1);
      }


  // now try to create the COS EC.
  if (this->create_CosEC () == -1)
    {
      // CosEC creation failed.. cleanup
      this->shutdown ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Failed to create CosEC.\n"),
                        -1);
    }

  // finally, register it with the naming service.
  if (this->register_CosEC () == -1)
    {
      // CosEC registration failed.. cleanup
      this->shutdown ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Failed to register the CosEC with the naming service.\n"),
                        -1);
    }

  return 0;
}

int
CosEvent_Service::run (void)
{
  ACE_DEBUG ((LM_DEBUG, "%s; running the Cos event service\n", __FILE__));
  if (this->orb_->run () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "run"), 1);

  return 0;
}

int
CosEvent_Service::shutdown (void)
{
  ACE_DECLARE_NEW_CORBA_ENV
  ACE_TRY
    {
      if (!this->cos_ec_->_nil ())
        {
          // unbind the cosEC from the naming service.
          CosNaming::Name ec_obj_name (1);
          ec_obj_name.length (1);
          ec_obj_name[0].id =
            CORBA::string_dup (this->service_name);
          this->naming_client_->unbind (ec_obj_name,
                                        ACE_TRY_ENV);
          ACE_TRY_CHECK;

          this->cos_ec_->destroy (ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }

      this->ec_impl_->shutdown ();
      delete ec_impl_;

      // shutdown the ORB.
      if (!this->orb_->_nil ())
        this->orb_->shutdown ();

      return 0;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in shutdown.\n");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;
}

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

  if (service.startup (argc, argv) == -1)
    {
      service.shutdown ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Failed to setup the Cos Event Channel.\n"),
                        1);
    }

  if (service.run () == -1)
    {
      service.shutdown ();
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Failed to run the Cos Event Channel.\n"),
                        1);
    }

  service.shutdown ();

  return 0;
}