Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / ACE_wrappers / ace / Strategies_T.h
blobbf73bb22c3ddffc8baf7b73fe522f2814e02ddb6
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Strategies_T.h
7 * $Id: Strategies_T.h 81954 2008-06-15 13:24:16Z johnnyw $
9 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
11 //=============================================================================
13 #ifndef ACE_STRATEGIES_T_H
14 #define ACE_STRATEGIES_T_H
16 #include /**/ "ace/pre.h"
18 #include "ace/Hash_Map_Manager_T.h"
20 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 # pragma once
22 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 #include "ace/Reactor.h"
25 #include "ace/Thread_Manager.h"
26 #include "ace/Connection_Recycling_Strategy.h"
27 #include "ace/Refcountable.h"
28 #include "ace/Hashable.h"
29 #include "ace/Recyclable.h"
30 #include "ace/Reverse_Lock_T.h"
32 // Needed for broken linkers that can't grok long symbols.
33 #define ACE_Refcounted_Hash_Recyclable ARHR
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
37 class ACE_Service_Repository;
39 /**
40 * @class ACE_Recycling_Strategy
42 * @brief Defines the interface (and default implementation) for
43 * specifying a recycling strategy for a SVC_HANDLER.
45 * Acts as a consular to the Svc_Handler, preparing it for the
46 * tough times ahead when the Svc_Handler will be recycled.
48 template<class SVC_HANDLER>
49 class ACE_Recycling_Strategy
51 public:
53 // Useful STL-style traits.
54 typedef typename SVC_HANDLER::addr_type addr_type;
55 typedef SVC_HANDLER handler_type;
56 typedef typename SVC_HANDLER::stream_type stream_type;
58 /// Virtual Destructor
59 virtual ~ACE_Recycling_Strategy (void);
61 /// Tell the Svc_Handler something about the recycler, so that it can
62 /// reach the recycler when necessary.
63 virtual int assign_recycler (SVC_HANDLER *svc_handler,
64 ACE_Connection_Recycling_Strategy *recycler,
65 const void *recycling_act);
67 /// This allows us to prepare the svc_handler for recycling.
68 virtual int prepare_for_recycling (SVC_HANDLER *svc_handler);
71 /**
72 * @class ACE_Creation_Strategy
74 * @brief Defines the interface for specifying a creation strategy for
75 * a SVC_HANDLER.
77 * The default behavior is to make a new SVC_HANDLER. However,
78 * subclasses can override this strategy to perform SVC_HANDLER
79 * creation in any way that they like (such as creating subclass
80 * instances of SVC_HANDLER, using a singleton, dynamically
81 * linking the handler, etc.).
83 template <class SVC_HANDLER>
84 class ACE_Creation_Strategy
86 public:
88 // Useful STL-style traits.
89 typedef typename SVC_HANDLER::addr_type addr_type;
90 typedef SVC_HANDLER handler_type;
91 typedef typename SVC_HANDLER::stream_type stream_type;
93 // = Initialization and termination methods.
95 /// Default constructor.
96 ACE_Creation_Strategy (ACE_Thread_Manager * = 0,
97 ACE_Reactor * = ACE_Reactor::instance ());
99 /// An ACE_Thread_Manager is useful when creating active objects and
100 /// the ACE_Reactor is used to initialize the service handler's reactor.
101 int open (ACE_Thread_Manager * = 0,
102 ACE_Reactor * = ACE_Reactor::instance ());
104 virtual ~ACE_Creation_Strategy (void);
106 // = Factory method.
108 * Create a SVC_HANDLER with the appropriate creation strategy. The
109 * default behavior of this method is to make a new SVC_HANDLER if
110 * @a sh == 0 (passing in the <Thread_Manager>), else @a sh is
111 * unchanged. Returns -1 on failure, else 0.
113 virtual int make_svc_handler (SVC_HANDLER *&sh);
115 /// Dump the state of an object.
116 void dump (void) const;
118 /// Declare the dynamic allocation hooks.
119 ACE_ALLOC_HOOK_DECLARE;
121 protected:
122 /// Pointer to a thread manager.
123 ACE_Thread_Manager *thr_mgr_;
125 /// Pointer to an ACE_Reactor.
126 ACE_Reactor *reactor_;
130 * @class ACE_Singleton_Strategy
132 * @brief Defines the interface for specifying a creation strategy for
133 * a SVC_HANDLER that always returns the same SVC_HANDLER (i.e.,
134 * it's a Singleton).
136 * Note that this class takes over the ownership of the
137 * SVC_HANDLER passed into it as a parameter and it becomes
138 * responsible for deleting this object.
140 template <class SVC_HANDLER>
141 class ACE_Singleton_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
143 public:
145 // Useful STL-style traits.
146 typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
148 // = Initialization and termination methods.
149 ACE_Singleton_Strategy (SVC_HANDLER * = 0,
150 ACE_Thread_Manager * = 0);
151 int open (SVC_HANDLER *,
152 ACE_Thread_Manager * = 0);
153 virtual ~ACE_Singleton_Strategy (void);
155 // = Factory method.
156 /// Create a Singleton SVC_HANDLER by always returning the same
157 /// SVC_HANDLER. Returns -1 on failure, else 0.
158 virtual int make_svc_handler (SVC_HANDLER *&);
160 /// Dump the state of an object.
161 void dump (void) const;
163 /// Declare the dynamic allocation hooks.
164 ACE_ALLOC_HOOK_DECLARE;
166 protected:
167 /// Pointer to the Singleton svc_handler.
168 SVC_HANDLER *svc_handler_;
170 /// Keep track of whether we need to delete the SVC_HANDLER.
171 bool delete_svc_handler_;
175 * @class ACE_DLL_Strategy
177 * @brief Defines the interface for specifying a creation strategy for
178 * a SVC_HANDLER based on dynamic linking of the SVC_HANDLER.
180 template <class SVC_HANDLER>
181 class ACE_DLL_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
183 public:
185 // Useful STL-style traits.
186 typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
188 // = Intialization and termination methods.
190 /// "Do-nothing" constructor.
191 ACE_DLL_Strategy (void);
193 /// Initialize the DLL strategy based upon the service's DLL
194 /// information contained in the <svc_dll_info> string.
195 ACE_DLL_Strategy (const ACE_TCHAR dll_name[],
196 const ACE_TCHAR factory_function[],
197 const ACE_TCHAR svc_name[],
198 ACE_Service_Repository *,
199 ACE_Thread_Manager * = 0);
201 /// Initialize the DLL strategy based upon the service's DLL
202 /// information contained in the <svc_dll_info> string.
203 int open (const ACE_TCHAR dll_name[],
204 const ACE_TCHAR factory_function[],
205 const ACE_TCHAR svc_name[],
206 ACE_Service_Repository *,
207 ACE_Thread_Manager * = 0);
209 // = Factory method.
210 /// Create a SVC_HANDLER by dynamically linking it from a DLL.
211 /// Returns -1 on failure, else 0.
212 virtual int make_svc_handler (SVC_HANDLER *&);
214 /// Dump the state of an object.
215 void dump (void) const;
217 /// Declare the dynamic allocation hooks.
218 ACE_ALLOC_HOOK_DECLARE;
220 protected:
221 typedef ACE_Creation_Strategy<SVC_HANDLER> inherited;
223 /// Name of the DLL to dynamically link.
224 ACE_TCHAR dll_name_[MAXPATHLEN + 1];
226 /// Name of the factory function in the shared library to use to
227 /// obtain a pointer to the new SVC_HANDLER.
228 ACE_TCHAR factory_function_[MAXPATHLEN + 1];
230 /// Name of the service.
231 ACE_TCHAR svc_name_[MAXNAMELEN + 1];
233 /// Pointer to the <Service_Repository>.
234 ACE_Service_Repository *svc_rep_;
238 * @class ACE_Concurrency_Strategy
240 * @brief Defines the interface for specifying a concurrency strategy
241 * for a SVC_HANDLER.
243 * Default behavior is to activate the SVC_HANDLER by calling
244 * its <open> method (which allows the SVC_HANDLER to define its
245 * own concurrency strategy). However, subclasses can override
246 * this default strategy to do more sophisticated concurrency
247 * activations (such as creating the SVC_HANDLER as an active
248 * object via multi-threading or multi-processing).
250 template <class SVC_HANDLER>
251 class ACE_Concurrency_Strategy
253 public:
255 // Useful STL-style traits.
256 typedef typename SVC_HANDLER::addr_type addr_type;
257 typedef SVC_HANDLER handler_type;
258 typedef typename SVC_HANDLER::stream_type stream_type;
260 /// Constructor
261 ACE_Concurrency_Strategy (int flags = 0);
263 // = Factory method.
265 * Activate the <svc_handler> with an appropriate concurrency
266 * strategy. The default behavior of this method is to activate the
267 * SVC_HANDLER by calling its <open> method (which allows the
268 * SVC_HANDLER to define its own concurrency strategy).
270 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
271 void *arg = 0);
273 virtual ~ACE_Concurrency_Strategy (void);
275 /// Dump the state of an object.
276 void dump (void) const;
278 /// Declare the dynamic allocation hooks.
279 ACE_ALLOC_HOOK_DECLARE;
281 protected:
283 /// Flags that are parsed to set options for the connected
284 /// SVC_HANDLER.
285 int flags_;
289 * @class ACE_Reactive_Strategy
291 * @brief Defines the interface for specifying a reactive concurrency
292 * strategy for a SVC_HANDLER, where all upcalls to @c handle_*()
293 * methods run in the reactor's thread of control.
295 * This class provides a strategy that registers the
296 * SVC_HANDLER with a <Reactor>.
298 template <class SVC_HANDLER>
299 class ACE_Reactive_Strategy : public ACE_Concurrency_Strategy <SVC_HANDLER>
301 public:
303 // Useful STL-style traits.
304 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
306 // = Intialization and termination methods.
307 /// "Do-nothing constructor"
308 ACE_Reactive_Strategy (int flags = 0);
310 /// Initialize the strategy.
311 ACE_Reactive_Strategy (ACE_Reactor *reactor,
312 ACE_Reactor_Mask = ACE_Event_Handler::READ_MASK,
313 int flags = 0);
315 /// Initialize the strategy.
316 virtual int open (ACE_Reactor *reactor,
317 ACE_Reactor_Mask = ACE_Event_Handler::READ_MASK,
318 int flags = 0);
320 /// Destructor.
321 virtual ~ACE_Reactive_Strategy (void);
323 // = Factory method.
324 /// Activate the <svc_handler> by registering it with the <Reactor>
325 /// and then calling it's <open> hook.
326 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
327 void *arg = 0);
329 /// Dump the state of an object.
330 void dump (void) const;
332 /// Declare the dynamic allocation hooks.
333 ACE_ALLOC_HOOK_DECLARE;
335 protected:
336 typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
338 /// Pointer to the Reactor we'll use to register the SVC_HANDLER.
339 ACE_Reactor *reactor_;
341 /// The mask that we pass to the <Reactor> when we register the
342 /// SVC_HANDLER.
343 ACE_Reactor_Mask mask_;
347 * @class ACE_Thread_Strategy
349 * @brief Defines the interface for specifying a concurrency strategy
350 * for a SVC_HANDLER based on multithreading.
352 * This class provides a strategy that manages the creation of threads
353 * to handle requests from clients concurrently via a
354 * thread-per-connection model. It behaves as a "thread factory",
355 * spawning threads "on-demand" to run the service specified by a
356 * user-supplied SVC_HANDLER.
358 template <class SVC_HANDLER>
359 class ACE_Thread_Strategy : public ACE_Concurrency_Strategy<SVC_HANDLER>
361 public:
363 // Useful STL-style traits.
364 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
366 // = Intialization and termination methods.
367 /// "Do-nothing constructor"
368 ACE_Thread_Strategy (int flags = 0);
370 /// Initialize the strategy.
371 ACE_Thread_Strategy (ACE_Thread_Manager *tm,
372 long thr_flags,
373 int n_threads = 1,
374 int flags = 0);
376 /// Initialize the strategy.
377 virtual int open (ACE_Thread_Manager *tm,
378 long thr_flags,
379 int n_threads = 1,
380 int flags = 0);
382 virtual ~ACE_Thread_Strategy (void);
384 // = Factory method.
386 * Activate the @a svc_handler with an appropriate concurrency
387 * strategy. This method activates the SVC_HANDLER by first calling
388 * its <open> method and then calling its <activate> method to turn
389 * it into an active object.
391 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
392 void *arg = 0);
394 /// Dump the state of an object.
395 void dump (void) const;
397 /// Declare the dynamic allocation hooks.
398 ACE_ALLOC_HOOK_DECLARE;
400 protected:
401 typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
403 /// Thread manager for this class (must be provided).
404 ACE_Thread_Manager *thr_mgr_;
406 /// Flags to pass into the <SVC_HANDLER::activate> method.
407 long thr_flags_;
409 /// Number of threads to spawn.
410 int n_threads_;
414 * @class ACE_Process_Strategy
416 * @brief Defines the interface for specifying a concurrency strategy
417 * for a @c SVC_HANDLER based on multiprocessing.
419 * This class provides a strategy that manages the creation of
420 * processes to handle requests from clients concurrently using a
421 * process-per-connection model. It behaves as a "process factory",
422 * using @c ACE::fork() to fork threads "on-demand" to run the service
423 * specified by a user-supplied @c SVC_HANDLER in a separate process.
425 template <class SVC_HANDLER>
426 class ACE_Process_Strategy : public ACE_Concurrency_Strategy<SVC_HANDLER>
428 public:
430 // Useful STL-style traits.
431 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
433 // = Intialization and termination methods.
435 /// Initialize the strategy. If @a avoid_zombies is non-0 then set a
436 /// flag to ACE::fork() to avoid zombies.
437 ACE_Process_Strategy (size_t n_processes = 1,
438 ACE_Event_Handler *acceptor = 0,
439 ACE_Reactor * = 0,
440 int avoid_zombies = 0);
442 /// Initialize the strategy. If @a avoid_zombies is non-0 then set a
443 /// flag to ACE::fork() to avoid zombies.
444 virtual int open (size_t n_processes = 1,
445 ACE_Event_Handler *acceptor = 0,
446 ACE_Reactor * = 0,
447 int avoid_zombies = 0);
449 virtual ~ACE_Process_Strategy (void);
451 // = Factory method.
453 * Activate the @a svc_handler with an appropriate concurrency
454 * strategy. This method activates the SVC_HANDLER by first forking
455 * and then calling the @c open() method of the SVC_HANDLER in the
456 * child.
458 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
459 void *arg = 0);
461 /// Dump the state of an object.
462 void dump (void) const;
464 /// Declare the dynamic allocation hooks.
465 ACE_ALLOC_HOOK_DECLARE;
467 protected:
468 typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
470 /// Number of processes to spawn.
471 size_t n_processes_;
474 * This is the @c Acceptor in the parent is listening on. We need to
475 * make sure that we remove it from the Reactor and close it down in
476 * the child.
478 ACE_Event_Handler *acceptor_;
481 * This is the reactor the child is using in conjunction with the
482 * acceptor. We need to remove the acceptor from this reactor
483 * in the child.
485 ACE_Reactor *reactor_;
489 * @class ACE_Accept_Strategy
491 * @brief Defines the interface for specifying a passive connection
492 * acceptance strategy for a SVC_HANDLER.
494 * This class provides a strategy that manages passive
495 * connection acceptance of a client.
497 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1>
498 class ACE_Accept_Strategy
500 public:
502 // Useful STL-style traits.
503 typedef ACE_PEER_ACCEPTOR_ADDR addr_type;
504 typedef ACE_PEER_ACCEPTOR acceptor_type;
505 typedef SVC_HANDLER handler_type;
506 typedef typename SVC_HANDLER::stream_type stream_type;
508 // = Initialization and termination methods.
509 /// Default constructor.
510 ACE_Accept_Strategy (ACE_Reactor *reactor = ACE_Reactor::instance ());
512 /// Initialize the @c peer_acceptor_ with @a local_addr.
513 ACE_Accept_Strategy (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
514 int restart = 0,
515 ACE_Reactor *reactor = ACE_Reactor::instance ());
517 /// Initialize the <peer_acceptor_> with @a local_addr, indicating
518 /// whether to @a reuse_addr if it's already in use.
519 virtual int open (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
520 int reuse_addr = 0);
522 /// Return the underlying ACE_HANDLE of the <peer_acceptor_>.
523 virtual ACE_HANDLE get_handle (void) const;
525 /// Return a reference to the <peer_acceptor_>.
526 virtual ACE_PEER_ACCEPTOR &acceptor (void) const;
528 virtual ~ACE_Accept_Strategy (void);
530 // = Factory method.
531 /// The default behavior delegates to the <accept> method of the
532 /// PEER_ACCEPTOR.
533 virtual int accept_svc_handler (SVC_HANDLER *);
535 /// Dump the state of an object.
536 void dump (void) const;
538 /// Declare the dynamic allocation hooks.
539 ACE_ALLOC_HOOK_DECLARE;
541 protected:
542 /// Factory that establishes connections passively.
543 ACE_PEER_ACCEPTOR peer_acceptor_;
545 /// Pointer to the reactor used by the Acceptor.
546 ACE_Reactor *reactor_;
548 /// Needed to reopen the socket if <accept> fails.
549 int reuse_addr_;
551 /// Needed to reopen the socket if <accept> fails.
552 ACE_PEER_ACCEPTOR_ADDR peer_acceptor_addr_;
556 * @class ACE_Connect_Strategy
558 * @brief Defines the interface for specifying an active
559 * connection establishment strategy for a SVC_HANDLER.
561 * This class provides a strategy that manages active
562 * connection establishment to a server.
564 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1>
565 class ACE_Connect_Strategy
567 public:
569 // Useful STL-style traits.
570 typedef ACE_PEER_CONNECTOR_ADDR addr_type;
571 typedef ACE_PEER_CONNECTOR connector_type;
572 typedef SVC_HANDLER handler_type;
573 typedef typename SVC_HANDLER::stream_type stream_type;
575 // = Initialization and termination methods.
576 /// Default constructor.
577 ACE_Connect_Strategy (void);
579 /// Return a reference to the <peer_connector_>.
580 virtual ACE_PEER_CONNECTOR &connector (void) const;
582 virtual ~ACE_Connect_Strategy (void);
584 // = Factory method.
585 /// The default behavior delegates to the <connect> method of the
586 /// <PEER_CONNECTOR::connect>.
587 virtual int connect_svc_handler (SVC_HANDLER *&sh,
588 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
589 ACE_Time_Value *timeout,
590 const ACE_PEER_CONNECTOR_ADDR &local_addr,
591 int reuse_addr,
592 int flags,
593 int perms);
596 * The default behavior delegates to the <connect> method of the
597 * <PEER_CONNECTOR::connect>.
598 * Please check the documentation in Connector.h for more details.
600 virtual int connect_svc_handler (SVC_HANDLER *&sh,
601 SVC_HANDLER *&sh_copy,
602 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
603 ACE_Time_Value *timeout,
604 const ACE_PEER_CONNECTOR_ADDR &local_addr,
605 int reuse_addr,
606 int flags,
607 int perms);
609 /// Dump the state of an object.
610 void dump (void) const;
612 /// Declare the dynamic allocation hooks.
613 ACE_ALLOC_HOOK_DECLARE;
615 protected:
616 /// Factory that establishes connections actively.
617 ACE_PEER_CONNECTOR connector_;
621 * @class ACE_Scheduling_Strategy
623 * @brief Defines the interface for specifying how to suspend and
624 * resume a service .
626 * This class provides a strategy that allows arbitrarily
627 * sophisticated service suspension and resumption. The default
628 * behavior is to do nothing...
630 template <class SVC_HANDLER>
631 class ACE_Scheduling_Strategy
633 public:
635 // Useful STL-style traits.
636 typedef typename SVC_HANDLER::addr_type addr_type;
637 typedef SVC_HANDLER handler_type;
638 typedef typename SVC_HANDLER::stream_type stream_type;
640 // = Initialization and termination methods.
642 /// Constructor
643 ACE_Scheduling_Strategy (SVC_HANDLER * = 0);
645 /// Destructor
646 virtual ~ACE_Scheduling_Strategy (void);
648 // = Scheduling methods
650 /// Suspend hook.
651 virtual int suspend (void);
653 /// Resume hook.
654 virtual int resume (void);
656 /// Dump the state of the object.
657 virtual void dump (void) const;
661 * @class ACE_Schedule_All_Reactive_Strategy
663 * @brief Defines the interface for specifying how to suspend and
664 * resume a single-threaded reactive service .
666 * This class provides a strategy that suspends and resumes all
667 * the Event_Handlers in a Reactor in one fell swoop.
669 template <class SVC_HANDLER>
670 class ACE_Schedule_All_Reactive_Strategy
671 : public ACE_Scheduling_Strategy<SVC_HANDLER>
673 public:
675 // Useful STL-style traits.
676 typedef ACE_Scheduling_Strategy<SVC_HANDLER> base_type;
678 // = Initialization and termination methods.
679 /// Constructor
680 ACE_Schedule_All_Reactive_Strategy (SVC_HANDLER * = 0);
682 // = Scheduling methods
684 /// Suspend hook.
685 virtual int suspend (void);
687 /// Resume hook.
688 virtual int resume (void);
690 /// Dump the state of the object.
691 virtual void dump (void) const;
693 protected:
695 /// Reactor
696 ACE_Reactor *reactor_;
700 * @class ACE_Schedule_All_Threaded_Strategy
702 * @brief Defines the interface for specifying how to suspend and
703 * resume a multithreaded service .
705 * This class provides a strategy that suspends and resumes all
706 * the Event_Handlers controlled by a Thread_Manager in one fell swoop.
708 template <class SVC_HANDLER>
709 class ACE_Schedule_All_Threaded_Strategy
710 : public ACE_Scheduling_Strategy<SVC_HANDLER>
712 public:
714 // Useful STL-style traits.
715 typedef ACE_Scheduling_Strategy<SVC_HANDLER> base_type;
717 // = Initialization and termination methods.
718 /// Constructor
719 ACE_Schedule_All_Threaded_Strategy (SVC_HANDLER * = 0);
721 // = Scheduling methods
723 /// Suspend hook.
724 virtual int suspend (void);
726 /// Resume hook.
727 virtual int resume (void);
729 /// Dump the state of the object.
730 virtual void dump (void) const;
732 protected:
734 /// Thread Manager
735 ACE_Thread_Manager *thr_mgr_;
739 * @class ACE_NOOP_Creation_Strategy
741 * @brief Implements a no-op creation strategy in order to defer
742 * decisions regarding creation to some later point in time, such
743 * as in connect or accept strategy.
745 * An example of the use of this is in the
746 * ACE_Cached_Connect_Strategy, which only returns a single
747 * connection for a given endpoint.
749 template <class SVC_HANDLER>
750 class ACE_NOOP_Creation_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
752 public:
754 // Useful STL-style traits.
755 typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
757 /// This is a no-op.
758 virtual int make_svc_handler (SVC_HANDLER *&);
762 * @class ACE_NOOP_Concurrency_Strategy
764 * @brief Implements a no-op activation strategy in order to avoid
765 * calling open on a svc_handler multiple times.
767 * An example of the use of this is in the
768 * ACE_Cached_Connect_Strategy, which reuses svc_handlers.
769 * Therefore we don't want to call open on the recycled
770 * svc_handler more than once.
772 template <class SVC_HANDLER>
773 class ACE_NOOP_Concurrency_Strategy
774 : public ACE_Concurrency_Strategy<SVC_HANDLER>
776 public:
778 // Useful STL-style traits.
779 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
781 // = Factory method.
782 /// This is a no-op.
783 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
784 void *arg = 0);
787 template <class T>
788 class ACE_Refcounted_Hash_Recyclable : public ACE_Refcountable,
789 public ACE_Hashable,
790 public ACE_Recyclable
792 public:
793 /// Default constructor.
794 ACE_Refcounted_Hash_Recyclable (void);
796 /// Constructor.
797 ACE_Refcounted_Hash_Recyclable (const T &t,
798 long refcount = 0,
799 ACE_Recyclable_State state = ACE_RECYCLABLE_UNKNOWN);
801 /// Destructor
802 virtual ~ACE_Refcounted_Hash_Recyclable (void);
804 /// Compares two instances.
805 bool operator== (const ACE_Refcounted_Hash_Recyclable<T> &rhs) const;
806 bool operator!= (const ACE_Refcounted_Hash_Recyclable<T> &rhs) const;
808 T &subject ();
810 protected:
811 /// Computes and returns hash value.
812 u_long hash_i (void) const;
814 T t_;
818 * @class ACE_Cached_Connect_Strategy
820 * @brief A connection strategy which caches connections to peers
821 * (represented by SVC_HANDLER instances), thereby allowing
822 * subsequent re-use of unused, but available, connections.
824 * <ACE_Cached_Connect_Strategy> is intended to be used as a
825 * plug-in connection strategy for ACE_Strategy_Connector.
826 * It's added value is re-use of established connections.
828 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class MUTEX>
829 class ACE_Cached_Connect_Strategy
830 : public ACE_Connection_Recycling_Strategy,
831 public ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
833 public:
835 // Useful STL-style traits.
836 typedef ACE_Creation_Strategy<SVC_HANDLER>
837 creation_strategy_type;
838 typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
839 connect_strategy_type;
840 typedef ACE_Concurrency_Strategy<SVC_HANDLER>
841 concurrency_strategy_type;
842 typedef ACE_Recycling_Strategy<SVC_HANDLER> recycling_strategy_type;
844 // = Define some useful (old style) traits.
845 typedef ACE_Creation_Strategy<SVC_HANDLER>
846 CREATION_STRATEGY;
847 typedef ACE_Concurrency_Strategy<SVC_HANDLER>
848 CONCURRENCY_STRATEGY;
849 typedef ACE_Recycling_Strategy<SVC_HANDLER>
850 RECYCLING_STRATEGY;
852 // = Super class
853 typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
854 CONNECT_STRATEGY;
857 typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX> SELF;
859 /// Constructor
860 ACE_Cached_Connect_Strategy (ACE_Creation_Strategy<SVC_HANDLER> *cre_s = 0,
861 ACE_Concurrency_Strategy<SVC_HANDLER> *con_s = 0,
862 ACE_Recycling_Strategy<SVC_HANDLER> *rec_s = 0,
863 MUTEX *mutex = 0,
864 bool delete_lock = false);
866 /// Destructor
867 virtual ~ACE_Cached_Connect_Strategy (void);
869 /// This methods allow you to change the strategies used by the
870 /// cached connector.
871 virtual int open (ACE_Creation_Strategy<SVC_HANDLER> *cre_s,
872 ACE_Concurrency_Strategy<SVC_HANDLER> *con_s,
873 ACE_Recycling_Strategy<SVC_HANDLER> *rec_s);
875 /// Template method for making a new <svc_handler>
876 virtual int make_svc_handler (SVC_HANDLER *&sh);
878 /// Template method for activating a new <svc_handler>
879 virtual int activate_svc_handler (SVC_HANDLER *svc_handler);
881 /// Template method for setting the recycler information of the
882 /// svc_handler.
883 virtual int assign_recycler (SVC_HANDLER *svc_handler,
884 ACE_Connection_Recycling_Strategy *recycler,
885 const void *recycling_act);
887 /// Template method for preparing the svc_handler for recycling.
888 virtual int prepare_for_recycling (SVC_HANDLER *svc_handler);
891 * Checks to see if there is already a <SVC_HANDLER> in the cache
892 * connected to the <remote_addr>. If so, we return this pointer.
893 * Otherwise we establish the connection, put it into the cache, and
894 * return the SVC_HANDLER pointer. <[NOTE]>: the <{reuse_addr}>
895 * argument does NOT control re-use of addresses in the cache.
896 * Rather, if the underlying protocol requires a "dead time" prior
897 * to re-use of its addresses (TCP is a classic example of this),
898 * <{and}> the protocol provides a means by which to defeat the dead
899 * time, setting this argument to non-zero will defeat the dead-time
900 * requirement. <{Dev. Note: We might want to consider enhancing
901 * the interface at some point so that this also controls re-use of
902 * the cache.}>
904 virtual int connect_svc_handler (SVC_HANDLER *&sh,
905 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
906 ACE_Time_Value *timeout,
907 const ACE_PEER_CONNECTOR_ADDR &local_addr,
908 int reuse_addr,
909 int flags,
910 int perms);
911 virtual int connect_svc_handler (SVC_HANDLER *&sh,
912 SVC_HANDLER *&sh_copy,
913 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
914 ACE_Time_Value *timeout,
915 const ACE_PEER_CONNECTOR_ADDR &local_addr,
916 int reuse_addr,
917 int flags,
918 int perms);
920 /// Remove from cache.
921 virtual int purge (const void *recycling_act);
923 /// Add to cache.
924 virtual int cache (const void *recycling_act);
926 /// Get/Set <recycle_state>.
927 virtual int recycle_state (const void *recycling_act,
928 ACE_Recyclable_State new_state);
929 virtual ACE_Recyclable_State recycle_state (const void *recycling_act) const;
931 /// Mark as closed.
932 virtual int mark_as_closed (const void *recycling_act);
935 * Mark as closed (non-locking version). This method needs to be public
936 * as it is used in the cleanup of handlers where teh locked version causes
937 * a deadlock.
939 virtual int mark_as_closed_i (const void *recycling_act);
941 /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
942 virtual int cleanup_hint (const void *recycling_act,
943 void **act_holder = 0);
945 // = Traits for managing the map
946 typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
947 REFCOUNTED_HASH_RECYCLABLE_ADDRESS;
948 typedef ACE_Hash_Map_Manager_Ex<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, SVC_HANDLER *, ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, ACE_Null_Mutex>
949 CONNECTION_MAP;
951 typedef typename CONNECTION_MAP::ITERATOR CONNECTION_MAP_ITERATOR;
952 typedef typename CONNECTION_MAP::ENTRY CONNECTION_MAP_ENTRY;
954 typedef ACE_Reverse_Lock<MUTEX> REVERSE_MUTEX;
956 // = Strategy accessors
957 virtual ACE_Creation_Strategy<SVC_HANDLER> *creation_strategy (void) const;
958 virtual ACE_Recycling_Strategy<SVC_HANDLER> *recycling_strategy (void) const;
959 virtual ACE_Concurrency_Strategy<SVC_HANDLER> *concurrency_strategy (void) const;
961 protected:
963 /// Creates a new connection.
964 virtual int new_connection (SVC_HANDLER *&sh,
965 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
966 ACE_Time_Value *timeout,
967 const ACE_PEER_CONNECTOR_ADDR &local_addr,
968 int reuse_addr,
969 int flags,
970 int perms);
972 /// Find an idle handle.
973 int find (REFCOUNTED_HASH_RECYCLABLE_ADDRESS &search_addr,
974 CONNECTION_MAP_ENTRY *&entry);
976 /// Remove from cache (non-locking version).
977 virtual int purge_i (const void *recycling_act);
979 /// Add to cache (non-locking version).
980 virtual int cache_i (const void *recycling_act);
982 /// Set <recycle_state> (non-locking version).
983 virtual int recycle_state_i (const void *recycling_act,
984 ACE_Recyclable_State new_state);
986 /// Get <recycle_state> (non-locking version).
987 virtual ACE_Recyclable_State recycle_state_i (const void *recycling_act) const;
989 /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
990 virtual int cleanup_hint_i (const void *recycling_act,
991 void **act_holder);
993 // = Helpers
994 int check_hint_i (SVC_HANDLER *&sh,
995 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
996 ACE_Time_Value *timeout,
997 const ACE_PEER_CONNECTOR_ADDR &local_addr,
998 int reuse_addr,
999 int flags,
1000 int perms,
1001 CONNECTION_MAP_ENTRY *&entry,
1002 int &found);
1004 int find_or_create_svc_handler_i (SVC_HANDLER *&sh,
1005 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
1006 ACE_Time_Value *timeout,
1007 const ACE_PEER_CONNECTOR_ADDR &local_addr,
1008 int reuse_addr,
1009 int flags,
1010 int perms,
1011 CONNECTION_MAP_ENTRY *&entry,
1012 int &found);
1014 virtual int connect_svc_handler_i (
1015 SVC_HANDLER *&sh,
1016 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
1017 ACE_Time_Value *timeout,
1018 const ACE_PEER_CONNECTOR_ADDR &local_addr,
1019 int reuse_addr,
1020 int flags,
1021 int perms,
1022 int &found);
1024 /// Table that maintains the cache of connected SVC_HANDLERs.
1025 CONNECTION_MAP connection_map_;
1027 /// Mutual exclusion for this object.
1028 MUTEX *lock_;
1030 /// Mutual exclusion for this object.
1031 bool delete_lock_;
1033 /// Reverse lock.
1034 REVERSE_MUTEX *reverse_lock_;
1036 // = Strategy objects.
1038 /// Creation strategy for an <Connector>.
1039 CREATION_STRATEGY *creation_strategy_;
1041 /// true if <Connector> created the creation strategy and thus should
1042 /// delete it, else false.
1043 bool delete_creation_strategy_;
1045 /// Concurrency strategy for an <Connector>.
1046 CONCURRENCY_STRATEGY *concurrency_strategy_;
1048 /// true if <Connector> created the concurrency strategy and thus should
1049 /// delete it, else false.
1050 bool delete_concurrency_strategy_;
1052 /// Recycling strategy for an <Connector>.
1053 RECYCLING_STRATEGY *recycling_strategy_;
1055 /// true if <Connector> created the recycling strategy and thus should
1056 /// delete it, else false.
1057 bool delete_recycling_strategy_;
1060 ACE_END_VERSIONED_NAMESPACE_DECL
1062 #if defined (__ACE_INLINE__)
1063 #include "ace/Strategies_T.inl"
1064 #endif /* __ACE_INLINE__ */
1066 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
1067 #include "ace/Strategies_T.cpp"
1068 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
1070 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
1071 #pragma implementation ("Strategies_T.cpp")
1072 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
1074 #include /**/ "ace/post.h"
1076 #endif /* ACE_STRATEGIES_T_H */