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

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

// Static pointer member initialization for Singleton.

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

// Constructor.

DOVE_Supplier::DOVE_Supplier ()
  : initialized_ (0),
    internal_DOVE_Supplier_ptr_ (new Internal_DOVE_Supplier (this)),
    MIB_name_ (0),
    es_name_ (0),
    ss_name_ (0)
{
}

// Destructor.

DOVE_Supplier::~DOVE_Supplier ()
{
  delete internal_DOVE_Supplier_ptr_;
}

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

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

    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 (), TAO_TRY_ENV);
    TAO_CHECK_ENV;

    this->poa_manager_ =
       root_POA_var_->the_POAManager (TAO_TRY_ENV);
    TAO_CHECK_ENV;

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

    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 (),
                                       TAO_TRY_ENV);
    TAO_CHECK_ENV;
  }
  TAO_CATCHANY
  {
    TAO_TRY_ENV.print_exception ("DOVE_Supplier::init");
    return -1;
  }
  TAO_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 * rt_info)
{
  // Save the passed MIB and Event Service names
  MIB_name_ = (MIB_name == 0) ? "MIB_unknown" : MIB_name;
  es_name_ = (es_name == 0) ?  "EventService" : es_name;
  ss_name_ = (ss_name == 0) ?  "ScheduleService" : ss_name;

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

  // Resolve the event service reference.
  if (this->get_EventChannel () == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Unable to resolved 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 (rt_info) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         " (%P|%t) Unable to connect to the event service.\n"),
                        -1);
    }

  return 0;

}


void
DOVE_Supplier::disconnect ()
{
}


void
DOVE_Supplier::notify (CORBA::Any &message)
{
  TAO_TRY
  {
    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
    proxyPushConsumer_var_->push (events,
                                 TAO_TRY_ENV);
    TAO_CHECK_ENV;
  }
  TAO_CATCHANY
  {
    ACE_ERROR ((LM_ERROR,
                "DOVE_Supplier::notify: "
                "unexpected exception.\n"));
  }
  TAO_ENDTRY;
}


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

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

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

int
DOVE_Supplier::get_Scheduler ()
{
  TAO_TRY
    {
      CosNaming::Name schedule_name (1);
      schedule_name.length (1);
      schedule_name[0].id = CORBA::string_dup (ss_name_);

      CORBA::Object_var objref =
          namingContext_var_->resolve (schedule_name,
                                       TAO_TRY_ENV);
      TAO_CHECK_ENV;

      scheduler_var_ =
        RtecScheduler::Scheduler::_narrow(objref.in (),
                                            TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      scheduler_var_ = 0;
      ACE_ERROR_RETURN ((LM_ERROR,
                         "DOVE_Supplier::get_Scheduler: "
                         "error while resolving scheduler %s\n", ss_name_),
                        -1);
    }
  TAO_ENDTRY;

  return 0;
}


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

    CORBA::Object_var eventServiceObj_var =
      this->namingContext_var_->resolve (channel_name, TAO_TRY_ENV);
    TAO_CHECK_ENV;

    this->eventChannel_var_ =
       RtecEventChannelAdmin::EventChannel::_narrow (eventServiceObj_var.in(),
                                                   TAO_TRY_ENV);
    TAO_CHECK_ENV;

    if (CORBA::is_nil (eventChannel_var_.in()))
      ACE_ERROR_RETURN ((LM_ERROR,
                         "The reference to the event channel is nil!"),
                         1);
  }
  TAO_CATCHANY
  {
    TAO_TRY_ENV.print_exception ("DOVE_Supplier::get_EventChannel");
    return -1;
  }
  TAO_ENDTRY;

  return 0;
}


int
DOVE_Supplier::connect_Supplier (ACE_Scheduler_Factory::POD_RT_Info * rt_info)
{
  if (rt_info == 0)
    {
      // Get the default singleton if we were not passed the data
      rt_info = DOVE_Supplier::rt_info_instance ();
      if (rt_info == 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             " (%P|%t) Unable to obtain"
                             " the default RT_Info data.\n"),
                            -1);
        }
    }

  TAO_TRY
  {
    // Generate the Real-time information descriptor.
    this->rt_info_ = scheduler_var_->create (rt_info->entry_point, 
                                             TAO_TRY_ENV);

    this->scheduler_var_->set (this->rt_info_,
                               ACE_static_cast (RtecScheduler::Criticality_t,
	                                        rt_info->criticality),
                               rt_info->worst_case_execution_time,
                               rt_info->typical_execution_time,
                               rt_info->cached_execution_time,
                               rt_info->period,
                               ACE_static_cast (RtecScheduler::Importance_t,
                                                rt_info->importance),
                               rt_info->quantum,
                               rt_info->threads,
                               ACE_static_cast (RtecScheduler::Info_Type_t,
                                                rt_info->info_type),
                               TAO_TRY_ENV);
    TAO_CHECK_ENV;


    // 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.replace (CORBA::_tc_short,
						      &x,
						      0,
						      TAO_TRY_ENV);
    TAO_CHECK_ENV;
    qos.publications[0].dependency_info.number_of_calls = 1;
    qos.publications[0].dependency_info.rt_info = this->rt_info_;

    // = Connect as a supplier.
    this->supplierAdmin_var_ =
      this->eventChannel_var_->for_suppliers (TAO_TRY_ENV);
    TAO_CHECK_ENV;

    this->proxyPushConsumer_var_ =
      this->supplierAdmin_var_->obtain_push_consumer (TAO_TRY_ENV);
    TAO_CHECK_ENV;

    // 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 (TAO_TRY_ENV);
    TAO_CHECK_ENV;

    // Connect the supplier to the proxy consumer.
    ACE_SupplierQOS_Factory::debug (qos);
    this->proxyPushConsumer_var_->connect_push_supplier (pushSupplier_var.in (),
                                                         qos,
                                                         TAO_TRY_ENV);
    TAO_CHECK_ENV;
  }
  TAO_CATCHANY
  {
    TAO_TRY_ENV.print_exception ("DOVE_Supplier::open");
    return -1;
  }
  TAO_ENDTRY;

  return 0;
}


// Access the default rt_info singleton.

ACE_Scheduler_Factory::POD_RT_Info * 
DOVE_Supplier::rt_info_instance ()
{
  if (! rt_info_instance_)
    {
      ACE_NEW_RETURN (rt_info_instance_,
                      ACE_Scheduler_Factory::POD_RT_Info,
                      0);

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

  return rt_info_instance_;
}