Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Strategies_T.h
blob4b7b7f0051027f4547231e58b53aaac91425b69c
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Strategies_T.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
9 //=============================================================================
11 #ifndef ACE_STRATEGIES_T_H
12 #define ACE_STRATEGIES_T_H
14 #include /**/ "ace/pre.h"
16 #include "ace/Hash_Map_Manager_T.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # pragma once
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/Reactor.h"
23 #include "ace/Thread_Manager.h"
24 #include "ace/Connection_Recycling_Strategy.h"
25 #include "ace/Refcountable_T.h"
26 #include "ace/Hashable.h"
27 #include "ace/Recyclable.h"
28 #include "ace/Reverse_Lock_T.h"
30 // Needed for broken linkers that can't grok long symbols.
31 #define ACE_Refcounted_Hash_Recyclable ARHR
33 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
35 class ACE_Service_Repository;
37 /**
38 * @class ACE_Recycling_Strategy
40 * @brief Defines the interface (and default implementation) for
41 * specifying a recycling strategy for a SVC_HANDLER.
43 * Acts as a consular to the Svc_Handler, preparing it for the
44 * tough times ahead when the Svc_Handler will be recycled.
46 template<class SVC_HANDLER>
47 class ACE_Recycling_Strategy
49 public:
50 // Useful STL-style traits.
51 typedef typename SVC_HANDLER::addr_type addr_type;
52 typedef SVC_HANDLER handler_type;
53 typedef typename SVC_HANDLER::stream_type stream_type;
55 /// Virtual Destructor
56 virtual ~ACE_Recycling_Strategy ();
58 /// Tell the Svc_Handler something about the recycler, so that it can
59 /// reach the recycler when necessary.
60 virtual int assign_recycler (SVC_HANDLER *svc_handler,
61 ACE_Connection_Recycling_Strategy *recycler,
62 const void *recycling_act);
64 /// This allows us to prepare the svc_handler for recycling.
65 virtual int prepare_for_recycling (SVC_HANDLER *svc_handler);
68 /**
69 * @class ACE_Creation_Strategy
71 * @brief Defines the interface for specifying a creation strategy for
72 * a SVC_HANDLER.
74 * The default behavior is to make a new SVC_HANDLER. However,
75 * subclasses can override this strategy to perform SVC_HANDLER
76 * creation in any way that they like (such as creating subclass
77 * instances of SVC_HANDLER, using a singleton, dynamically
78 * linking the handler, etc.).
80 template <class SVC_HANDLER>
81 class ACE_Creation_Strategy
83 public:
84 // Useful STL-style traits.
85 typedef typename SVC_HANDLER::addr_type addr_type;
86 typedef SVC_HANDLER handler_type;
87 typedef typename SVC_HANDLER::stream_type stream_type;
89 /// Default constructor.
90 ACE_Creation_Strategy (ACE_Thread_Manager * = 0,
91 ACE_Reactor * = ACE_Reactor::instance ());
93 /// An ACE_Thread_Manager is useful when creating active objects and
94 /// the ACE_Reactor is used to initialize the service handler's reactor.
95 int open (ACE_Thread_Manager * = 0,
96 ACE_Reactor * = ACE_Reactor::instance ());
98 virtual ~ACE_Creation_Strategy ();
100 // = Factory method.
102 * Create a SVC_HANDLER with the appropriate creation strategy. The
103 * default behavior of this method is to make a new SVC_HANDLER if
104 * @a sh == 0 (passing in the Thread_Manager), else @a sh is
105 * unchanged. Returns -1 on failure, else 0.
107 virtual int make_svc_handler (SVC_HANDLER *&sh);
109 /// Dump the state of an object.
110 void dump () const;
112 /// Declare the dynamic allocation hooks.
113 ACE_ALLOC_HOOK_DECLARE;
115 protected:
116 /// Pointer to a thread manager.
117 ACE_Thread_Manager *thr_mgr_;
119 /// Pointer to an ACE_Reactor.
120 ACE_Reactor *reactor_;
124 * @class ACE_Singleton_Strategy
126 * @brief Defines the interface for specifying a creation strategy for
127 * a SVC_HANDLER that always returns the same SVC_HANDLER (i.e.,
128 * it's a Singleton).
130 * Note that this class takes over the ownership of the
131 * SVC_HANDLER passed into it as a parameter and it becomes
132 * responsible for deleting this object.
134 template <class SVC_HANDLER>
135 class ACE_Singleton_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
137 public:
138 // Useful STL-style traits.
139 typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
141 ACE_Singleton_Strategy (SVC_HANDLER * = 0,
142 ACE_Thread_Manager * = 0);
143 int open (SVC_HANDLER *,
144 ACE_Thread_Manager * = 0);
145 virtual ~ACE_Singleton_Strategy ();
147 // = Factory method.
148 /// Create a Singleton SVC_HANDLER by always returning the same
149 /// SVC_HANDLER. Returns -1 on failure, else 0.
150 virtual int make_svc_handler (SVC_HANDLER *&);
152 /// Dump the state of an object.
153 void dump () const;
155 /// Declare the dynamic allocation hooks.
156 ACE_ALLOC_HOOK_DECLARE;
158 protected:
159 /// Pointer to the Singleton svc_handler.
160 SVC_HANDLER *svc_handler_;
162 /// Keep track of whether we need to delete the SVC_HANDLER.
163 bool delete_svc_handler_;
167 * @class ACE_DLL_Strategy
169 * @brief Defines the interface for specifying a creation strategy for
170 * a SVC_HANDLER based on dynamic linking of the SVC_HANDLER.
172 template <class SVC_HANDLER>
173 class ACE_DLL_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
175 public:
176 // Useful STL-style traits.
177 typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
179 // = Intialization and termination methods.
181 /// "Do-nothing" constructor.
182 ACE_DLL_Strategy ();
184 /// Initialize the DLL strategy based upon the service's DLL
185 /// information contained in the <svc_dll_info> string.
186 ACE_DLL_Strategy (const ACE_TCHAR dll_name[],
187 const ACE_TCHAR factory_function[],
188 const ACE_TCHAR svc_name[],
189 ACE_Service_Repository *,
190 ACE_Thread_Manager * = 0);
192 /// Initialize the DLL strategy based upon the service's DLL
193 /// information contained in the <svc_dll_info> string.
194 int open (const ACE_TCHAR dll_name[],
195 const ACE_TCHAR factory_function[],
196 const ACE_TCHAR svc_name[],
197 ACE_Service_Repository *,
198 ACE_Thread_Manager * = 0);
200 // = Factory method.
201 /// Create a SVC_HANDLER by dynamically linking it from a DLL.
202 /// Returns -1 on failure, else 0.
203 virtual int make_svc_handler (SVC_HANDLER *&);
205 /// Dump the state of an object.
206 void dump () const;
208 /// Declare the dynamic allocation hooks.
209 ACE_ALLOC_HOOK_DECLARE;
211 protected:
212 typedef ACE_Creation_Strategy<SVC_HANDLER> inherited;
214 /// Name of the DLL to dynamically link.
215 ACE_TCHAR dll_name_[MAXPATHLEN + 1];
217 /// Name of the factory function in the shared library to use to
218 /// obtain a pointer to the new SVC_HANDLER.
219 ACE_TCHAR factory_function_[MAXPATHLEN + 1];
221 /// Name of the service.
222 ACE_TCHAR svc_name_[MAXNAMELEN + 1];
224 /// Pointer to the <Service_Repository>.
225 ACE_Service_Repository *svc_rep_;
229 * @class ACE_Concurrency_Strategy
231 * @brief Defines the interface for specifying a concurrency strategy
232 * for a SVC_HANDLER.
234 * Default behavior is to activate the SVC_HANDLER by calling
235 * its <open> method (which allows the SVC_HANDLER to define its
236 * own concurrency strategy). However, subclasses can override
237 * this default strategy to do more sophisticated concurrency
238 * activations (such as creating the SVC_HANDLER as an active
239 * object via multi-threading or multi-processing).
241 template <class SVC_HANDLER>
242 class ACE_Concurrency_Strategy
244 public:
245 // Useful STL-style traits.
246 typedef typename SVC_HANDLER::addr_type addr_type;
247 typedef SVC_HANDLER handler_type;
248 typedef typename SVC_HANDLER::stream_type stream_type;
250 /// Constructor
251 ACE_Concurrency_Strategy (int flags = 0);
253 // = Factory method.
255 * Activate the @a svc_handler with an appropriate concurrency
256 * strategy. The default behavior of this method is to activate the
257 * SVC_HANDLER by calling its <open> method (which allows the
258 * SVC_HANDLER to define its own concurrency strategy).
260 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
261 void *arg = 0);
263 virtual ~ACE_Concurrency_Strategy ();
265 /// Dump the state of an object.
266 void dump () const;
268 /// Declare the dynamic allocation hooks.
269 ACE_ALLOC_HOOK_DECLARE;
271 protected:
272 /// Flags that are parsed to set options for the connected
273 /// SVC_HANDLER.
274 int flags_;
278 * @class ACE_Reactive_Strategy
280 * @brief Defines the interface for specifying a reactive concurrency
281 * strategy for a SVC_HANDLER, where all upcalls to @c handle_*()
282 * methods run in the reactor's thread of control.
284 * This class provides a strategy that registers the
285 * SVC_HANDLER with a <Reactor>.
287 template <class SVC_HANDLER>
288 class ACE_Reactive_Strategy : public ACE_Concurrency_Strategy <SVC_HANDLER>
290 public:
291 // Useful STL-style traits.
292 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
294 // = Intialization and termination methods.
295 /// "Do-nothing constructor"
296 ACE_Reactive_Strategy (int flags = 0);
298 /// Initialize the strategy.
299 ACE_Reactive_Strategy (ACE_Reactor *reactor,
300 ACE_Reactor_Mask = ACE_Event_Handler::READ_MASK,
301 int flags = 0);
303 /// Initialize the strategy.
304 virtual int open (ACE_Reactor *reactor,
305 ACE_Reactor_Mask = ACE_Event_Handler::READ_MASK,
306 int flags = 0);
308 /// Destructor.
309 virtual ~ACE_Reactive_Strategy ();
311 // = Factory method.
312 /// Activate the @a svc_handler by registering it with the <Reactor>
313 /// and then calling it's <open> hook.
314 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
315 void *arg = 0);
317 /// Dump the state of an object.
318 void dump () const;
320 /// Declare the dynamic allocation hooks.
321 ACE_ALLOC_HOOK_DECLARE;
323 protected:
324 typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
326 /// Pointer to the Reactor we'll use to register the SVC_HANDLER.
327 ACE_Reactor *reactor_;
329 /// The mask that we pass to the <Reactor> when we register the
330 /// SVC_HANDLER.
331 ACE_Reactor_Mask mask_;
335 * @class ACE_Thread_Strategy
337 * @brief Defines the interface for specifying a concurrency strategy
338 * for a SVC_HANDLER based on multithreading.
340 * This class provides a strategy that manages the creation of threads
341 * to handle requests from clients concurrently via a
342 * thread-per-connection model. It behaves as a "thread factory",
343 * spawning threads "on-demand" to run the service specified by a
344 * user-supplied SVC_HANDLER.
346 template <class SVC_HANDLER>
347 class ACE_Thread_Strategy : public ACE_Concurrency_Strategy<SVC_HANDLER>
349 public:
350 // Useful STL-style traits.
351 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
353 // = Intialization and termination methods.
354 /// "Do-nothing constructor"
355 ACE_Thread_Strategy (int flags = 0);
357 /// Initialize the strategy.
358 ACE_Thread_Strategy (ACE_Thread_Manager *tm,
359 long thr_flags,
360 int n_threads = 1,
361 int flags = 0);
363 /// Initialize the strategy.
364 virtual int open (ACE_Thread_Manager *tm,
365 long thr_flags,
366 int n_threads = 1,
367 int flags = 0);
369 virtual ~ACE_Thread_Strategy ();
371 // = Factory method.
373 * Activate the @a svc_handler with an appropriate concurrency
374 * strategy. This method activates the SVC_HANDLER by first calling
375 * its <open> method and then calling its <activate> method to turn
376 * it into an active object.
378 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
379 void *arg = 0);
381 /// Dump the state of an object.
382 void dump () const;
384 /// Declare the dynamic allocation hooks.
385 ACE_ALLOC_HOOK_DECLARE;
387 protected:
388 typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
390 /// Thread manager for this class (must be provided).
391 ACE_Thread_Manager *thr_mgr_;
393 /// Flags to pass into the <SVC_HANDLER::activate> method.
394 long thr_flags_;
396 /// Number of threads to spawn.
397 int n_threads_;
401 * @class ACE_Process_Strategy
403 * @brief Defines the interface for specifying a concurrency strategy
404 * for a @c SVC_HANDLER based on multiprocessing.
406 * This class provides a strategy that manages the creation of
407 * processes to handle requests from clients concurrently using a
408 * process-per-connection model. It behaves as a "process factory",
409 * using @c ACE::fork() to fork threads "on-demand" to run the service
410 * specified by a user-supplied @c SVC_HANDLER in a separate process.
412 template <class SVC_HANDLER>
413 class ACE_Process_Strategy : public ACE_Concurrency_Strategy<SVC_HANDLER>
415 public:
416 // Useful STL-style traits.
417 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
419 // = Intialization and termination methods.
421 /// Initialize the strategy. If @a avoid_zombies is non-0 then set a
422 /// flag to ACE::fork() to avoid zombies.
423 ACE_Process_Strategy (size_t n_processes = 1,
424 ACE_Event_Handler *acceptor = 0,
425 ACE_Reactor * = 0,
426 int avoid_zombies = 0);
428 /// Initialize the strategy. If @a avoid_zombies is non-0 then set a
429 /// flag to ACE::fork() to avoid zombies.
430 virtual int open (size_t n_processes = 1,
431 ACE_Event_Handler *acceptor = 0,
432 ACE_Reactor * = 0,
433 int avoid_zombies = 0);
435 virtual ~ACE_Process_Strategy ();
437 // = Factory method.
439 * Activate the @a svc_handler with an appropriate concurrency
440 * strategy. This method activates the SVC_HANDLER by first forking
441 * and then calling the @c open() method of the SVC_HANDLER in the
442 * child.
444 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
445 void *arg = 0);
447 /// Dump the state of an object.
448 void dump () const;
450 /// Declare the dynamic allocation hooks.
451 ACE_ALLOC_HOOK_DECLARE;
453 protected:
454 typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
456 /// Number of processes to spawn.
457 size_t n_processes_;
460 * This is the @c Acceptor in the parent is listening on. We need to
461 * make sure that we remove it from the Reactor and close it down in
462 * the child.
464 ACE_Event_Handler *acceptor_;
467 * This is the reactor the child is using in conjunction with the
468 * acceptor. We need to remove the acceptor from this reactor
469 * in the child.
471 ACE_Reactor *reactor_;
475 * @class ACE_Accept_Strategy
477 * @brief Defines the interface for specifying a passive connection
478 * acceptance strategy for a SVC_HANDLER.
480 * This class provides a strategy that manages passive
481 * connection acceptance of a client.
483 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1>
484 class ACE_Accept_Strategy
486 public:
487 // Useful STL-style traits.
488 typedef ACE_PEER_ACCEPTOR_ADDR addr_type;
489 typedef ACE_PEER_ACCEPTOR acceptor_type;
490 typedef SVC_HANDLER handler_type;
491 typedef typename SVC_HANDLER::stream_type stream_type;
493 /// Default constructor.
494 ACE_Accept_Strategy (ACE_Reactor *reactor = ACE_Reactor::instance ());
496 /// Initialize the @c peer_acceptor_ with @a local_addr.
497 ACE_Accept_Strategy (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
498 bool restart = false,
499 ACE_Reactor *reactor = ACE_Reactor::instance ());
501 /// Initialize the <peer_acceptor_> with @a local_addr, indicating
502 /// whether to @a reuse_addr if it's already in use.
503 virtual int open (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
504 bool reuse_addr = false);
506 /// Return the underlying ACE_HANDLE of the <peer_acceptor_>.
507 virtual ACE_HANDLE get_handle () const;
509 /// Return a reference to the <peer_acceptor_>.
510 virtual ACE_PEER_ACCEPTOR &acceptor () const;
512 virtual ~ACE_Accept_Strategy ();
514 // = Factory method.
515 /// The default behavior delegates to the <accept> method of the
516 /// PEER_ACCEPTOR.
517 virtual int accept_svc_handler (SVC_HANDLER *);
519 /// Dump the state of an object.
520 void dump () const;
522 /// Declare the dynamic allocation hooks.
523 ACE_ALLOC_HOOK_DECLARE;
525 protected:
526 /// Factory that establishes connections passively.
527 ACE_PEER_ACCEPTOR peer_acceptor_;
529 /// Pointer to the reactor used by the Acceptor.
530 ACE_Reactor *reactor_;
532 /// Needed to reopen the socket if <accept> fails.
533 bool reuse_addr_;
535 /// Needed to reopen the socket if <accept> fails.
536 ACE_PEER_ACCEPTOR_ADDR peer_acceptor_addr_;
540 * @class ACE_Connect_Strategy
542 * @brief Defines the interface for specifying an active
543 * connection establishment strategy for a SVC_HANDLER.
545 * This class provides a strategy that manages active
546 * connection establishment to a server.
548 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1>
549 class ACE_Connect_Strategy
551 public:
552 // Useful STL-style traits.
553 typedef ACE_PEER_CONNECTOR_ADDR addr_type;
554 typedef ACE_PEER_CONNECTOR connector_type;
555 typedef SVC_HANDLER handler_type;
556 typedef typename SVC_HANDLER::stream_type stream_type;
558 /// Default constructor.
559 ACE_Connect_Strategy ();
561 /// Return a reference to the <peer_connector_>.
562 virtual ACE_PEER_CONNECTOR &connector () const;
564 virtual ~ACE_Connect_Strategy ();
566 // = Factory method.
567 /// The default behavior delegates to the <connect> method of the
568 /// <PEER_CONNECTOR::connect>.
569 virtual int connect_svc_handler (SVC_HANDLER *&sh,
570 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
571 ACE_Time_Value *timeout,
572 const ACE_PEER_CONNECTOR_ADDR &local_addr,
573 bool reuse_addr,
574 int flags,
575 int perms);
578 * The default behavior delegates to the <connect> method of the
579 * <PEER_CONNECTOR::connect>.
580 * Please check the documentation in Connector.h for more details.
582 virtual int connect_svc_handler (SVC_HANDLER *&sh,
583 SVC_HANDLER *&sh_copy,
584 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
585 ACE_Time_Value *timeout,
586 const ACE_PEER_CONNECTOR_ADDR &local_addr,
587 bool reuse_addr,
588 int flags,
589 int perms);
591 /// Dump the state of an object.
592 void dump () const;
594 /// Declare the dynamic allocation hooks.
595 ACE_ALLOC_HOOK_DECLARE;
597 protected:
598 /// Factory that establishes connections actively.
599 ACE_PEER_CONNECTOR connector_;
603 * @class ACE_Scheduling_Strategy
605 * @brief Defines the interface for specifying how to suspend and
606 * resume a service .
608 * This class provides a strategy that allows arbitrarily
609 * sophisticated service suspension and resumption. The default
610 * behavior is to do nothing...
612 template <class SVC_HANDLER>
613 class ACE_Scheduling_Strategy
615 public:
616 // Useful STL-style traits.
617 typedef typename SVC_HANDLER::addr_type addr_type;
618 typedef SVC_HANDLER handler_type;
619 typedef typename SVC_HANDLER::stream_type stream_type;
621 /// Constructor
622 ACE_Scheduling_Strategy (SVC_HANDLER * = 0);
624 /// Destructor
625 virtual ~ACE_Scheduling_Strategy ();
627 // = Scheduling methods
629 /// Suspend hook.
630 virtual int suspend ();
632 /// Resume hook.
633 virtual int resume ();
635 /// Dump the state of the object.
636 virtual void dump () const;
640 * @class ACE_Schedule_All_Reactive_Strategy
642 * @brief Defines the interface for specifying how to suspend and
643 * resume a single-threaded reactive service .
645 * This class provides a strategy that suspends and resumes all
646 * the Event_Handlers in a Reactor in one fell swoop.
648 template <class SVC_HANDLER>
649 class ACE_Schedule_All_Reactive_Strategy
650 : public ACE_Scheduling_Strategy<SVC_HANDLER>
652 public:
653 // Useful STL-style traits.
654 typedef ACE_Scheduling_Strategy<SVC_HANDLER> base_type;
656 /// Constructor
657 ACE_Schedule_All_Reactive_Strategy (SVC_HANDLER * = 0);
659 // = Scheduling methods
661 /// Suspend hook.
662 virtual int suspend ();
664 /// Resume hook.
665 virtual int resume ();
667 /// Dump the state of the object.
668 virtual void dump () const;
670 protected:
671 /// Reactor
672 ACE_Reactor *reactor_;
676 * @class ACE_Schedule_All_Threaded_Strategy
678 * @brief Defines the interface for specifying how to suspend and
679 * resume a multithreaded service .
681 * This class provides a strategy that suspends and resumes all
682 * the Event_Handlers controlled by a Thread_Manager in one fell swoop.
684 template <class SVC_HANDLER>
685 class ACE_Schedule_All_Threaded_Strategy
686 : public ACE_Scheduling_Strategy<SVC_HANDLER>
688 public:
689 // Useful STL-style traits.
690 typedef ACE_Scheduling_Strategy<SVC_HANDLER> base_type;
692 /// Constructor
693 ACE_Schedule_All_Threaded_Strategy (SVC_HANDLER * = 0);
695 // = Scheduling methods
697 /// Suspend hook.
698 virtual int suspend ();
700 /// Resume hook.
701 virtual int resume ();
703 /// Dump the state of the object.
704 virtual void dump () const;
706 protected:
707 /// Thread Manager
708 ACE_Thread_Manager *thr_mgr_;
712 * @class ACE_NOOP_Creation_Strategy
714 * @brief Implements a no-op creation strategy in order to defer
715 * decisions regarding creation to some later point in time, such
716 * as in connect or accept strategy.
718 * An example of the use of this is in the
719 * ACE_Cached_Connect_Strategy, which only returns a single
720 * connection for a given endpoint.
722 template <class SVC_HANDLER>
723 class ACE_NOOP_Creation_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
725 public:
726 // Useful STL-style traits.
727 typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
729 /// This is a no-op.
730 virtual int make_svc_handler (SVC_HANDLER *&);
734 * @class ACE_NOOP_Concurrency_Strategy
736 * @brief Implements a no-op activation strategy in order to avoid
737 * calling open on a svc_handler multiple times.
739 * An example of the use of this is in the
740 * ACE_Cached_Connect_Strategy, which reuses svc_handlers.
741 * Therefore we don't want to call open on the recycled
742 * svc_handler more than once.
744 template <class SVC_HANDLER>
745 class ACE_NOOP_Concurrency_Strategy
746 : public ACE_Concurrency_Strategy<SVC_HANDLER>
748 public:
749 // Useful STL-style traits.
750 typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
752 // = Factory method.
753 /// This is a no-op.
754 virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
755 void *arg = 0);
758 template <class T>
759 class ACE_Refcounted_Hash_Recyclable : public ACE_Refcountable_T<ACE_Null_Mutex>,
760 public ACE_Hashable,
761 public ACE_Recyclable
763 public:
764 /// Default constructor.
765 ACE_Refcounted_Hash_Recyclable ();
767 /// Constructor.
768 ACE_Refcounted_Hash_Recyclable (const T &t,
769 long refcount = 0,
770 ACE_Recyclable_State state = ACE_RECYCLABLE_UNKNOWN);
772 /// Destructor
773 virtual ~ACE_Refcounted_Hash_Recyclable ();
775 /// Compares two instances.
776 bool operator== (const ACE_Refcounted_Hash_Recyclable<T> &rhs) const;
777 bool operator!= (const ACE_Refcounted_Hash_Recyclable<T> &rhs) const;
779 T &subject ();
781 protected:
782 /// Computes and returns hash value.
783 u_long hash_i () const;
785 T t_;
789 * @class ACE_Cached_Connect_Strategy
791 * @brief A connection strategy which caches connections to peers
792 * (represented by SVC_HANDLER instances), thereby allowing
793 * subsequent re-use of unused, but available, connections.
795 * <ACE_Cached_Connect_Strategy> is intended to be used as a
796 * plug-in connection strategy for ACE_Strategy_Connector.
797 * It's added value is re-use of established connections.
799 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class MUTEX>
800 class ACE_Cached_Connect_Strategy
801 : public ACE_Connection_Recycling_Strategy,
802 public ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
804 public:
805 // Useful STL-style traits.
806 typedef ACE_Creation_Strategy<SVC_HANDLER>
807 creation_strategy_type;
808 typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
809 connect_strategy_type;
810 typedef ACE_Concurrency_Strategy<SVC_HANDLER>
811 concurrency_strategy_type;
812 typedef ACE_Recycling_Strategy<SVC_HANDLER> recycling_strategy_type;
814 // = Define some useful (old style) traits.
815 typedef ACE_Creation_Strategy<SVC_HANDLER>
816 CREATION_STRATEGY;
817 typedef ACE_Concurrency_Strategy<SVC_HANDLER>
818 CONCURRENCY_STRATEGY;
819 typedef ACE_Recycling_Strategy<SVC_HANDLER>
820 RECYCLING_STRATEGY;
822 // = Super class
823 typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
824 CONNECT_STRATEGY;
827 typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX> SELF;
829 /// Constructor
830 ACE_Cached_Connect_Strategy (ACE_Creation_Strategy<SVC_HANDLER> *cre_s = 0,
831 ACE_Concurrency_Strategy<SVC_HANDLER> *con_s = 0,
832 ACE_Recycling_Strategy<SVC_HANDLER> *rec_s = 0,
833 MUTEX *mutex = 0,
834 bool delete_lock = false);
836 /// Destructor
837 virtual ~ACE_Cached_Connect_Strategy ();
839 /// This methods allow you to change the strategies used by the
840 /// cached connector.
841 virtual int open (ACE_Creation_Strategy<SVC_HANDLER> *cre_s,
842 ACE_Concurrency_Strategy<SVC_HANDLER> *con_s,
843 ACE_Recycling_Strategy<SVC_HANDLER> *rec_s);
845 /// Template method for making a new <svc_handler>
846 virtual int make_svc_handler (SVC_HANDLER *&sh);
848 /// Template method for activating a new @a svc_handler
849 virtual int activate_svc_handler (SVC_HANDLER *svc_handler);
851 /// Template method for setting the recycler information of the
852 /// svc_handler.
853 virtual int assign_recycler (SVC_HANDLER *svc_handler,
854 ACE_Connection_Recycling_Strategy *recycler,
855 const void *recycling_act);
857 /// Template method for preparing the svc_handler for recycling.
858 virtual int prepare_for_recycling (SVC_HANDLER *svc_handler);
861 * Checks to see if there is already a <SVC_HANDLER> in the cache
862 * connected to the <remote_addr>. If so, we return this pointer.
863 * Otherwise we establish the connection, put it into the cache, and
864 * return the SVC_HANDLER pointer. <[NOTE]>: the <{reuse_addr}>
865 * argument does NOT control re-use of addresses in the cache.
866 * Rather, if the underlying protocol requires a "dead time" prior
867 * to re-use of its addresses (TCP is a classic example of this),
868 * <{and}> the protocol provides a means by which to defeat the dead
869 * time, setting this argument to non-zero will defeat the dead-time
870 * requirement. <{Dev. Note: We might want to consider enhancing
871 * the interface at some point so that this also controls re-use of
872 * the cache.}>
874 virtual int connect_svc_handler (SVC_HANDLER *&sh,
875 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
876 ACE_Time_Value *timeout,
877 const ACE_PEER_CONNECTOR_ADDR &local_addr,
878 bool reuse_addr,
879 int flags,
880 int perms);
881 virtual int connect_svc_handler (SVC_HANDLER *&sh,
882 SVC_HANDLER *&sh_copy,
883 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
884 ACE_Time_Value *timeout,
885 const ACE_PEER_CONNECTOR_ADDR &local_addr,
886 bool reuse_addr,
887 int flags,
888 int perms);
890 /// Remove from cache.
891 virtual int purge (const void *recycling_act);
893 /// Add to cache.
894 virtual int cache (const void *recycling_act);
896 /// Get/Set <recycle_state>.
897 virtual int recycle_state (const void *recycling_act,
898 ACE_Recyclable_State new_state);
899 virtual ACE_Recyclable_State recycle_state (const void *recycling_act) const;
901 /// Mark as closed.
902 virtual int mark_as_closed (const void *recycling_act);
905 * Mark as closed (non-locking version). This method needs to be public
906 * as it is used in the cleanup of handlers where teh locked version causes
907 * a deadlock.
909 virtual int mark_as_closed_i (const void *recycling_act);
911 /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
912 virtual int cleanup_hint (const void *recycling_act,
913 void **act_holder = 0);
915 // = Traits for managing the map
916 typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
917 REFCOUNTED_HASH_RECYCLABLE_ADDRESS;
918 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>
919 CONNECTION_MAP;
921 typedef typename CONNECTION_MAP::ITERATOR CONNECTION_MAP_ITERATOR;
922 typedef typename CONNECTION_MAP::ENTRY CONNECTION_MAP_ENTRY;
924 typedef ACE_Reverse_Lock<MUTEX> REVERSE_MUTEX;
926 // = Strategy accessors
927 virtual ACE_Creation_Strategy<SVC_HANDLER> *creation_strategy () const;
928 virtual ACE_Recycling_Strategy<SVC_HANDLER> *recycling_strategy () const;
929 virtual ACE_Concurrency_Strategy<SVC_HANDLER> *concurrency_strategy () const;
931 protected:
932 /// Creates a new connection.
933 virtual int new_connection (SVC_HANDLER *&sh,
934 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
935 ACE_Time_Value *timeout,
936 const ACE_PEER_CONNECTOR_ADDR &local_addr,
937 bool reuse_addr,
938 int flags,
939 int perms);
941 /// Find an idle handle.
942 int find (REFCOUNTED_HASH_RECYCLABLE_ADDRESS &search_addr,
943 CONNECTION_MAP_ENTRY *&entry);
945 /// Remove from cache (non-locking version).
946 virtual int purge_i (const void *recycling_act);
948 /// Add to cache (non-locking version).
949 virtual int cache_i (const void *recycling_act);
951 /// Set <recycle_state> (non-locking version).
952 virtual int recycle_state_i (const void *recycling_act,
953 ACE_Recyclable_State new_state);
955 /// Get <recycle_state> (non-locking version).
956 virtual ACE_Recyclable_State recycle_state_i (const void *recycling_act) const;
958 /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
959 virtual int cleanup_hint_i (const void *recycling_act,
960 void **act_holder);
962 // = Helpers
963 int check_hint_i (SVC_HANDLER *&sh,
964 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
965 ACE_Time_Value *timeout,
966 const ACE_PEER_CONNECTOR_ADDR &local_addr,
967 bool reuse_addr,
968 int flags,
969 int perms,
970 CONNECTION_MAP_ENTRY *&entry,
971 int &found);
973 int find_or_create_svc_handler_i (SVC_HANDLER *&sh,
974 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
975 ACE_Time_Value *timeout,
976 const ACE_PEER_CONNECTOR_ADDR &local_addr,
977 bool reuse_addr,
978 int flags,
979 int perms,
980 CONNECTION_MAP_ENTRY *&entry,
981 int &found);
983 virtual int connect_svc_handler_i (
984 SVC_HANDLER *&sh,
985 const ACE_PEER_CONNECTOR_ADDR &remote_addr,
986 ACE_Time_Value *timeout,
987 const ACE_PEER_CONNECTOR_ADDR &local_addr,
988 bool reuse_addr,
989 int flags,
990 int perms,
991 int &found);
993 /// Table that maintains the cache of connected SVC_HANDLERs.
994 CONNECTION_MAP connection_map_;
996 /// Mutual exclusion for this object.
997 MUTEX *lock_;
999 /// Mutual exclusion for this object.
1000 bool delete_lock_;
1002 /// Reverse lock.
1003 REVERSE_MUTEX *reverse_lock_;
1005 // = Strategy objects.
1007 /// Creation strategy for an <Connector>.
1008 CREATION_STRATEGY *creation_strategy_;
1010 /// true if <Connector> created the creation strategy and thus should
1011 /// delete it, else false.
1012 bool delete_creation_strategy_;
1014 /// Concurrency strategy for an <Connector>.
1015 CONCURRENCY_STRATEGY *concurrency_strategy_;
1017 /// true if <Connector> created the concurrency strategy and thus should
1018 /// delete it, else false.
1019 bool delete_concurrency_strategy_;
1021 /// Recycling strategy for an <Connector>.
1022 RECYCLING_STRATEGY *recycling_strategy_;
1024 /// true if <Connector> created the recycling strategy and thus should
1025 /// delete it, else false.
1026 bool delete_recycling_strategy_;
1029 ACE_END_VERSIONED_NAMESPACE_DECL
1031 #if defined (__ACE_INLINE__)
1032 #include "ace/Strategies_T.inl"
1033 #endif /* __ACE_INLINE__ */
1035 #include "ace/Strategies_T.cpp"
1037 #include /**/ "ace/post.h"
1039 #endif /* ACE_STRATEGIES_T_H */