summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Event/EC_Gateway_UDP.cpp
blob: 17175554ab6ad6798383399145bfa983428850af (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
// $Id$

#include "orbsvcs/Event/EC_Gateway_UDP.h"
#include "orbsvcs/Event_Utilities.h"
#include "orbsvcs/Time_Utilities.h"

ACE_RCSID(Event, EC_Gateway_UDP, "$Id$")

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

TAO_ECG_UDP_Sender::TAO_ECG_UDP_Sender (void)
{
}

int
TAO_ECG_UDP_Sender::get_local_addr (ACE_INET_Addr& addr)
{
  if (this->dgram_ == 0)
    return -1;
  return this->dgram_->get_local_addr (addr);
}

void
TAO_ECG_UDP_Sender::init (RtecEventChannelAdmin::EventChannel_ptr lcl_ec,
                          RtecScheduler::Scheduler_ptr lcl_sched,
                          const char* lcl_name,
                          RtecUDPAdmin::AddrServer_ptr addr_server,
                          ACE_SOCK_Dgram *dgram,
                          CORBA::Environment &_env)
{
  this->lcl_ec_ =
    RtecEventChannelAdmin::EventChannel::_duplicate (lcl_ec);

  this->addr_server_ =
    RtecUDPAdmin::AddrServer::_duplicate (addr_server);

  this->dgram_ = dgram;

  this->lcl_info_ =
    lcl_sched->create (lcl_name, _env);
  if (_env.exception () != 0) return;

  ACE_Time_Value tv (0, 500);
  TimeBase::TimeT time;
  ORBSVCS_Time::Time_Value_to_TimeT (time, tv);
  lcl_sched->set (this->lcl_info_,
                  RtecScheduler::VERY_HIGH_CRITICALITY,
                  time, time, time,
                  25000 * 10,
                  RtecScheduler::VERY_LOW_IMPORTANCE,
                  time,
                  0,
                  RtecScheduler::OPERATION,
                  _env);
  if (_env.exception () != 0) return;
}

void
TAO_ECG_UDP_Sender::shutdown (CORBA::Environment& _env)
{
  this->close (_env);
  if (_env.exception () == 0) return;
  this->lcl_ec_ = RtecEventChannelAdmin::EventChannel::_nil ();
}

void
TAO_ECG_UDP_Sender::open (RtecEventChannelAdmin::ConsumerQOS& sub,
                          CORBA::Environment &_env)
{
  // ACE_DEBUG ((LM_DEBUG, "ECG (%t) Open gateway\n"));
  if (CORBA::is_nil (this->lcl_ec_.in ()))
    return;

  if (!CORBA::is_nil (this->supplier_proxy_.in ()))
    this->close (_env);
  if (_env.exception () != 0) return;

  if (sub.dependencies.length () == 0)
    return;
  for (CORBA::ULong j = 0; j < sub.dependencies.length (); ++j)
    {
      sub.dependencies[j].rt_info = this->lcl_info_;
    }

  //ACE_DEBUG ((LM_DEBUG, "ECG (%t) Gateway/Supplier "));
  //ACE_SupplierQOS_Factory::debug (pub);

  RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
    this->lcl_ec_->for_consumers (_env);
  if (_env.exception () != 0) return;

  this->supplier_proxy_ =
    consumer_admin->obtain_push_supplier (_env);
  if (_env.exception () != 0) return;

  RtecEventComm::PushConsumer_var consumer_ref =
    this->_this (_env);
  if (_env.exception () != 0) return;

  //ACE_DEBUG ((LM_DEBUG, "ECG (%t) Gateway/Consumer "));
  //ACE_ConsumerQOS_Factory::debug (sub);

  this->supplier_proxy_->connect_push_consumer (consumer_ref.in (),
                                                sub,
                                                _env);
  if (_env.exception () != 0) return;
}

void
TAO_ECG_UDP_Sender::close (CORBA::Environment &env)
{
  // ACE_DEBUG ((LM_DEBUG, "ECG (%t) Closing gateway\n"));
  if (CORBA::is_nil (this->supplier_proxy_.in ()))
    return;

  this->supplier_proxy_->disconnect_push_supplier (env);
  if (env.exception () != 0) return;
  this->supplier_proxy_ =
    RtecEventChannelAdmin::ProxyPushSupplier::_nil ();
}

void
TAO_ECG_UDP_Sender::disconnect_push_consumer (CORBA::Environment &)
{
  ACE_DEBUG ((LM_DEBUG,
              "ECG (%t): Supplier-consumer received "
              "disconnect from channel.\n"));
}

void
TAO_ECG_UDP_Sender::push (const RtecEventComm::EventSet &events,
                          CORBA::Environment & _env)
{
  // ACE_DEBUG ((LM_DEBUG, "ECG_UDP_Sender::push - "));

  if (events.length () == 0)
    {
      ACE_DEBUG ((LM_DEBUG, "no events\n"));
      return;
    }

  // ACE_DEBUG ((LM_DEBUG, "%d event(s) - ", events.length ()));

  // Send each event in a separate message.
  // @@ TODO It is interesting to group events destined to the
  // same mcast group in a single message.
  for (u_int i = 0; i < events.length (); ++i)
    {
      // To avoid loops we keep a TTL field on the events and skip the
      // events with TTL <= 0
      if (events[i].header.ttl <= 0)
        continue;

      const RtecEventComm::Event& e = events[i];

      // We need to modify the TTL field, but copying the entire event
      // would be wasteful; instead we create a new header and only
      // modify the header portion.
      RtecEventComm::EventHeader header = e.header;
      header.ttl--;

      // Grab the right mcast group for this event...
      RtecUDPAdmin::UDP_Addr udp_addr;
      this->addr_server_->get_addr (header, udp_addr, _env);
      TAO_CHECK_ENV_RETURN_VOID(_env);

      // Start building the message
      TAO_OutputCDR cdr;
      cdr.write_boolean (TAO_ENCAP_BYTE_ORDER);
      cdr.write_ulong (0); // Place holder for size...

      // Marshal as if it was a sequence of one element, notice how we
      // marshal a modified version of the header, but the payload is
      // marshal without any extra copies.
      cdr.write_ulong (1);
      cdr.encode (RtecEventComm::_tc_EventHeader, &header, 0, _env);
      TAO_CHECK_ENV_RETURN_VOID(_env);

      cdr.encode (RtecEventComm::_tc_EventData, &e.data, 0, _env);
      TAO_CHECK_ENV_RETURN_VOID(_env);

      // Now fill up the <size> field on the header.
      CORBA::ULong bodylen = cdr.total_length ();
      char* buf = ACE_const_cast(char*,cdr.buffer ());
      buf += 4;
#if !defined (TAO_ENABLE_SWAP_ON_WRITE)
      *ACE_reinterpret_cast(CORBA::ULong*,buf) = bodylen;
#else
      if (!cdr.do_byte_swap ())
        {
          *ACE_reinterpret_cast(CORBA::ULong*, buf) = bodylen;
        }
      else
        {
          CDR::swap_4 (ACE_reinterpret_cast(char*,&bodylen), buf);
        }
#endif

      // This is a good maximum, because Dgrams cannot be longer than
      // 64K and the usual size for a CDR fragment is 512 bytes.
      // @@ TODO In the future we may need to allocate some memory
      // from the heap.
      const int TAO_WRITEV_MAX = 128;
      iovec iov[TAO_WRITEV_MAX];

      int iovcnt = 0;
      for (const ACE_Message_Block* b = cdr.begin ();
           b != cdr.end () && iovcnt < TAO_WRITEV_MAX;
           b = b->cont ())
        {
          iov[iovcnt].iov_base = b->rd_ptr ();
          iov[iovcnt].iov_len =  b->length ();
          iovcnt++;
        }

      ACE_INET_Addr inet_addr (udp_addr.port,
                               udp_addr.ipaddr);
      // ACE_DEBUG ((LM_DEBUG, "sending to (%d,%u)\n",
      // udp_addr.port, udp_addr.ipaddr));

      ssize_t n = this->dgram_->send (iov,
                                      iovcnt,
                                      inet_addr);
      if (n == -1)
        {
          // @@ TODO Use a Event Channel specific exception
          ACE_DEBUG ((LM_DEBUG,
                      "ECG_UDP (%t) send failed %p\n", ""));
          TAO_THROW(CORBA::COMM_FAILURE (CORBA::COMPLETED_NO));
        }
      else if (n == 0)
        {
          // @@ TODO Use a Event Channel specific exception
          ACE_DEBUG ((LM_DEBUG,
                      "ECG_UDP (%t) EOF on send \n"));
          TAO_THROW(CORBA::COMM_FAILURE (CORBA::COMPLETED_NO));
        }
    }
}

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

TAO_ECG_UDP_Receiver::TAO_ECG_UDP_Receiver (void)
{
}

void
TAO_ECG_UDP_Receiver::init (RtecEventChannelAdmin::EventChannel_ptr lcl_ec,
                            RtecScheduler::Scheduler_ptr lcl_sched,
                            const char* lcl_name,
                            const ACE_INET_Addr& ignore_from,
			    RtecUDPAdmin::AddrServer_ptr addr_server,
                            CORBA::Environment &_env)
{
  this->ignore_from_ = ignore_from;

  this->lcl_ec_ =
    RtecEventChannelAdmin::EventChannel::_duplicate (lcl_ec);

  this->addr_server_ = 
    RtecUDPAdmin::AddrServer::_duplicate (addr_server);
  
  this->lcl_info_ =
    lcl_sched->create (lcl_name, _env);
  TAO_CHECK_ENV_RETURN_VOID (_env);

  ACE_Time_Value tv (0, 500);
  TimeBase::TimeT time;
  ORBSVCS_Time::Time_Value_to_TimeT (time, tv);
  lcl_sched->set (this->lcl_info_,
                  RtecScheduler::VERY_HIGH_CRITICALITY,
                  time, time, time,
                  25000 * 10,
                  RtecScheduler::VERY_LOW_IMPORTANCE,
                  time,
                  1,
                  RtecScheduler::REMOTE_DEPENDANT,
                  _env);
  if (_env.exception () != 0) return;
}

void
TAO_ECG_UDP_Receiver::open (RtecEventChannelAdmin::SupplierQOS& pub,
                            CORBA::Environment &_env)
{
  if (CORBA::is_nil (this->lcl_ec_.in ()))
    return;

  if (!CORBA::is_nil (this->consumer_proxy_.in ()))
    this->close (_env);
  if (_env.exception () != 0) return;

  if (pub.publications.length () == 0)
    return;

  for (CORBA::ULong i = 0; i < pub.publications.length (); ++i)
    {
      pub.publications[i].dependency_info.rt_info = this->lcl_info_;
    }

  // = Connect as a supplier to the local EC
  RtecEventChannelAdmin::SupplierAdmin_var supplier_admin =
    this->lcl_ec_->for_suppliers (_env);
  if (_env.exception () != 0) return;

  this->consumer_proxy_ =
    supplier_admin->obtain_push_consumer (_env);
  if (_env.exception () != 0) return;

  RtecEventComm::PushSupplier_var supplier_ref =
    this->_this (_env);
  if (_env.exception () != 0) return;

  // ACE_DEBUG ((LM_DEBUG, "ECG_UDP_Receiver (%t) Gateway/Supplier "));
  // ACE_SupplierQOS_Factory::debug (pub);

  this->consumer_proxy_->connect_push_supplier (supplier_ref.in (),
                                                pub,
                                                _env);
  if (_env.exception () != 0) return;
}

void
TAO_ECG_UDP_Receiver::close (CORBA::Environment &env)
{
  // ACE_DEBUG ((LM_DEBUG, "ECG (%t) Closing gateway\n"));
  if (CORBA::is_nil (this->consumer_proxy_.in ()))
    return;

  this->consumer_proxy_->disconnect_push_consumer (env);
  if (env.exception () != 0) return;
  this->consumer_proxy_ =
    RtecEventChannelAdmin::ProxyPushConsumer::_nil ();

}

void
TAO_ECG_UDP_Receiver::disconnect_push_supplier (CORBA::Environment &)
{
  ACE_DEBUG ((LM_DEBUG,
              "ECG (%t): Supplier received "
              "disconnect from channel.\n"));
}

void
TAO_ECG_UDP_Receiver::shutdown (CORBA::Environment& _env)
{
  this->close (_env);
  if (_env.exception () == 0) return;

  this->lcl_ec_ = RtecEventChannelAdmin::EventChannel::_nil ();
}

int
TAO_ECG_UDP_Receiver::handle_input (ACE_SOCK_Dgram& dgram)
{
  // Use ULong so the alignment is right.
  CORBA::ULong header[2];
  ACE_Addr from;

  ssize_t n = dgram.recv (header, sizeof(header), from, MSG_PEEK);
  if (n == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ECG_UDP_Receive_EH::handle_input - peek\n"), -1);
  else if (n == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ECG_UDP_Receive_EH::handle_input - peek 0\n"),
                      0);

  char* buf = ACE_reinterpret_cast(char*,header);
  int byte_order = buf[0];
  CORBA::ULong length = header[1];

  if (byte_order != TAO_ENCAP_BYTE_ORDER)
    {
      CDR::swap_4 (buf + 4,
                   ACE_reinterpret_cast(char*,&length));
    }

  ACE_Message_Block mb (length + CDR::MAX_ALIGNMENT);
  CDR::mb_align (&mb);
  mb.wr_ptr (length);

  n = dgram.recv (mb.rd_ptr (), length, from);

  if (n == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ECG_UDP_Receive_EH::handle_input - read\n"), -1);
  else if (n == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ECG_UDP_Receive_EH::handle_input - read 0\n"),
                      0);
  // This is to avoid receiving the events we send; notice that we
  // drop the message after reading it.
  if (from == this->ignore_from_)
    return 0;

  TAO_TRY
    {
      TAO_InputCDR cdr (&mb, byte_order);
      cdr.skip_bytes (8); // skip the header...

      RtecEventComm::EventSet events;
      cdr.decode (RtecEventComm::_tc_EventSet, &events, 0,
                  TAO_TRY_ENV);
      TAO_CHECK_ENV;

      this->consumer_proxy_->push (events, TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("ECG_UDP_Receive_EH::handle_input");
    }
  TAO_ENDTRY;
  return 0;
}

void
TAO_ECG_UDP_Receiver::get_addr (const RtecEventComm::EventHeader& header,
				RtecUDPAdmin::UDP_Addr_out addr,
				CORBA::Environment& env)
{
  this->addr_server_->get_addr (header, addr, env);
}

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

TAO_ECG_UDP_EH::TAO_ECG_UDP_EH (TAO_ECG_UDP_Receiver *recv)
  :  receiver_ (recv)
{
}

int
TAO_ECG_UDP_EH::open (const ACE_INET_Addr& ipaddr)
{
  if (this->dgram_.open (ipaddr) == -1)
    return -1;
  return this->reactor ()->register_handler (this,
                                             ACE_Event_Handler::READ_MASK);
}

int
TAO_ECG_UDP_EH::close (void)
{
  if (this->reactor ()->remove_handler (this,
                                        ACE_Event_Handler::READ_MASK) == -1)
    return -1;

  return this->dgram_.close ();
}

int
TAO_ECG_UDP_EH::handle_input (ACE_HANDLE)
{
  return this->receiver_->handle_input (this->dgram_);
}

ACE_HANDLE
TAO_ECG_UDP_EH::get_handle (void) const
{
  return this->dgram_.get_handle ();
}


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

TAO_ECG_Mcast_EH::TAO_ECG_Mcast_EH (TAO_ECG_UDP_Receiver *recv)
  :  receiver_ (recv),
     observer_ (this)
{
}

int
TAO_ECG_Mcast_EH::open (RtecEventChannelAdmin::EventChannel_ptr ec,
			CORBA::Environment& _env)
{
  this->ec_ = RtecEventChannelAdmin::EventChannel::_duplicate (ec);
  RtecEventChannelAdmin::Observer_var obs =
    this->observer_._this (_env);
  TAO_CHECK_ENV_RETURN (_env, -1);

  this->handle_ = this->ec_->append_observer (obs.in (), _env);
  TAO_CHECK_ENV_RETURN (_env, -1);

  return 0;
}

int
TAO_ECG_Mcast_EH::close (CORBA::Environment& _env)
{
  if (this->reactor ()->remove_handler (this,
                                        ACE_Event_Handler::READ_MASK) == -1)
    return -1;

  if (this->dgram_.unsubscribe () == -1)
    return -1;
  
  this->ec_->remove_observer (this->handle_, _env);
  this->handle_ = 0;
  TAO_CHECK_ENV_RETURN (_env, -1);

  return 0;  
}

int
TAO_ECG_Mcast_EH::handle_input (ACE_HANDLE)
{
  return this->receiver_->handle_input (this->dgram_);
}

ACE_HANDLE
TAO_ECG_Mcast_EH::get_handle (void) const
{
  return this->dgram_.get_handle ();
}

int
TAO_ECG_Mcast_EH::subscribe (const ACE_INET_Addr &mcast_addr)
{
  return this->dgram_.subscribe (mcast_addr);
}

int
TAO_ECG_Mcast_EH::unsubscribe (const ACE_INET_Addr &mcast_addr)
{
  return this->dgram_.unsubscribe (mcast_addr);
}

void
TAO_ECG_Mcast_EH::update_consumer (const RtecEventChannelAdmin::ConsumerQOS& sub,
				   CORBA::Environment& _env)
{
  // ACE_DEBUG ((LM_DEBUG,
  //	      "ECG_Mcast_EH (%t) updating consumer\n"));

  // @@ TODO: If we are more careful we may simply subscribe for the
  // new event types and unsubscribe from the old ones...
  this->reactor ()->remove_handler (this,
				    ACE_Event_Handler::READ_MASK);
  this->dgram_.close ();

  int must_register = 0;
  for (CORBA::ULong i = 0; i < sub.dependencies.length (); ++i)
    {
      const RtecEventComm::EventHeader& header =
	sub.dependencies[i].event.header;

      if (0 <= header.type && header.type <= ACE_ES_EVENT_UNDEFINED)
	{
	  // ACE_DEBUG ((LM_DEBUG,
	  //	      "ECG_Mcast_EH (%t) type = %d skipped\n",
	  //	      header.type));
	  continue;
	}
      must_register = 1;
      RtecUDPAdmin::UDP_Addr addr;

      this->receiver_->get_addr (header, addr, _env);
      TAO_CHECK_ENV_RETURN_VOID (_env);

      ACE_INET_Addr inet_addr (addr.port, addr.ipaddr);
      if (this->subscribe (inet_addr) == -1)
	ACE_ERROR ((LM_DEBUG,
		    "cannot subscribe to %s:%d\n",
		    inet_addr.get_host_addr (),
		    inet_addr.get_port_number ()));
      // ACE_DEBUG ((LM_DEBUG,
      //	  "ECG_Mcast_EH (%t) subscribed to %s:%d\n",
      //	  inet_addr.get_host_addr (),
      //	  inet_addr.get_port_number ()));
    }

  if (must_register)
    this->reactor ()->register_handler (this,
					ACE_Event_Handler::READ_MASK);
}

void
TAO_ECG_Mcast_EH::update_supplier (const RtecEventChannelAdmin::SupplierQOS&,
				   CORBA::Environment&)
{
  // Do nothing
}

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

TAO_ECG_Mcast_EH::Observer::Observer (TAO_ECG_Mcast_EH* eh)
  :  eh_ (eh)
{
}

void
TAO_ECG_Mcast_EH::Observer::update_consumer (const RtecEventChannelAdmin::ConsumerQOS& sub,
					     CORBA::Environment& _env)
{
  this->eh_->update_consumer (sub, _env);
}

void
TAO_ECG_Mcast_EH::Observer::update_supplier (const
					     RtecEventChannelAdmin::SupplierQOS& pub,
					     CORBA::Environment& _env)
{
  this->eh_->update_supplier (pub, _env);
}

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

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */