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

#define ACE_BUILD_SVC_DLL
#include "ace/Get_Opt.h"
#include "Config_Files.h"
#include "Proxy_Handler_Connector.h"
#include "Event_Channel.h"

#if !defined (ACE_EVENT_CHANNEL_C)
#define ACE_EVENT_CHANNEL_C

template <class SH, class CH> 
ACE_Event_Channel<SH, CH>::~ACE_Event_Channel (void)
{
}

template <class SH, class CH> 
ACE_Event_Channel<SH, CH>::ACE_Event_Channel (void)
  : connection_config_file_ ("connection_config"),
    consumer_config_file_ ("consumer_config"),
    active_connector_role_ (1),
    performance_window_ (0),
    blocking_semantics_ (ACE_NONBLOCK),
    debug_ (0),
    connector_ (0),
    socket_queue_size_ (0)
{
}

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::handle_timeout (const ACE_Time_Value &, 
					   const void *)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) doing the performance timeout here...\n"));
  CONNECTION_MAP_ITERATOR cti (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 defined (USE_OUTPUT_MT) || defined (USE_INPUT_MT)
  if (ACE_Service_Config::thr_mgr ()->suspend_all () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "suspend_all"), -1);
  ACE_DEBUG ((LM_DEBUG, "(%t) suspending all threads..."));
#endif /* USE_INPUT_MT || USE_OUTPUT_MT */

  size_t total_bytes_in = 0;
  size_t total_bytes_out = 0;
  
  // Iterate through the consumer map connecting all the Proxy_Handlers.

  for (CONNECTION_MAP_ENTRY *me = 0;
       cti.next (me) != 0; 
       cti.advance ())
    {
      Proxy_Handler *proxy_handler = me->int_id_;

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

#if defined (ACE_NLOGGING)
  ACE_OS::fprintf (stderr, "After %d seconds, \ntotal_bytes_in = %d\ntotal_bytes_out = %d\n",
		   performance_window_,
		   total_bytes_in, 
		   total_bytes_out);

  ACE_OS::fprintf (stderr, "%f Mbits/sec received.\n", 
		   (float) (total_bytes_in * 8 / (float) (1024*1024*this->performance_window_)));

  ACE_OS::fprintf (stderr, "%f Mbits/sec sent.\n", 
		   (float) (total_bytes_out * 8 / (float) (1024*1024*this->performance_window_)));
#else
  ACE_DEBUG ((LM_DEBUG, "(%t) after %d seconds, \ntotal_bytes_in = %d\ntotal_bytes_out = %d\n",
	      this->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*this->performance_window_))));
  ACE_DEBUG ((LM_DEBUG, "(%t) %f Mbits/sec sent.\n", 
	      (float) (total_bytes_out * 8 / (float) (1024*1024*this->performance_window_))));
#endif /* ACE_NLOGGING */

#if defined (USE_INPUT_MT) || defined (USE_OUTPUT_MT)
  ACE_DEBUG ((LM_DEBUG, "(%t) resuming all threads..."));

  // Resume all the threads again.

  if (ACE_Service_Config::thr_mgr ()->resume_all () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "resume_all"), -1);
#endif /* USE_INPUT_MT || USE_OUTPUT_MT */

  return 0;
}

// Initiate connections with the Consumer and Supplier Peers.

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::initiate_connections (void)
{
  CONNECTION_MAP_ITERATOR cti (this->connection_map_);
				
  ACE_Synch_Options synch_options;
  
  if (this->blocking_semantics_ == ACE_NONBLOCK)
    synch_options = ACE_Synch_Options::asynch;
  else 
    synch_options = ACE_Synch_Options::synch;

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

  for (CONNECTION_MAP_ENTRY *me = 0;
       cti.next (me) != 0;
       cti.advance ())
    {
      Proxy_Handler *proxy_handler = me->int_id_;

      if (this->connector_->initiate_connection 
	  (proxy_handler, synch_options) == -1)
	continue;
    }

  return 0;
}

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

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::close (void)
{
#if defined (USE_INPUT_MT) || defined (USE_OUTPUT_MT)
  ACE_DEBUG ((LM_DEBUG, "(%t) suspending all threads\n"));
  if (ACE_Service_Config::thr_mgr ()->suspend_all () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", "suspend_all"), -1);
#endif /* USE_INPUT_MT || USE_OUTPUT_MT */

  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 ())
    {
      Proxy_Handler *proxy_handler = me->int_id_;

      ACE_DEBUG ((LM_DEBUG, "(%t) closing down route %d\n",
		 proxy_handler->id ()));

      if (proxy_handler->state () != Proxy_Handler::IDLE)
	// Mark Proxy_Handler as DISCONNECTING so we don't try to
	// reconnect...
	proxy_handler->state (Proxy_Handler::DISCONNECTING);

      // Deallocate Proxy_Handler resources.
      proxy_handler->destroy (); // Will trigger a delete.
    }

  // Free up the resources allocated dynamically by the ACE_Connector.
  delete this->connector_;
  return 0;
}

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::open (int argc, char *argv[])
{
  this->parse_args (argc, argv);

  ACE_NEW_RETURN (this->connector_, Proxy_Handler_Connector (), -1);

  // Ignore SIPPIPE so each Consumer_Proxy can handle it.
  ACE_Sig_Action sig (ACE_SignalHandler (SIG_IGN), SIGPIPE);

  if (this->active_connector_role_)
    {
      // Parse the connection configuration file.
      this->parse_connection_config_file ();

      // Parse the consumer map config file and build the consumer map.
      this->parse_consumer_config_file ();

      // Initiate connections with the peers.
      this->initiate_connections ();
    }

  // If this->performance_window_ > 0 start a timer.

  if (this->performance_window_ > 0)
    {
      if (ACE_Service_Config::reactor ()->schedule_timer 
	  (this, 0, this->performance_window_) == -1)
	ACE_ERROR ((LM_ERROR, "(%t) %p\n", "schedule_timer"));
      else
	ACE_DEBUG ((LM_DEBUG, "starting timer for %d seconds...\n", 
		   this->performance_window_));
    }

  return 0;
}

// Parse and build the connection table.

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::parse_connection_config_file (void)
{
  // File that contains the consumer map configuration information.
  Connection_Config_File_Parser connection_file;
  Connection_Config_File_Entry entry;
  int file_empty = 1;
  int line_number = 0;

  if (connection_file.open (this->connection_config_file_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", this->connection_config_file_), -1);

  // Read config file one line at a time.
  while (connection_file.read_entry (entry, line_number) != FP::EOFILE)
    {
      file_empty = 0;

      if (this->debug_)
	ACE_DEBUG ((LM_DEBUG, "(%t) conn id = %d, host = %s, remote port = %d, "
		    "direction = %c, max retry timeout = %d, local port = %d\n",
		    entry.conn_id_, 
		    entry.host_, 
		    entry.remote_poconsumer_, 
		    entry.direction_,
		    entry.max_retry_delay_, 
		    entry.local_poconsumer_));

      Proxy_Handler *proxy_handler = 0;

      // The next few lines of code are dependent on whether we are
      // making an Supplier_Proxy or an Consumer_Proxy.

      if (entry.direction_ == 'C') // Configure a Consumer_Proxy.
	ACE_NEW_RETURN (proxy_handler,
			CONSUMER_HANDLER (&this->efd_, 
					  this->connector_, 
					  ACE_Service_Config::thr_mgr (),
					  this->socket_queue_size_),
			-1);
      else /* direction == 'S' */ // Configure a Supplier_Proxy.
	ACE_NEW_RETURN (proxy_handler,
			SUPPLIER_HANDLER (&this->efd_, 
					  this->connector_, 
					  ACE_Service_Config::thr_mgr (),
					  this->socket_queue_size_),
			-1);

      // The following code is common to both Supplier_Proxys_ and
      // Consumer_Proxys.

      // Initialize the routing entry's peer addressing info.
      proxy_handler->bind (ACE_INET_Addr (entry.remote_poconsumer_, entry.host_), 
			ACE_INET_Addr (entry.local_poconsumer_), entry.conn_id_);

      // Initialize max timeout.
      proxy_handler->max_timeout (entry.max_retry_delay_);

      // Try to bind the new Proxy_Handler to the connection ID.
      switch (this->connection_map_.bind (entry.conn_id_, proxy_handler))
	{
	case -1:
	  ACE_ERROR_RETURN ((LM_ERROR, 
			     "(%t) bind failed for connection %d\n", 
			    entry.conn_id_), -1);
	  /* NOTREACHED */
	case 1: // Oops, found a duplicate!
	  ACE_DEBUG ((LM_DEBUG, 
		      "(%t) duplicate connection %d, already bound\n", 
		     entry.conn_id_));
	  break;
	case 0:
	  // Success.
	  break;
	}
    }

  if (file_empty)
    ACE_ERROR ((LM_WARNING, 
	       "warning: connection proxy_handler configuration file was empty\n"));
  return 0;
}

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::parse_consumer_config_file (void)
{
  // File that contains the consumer map configuration information.
  Consumer_Config_File_Parser consumer_file;
  Consumer_Config_File_Entry  entry;  
  int file_empty = 1;
  int line_number = 0;

  if (consumer_file.open (this->consumer_config_file_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "(%t) %p\n", this->consumer_config_file_), -1);

  // Read config file line at a time.
  while (consumer_file.read_entry (entry, line_number) != FP::EOFILE)
    {
      file_empty = 0;

      if (this->debug_)
	{
	  ACE_DEBUG ((LM_DEBUG, "(%t) conn id = %d, logical id = %d, payload = %d, "
		      "number of destinations = %d\n",
		      entry.conn_id_, 
		      entry.supplier_id_, 
		      entry.type_,
		      entry.total_destinations_));
	  for (int i = 0; i < entry.total_destinations_; i++)
	    ACE_DEBUG ((LM_DEBUG, "(%t) destination[%d] = %d\n",
		       i, entry.destinations_[i]));
	}

      Dispatch_Set *dispatch_set;
      ACE_NEW_RETURN (dispatch_set, Dispatch_Set, -1);

      Event_Addr event_addr (entry.conn_id_, 
			     entry.supplier_id_, 
			     entry.type_);

      // Add the destinations to the Routing Entry.
      for (int i = 0; i < entry.total_destinations_; i++)
	{
	  Proxy_Handler *proxy_handler = 0;

	  // Lookup destination and add to Dispatch_Set set if found.
	  if (this->connection_map_.find (entry.destinations_[i], 
					  proxy_handler) != -1)
	    dispatch_set->insert (proxy_handler);
	  else
	    ACE_ERROR ((LM_ERROR, "(%t) not found: destination[%d] = %d\n", 
		       i, entry.destinations_[i]));
	}

      // Bind with consumer map, keyed by peer address.
      switch (this->efd_.bind (event_addr, dispatch_set))
	{
	case -1:
	  ACE_ERROR_RETURN ((LM_ERROR, "(%t) bind failed for connection %d\n", 
			    entry.conn_id_), -1);
	  /* NOTREACHED */
	case 1: // Oops, found a duplicate!
	  ACE_DEBUG ((LM_DEBUG, "(%t) duplicate consumer map entry %d, "
		     "already bound\n", entry.conn_id_));
	  break;
	case 0:
	  // Success.
	  break;
	}
    }

  if (file_empty)
    ACE_ERROR ((LM_WARNING, 
	       "warning: consumer map configuration file was empty\n"));
  return 0;
}

// Parse the "command-line" arguments and set the corresponding flags.

template <class SH, class CH> int
ACE_Event_Channel<SH, CH>::parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, "bc:dpr:q:w:", 0);

  for (int c; (c = get_opt ()) != -1; )
    {
      switch (c)
	{
	case 'b': // Use blocking connection establishment.
	  this->blocking_semantics_ = 0;
	  break;
	case 'c':
	  this->connection_config_file_ = get_opt.optarg;
	  break;
	case 'd':
	  this->debug_ = 1;
	  break;
	case 'p':
	  // We are not playing the active Connector role.
	  this->active_connector_role_ = 0;
	  break;
	case 'q':
	  this->socket_queue_size_ = ACE_OS::atoi (get_opt.optarg);
	  break;
	case 'r':
	  this->consumer_config_file_ = get_opt.optarg;
	  break;
	case 'w': // Time performance for a designated amount of time.
	  this->performance_window_ = ACE_OS::atoi (get_opt.optarg);
	  // Use blocking connection semantics so that we get accurate
	  // timings (since all connections start at once).
	  this->blocking_semantics_ = 0;
	  break;
	default:
	  break;
	}
    }
  return 0;
}

#endif /* ACE_EVENT_CHANNEL_C */