summaryrefslogtreecommitdiff
path: root/ace/Connector.h
blob: 55701369f98b8c115ac270a54eeb2107566cbd36 (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
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Connector.h
 *
 *  $Id$
 *
 *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
 */
//=============================================================================

#ifndef ACE_CONNECTOR_H
#define ACE_CONNECTOR_H

#include /**/ "ace/pre.h"

#include "ace/Service_Object.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Strategies_T.h"
#include "ace/Synch_Options.h"

/**
 * @class ACE_Connector_Base
 *
 * @brief This base interface allows ACE_NonBlocking_Connect_Handler
 * to only care about the SVC_HANDLER template parameter of the
 * ACE_Connector.  Otherwise, ACE_NonBlocking_Connect_Handler would
 * have to be configured with all the template parameters that
 * ACE_Connector is configured with.
 */
template <class SVC_HANDLER>
class ACE_Connector_Base
{
public:

  /// Initialize the Svc_Handler.
  virtual void initialize_svc_handler (ACE_HANDLE handle,
                                       SVC_HANDLER *svc_handler) = 0;

  /// Return the handle set representing the non-blocking connects in
  /// progress.
  virtual ACE_Handle_Set &non_blocking_handles (void) = 0;
};

/**
 * @class ACE_NonBlocking_Connect_Handler
 *
 * @brief Performs non-blocking connects on behalf of the Connector.
 */
template <class SVC_HANDLER>
class ACE_NonBlocking_Connect_Handler : public ACE_Event_Handler
{
public:

  // Constructor.
  ACE_NonBlocking_Connect_Handler (ACE_Connector_Base<SVC_HANDLER> &connector,
                                   SVC_HANDLER *,
                                   long timer_id);

  /// Close up and return underlying SVC_HANDLER *.
  SVC_HANDLER *close (void);

  /// Get SVC_HANDLER.
  SVC_HANDLER *svc_handler (void);

  // = Get/set handle.
  /// Get handle.
  ACE_HANDLE handle (void);

  /// Set handle.
  void handle (ACE_HANDLE);

  // = Set/get timer id.
  /// Get timer id.
  long timer_id (void);

  /// Set timer id.
  void timer_id (long timer_id);

  /// Called by ACE_Reactor when asynchronous connections fail.
  virtual int handle_input (ACE_HANDLE);

  /// Called by ACE_Reactor when asynchronous connections succeed.
  virtual int handle_output (ACE_HANDLE);

  /// Called by ACE_Reactor when asynchronous connections suceeds (on
  /// some platforms only).
  virtual int handle_exception (ACE_HANDLE fd);

  /// This method is called if a connection times out before
  /// completing.
  virtual int handle_timeout (const ACE_Time_Value &tv,
                              const void *arg);

  /// Should Reactor resume us if we have been suspended before the
  /// upcall?
  virtual int resume_handler (void);

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

private:

  /// Connector base.
  ACE_Connector_Base<SVC_HANDLER> &connector_;

  /// Associated SVC_HANDLER.
  SVC_HANDLER *svc_handler_;

  /// Associated timer id.
  long timer_id_;
};

/**
 * @class ACE_Connector
 *
 * @brief Generic factory for actively connecting clients and creating
 * service handlers (SVC_HANDLERs).
 *
 * Implements the strategy for actively establishing connections with
 * clients.  An ACE_Connector is parameterized by concrete types that
 * conform to the interfaces of PEER_CONNECTOR and SVC_HANDLER.  The
 * PEER_CONNECTOR is instantiated with a transport mechanism that
 * actively establishes connections.  The SVC_HANDLER is instantiated
 * with a concrete type that performs the application-specific
 * service.  Both blocking and non-blocking connects are supported.
 * Further, non-blocking connects support timeouts.
 */
template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1>
class ACE_Connector : public ACE_Connector_Base<SVC_HANDLER>, public ACE_Service_Object
{
public:

  // Useful STL-style traits.
  typedef ACE_TYPENAME SVC_HANDLER::addr_type    addr_type;
  typedef ACE_PEER_CONNECTOR                     connector_type;
  typedef SVC_HANDLER                            handler_type;
  typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;

  // typedef ACE_TYPENAME ACE_PEER_CONNECTOR_ADDR PEER_ADDR;
#if defined (ACE_HAS_TYPENAME_KEYWORD)
  typedef ACE_PEER_CONNECTOR_ADDR ACE_PEER_ADDR_TYPEDEF;
#endif /* ACE_HAS_TYPENAME_KEYWORD */

  typedef ACE_TYPENAME _ACE_PEER_CONNECTOR::PEER_ADDR
  ACE_TYPENAME_ACE_PEER_CONNECTOR_PEER_ADDR;

  /**
   * Initialize a connector.  @a flags indicates how <SVC_HANDLER>'s
   * should be initialized prior to being activated.  Right now, the
   * only flag that is processed is <ACE_NONBLOCK>, which enabled
   * non-blocking I/O on the <SVC_HANDLER> when it is opened.
   */
  ACE_Connector (ACE_Reactor *r = ACE_Reactor::instance (),
                 int flags = 0);

  /**
   * Initialize a connector.  @a flags indicates how <SVC_HANDLER>'s
   * should be initialized prior to being activated.  Right now, the
   * only flag that is processed is <ACE_NONBLOCK>, which enabled
   * non-blocking I/O on the <SVC_HANDLER> when it is opened.
   */
  virtual int open (ACE_Reactor *r = ACE_Reactor::instance (),
                    int flags = 0);

  /// Shutdown a connector and release resources.
  virtual ~ACE_Connector (void);

  // = Connection establishment methods.

  /**
   * Initiate connection of <svc_handler> to peer at <remote_addr>
   * using <synch_options>.  If the caller wants to designate the
   * selected <local_addr> they can (and can also insist that the
   * <local_addr> be reused by passing a value <reuse_addr> ==
   * 1). <flags> and <perms> can be used to pass any flags that are
   * needed to perform specific operations such as opening a file
   * within connect with certain permissions.  If the connection fails
   * the <close> hook on the <svc_handler> will be called
   * automatically to prevent resource leaks.
   */
  virtual int connect (SVC_HANDLER *&svc_handler,
                       const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                       const ACE_Synch_Options &synch_options = ACE_Synch_Options::defaults,
                       const ACE_PEER_CONNECTOR_ADDR &local_addr
                       = (ACE_TYPENAME_ACE_PEER_CONNECTOR_PEER_ADDR &) ACE_PEER_CONNECTOR_ADDR_ANY,
                       int reuse_addr = 0,
                       int flags = O_RDWR,
                       int perms = 0);

  /**
   * This is a variation on the previous <connect> method.  On cached
   * connectors the <svc_handler_hint> variable can be used as a hint
   * for future lookups.  Since this variable is modified in the
   * context of the internal cache its use is thread-safe.  But the
   * actual svc_handler for the current connection is returned in the
   * second parameter <svc_handler>.  If the connection fails the
   * <close> hook on the <svc_handler> will be called automatically to
   * prevent resource leaks.
   */
  virtual int connect (SVC_HANDLER *&svc_handler_hint,
                       SVC_HANDLER *&svc_handler,
                       const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                       const ACE_Synch_Options &synch_options = ACE_Synch_Options::defaults,
                       const ACE_PEER_CONNECTOR_ADDR &local_addr
                       = (ACE_TYPENAME_ACE_PEER_CONNECTOR_PEER_ADDR &) ACE_PEER_CONNECTOR_ADDR_ANY,
                       int reuse_addr = 0,
                       int flags = O_RDWR,
                       int perms = 0);

  /**
   * Initiate connection of <n> <svc_handlers> to peers at
   * <remote_addrs> using <synch_options>.  Returns -1 if failure
   * occurs and 0 otherwise.  If <failed_svc_handlers> is non-NULL, a
   * 1 is placed in the corresponding index of <failed_svc_handler>
   * for each <svc_handlers[i]> that failed to connect, else a 0 is
   * placed in that index.
   */
  virtual int connect_n (size_t n,
                         SVC_HANDLER *svc_handlers[],
                         ACE_PEER_CONNECTOR_ADDR remote_addrs[],
                         ACE_TCHAR *failed_svc_handlers = 0,
                         const ACE_Synch_Options &synch_options =
                         ACE_Synch_Options::defaults);

  /**
   * Cancel a <svc_handler> that was started asynchronously. Note that
   * this is the only case when the Connector does not actively close
   * the <svc_handler>. It is left up to the caller of <cancel> to
   * decide the fate of the <svc_handler>.
   */
  virtual int cancel (SVC_HANDLER *svc_handler);

  /// Close down the Connector.  All pending non-blocking connects are
  /// canceled and the corresponding svc_handler is closed.
  virtual int close (void);

  /// Return the underlying PEER_CONNECTOR object.
  virtual ACE_PEER_CONNECTOR &connector (void) const;

  /// Initialize Svc_Handler.
  virtual void initialize_svc_handler (ACE_HANDLE handle,
                                       SVC_HANDLER *svc_handler);

  /// Set Reactor.
  virtual void reactor (ACE_Reactor *reactor);

  /// Get Reactor.
  virtual ACE_Reactor *reactor (void) const;

  /// Dump the state of an object.
  void dump (void) const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

protected:
  // = Helpful typedefs.
  typedef ACE_NonBlocking_Connect_Handler<SVC_HANDLER> NBCH;

  // = The following two methods define the Connector's strategies for
  // creating, connecting, and activating SVC_HANDLER's, respectively.

  /**
   * Bridge method for creating a SVC_HANDLER.  The default is to
   * create a new SVC_HANDLER only if <sh> == 0, else <sh> is
   * unchanged.  However, subclasses can override this policy to
   * perform SVC_HANDLER creation in any way that they like (such as
   * creating subclass instances of SVC_HANDLER, using a singleton,
   * dynamically linking the handler, etc.).  Returns -1 if failure,
   * else 0.
   */
  virtual int make_svc_handler (SVC_HANDLER *&sh);

  /**
   * Bridge method for connecting the <svc_handler> to the
   * <remote_addr>.  The default behavior delegates to the
   * <PEER_CONNECTOR::connect>.
   */
  virtual int connect_svc_handler (SVC_HANDLER *&svc_handler,
                                   const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                                   ACE_Time_Value *timeout,
                                   const ACE_PEER_CONNECTOR_ADDR &local_addr,
                                   int reuse_addr,
                                   int flags,
                                   int perms);
  virtual int connect_svc_handler (SVC_HANDLER *&svc_handler,
                                   SVC_HANDLER *&sh_copy,
                                   const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                                   ACE_Time_Value *timeout,
                                   const ACE_PEER_CONNECTOR_ADDR &local_addr,
                                   int reuse_addr,
                                   int flags,
                                   int perms);

  /**
   * Bridge method for activating a <svc_handler> with the appropriate
   * concurrency strategy.  The default behavior of this method is to
   * activate the SVC_HANDLER by calling its <open> method (which
   * allows the SVC_HANDLER to define its own concurrency strategy).
   * However, subclasses can override this strategy to do more
   * sophisticated concurrency activations (such as creating the
   * SVC_HANDLER as an "active object" via multi-threading or
   * multi-processing).
   */
  virtual int activate_svc_handler (SVC_HANDLER *svc_handler);

  /// Creates and registers ACE_NonBlocking_Connect_Handler.
  int nonblocking_connect (SVC_HANDLER *,
                           const ACE_Synch_Options &);

  /// Implementation the <connect> methods.
  virtual int connect_i (SVC_HANDLER *&svc_handler,
                         SVC_HANDLER **sh_copy,
                         const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                         const ACE_Synch_Options &synch_options,
                         const ACE_PEER_CONNECTOR_ADDR &local_addr,
                         int reuse_addr,
                         int flags,
                         int perms);

  /// Return the handle set representing the non-blocking connects in
  /// progress.
  ACE_Handle_Set &non_blocking_handles (void);

  // = Dynamic linking hooks.
  /// Default version does no work and returns -1.  Must be overloaded
  /// by application developer to do anything meaningful.
  virtual int init (int argc, ACE_TCHAR *argv[]);

  /// Calls <handle_close> to shutdown the Connector gracefully.
  virtual int fini (void);

  /// Default version returns address info in <buf>.
  virtual int info (ACE_TCHAR **, size_t) const;

  // = Service management hooks.
  /// Default version does no work and returns -1.  Must be overloaded
  /// by application developer to do anything meaningful.
  virtual int suspend (void);

  /// Default version does no work and returns -1.  Must be overloaded
  /// by application developer to do anything meaningful.
  virtual int resume (void);

private:
  /// This is the peer connector factory.
  ACE_PEER_CONNECTOR connector_;

  /**
   * Flags that indicate how <SVC_HANDLER>'s should be initialized
   * prior to being activated.  Right now, the only flag that is
   * processed is <ACE_NONBLOCK>, which enabled non-blocking I/O on
   * the <SVC_HANDLER> when it is opened.
   */
  int flags_;

  /// Pointer to the Reactor.
  ACE_Reactor *reactor_;

  /// Handle set representing the non-blocking connects in progress.
  ACE_Handle_Set non_blocking_handles_;

};

/**
 * @class ACE_Strategy_Connector
 *
 * @brief Abstract factory for creating a service handler
 * (SVC_HANDLER), connecting the SVC_HANDLER, and activating the
 * SVC_HANDLER.
 *
 * Implements a flexible and extensible set of strategies for
 * actively establishing connections with clients.  There are
 * three main strategies: (1) creating a SVC_HANDLER, (2)
 * actively initiating a new connection from the client,
 * and (3) activating the SVC_HANDLER with a
 * particular concurrency mechanism after the connection is established.
 */
template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1>
class ACE_Strategy_Connector
  : public ACE_Connector <SVC_HANDLER, ACE_PEER_CONNECTOR_2>
{
public:

  // Useful STL-style traits.
  typedef ACE_Creation_Strategy<SVC_HANDLER>
  creation_strategy_type;
  typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
  connect_strategy_type;
  typedef ACE_Concurrency_Strategy<SVC_HANDLER>
  concurrency_strategy_type;
  typedef ACE_Connector <SVC_HANDLER, ACE_PEER_CONNECTOR_2>
  base_type;

  // = Define some useful (old style) traits.
  typedef ACE_Creation_Strategy<SVC_HANDLER>
  CREATION_STRATEGY;
  typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
  CONNECT_STRATEGY;
  typedef ACE_Concurrency_Strategy<SVC_HANDLER>
  CONCURRENCY_STRATEGY;
  typedef ACE_Connector <SVC_HANDLER, ACE_PEER_CONNECTOR_2>
  SUPER;

  /**
   * Initialize a connector.  <flags> indicates how <SVC_HANDLER>'s
   * should be initialized prior to being activated.  Right now, the
   * only flag that is processed is <ACE_NONBLOCK>, which enabled
   * non-blocking I/O on the <SVC_HANDLER> when it is opened.
   */
  ACE_Strategy_Connector (ACE_Reactor *r = ACE_Reactor::instance (),
                          ACE_Creation_Strategy<SVC_HANDLER> * = 0,
                          ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2> * = 0,
                          ACE_Concurrency_Strategy<SVC_HANDLER> * = 0,
                          int flags = 0);

  /**
   * Initialize a connector.  <flags> indicates how <SVC_HANDLER>'s
   * should be initialized prior to being activated.  Right now, the
   * only flag that is processed is <ACE_NONBLOCK>, which enabled
   * non-blocking I/O on the <SVC_HANDLER> when it is opened.
   * Default strategies would be created and used.
   */
  virtual int open (ACE_Reactor *r,
                    int flags);

  /**
   * Initialize a connector.  <flags> indicates how <SVC_HANDLER>'s
   * should be initialized prior to being activated.  Right now, the
   * only flag that is processed is <ACE_NONBLOCK>, which enabled
   * non-blocking I/O on the <SVC_HANDLER> when it is opened.
   */
  virtual int open (ACE_Reactor *r = ACE_Reactor::instance (),
                    ACE_Creation_Strategy<SVC_HANDLER> * = 0,
                    ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2> * = 0,
                    ACE_Concurrency_Strategy<SVC_HANDLER> * = 0,
                    int flags = 0);

  /// Shutdown a connector and release resources.
  virtual ~ACE_Strategy_Connector (void);

  /// Close down the Connector
  virtual int close (void);

  // = Strategies accessors
  virtual ACE_Creation_Strategy<SVC_HANDLER> *creation_strategy (void) const;
  virtual ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2> *connect_strategy (void) const;
  virtual ACE_Concurrency_Strategy<SVC_HANDLER> *concurrency_strategy (void) const;

protected:
  // = The following three methods define the <Connector>'s strategies
  // for creating, connecting, and activating <SVC_HANDLER>'s,
  // respectively.

  /**
   * Bridge method for creating a <SVC_HANDLER>.  The strategy for
   * creating a <SVC_HANDLER> are configured into the Connector via
   * it's <creation_strategy_>.  The default is to create a new
   * <SVC_HANDLER> only if <sh> == 0, else <sh> is unchanged.
   * However, subclasses can override this policy to perform
   * <SVC_HANDLER> creation in any way that they like (such as
   * creating subclass instances of <SVC_HANDLER>, using a singleton,
   * dynamically linking the handler, etc.).  Returns -1 if failure,
   * else 0.
   */
  virtual int make_svc_handler (SVC_HANDLER *&sh);

  /**
   * Bridge method for connecting the new connection into the
   * <SVC_HANDLER>.  The default behavior delegates to the
   * <PEER_CONNECTOR::connect> in the <Connect_Strategy>.
   */
  virtual int connect_svc_handler (SVC_HANDLER *&sh,
                                   const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                                   ACE_Time_Value *timeout,
                                   const ACE_PEER_CONNECTOR_ADDR &local_addr,
                                   int reuse_addr,
                                   int flags,
                                   int perms);

  /**
   * Bridge method for connecting the new connection into the
   * <SVC_HANDLER>.  The default behavior delegates to the
   * <PEER_CONNECTOR::connect> in the <Connect_Strategy>.
   * <sh_copy> is used to obtain a copy of the <sh> pointer, but that
   * can be kept in the stack; the motivation is a bit too long to
   * include here, but basically we want to modify <sh> safely, using
   * the internal locks in the Connect_Strategy, while saving a TSS
   * copy in <sh_copy>, usually located in the stack.
   */
  virtual int connect_svc_handler (SVC_HANDLER *&sh,
                                   SVC_HANDLER *&sh_copy,
                                   const ACE_PEER_CONNECTOR_ADDR &remote_addr,
                                   ACE_Time_Value *timeout,
                                   const ACE_PEER_CONNECTOR_ADDR &local_addr,
                                   int reuse_addr,
                                   int flags,
                                   int perms);

  /**
   * Bridge method for activating a <SVC_HANDLER> with the appropriate
   * concurrency strategy.  The default behavior of this method is to
   * activate the <SVC_HANDLER> by calling its <open> method (which
   * allows the <SVC_HANDLER> to define its own concurrency strategy).
   * However, subclasses can override this strategy to do more
   * sophisticated concurrency activations (such as creating the
   * <SVC_HANDLER> as an "active object" via multi-threading or
   * multi-processing).
   */
  virtual int activate_svc_handler (SVC_HANDLER *svc_handler);

  // = Strategy objects.

  /// Creation strategy for an <Connector>.
  CREATION_STRATEGY *creation_strategy_;

  /// 1 if <Connector> created the creation strategy and thus should
  /// delete it, else 0.
  int delete_creation_strategy_;

  /// Connect strategy for a <Connector>.
  CONNECT_STRATEGY *connect_strategy_;

  /// 1 if <Connector> created the connect strategy and thus should
  /// delete it, else 0.
  int delete_connect_strategy_;

  /// Concurrency strategy for an <Connector>.
  CONCURRENCY_STRATEGY *concurrency_strategy_;

  /// 1 if <Connector> created the concurrency strategy and thus should
  /// delete it, else 0.
  int delete_concurrency_strategy_;
};

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Connector.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Connector.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#include /**/ "ace/post.h"

#endif /* ACE_CONNECTOR_H */