summaryrefslogtreecommitdiff
path: root/apps/Gateway/Gateway/Event_Channel.cpp
blob: 7d3477166ed2f24854585fa99b7576c19eac7a36 (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
/* -*- C++ -*- */
// $Id$

#define ACE_BUILD_SVC_DLL
#include "Connection_Handler_Connector.h"
#include "Event_Channel.h"

Event_Channel::~Event_Channel (void)
{
}

Event_Channel::Event_Channel (void)
  : supplier_acceptor_ (*this, 'S'),
    consumer_acceptor_ (*this, 'C')
{
}

int
Event_Channel::compute_performance_statistics (void)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) doing the performance timeout here...\n"));
  CONNECTION_MAP_ITERATOR cmi (this->connection_map_);

  // If we've got a <ACE_Thread_Manager> then use it to suspend all
  // the threads.  This will enable us to get an accurate count.

  if (Options::instance ()->threading_strategy ()
      != Options::REACTIVE)
    {
      if (ACE_Thread_Manager::instance ()->suspend_all () == -1)
	ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "suspend_all"), -1);
      ACE_DEBUG ((LM_DEBUG, "(%t) suspending all threads..."));
    }

  size_t total_bytes_in = 0;
  size_t total_bytes_out = 0;

  // Iterate through the connection map summing up the number of bytes
  // sent/received.

  for (CONNECTION_MAP_ENTRY *me = 0;
       cmi.next (me) != 0;
       cmi.advance ())
    {
      Connection_Handler *connection_handler = me->int_id_;

      if (connection_handler->connection_role () == 'C')
	total_bytes_out += connection_handler->total_bytes ();
      else // connection_handler->connection_role () == 'S'
	total_bytes_in += connection_handler->total_bytes ();
    }

  ACE_DEBUG ((LM_DEBUG,
              "(%t) after %d seconds, \ntotal_bytes_in = %d\ntotal_bytes_out = %d\n",
	      Options::instance ()->performance_window (),
	      total_bytes_in,
	      total_bytes_out));

  ACE_DEBUG ((LM_DEBUG,
              "(%t) %f Mbits/sec received.\n",
	      (float) (total_bytes_in * 8 /
                       (float) (1024 * 1024 * Options::instance ()->performance_window ()))));

  ACE_DEBUG ((LM_DEBUG,
              "(%t) %f Mbits/sec sent.\n",
	      (float) (total_bytes_out * 8 /
                       (float) (1024 * 1024 * Options::instance ()->performance_window ()))));

  // Resume all the threads again.

  if (Options::instance ()->threading_strategy ()
      != Options::REACTIVE)
    {
      if (ACE_Thread_Manager::instance ()->resume_all () == -1)
	ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "resume_all"), -1);
      ACE_DEBUG ((LM_DEBUG, "(%t) resuming all threads..."));
    }

  return 0;
}

int
Event_Channel::handle_timeout (const ACE_Time_Value &,
				   const void *)
{
  // This is called periodically to compute performance statistics.
  return this->compute_performance_statistics ();
}

// This method forwards the <event> to Consumer that have registered
// to receive it.

int
Event_Channel::put (ACE_Message_Block *event,
                    ACE_Time_Value *)
{
  // We got a valid event, so determine its virtual forwarding
  // address, which is stored in the first of the two event blocks,
  // which are chained together by <ACE::recv>.

  Event_Key *forwarding_addr = (Event_Key *) event->rd_ptr ();

  // Skip over the address portion and get the data.
  ACE_Message_Block *data = event->cont ();

  // <dispatch_set> points to the set of Consumers associated with
  // this forwarding address.
  Consumer_Dispatch_Set *dispatch_set = 0;

  if (this->efd_.find (*forwarding_addr, dispatch_set) == -1)
    // Failure.
    ACE_ERROR ((LM_DEBUG,
		"(%t) find failed on connection id = %d, type = %d\n",
		forwarding_addr->connection_id_,
		forwarding_addr->type_));
  else
    {
      // Check to see if there are any consumers.
      if (dispatch_set->size () == 0)
	ACE_DEBUG ((LM_WARNING,
		   "there are no active consumers for this event currently\n"));

      else // There are consumers, so forward the event.
	{
	  Consumer_Dispatch_Set_Iterator dsi (*dispatch_set);

	  // At this point, we should assign a thread-safe locking
	  // strategy to the Message_Block is we're running in a
	  // multi-threaded configuration.
          data->locking_strategy (Options::instance ()->locking_strategy ());

	  for (Connection_Handler **connection_handler = 0;
	       dsi.next (connection_handler) != 0;
	       dsi.advance ())
	    {
	      // Only process active connection_handlers.
	      if ((*connection_handler)->state () == Connection_Handler::ESTABLISHED)
		{
		  // Duplicate the event portion via reference
		  // counting.
		  ACE_Message_Block *dup_msg = data->duplicate ();

		  ACE_DEBUG ((LM_DEBUG, "(%t) forwarding to Consumer %d\n",
			      (*connection_handler)->connection_id ()));

		  if ((*connection_handler)->put (dup_msg) == -1)
		    {
		      if (errno == EWOULDBLOCK) // The queue has filled up!
			ACE_ERROR ((LM_ERROR, "(%t) %p\n",
				   "gateway is flow controlled, so we're dropping events"));
		      else
			ACE_ERROR ((LM_ERROR,
                                    "(%t) %p transmission error to peer %d\n",
				   "put",
                                    (*connection_handler)->connection_id ()));

		      // We are responsible for releasing an
		      // ACE_Message_Block if failures occur.
		      dup_msg->release ();
		    }
		}
	    }
	}
    }

  // Release the memory in the message block.
  event->release ();
  return 0;
}

int
Event_Channel::svc (void)
{
  return 0;
}

int
Event_Channel::initiate_connection_connection (Connection_Handler *connection_handler)
{
  ACE_Synch_Options synch_options;

  if (Options::instance ()->blocking_semantics () == ACE_NONBLOCK)
    synch_options = ACE_Synch_Options::asynch;
  else
    synch_options = ACE_Synch_Options::synch;

  return this->connector_.initiate_connection (connection_handler,
					       synch_options);
}

int
Event_Channel::complete_connection_connection (Connection_Handler *connection_handler)
{
  int option = connection_handler->connection_role () == 'S' ? SO_RCVBUF : SO_SNDBUF;
  int socket_queue_size = Options::instance ()->socket_queue_size ();

  if (socket_queue_size > 0)
    if (connection_handler->peer ().set_option (SOL_SOCKET,
					   option,
					   &socket_queue_size,
					   sizeof (int)) == -1)
      ACE_ERROR ((LM_ERROR, "(%t) %p\n", "set_option"));

  connection_handler->thr_mgr (ACE_Thread_Manager::instance ());

  // Our state is now "established."
  connection_handler->state (Connection_Handler::ESTABLISHED);

  // Restart the timeout to 1.
  connection_handler->timeout (1);

  ACE_INT32 id = htonl (connection_handler->connection_id ());

  // Send the connection id to the peerd.

  ssize_t n = connection_handler->peer ().send ((const void *) &id, sizeof id);

  if (n != sizeof id)
    ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n",
		      n == 0 ? "peer has closed down unexpectedly" : "send"),
		      -1);
  return 0;
}

// Restart connection (blocking_semantics dicates whether we restart
// synchronously or asynchronously).

int
Event_Channel::reinitiate_connection_connection (Connection_Handler *connection_handler)
{
  // Skip over proxies with deactivated handles.
  if (connection_handler->get_handle () != ACE_INVALID_HANDLE)
    // Make sure to close down peer to reclaim descriptor.
    connection_handler->peer ().close ();

  if (connection_handler->state () != Connection_Handler::DISCONNECTING)
    {
      ACE_DEBUG ((LM_DEBUG,
		  "(%t) scheduling reinitiation of Connection_Handler %d\n",
		  connection_handler->connection_id ()));

      // Reschedule ourselves to try and connect again.
      if (ACE_Reactor::instance ()->schedule_timer
	  (connection_handler, 0, connection_handler->timeout ()) == -1)
	ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n",
			   "schedule_timer"), -1);
    }
  return 0;
}

// Initiate active connections with the Consumer and Supplier Peers.

void
Event_Channel::initiate_connector (void)
{
  if (Options::instance ()->enabled
      (Options::CONSUMER_CONNECTOR | Options::SUPPLIER_CONNECTOR))
    {
      CONNECTION_MAP_ITERATOR cmi (this->connection_map_);

      // Iterate through the Consumer Map connecting all the
      // Connection_Handlers.

      for (CONNECTION_MAP_ENTRY *me = 0;
           cmi.next (me) != 0;
           cmi.advance ())
        {
          Connection_Handler *connection_handler = me->int_id_;

          if (this->initiate_connection_connection (connection_handler) == -1)
            continue; // Failures are handled elsewhere...
        }
    }
}

// Initiate passive acceptor to wait for Consumer and Supplier Peers
// to accept.

int
Event_Channel::initiate_acceptors (void)
{
  if (Options::instance ()->enabled (Options::CONSUMER_ACCEPTOR)
      && this->consumer_acceptor_.open
      (Options::instance ()->consumer_acceptor_port (),
       ACE_Reactor::instance (),
       Options::instance ()->blocking_semantics ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n",
                       "cannot register acceptor"),
                       -1);

  if (Options::instance ()->enabled (Options::SUPPLIER_CONNECTOR)
      && this->supplier_acceptor_.open
      (Options::instance ()->supplier_acceptor_port (),
       ACE_Reactor::instance (),
       Options::instance ()->blocking_semantics ()) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n",
                       "cannot register acceptor"),
                      -1);

  return 0;
}

// This method gracefully shuts down all the Handlers in the
// Connection_Handler Connection Map.

int
Event_Channel::close (u_long)
{
  if (Options::instance ()->threading_strategy ()
      != Options::REACTIVE)
    {
      if (ACE_Thread_Manager::instance ()->suspend_all () == -1)
	ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "suspend_all"), -1);
      ACE_DEBUG ((LM_DEBUG, "(%t) suspending all threads\n"));
    }

  // First tell everyone that the spaceship is here...
  {
    CONNECTION_MAP_ITERATOR cmi (this->connection_map_);

    // Iterate over all the handlers and shut them down.

    for (CONNECTION_MAP_ENTRY *me;
	 cmi.next (me) != 0;
	 cmi.advance ())
      {
	Connection_Handler *connection_handler = me->int_id_;

	ACE_DEBUG ((LM_DEBUG,
                    "(%t) closing down connection %d\n",
		    connection_handler->connection_id ()));

	// Mark Connection_Handler as DISCONNECTING so we don't try to
	// reconnect...
	connection_handler->state (Connection_Handler::DISCONNECTING);
      }
  }

  // Close down the connector
  this->connector_.close ();

  // Close down the supplier acceptor.
  this->supplier_acceptor_.close ();

  // Close down the consumer acceptor.
  this->consumer_acceptor_.close ();

  // Now tell everyone that it is now time to commit suicide.
  {
    CONNECTION_MAP_ITERATOR cmi (this->connection_map_);

    for (CONNECTION_MAP_ENTRY *me;
	 cmi.next (me) != 0;
	 cmi.advance ())
      {
	Connection_Handler *connection_handler = me->int_id_;

	// Deallocate Connection_Handler resources.
	connection_handler->destroy (); // Will trigger a delete.
      }
  }

  return 0;
}

int
Event_Channel::find_proxy (ACE_INT32 connection_id,
			       Connection_Handler *&connection_handler)
{
  return this->connection_map_.find (connection_id,
                                     connection_handler);
}

int
Event_Channel::bind_proxy (Connection_Handler *connection_handler)
{
  int result = this->connection_map_.bind (connection_handler->connection_id (),
                                           connection_handler);

  switch (result)
    {
    case -1:
      ACE_ERROR_RETURN ((LM_ERROR,
			 "(%t) bind failed for connection %d\n",
			 connection_handler->connection_id ()),
                        -1);
      /* NOTREACHED */
    case 1: // Oops, found a duplicate!
      ACE_ERROR_RETURN ((LM_ERROR,
			 "(%t) duplicate connection %d, already bound\n",
			 connection_handler->connection_id ()),
                        -1);
      /* NOTREACHED */
    case 0:
      // Success.
      return 0;
      /* NOTREACHED */
    default:
      ACE_ERROR_RETURN ((LM_DEBUG,
			 "(%t) invalid result %d\n",
                         result),
                        -1);
      /* NOTREACHED */
    }

  return 0;
}

int
Event_Channel::subscribe (const Event_Key &event_addr,
			      Consumer_Dispatch_Set *cds)
{
  int result = this->efd_.bind (event_addr, cds);

  // Bind with consumer map, keyed by peer address.
  switch (result)
    {
    case -1:
      ACE_ERROR_RETURN ((LM_ERROR,
			 "(%t) bind failed for connection %d\n",
			 event_addr.connection_id_),
                        -1);
      /* NOTREACHED */
    case 1: // Oops, found a duplicate!
      ACE_ERROR_RETURN ((LM_DEBUG,
			 "(%t) duplicate consumer map entry %d, "
			 "already bound\n",
                         event_addr.connection_id_),
                        -1);
      /* NOTREACHED */
    case 0:
      // Success.
      return 0;
    default:
      ACE_ERROR_RETURN ((LM_DEBUG,
			 "(%t) invalid result %d\n",
                         result),
                        -1);
      /* NOTREACHED */
    }

  return 0;
}

int
Event_Channel::open (void *)
{
  // Ignore <SIGPIPE> so each <Consumer_Handler> can handle it.
  ACE_Sig_Action sig (ACE_SignalHandler (SIG_IGN), SIGPIPE);
  ACE_UNUSED_ARG (sig);

  // Actively initiate Peer connections.
  this->initiate_connector ();

  // Passively initiate Peer acceptor.
  if (this->initiate_acceptors () == -1)
    return -1;

  // If we're not running reactively, then we need to make sure that
  // <ACE_Message_Block> reference counting operations are
  // thread-safe.  Therefore, we create an <ACE_Lock_Adapter> that is
  // parameterized by <ACE_SYNCH_MUTEX> to prevent race conditions.
  if (Options::instance ()->threading_strategy ()
      != Options::REACTIVE)
    {
      ACE_Lock_Adapter<ACE_SYNCH_MUTEX> *la;

      ACE_NEW_RETURN (la,
                      ACE_Lock_Adapter<ACE_SYNCH_MUTEX>,
                      -1);

      Options::instance ()->locking_strategy (la);
    }

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Lock_Adapter<ACE_SYNCH_MUTEX>;
template class ACE_Map_Entry<ACE_INT32, Connection_Handler *>;
template class ACE_Map_Iterator<ACE_INT32, Connection_Handler *, MAP_MUTEX>;
template class ACE_Map_Reverse_Iterator<ACE_INT32, Connection_Handler *, MAP_MUTEX>;
template class ACE_Map_Iterator_Base<ACE_INT32, Connection_Handler *, MAP_MUTEX>;
template class ACE_Map_Manager<ACE_INT32, Connection_Handler *, MAP_MUTEX>;
template class ACE_Unbounded_Set_Iterator<Connection_Handler *>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Lock_Adapter<ACE_SYNCH_MUTEX>
#pragma instantiate ACE_Map_Entry<ACE_INT32, Connection_Handler *>
#pragma instantiate ACE_Map_Iterator<ACE_INT32, Connection_Handler *, MAP_MUTEX>
#pragma instantiate ACE_Map_Reverse_Iterator<ACE_INT32, Connection_Handler *, MAP_MUTEX>
#pragma instantiate ACE_Map_Iterator_Base<ACE_INT32, Connection_Handler *, MAP_MUTEX>
#pragma instantiate ACE_Map_Manager<ACE_INT32, Connection_Handler *, MAP_MUTEX>
#pragma instantiate ACE_Unbounded_Set_Iterator<Connection_Handler *>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */