summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/Event_Supplier/DOVE_Supplier.cpp
blob: 2f16dd37bb5030037473ffe1bc0ab8d036f3d536 (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
// $Id$

// ============================================================================
//
// = FILENAME
//    DOVE_Supplier.cpp
//
// = DESCRIPTION
//    A wrapper around the event service initialization and
//    marshalling
//
// = AUTHOR
//    Michael Kircher (mk1@cs.wustl.edu)
//
// ============================================================================

#include "DOVE_Supplier.h"
#include "tao/ORB_Core.h"

ACE_RCSID (Event_Supplier, 
           DOVE_Supplier, 
           "$Id$")

// Static pointer member initialization for Singleton.

ACE_Scheduler_Factory::POD_RT_Info *
DOVE_Supplier::pod_rt_info_instance_ = 0;

// Constructor.

DOVE_Supplier::DOVE_Supplier ()
  : initialized_ (0),
    connected_ (0),
    connection_params_list_ (0),
    current_connection_params_ (0),
    connection_count_ (0),
    current_connection_index_ (0),
    internal_DOVE_Supplier_ptr_ (0),
    MIB_name_ (0)
{
  ACE_NEW (internal_DOVE_Supplier_ptr_,
           Internal_DOVE_Supplier (this));

  if (internal_DOVE_Supplier_ptr_ == 0)
    {
      ACE_ERROR ((LM_ERROR,
                  "DOVE_Supplier::DOVE_Supplier internal "
                  "supplier not allocated."));
    }
}

// Destructor.

DOVE_Supplier::~DOVE_Supplier ()
{
  for (int i = 0; i < this->connection_count_; ++i)
    {
      delete (this->connection_params_list_ [i]);
    }

  delete [] this->connection_params_list_;

  delete internal_DOVE_Supplier_ptr_;

}

// Initialize the ORB and the connection to the Name Service

int
DOVE_Supplier::init (void)
{
  ACE_TRY_NEW_ENV
  {
    // Connect to the RootPOA.
    CORBA::Object_var poaObject_var =
      TAO_ORB_Core_instance()->orb()->resolve_initial_references("RootPOA"
                                                                 ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;

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

    this->root_POA_var_ =
      PortableServer::POA::_narrow (poaObject_var.in () ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;

    this->poa_manager_ =
       root_POA_var_->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_TRY_CHECK;

    // Get the Naming Service object reference.
    CORBA::Object_var namingObj_var =
      TAO_ORB_Core_instance()->orb()->resolve_initial_references (
          "NameService"
          ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;

    if (CORBA::is_nil (namingObj_var.in ()))
      ACE_ERROR_RETURN ((LM_ERROR,
                        " (%P|%t) Unable to get the Naming Service.\n"),
                        -1);

    this->namingContext_var_ =
      CosNaming::NamingContext::_narrow (namingObj_var.in ()
                                         ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;
  }
  ACE_CATCHANY
  {
    ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                         "DOVE_Supplier::init");
    return -1;
  }
  ACE_ENDTRY;

  initialized_ = 1;
  return 0;
}

int
DOVE_Supplier::connect (const char* MIB_name,
                        const char* es_name,
                        const char * ss_name,
                        ACE_Scheduler_Factory::POD_RT_Info * pod_rt_info)
{
  // Initialize the supplier if this has not already been done.
  if ((initialized_ == 0) &&  (this->init () == -1))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Unable to initialize the DOVE_Supplier.\n"),
                        -1);
    }


  // Grab the default RT_Info settings if others were not provided.
  if (pod_rt_info == 0)
    {
      // Get the default singleton if we were not passed the data
      pod_rt_info = DOVE_Supplier::pod_rt_info_instance ();
      if (pod_rt_info == 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             " (%P|%t) Unable to obtain"
                             " the default RT_Info data.\n"),
                            -1);
        }
    }

  // Save the passed MIB name
  MIB_name_ = (MIB_name == 0) ? "MIB_unknown" : MIB_name;

  // Create a new connection parameters structure.
  Connection_Params * cp_temp = 0;
  ACE_NEW_RETURN (cp_temp, Connection_Params, -1);

  // Populate the known fields of the new connection params struct.
  cp_temp->pod_rt_info_ = *pod_rt_info;
  cp_temp->es_name_ = (es_name == 0) ?  "EventService" : es_name;
  cp_temp->ss_name_ = (ss_name == 0) ?  "ScheduleService" : ss_name;

  // Allocate a new connection parameters pointer array.
  // Cannot use ACE_NEW_RETURN here, as we need to clean up
  // cp_temp if we fail here, and we need what cp_temp points
  // to after the current scope if we succeed here.
  Connection_Params ** cp_list_temp;
  cp_list_temp =
    new Connection_Params * [this->connection_count_ + 1];
  if (cp_list_temp == 0)
    {
      // Avoid a memory leak if we failed to allocate.
      delete cp_temp;

      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) DOVE_Supplier::connect could not "
                         "reallocate connection params list"),
                        -1);
    }

  // Copy the connection struct pointers from
  // the old list (if any) to the new one.
  for (int i = 0; i < this->connection_count_; ++i)
    {
      cp_list_temp [i] =
        this->connection_params_list_ [i];
    }

  // Put a pointer to the new connection params structure
  // in the new list, increment the connection params count,
  // and point to the latest connection parameters.
  cp_list_temp [this->connection_count_] = cp_temp;
  this->current_connection_params_ = cp_temp;
  current_connection_index_ = connection_count_;
  ++ (this->connection_count_);

  // Replace the old list of pointers with the new one
  delete [] this->connection_params_list_;
  this->connection_params_list_ = cp_list_temp;

  // Resolve the event service reference.
  if (this->get_EventChannel () == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Unable to resolve the event service.\n"),
                        -1);
    }

  // Resolve the scheduling service reference.
  if (this->get_Scheduler () == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Unable to resolve the scheduler.\n"),
                        -1);
    }

  // Connect to the event service as a supplier.
  if (this->connect_Supplier () == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Unable to connect to the event service.\n"),
                        -1);
    }

  return 0;

}


// This method is invoked after all connect calls are done.

void
DOVE_Supplier::connected ()
{
  if (! connected_)
    {
      // Code to do post-connection-establishment
      // one-time logic goes here.

      connected_ = 1;
    }
}


void
DOVE_Supplier::disconnect ()
{
}


void
DOVE_Supplier::notify (CORBA::Any &message)
{
  // Finalize connection establishment no later than the first event notification
  if (! connected_)
    {
      this->connected ();
    }

  ACE_TRY_NEW_ENV
  {
    RtecEventComm::Event event;
    event.header.source = SOURCE_ID;
    event.header.type = ACE_ES_EVENT_NOTIFICATION;
    event.header.ttl = 1;
    ACE_hrtime_t creation_time = ACE_OS::gethrtime ();
    ORBSVCS_Time::hrtime_to_TimeT (event.header.creation_time, creation_time);
    event.header.ec_recv_time = ORBSVCS_Time::zero ();
    event.header.ec_send_time = ORBSVCS_Time::zero ();
    event.data.any_value = message;

    RtecEventComm::EventSet events;
    events.length (1);
    events[0] = event;

    // Now we invoke a RPC
    this->current_connection_params_->proxyPushConsumer_var_->push (events
                                                                    ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;
  }
  ACE_CATCHANY
  {
    ACE_ERROR ((LM_ERROR,
                "DOVE_Supplier::notify: "
                "unexpected exception.\n"));
  }
  ACE_ENDTRY;
}


// Use the next connection in the list of established connections.

void
DOVE_Supplier::use_next_connection ()
{
  if (connection_count_ > 0)
    {
      current_connection_index_ =
        (current_connection_index_ == connection_count_ - 1)
        ? 0 : current_connection_index_ + 1;

      current_connection_params_ =
        connection_params_list_ [current_connection_index_];
    }
}


// Use the previous connection in the list of established connections.

void
DOVE_Supplier::use_prev_connection ()
{
  if (connection_count_ > 0)
    {
      current_connection_index_ =
        (current_connection_index_ == 0)
        ? connection_count_ - 1
        : current_connection_index_ - 1;

      current_connection_params_ =
        connection_params_list_ [current_connection_index_];
    }
}



// -------------------- Internal Demo Supplier -----------------------------

DOVE_Supplier::Internal_DOVE_Supplier::Internal_DOVE_Supplier (DOVE_Supplier *impl_ptr)
  : impl_ptr_ (impl_ptr)
{
}

// ----------------------------------------------------------------------------

int
DOVE_Supplier::get_Scheduler ()
{
  ACE_TRY_NEW_ENV
    {
      CosNaming::Name schedule_name (1);
      schedule_name.length (1);
      schedule_name[0].id =
        CORBA::string_dup (this->current_connection_params_->ss_name_);

      CORBA::Object_var objref =
          namingContext_var_->resolve (schedule_name
                                       ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      this->current_connection_params_->scheduler_var_ =
        RtecScheduler::Scheduler::_narrow(objref.in ()
                                          ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      current_connection_params_->scheduler_var_ = 0;
      ACE_ERROR_RETURN ((LM_ERROR,
                         "DOVE_Supplier::get_Scheduler: "
                         "error while resolving scheduler %s\n",
                         this->current_connection_params_->ss_name_),
                        -1);
    }
  ACE_ENDTRY;

  return 0;
}


int
DOVE_Supplier::get_EventChannel ()
{
  ACE_TRY_NEW_ENV
  {
    // Get a reference to the Event Service
    CosNaming::Name channel_name (1);
    channel_name.length (1);
    channel_name[0].id =
      CORBA::string_dup (this->current_connection_params_->es_name_);

    CORBA::Object_var eventServiceObj_var =
      this->namingContext_var_->resolve (channel_name ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;

    this->current_connection_params_->eventChannel_var_ =
       RtecEventChannelAdmin::EventChannel::_narrow (eventServiceObj_var.in()
                                                   ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;

    if (CORBA::is_nil (this->current_connection_params_->eventChannel_var_.in()))
      ACE_ERROR_RETURN ((LM_ERROR,
                         "The reference to the event channel is nil!"),
                         1);
  }
  ACE_CATCHANY
  {
    ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                         "DOVE_Supplier::get_EventChannel");
    return -1;
  }
  ACE_ENDTRY;

  return 0;
}


int
DOVE_Supplier::connect_Supplier ()
{
  ACE_TRY_NEW_ENV
  {
    // Generate the Real-time information descriptor.
    this->current_connection_params_->rt_info_ =
      this->current_connection_params_->
        scheduler_var_->
          create (this->current_connection_params_->pod_rt_info_.entry_point
                  ACE_ENV_ARG_PARAMETER);

    ACE_TRY_CHECK;

    this->current_connection_params_->scheduler_var_->
      set (this->current_connection_params_->rt_info_,
           static_cast<RtecScheduler::Criticality_t> (this->current_connection_params_->pod_rt_info_.criticality),
           this->current_connection_params_->pod_rt_info_.worst_case_execution_time,
           this->current_connection_params_->pod_rt_info_.typical_execution_time,
           this->current_connection_params_->pod_rt_info_.cached_execution_time,
           this->current_connection_params_->pod_rt_info_.period,
           static_cast<RtecScheduler::Importance_t> (this->current_connection_params_->pod_rt_info_.importance),
           this->current_connection_params_->pod_rt_info_.quantum,
           this->current_connection_params_->pod_rt_info_.threads,
           static_cast<RtecScheduler::Info_Type_t> (this->current_connection_params_->pod_rt_info_.info_type)
           ACE_ENV_ARG_PARAMETER);

    ACE_TRY_CHECK;


    // Set the publications to report them to the event channel.

    CORBA::Short x = 0;
    RtecEventChannelAdmin::SupplierQOS qos;
    qos.publications.length (1);
    qos.publications[0].event.header.source = SOURCE_ID;
    qos.publications[0].event.header.type = ACE_ES_EVENT_NOTIFICATION;
    qos.publications[0].event.header.ttl = 1;
    qos.publications[0].event.header.creation_time = ORBSVCS_Time::zero ();
    qos.publications[0].event.header.ec_recv_time = ORBSVCS_Time::zero ();
    qos.publications[0].event.header.ec_send_time = ORBSVCS_Time::zero ();
    qos.publications[0].event.data.any_value <<= x;
    ACE_TRY_CHECK;
    qos.publications[0].dependency_info.number_of_calls = 1;
    qos.publications[0].dependency_info.rt_info =
      this->current_connection_params_->rt_info_;

    // = Connect as a supplier.
    this->current_connection_params_->supplierAdmin_var_ =
      this->current_connection_params_->eventChannel_var_->for_suppliers (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_TRY_CHECK;

    this->current_connection_params_->proxyPushConsumer_var_ =
      this->current_connection_params_->supplierAdmin_var_->obtain_push_consumer (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_TRY_CHECK;

    // In calling _this we get back an object reference and register
    // the servant with the POA.
    RtecEventComm::PushSupplier_var pushSupplier_var =
      this->internal_DOVE_Supplier_ptr_->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_TRY_CHECK;

    // Connect the supplier to the proxy consumer.
    ACE_SupplierQOS_Factory::debug (qos);
    this->current_connection_params_->
      proxyPushConsumer_var_->connect_push_supplier (pushSupplier_var.in (),
                                                     qos
                                                     ACE_ENV_ARG_PARAMETER);
    ACE_TRY_CHECK;
  }
  ACE_CATCHANY
  {
    ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                         "DOVE_Supplier::connect_supplier");
    return -1;
  }
  ACE_ENDTRY;

  return 0;

}


// Access the default rt_info singleton.

ACE_Scheduler_Factory::POD_RT_Info *
DOVE_Supplier::pod_rt_info_instance ()
{
  if (DOVE_Supplier::pod_rt_info_instance_ == 0)
    {
      ACE_NEW_RETURN (DOVE_Supplier::pod_rt_info_instance_,
                      ACE_Scheduler_Factory::POD_RT_Info,
                      0);

      // Set up the default data.
      DOVE_Supplier::pod_rt_info_instance_->entry_point = "ABC";
      DOVE_Supplier::pod_rt_info_instance_->criticality =
        RtecScheduler::VERY_LOW_CRITICALITY;
      DOVE_Supplier::pod_rt_info_instance_->worst_case_execution_time =
        ORBSVCS_Time::zero ();
      DOVE_Supplier::pod_rt_info_instance_->typical_execution_time =
        ORBSVCS_Time::zero ();
      DOVE_Supplier::pod_rt_info_instance_->cached_execution_time =
        ORBSVCS_Time::zero ();
      DOVE_Supplier::pod_rt_info_instance_->period = 10000000;
      DOVE_Supplier::pod_rt_info_instance_->importance =
        RtecScheduler::VERY_LOW_IMPORTANCE;
      DOVE_Supplier::pod_rt_info_instance_->quantum = ORBSVCS_Time::zero ();
      DOVE_Supplier::pod_rt_info_instance_->threads = 1;
      DOVE_Supplier::pod_rt_info_instance_->info_type =
        RtecScheduler::OPERATION;
    }

  return DOVE_Supplier::pod_rt_info_instance_;
}