summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/CosEvent_Service/CosEvent_Service.cpp
blob: 0459199460b8c9b0ab67b29dc9f86745e0ebd434 (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
// $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"),
    ec_impl_ (0,
              ACE_DEFAULT_EVENT_CHANNEL_TYPE,
              &module_factory_),
    rtec_ (RtecEventChannelAdmin::EventChannel::_nil ()),
    scheduler_ (RtecScheduler::Scheduler::_nil ()),
    cos_ec_ (CosEventChannelAdmin::EventChannel::_nil ()),
    global_scheduler_ (0),
    remote_Rtec_ (1)
{
  // No-Op.
}

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

int
CosEvent_Service::init_ORB  (int argc, char *argv [])
{
  TAO_TRY
    {
      this->orb_ = CORBA::ORB_init (argc,
                                    argv,
                                    "",
                                    TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      CORBA::Object_var poa_object  =
        this->orb_->resolve_initial_references("RootPOA",
                                               TAO_TRY_ENV);
      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 (),
                                      TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      poa_manager->activate (TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      return 0;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Exception in CosEvent_Service::init_ORB");
      return -1;
    }
  TAO_ENDTRY;
}

int
CosEvent_Service::parse_args (int argc, char *argv [])
{
  ACE_Get_Opt get_opt (argc, argv, "r:n:s: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 '?':
        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.
                      " \n",
                      argv[0]));
          return -1;
        }
    }

  return 0;
}

int
CosEvent_Service::init_NamingService (void)
{
  TAO_TRY
  {
    // 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;
  }
 TAO_CATCHANY
   {
     TAO_TRY_ENV.print_exception ("Exception in CosEvent_Service::get_Rtec_viaNamingService\n");
     return -1;
   }
 TAO_ENDTRY;
}

int
CosEvent_Service::get_Rtec_viaNamingService (void)
{
  TAO_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,
				      TAO_TRY_ENV);
      TAO_CHECK_ENV;

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

      return 0;
  }
 TAO_CATCHANY
   {
     TAO_TRY_ENV.print_exception ("Exception in CosEvent_Service::get_Rtec_viaNamingService\n");
     return -1;
   }
 TAO_ENDTRY;
}

int
CosEvent_Service::start_Scheduler (void)
{
  TAO_TRY
    {
       CosNaming::Name schedule_name (1);
       schedule_name.length (1);
       schedule_name[0].id = CORBA::string_dup (this->schedule_name_);

      if (this->global_scheduler_ == 0)
        {
          this->scheduler_ =
            this->scheduler_impl_._this (TAO_TRY_ENV);
          TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

          CORBA::String_var str =
            this->orb_->object_to_string (this->scheduler_.in (),
                                          TAO_TRY_ENV);
          TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

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

          // Register the servant with the Naming Context....
	  this->naming_client_->rebind (schedule_name,
                                        this->scheduler_.in (),
                                        TAO_TRY_ENV);
	  TAO_CHECK_ENV;

          if (ACE_Scheduler_Factory::server (this->scheduler_.in ()) == -1)
            return -1;
        }
      else // get the global scheduler
        {
          CORBA::Object_var sched_obj =
            this->naming_client_->resolve (schedule_name,
                                           TAO_TRY_ENV);
          TAO_CHECK_ENV;

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

      ACE_Scheduler_Factory::use_config (this->naming_client_.get_context());
      return 0;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Exception in CosEvent_Service::start_Scheduler\n");
      return -1;
    }
  TAO_ENDTRY;
}
int
CosEvent_Service::create_local_RtecService (void)
{
 TAO_TRY
    {
      this->rtec_ = this->ec_impl_._this (TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);


      CORBA::String_var str = this->orb_->object_to_string (this->rtec_.in (),
                                                            TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

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

      this->ec_impl_.activate ();

      return 0;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Exception in CosEvent_Service::create_local_RtecService\n");
      return -1;
    }
  TAO_ENDTRY;
}

int
CosEvent_Service::create_CosEC (void)
{
  TAO_TRY
    {
      // @@ 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...

      // Setup the QOS params..
      this->supplier_qos_.insert (1,
                                  ACE_ES_EVENT_ANY, //@@this should be a param.
                                  this->scheduler_->create ("supplier",
                                                            TAO_TRY_ENV),
                                  1);

      // @@ 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).
      this->consumer_qos_.start_disjunction_group ();
      this->consumer_qos_.insert_source (1,
                                         this->scheduler_->create ("consumer",
                                                                   TAO_TRY_ENV));
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      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_,
                            TAO_TRY_ENV) != 0)
        return -1;

      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      this->cos_ec_ = this->ec_i_._this (TAO_TRY_ENV);
      TAO_CHECK_ENV_RETURN (TAO_TRY_ENV, -1);

      return 0;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Exception in CosEvent_Service::create_CosEC");
      return -1;
    }
  TAO_ENDTRY;
}

int
CosEvent_Service::register_CosEC (void)
{
  TAO_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_,
                                    TAO_TRY_ENV);
      TAO_CHECK_ENV;
      return 0;
    }
 TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("CosEvent_Service::register_CosEC.\n");
      return -1;
    }
  TAO_ENDTRY;
}

int
CosEvent_Service::startup (int argc, char *argv[])
{
  // check command line args.
  if (this->parse_args (argc, argv) == -1)
    return -1;

  // initalize the ORB.
  if (this->init_ORB (argc, argv) == -1)
    return -1;

  // initialize the Naming Client.
  if (this->init_NamingService () == -1)
    return -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);
      }

  // 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);
    }
  // 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)
{
 TAO_TRY
    {
      if (this->global_scheduler_ == 0 &&
          this->scheduler_ != RtecScheduler::Scheduler::_nil ())
	{
          CosNaming::Name schedule_name (1);
          schedule_name.length (1);
          schedule_name[0].id = CORBA::string_dup (this->schedule_name_);

	  this->naming_client_->unbind (schedule_name, TAO_TRY_ENV);
	  TAO_CHECK_ENV;
	}

      if (this->cos_ec_ != CosEventChannelAdmin::EventChannel::_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,
                                        TAO_TRY_ENV);
          TAO_CHECK_ENV;

          this->cos_ec_->destroy (TAO_TRY_ENV);
          TAO_CHECK_ENV;
        }

      if (TAO_ORB_Core_instance ()->orb () != CORBA::ORB::_nil ())
        this->ec_impl_.shutdown ();
      // The Rtec shutdown method tries to use the orb , which might not
      // exist at that point so this check avoids that crash.

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

      return 0;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("Exception in shutdown.\n");
      return -1;
    }
  TAO_ENDTRY;
}

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 ();
}