Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Strategies_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file   Strategies_T.h
00006  *
00007  *  $Id: Strategies_T.h,v 1.1.1.4 2003/02/21 18:36:32 chad Exp $
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_STRATEGIES_T_H
00014 #define ACE_STRATEGIES_T_H
00015 
00016 #include "ace/pre.h"
00017 
00018 #include "ace/Hash_Map_Manager.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Service_Config.h"
00025 #include "ace/Reactor.h"
00026 #include "ace/Synch_Options.h"
00027 #include "ace/Thread_Manager.h"
00028 #include "ace/Connection_Recycling_Strategy.h"
00029 #include "ace/Refcountable.h"
00030 #include "ace/Hashable.h"
00031 #include "ace/Recyclable.h"
00032 
00033 // Needed for broken linkers that can't grok long symbols.
00034 #define ACE_Refcounted_Hash_Recyclable ARHR
00035 
00036 /**
00037  * @class ACE_Recycling_Strategy
00038  *
00039  * @brief Defines the interface (and default implementation) for
00040  * specifying a recycling strategy for a SVC_HANDLER.
00041  *
00042  * Acts as a consular to the Svc_Handler, preparing it for the
00043  * tough times ahead when the Svc_Handler will be recycled.
00044  */
00045 template<class SVC_HANDLER>
00046 class ACE_Recycling_Strategy
00047 {
00048 public:
00049 
00050   // Useful STL-style traits.
00051   typedef ACE_TYPENAME SVC_HANDLER::addr_type    addr_type;
00052   typedef SVC_HANDLER                            handler_type;
00053   typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;
00054 
00055   /// Virtual Destructor
00056   virtual ~ACE_Recycling_Strategy (void);
00057 
00058   /// Tell the Svc_Handler something about the recycler, so that it can
00059   /// reach the recycler when necessary.
00060   virtual int assign_recycler (SVC_HANDLER *svc_handler,
00061                                ACE_Connection_Recycling_Strategy *recycler,
00062                                const void *recycling_act);
00063 
00064   /// This allows us to prepare the svc_handler for recycling.
00065   virtual int prepare_for_recycling (SVC_HANDLER *svc_handler);
00066 };
00067 
00068 /**
00069  * @class ACE_Creation_Strategy
00070  *
00071  * @brief Defines the interface for specifying a creation strategy for
00072  * a SVC_HANDLER.
00073  *
00074  * The default behavior is to make a new SVC_HANDLER.  However,
00075  * subclasses can override this strategy to perform SVC_HANDLER
00076  * creation in any way that they like (such as creating subclass
00077  * instances of SVC_HANDLER, using a singleton, dynamically
00078  * linking the handler, etc.).
00079  */
00080 template <class SVC_HANDLER>
00081 class ACE_Creation_Strategy
00082 {
00083 public:
00084 
00085   // Useful STL-style traits.
00086   typedef ACE_TYPENAME SVC_HANDLER::addr_type    addr_type;
00087   typedef SVC_HANDLER                            handler_type;
00088   typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;
00089 
00090   // = Initialization and termination methods.
00091 
00092   /// Default constructor.
00093   ACE_Creation_Strategy (ACE_Thread_Manager * = 0,
00094                          ACE_Reactor * = ACE_Reactor::instance ());
00095 
00096   /// An ACE_Thread_Manager is useful when creating active objects and
00097   /// the ACE_Reactor is used to initialize the service handler's reactor.
00098   int open (ACE_Thread_Manager * = 0,
00099             ACE_Reactor * = ACE_Reactor::instance ());
00100 
00101   virtual ~ACE_Creation_Strategy (void);
00102 
00103   // = Factory method.
00104   /**
00105    * Create a SVC_HANDLER with the appropriate creation strategy.  The
00106    * default behavior of this method is to make a new <SVC_HANDLER> if
00107    * <sh> == 0 (passing in the <Thread_Manager>), else <sh> is
00108    * unchanged.  Returns -1 on failure, else 0.
00109    */
00110   virtual int make_svc_handler (SVC_HANDLER *&sh);
00111 
00112   /// Dump the state of an object.
00113   void dump (void) const;
00114 
00115   /// Declare the dynamic allocation hooks.
00116   ACE_ALLOC_HOOK_DECLARE;
00117 
00118 protected:
00119   /// Pointer to a thread manager.
00120   ACE_Thread_Manager *thr_mgr_;
00121 
00122   /// Pointer to an ACE_Reactor.
00123   ACE_Reactor *reactor_;
00124 };
00125 
00126 /**
00127  * @class ACE_Singleton_Strategy
00128  *
00129  * @brief Defines the interface for specifying a creation strategy for
00130  * a <SVC_HANDLER> that always returns the same <SVC_HANDLER> (i.e.,
00131  * it's a Singleton).
00132  *
00133  * Note that this class takes over the ownership of the
00134  * SVC_HANDLER passed into it as a parameter and it becomes
00135  * responsible for deleting this object.
00136  */
00137 template <class SVC_HANDLER>
00138 class ACE_Singleton_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
00139 {
00140 public:
00141 
00142   // Useful STL-style traits.
00143   typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
00144 
00145   // = Initialization and termination methods.
00146   ACE_Singleton_Strategy (SVC_HANDLER * = 0,
00147                           ACE_Thread_Manager * = 0);
00148   int open (SVC_HANDLER *,
00149             ACE_Thread_Manager * = 0);
00150   virtual ~ACE_Singleton_Strategy (void);
00151 
00152   // = Factory method.
00153   /// Create a Singleton SVC_HANDLER by always returning the same
00154   /// SVC_HANDLER.  Returns -1 on failure, else 0.
00155   virtual int make_svc_handler (SVC_HANDLER *&);
00156 
00157   /// Dump the state of an object.
00158   void dump (void) const;
00159 
00160   /// Declare the dynamic allocation hooks.
00161   ACE_ALLOC_HOOK_DECLARE;
00162 
00163 protected:
00164   /// Pointer to the Singleton svc_handler.
00165   SVC_HANDLER *svc_handler_;
00166 
00167   /// Keep track of whether we need to delete the <SVC_HANDLER>.
00168   int delete_svc_handler_;
00169 };
00170 
00171 /**
00172  * @class ACE_DLL_Strategy
00173  *
00174  * @brief Defines the interface for specifying a creation strategy for
00175  * a SVC_HANDLER based on dynamic linking of the SVC_HANDLER.
00176  */
00177 template <class SVC_HANDLER>
00178 class ACE_DLL_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
00179 {
00180 public:
00181 
00182   // Useful STL-style traits.
00183   typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
00184 
00185   // = Intialization and termination methods.
00186 
00187   /// "Do-nothing" constructor.
00188   ACE_DLL_Strategy (void);
00189 
00190   /// Initialize the DLL strategy based upon the service's DLL
00191   /// information contained in the <svc_dll_info> string.
00192   ACE_DLL_Strategy (const char dll_name[],
00193                     const char factory_function[],
00194                     const char svc_name[],
00195                     ACE_Service_Repository *,
00196                     ACE_Thread_Manager * = 0);
00197 
00198   /// Initialize the DLL strategy based upon the service's DLL
00199   /// information contained in the <svc_dll_info> string.
00200   int open (const char dll_name[],
00201             const char factory_function[],
00202             const char svc_name[],
00203             ACE_Service_Repository *,
00204             ACE_Thread_Manager * = 0);
00205 
00206   // = Factory method.
00207   /// Create a SVC_HANDLER by dynamically linking it from a DLL.
00208   /// Returns -1 on failure, else 0.
00209   virtual int make_svc_handler (SVC_HANDLER *&);
00210 
00211   /// Dump the state of an object.
00212   void dump (void) const;
00213 
00214   /// Declare the dynamic allocation hooks.
00215   ACE_ALLOC_HOOK_DECLARE;
00216 
00217 protected:
00218   typedef ACE_Creation_Strategy<SVC_HANDLER> inherited;
00219 
00220   /// Name of the DLL to dynamically link.
00221   char dll_name_[MAXPATHLEN + 1];
00222 
00223   /// Name of the factory function in the shared library to use to
00224   /// obtain a pointer to the new SVC_HANDLER.
00225   char factory_function_[MAXPATHLEN + 1];
00226 
00227   /// Name of the service.
00228   char svc_name_[MAXNAMELEN + 1];
00229 
00230   /// Pointer to the <Service_Repository>.
00231   ACE_Service_Repository *svc_rep_;
00232 };
00233 
00234 /**
00235  * @class ACE_Concurrency_Strategy
00236  *
00237  * @brief Defines the interface for specifying a concurrency strategy
00238  * for a SVC_HANDLER.
00239  *
00240  * Default behavior is to activate the SVC_HANDLER by calling
00241  * its <open> method (which allows the SVC_HANDLER to define its
00242  * own concurrency strategy).  However, subclasses can override
00243  * this default strategy to do more sophisticated concurrency
00244  * activations (such as creating the SVC_HANDLER as an active
00245  * object via multi-threading or multi-processing).
00246  */
00247 template <class SVC_HANDLER>
00248 class ACE_Concurrency_Strategy
00249 {
00250 public:
00251 
00252   // Useful STL-style traits.
00253   typedef ACE_TYPENAME SVC_HANDLER::addr_type    addr_type;
00254   typedef SVC_HANDLER                            handler_type;
00255   typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;
00256 
00257   /// Constructor
00258   ACE_Concurrency_Strategy (int flags = 0);
00259 
00260   // = Factory method.
00261   /**
00262    * Activate the <svc_handler> with an appropriate concurrency
00263    * strategy.  The default behavior of this method is to activate the
00264    * SVC_HANDLER by calling its <open> method (which allows the
00265    * SVC_HANDLER to define its own concurrency strategy).
00266    */
00267   virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
00268                                     void *arg = 0);
00269 
00270   virtual ~ACE_Concurrency_Strategy (void);
00271 
00272   /// Dump the state of an object.
00273   void dump (void) const;
00274 
00275   /// Declare the dynamic allocation hooks.
00276   ACE_ALLOC_HOOK_DECLARE;
00277 
00278 protected:
00279 
00280   /// Flags that are parsed to set options for the connected
00281   /// <SVC_HANDLER>.
00282   int flags_;
00283 };
00284 
00285 /**
00286  * @class ACE_Reactive_Strategy
00287  *
00288  * @brief Defines the interface for specifying a Reactive concurrency
00289  * strategy for a SVC_HANDLER.
00290  *
00291  * This class provides a strategy that registers the
00292  * <SVC_HANDLER> with a <Reactor>.
00293  */
00294 template <class SVC_HANDLER>
00295 class ACE_Reactive_Strategy : public ACE_Concurrency_Strategy <SVC_HANDLER>
00296 {
00297 public:
00298 
00299   // Useful STL-style traits.
00300   typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
00301 
00302   // = Intialization and termination methods.
00303   /// "Do-nothing constructor"
00304   ACE_Reactive_Strategy (int flags = 0);
00305 
00306   /// Initialize the strategy.
00307   ACE_Reactive_Strategy (ACE_Reactor *reactor,
00308                          ACE_Reactor_Mask = ACE_Event_Handler::READ_MASK,
00309                          int flags = 0);
00310 
00311   /// Initialize the strategy.
00312   virtual int open (ACE_Reactor *reactor,
00313                     ACE_Reactor_Mask = ACE_Event_Handler::READ_MASK,
00314                     int flags = 0);
00315 
00316   /// Destructor.
00317   virtual ~ACE_Reactive_Strategy (void);
00318 
00319   // = Factory method.
00320   /// Activate the <svc_handler> by registering it with the <Reactor>
00321   /// and then calling it's <open> hook.
00322   virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
00323                                     void *arg = 0);
00324 
00325   /// Dump the state of an object.
00326   void dump (void) const;
00327 
00328   /// Declare the dynamic allocation hooks.
00329   ACE_ALLOC_HOOK_DECLARE;
00330 
00331 protected:
00332   typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
00333 
00334   /// Pointer to the Reactor we'll use to register the <SVC_HANDLER>.
00335   ACE_Reactor *reactor_;
00336 
00337   /// The mask that we pass to the <Reactor> when we register the
00338   /// <SVC_HANDLER>.
00339   ACE_Reactor_Mask mask_;
00340 };
00341 
00342 /**
00343  * @class ACE_Thread_Strategy
00344  *
00345  * @brief Defines the interface for specifying a concurrency strategy
00346  * for a <SVC_HANDLER> based on multithreading.
00347  *
00348  * This class provides a strategy that manages the creation of
00349  * threads to handle requests from clients concurrently.  It
00350  * behaves as a "thread factory", spawning threads "on-demand"
00351  * to run the service specified by a user-supplied
00352  * <SVC_HANDLER>.
00353  */
00354 template <class SVC_HANDLER>
00355 class ACE_Thread_Strategy : public ACE_Concurrency_Strategy<SVC_HANDLER>
00356 {
00357 public:
00358 
00359   // Useful STL-style traits.
00360   typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
00361 
00362   // = Intialization and termination methods.
00363   /// "Do-nothing constructor"
00364   ACE_Thread_Strategy (int flags = 0);
00365 
00366   /// Initialize the strategy.
00367   ACE_Thread_Strategy (ACE_Thread_Manager *tm,
00368                        long thr_flags,
00369                        int n_threads = 1,
00370                        int flags = 0);
00371 
00372   /// Initialize the strategy.
00373   virtual int open (ACE_Thread_Manager *tm,
00374                     long thr_flags,
00375                     int n_threads = 1,
00376                     int flags = 0);
00377 
00378   virtual ~ACE_Thread_Strategy (void);
00379 
00380   // = Factory method.
00381   /**
00382    * Activate the <svc_handler> with an appropriate concurrency
00383    * strategy.  This method activates the SVC_HANDLER by first calling
00384    * its <open> method and then calling its <activate> method to turn
00385    * it into an active object.
00386    */
00387   virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
00388                                     void *arg = 0);
00389 
00390   /// Dump the state of an object.
00391   void dump (void) const;
00392 
00393   /// Declare the dynamic allocation hooks.
00394   ACE_ALLOC_HOOK_DECLARE;
00395 
00396 protected:
00397   typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
00398 
00399   /// Thread manager for this class (must be provided).
00400   ACE_Thread_Manager *thr_mgr_;
00401 
00402   /// Flags to pass into the <SVC_HANDLER::activate> method.
00403   long thr_flags_;
00404 
00405   /// Number of threads to spawn.
00406   int n_threads_;
00407 };
00408 
00409 /**
00410  * @class ACE_Process_Strategy
00411  *
00412  * @brief Defines the interface for specifying a concurrency strategy
00413  * for a <SVC_HANDLER> based on multiprocessing.
00414  *
00415  * This class provides a strategy that manages the creation of
00416  * processes to handle requests from clients concurrently.  It
00417  * behaves as a "process factory", using <ACE::fork> to fork
00418  * threads "on-demand" to run the service specified by a
00419  * user-supplied <SVC_HANDLER> in a separate process.
00420  */
00421 template <class SVC_HANDLER>
00422 class ACE_Process_Strategy : public ACE_Concurrency_Strategy<SVC_HANDLER>
00423 {
00424 public:
00425 
00426   // Useful STL-style traits.
00427   typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
00428 
00429   // = Intialization and termination methods.
00430 
00431   /// Initialize the strategy.  If <avoid_zombies> is non-0 then set a
00432   /// flag to <ACE::fork> to avoid zombies.
00433   ACE_Process_Strategy (size_t n_processes = 1,
00434                         ACE_Event_Handler *acceptor = 0,
00435                         ACE_Reactor * = 0,
00436                         int avoid_zombies = 0);
00437 
00438   /// Initialize the strategy.  If <avoid_zombies> is non-0 then set a
00439   /// flag to <ACE::fork> to avoid zombies.
00440   virtual int open (size_t n_processes = 1,
00441                     ACE_Event_Handler *acceptor = 0,
00442                     ACE_Reactor * = 0,
00443                     int avoid_zombies = 0);
00444 
00445   virtual ~ACE_Process_Strategy (void);
00446 
00447   // = Factory method.
00448   /**
00449    * Activate the <svc_handler> with an appropriate concurrency
00450    * strategy.  This method activates the SVC_HANDLER by first forking
00451    * and then calling the <open> method of the SVC_HANDLER in the
00452    * child.
00453    */
00454   virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
00455                                     void *arg = 0);
00456 
00457   /// Dump the state of an object.
00458   void dump (void) const;
00459 
00460   /// Declare the dynamic allocation hooks.
00461   ACE_ALLOC_HOOK_DECLARE;
00462 
00463 protected:
00464   typedef ACE_Concurrency_Strategy<SVC_HANDLER> inherited;
00465 
00466   /// Number of processes to spawn.
00467   size_t n_processes_;
00468 
00469   /**
00470    * This is the <Acceptor> in the parent is listening on.  We need to
00471    * make sure that we remove it from the Reactor and close it down in
00472    * the child.
00473    */
00474   ACE_Event_Handler *acceptor_;
00475 
00476   /**
00477    * This is the <Reactor> the child is using in conjunction with the
00478    * <Acceptor>.  We need to remove the <Acceptor> from this <Reactor>
00479    * in the child.
00480    */
00481   ACE_Reactor *reactor_;
00482 };
00483 
00484 /**
00485  * @class ACE_Accept_Strategy
00486  *
00487  * @brief Defines the interface for specifying a passive connection
00488  * acceptance strategy for a SVC_HANDLER.
00489  *
00490  * This class provides a strategy that manages passive
00491  * connection acceptance of a client.
00492  */
00493 template <class SVC_HANDLER, ACE_PEER_ACCEPTOR_1>
00494 class ACE_Accept_Strategy
00495 {
00496 public:
00497 
00498   // Useful STL-style traits.
00499   typedef ACE_PEER_ACCEPTOR_ADDR                 addr_type;
00500   typedef ACE_PEER_ACCEPTOR                      acceptor_type;
00501   typedef SVC_HANDLER                            handler_type;
00502   typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;
00503 
00504   // = Initialization and termination methods.
00505   /// Default constructor.
00506   ACE_Accept_Strategy (ACE_Reactor *reactor = ACE_Reactor::instance ());
00507 
00508   /// Initialize the <peer_acceptor_> with <local_addr>.
00509   ACE_Accept_Strategy (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
00510                        int restart = 0,
00511                        ACE_Reactor *reactor = ACE_Reactor::instance ());
00512 
00513   /// Initialize the <peer_acceptor_> with <local_addr>, indicating
00514   /// whether to <reuse_addr> if it's already in use.
00515   virtual int open (const ACE_PEER_ACCEPTOR_ADDR &local_addr,
00516                     int reuse_addr = 0);
00517 
00518   /// Return the underlying ACE_HANDLE of the <peer_acceptor_>.
00519   virtual ACE_HANDLE get_handle (void) const;
00520 
00521   /// Return a reference to the <peer_acceptor_>.
00522   virtual ACE_PEER_ACCEPTOR &acceptor (void) const;
00523 
00524   virtual ~ACE_Accept_Strategy (void);
00525 
00526   // = Factory method.
00527   /// The default behavior delegates to the <accept> method of the
00528   /// PEER_ACCEPTOR.
00529   virtual int accept_svc_handler (SVC_HANDLER *);
00530 
00531   /// Dump the state of an object.
00532   void dump (void) const;
00533 
00534   /// Declare the dynamic allocation hooks.
00535   ACE_ALLOC_HOOK_DECLARE;
00536 
00537 protected:
00538   /// Factory that establishes connections passively.
00539   ACE_PEER_ACCEPTOR peer_acceptor_;
00540 
00541   /// Pointer to the reactor used by the Acceptor.
00542   ACE_Reactor *reactor_;
00543 
00544   /// Needed to reopen the socket if <accept> fails.
00545   int reuse_addr_;
00546 
00547   /// Needed to reopen the socket if <accept> fails.
00548   ACE_PEER_ACCEPTOR_ADDR peer_acceptor_addr_;
00549 };
00550 
00551 /**
00552  * @class ACE_Connect_Strategy
00553  *
00554  * @brief Defines the interface for specifying an active
00555  * connection establishment strategy for a SVC_HANDLER.
00556  *
00557  * This class provides a strategy that manages active
00558  * connection establishment to a server.
00559  */
00560 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1>
00561 class ACE_Connect_Strategy
00562 {
00563 public:
00564 
00565   // Useful STL-style traits.
00566   typedef ACE_PEER_CONNECTOR_ADDR                addr_type;
00567   typedef ACE_PEER_CONNECTOR                     connector_type;
00568   typedef SVC_HANDLER                            handler_type;
00569   typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;
00570 
00571   // = Initialization and termination methods.
00572   /// Default constructor.
00573   ACE_Connect_Strategy (void);
00574 
00575   /// Return a reference to the <peer_connector_>.
00576   virtual ACE_PEER_CONNECTOR &connector (void) const;
00577 
00578   virtual ~ACE_Connect_Strategy (void);
00579 
00580   // = Factory method.
00581   /// The default behavior delegates to the <connect> method of the
00582   /// <PEER_CONNECTOR::connect>.
00583   virtual int connect_svc_handler (SVC_HANDLER *&sh,
00584                                    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00585                                    ACE_Time_Value *timeout,
00586                                    const ACE_PEER_CONNECTOR_ADDR &local_addr,
00587                                    int reuse_addr,
00588                                    int flags,
00589                                    int perms);
00590 
00591   /**
00592    * The default behavior delegates to the <connect> method of the
00593    * <PEER_CONNECTOR::connect>.
00594    * Please check the documentation in Connector.h for more details.
00595    */
00596   virtual int connect_svc_handler (SVC_HANDLER *&sh,
00597                                    SVC_HANDLER *&sh_copy,
00598                                    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00599                                    ACE_Time_Value *timeout,
00600                                    const ACE_PEER_CONNECTOR_ADDR &local_addr,
00601                                    int reuse_addr,
00602                                    int flags,
00603                                    int perms);
00604 
00605   /// Dump the state of an object.
00606   void dump (void) const;
00607 
00608   /// Declare the dynamic allocation hooks.
00609   ACE_ALLOC_HOOK_DECLARE;
00610 
00611 protected:
00612   /// Factory that establishes connections actively.
00613   ACE_PEER_CONNECTOR connector_;
00614 };
00615 
00616 /**
00617  * @class ACE_Scheduling_Strategy
00618  *
00619  * @brief Defines the interface for specifying how to suspend and
00620  * resume a service .
00621  *
00622  * This class provides a strategy that allows arbitrarily
00623  * sophisticated service suspension and resumption.  The default
00624  * behavior is to do nothing...
00625  */
00626 template <class SVC_HANDLER>
00627 class ACE_Scheduling_Strategy
00628 {
00629 public:
00630 
00631   // Useful STL-style traits.
00632   typedef ACE_TYPENAME SVC_HANDLER::addr_type    addr_type;
00633   typedef SVC_HANDLER                            handler_type;
00634   typedef ACE_TYPENAME SVC_HANDLER::stream_type  stream_type;
00635 
00636   // = Initialization and termination methods.
00637 
00638   /// Constructor
00639   ACE_Scheduling_Strategy (SVC_HANDLER * = 0);
00640 
00641   /// Destructor
00642   virtual ~ACE_Scheduling_Strategy (void);
00643 
00644   // = Scheduling methods
00645 
00646   /// Suspend hook.
00647   virtual int suspend (void);
00648 
00649   /// Resume hook.
00650   virtual int resume (void);
00651 
00652   /// Dump the state of the object.
00653   virtual void dump (void) const;
00654 };
00655 
00656 /**
00657  * @class ACE_Schedule_All_Reactive_Strategy
00658  *
00659  * @brief Defines the interface for specifying how to suspend and
00660  * resume a single-threaded reactive service .
00661  *
00662  * This class provides a strategy that suspends and resumes all
00663  * the Event_Handlers in a Reactor in one fell swoop.
00664  */
00665 template <class SVC_HANDLER>
00666 class ACE_Schedule_All_Reactive_Strategy
00667   : public ACE_Scheduling_Strategy<SVC_HANDLER>
00668 {
00669 public:
00670 
00671   // Useful STL-style traits.
00672   typedef ACE_Scheduling_Strategy<SVC_HANDLER> base_type;
00673 
00674   // = Initialization and termination methods.
00675   /// Constructor
00676   ACE_Schedule_All_Reactive_Strategy (SVC_HANDLER * = 0);
00677 
00678   // = Scheduling methods
00679 
00680   /// Suspend hook.
00681   virtual int suspend (void);
00682 
00683   /// Resume hook.
00684   virtual int resume (void);
00685 
00686   /// Dump the state of the object.
00687   virtual void dump (void) const;
00688 
00689 protected:
00690 
00691   /// Reactor
00692   ACE_Reactor *reactor_;
00693 };
00694 
00695 /**
00696  * @class ACE_Schedule_All_Threaded_Strategy
00697  *
00698  * @brief Defines the interface for specifying how to suspend and
00699  * resume a multithreaded service .
00700  *
00701  * This class provides a strategy that suspends and resumes all
00702  * the Event_Handlers controlled by a Thread_Manager in one fell swoop.
00703  */
00704 template <class SVC_HANDLER>
00705 class ACE_Schedule_All_Threaded_Strategy
00706   : public ACE_Scheduling_Strategy<SVC_HANDLER>
00707 {
00708 public:
00709 
00710   // Useful STL-style traits.
00711   typedef ACE_Scheduling_Strategy<SVC_HANDLER> base_type;
00712 
00713   // = Initialization and termination methods.
00714   /// Constructor
00715   ACE_Schedule_All_Threaded_Strategy (SVC_HANDLER * = 0);
00716 
00717   // = Scheduling methods
00718 
00719   /// Suspend hook.
00720   virtual int suspend (void);
00721 
00722   /// Resume hook.
00723   virtual int resume (void);
00724 
00725   /// Dump the state of the object.
00726   virtual void dump (void) const;
00727 
00728 protected:
00729 
00730   /// Thread Manager
00731   ACE_Thread_Manager *thr_mgr_;
00732 };
00733 
00734 /**
00735  * @class ACE_NOOP_Creation_Strategy
00736  *
00737  * @brief Implements a no-op creation strategy in order to defer
00738  * decisions regarding creation to some later point in time, such
00739  * as in connect or accept strategy.
00740  *
00741  * An example of the use of this is in the
00742  * <ACE_Cached_Connect_Strategy>, which only returns a single
00743  * connection for a given endpoint.
00744  */
00745 template <class SVC_HANDLER>
00746 class ACE_NOOP_Creation_Strategy : public ACE_Creation_Strategy<SVC_HANDLER>
00747 {
00748 public:
00749 
00750   // Useful STL-style traits.
00751   typedef ACE_Creation_Strategy<SVC_HANDLER> base_type;
00752 
00753   /// This is a no-op.
00754   virtual int make_svc_handler (SVC_HANDLER *&);
00755 };
00756 
00757 /**
00758  * @class ACE_NOOP_Concurrency_Strategy
00759  *
00760  * @brief Implements a no-op activation strategy in order to avoid
00761  * calling open on a svc_handler multiple times.
00762  *
00763  * An example of the use of this is in the
00764  * <ACE_Cached_Connect_Strategy>, which reuses svc_handlers.
00765  * Therefore we don't want to call open on the recycled
00766  * svc_handler more than once.
00767  */
00768 template <class SVC_HANDLER>
00769 class ACE_NOOP_Concurrency_Strategy
00770   : public ACE_Concurrency_Strategy<SVC_HANDLER>
00771 {
00772 public:
00773 
00774   // Useful STL-style traits.
00775   typedef ACE_Concurrency_Strategy<SVC_HANDLER> base_type;
00776 
00777   // = Factory method.
00778   /// This is a no-op.
00779   virtual int activate_svc_handler (SVC_HANDLER *svc_handler,
00780                                     void *arg = 0);
00781 };
00782 
00783 template <class T>
00784 class ACE_Refcounted_Hash_Recyclable :  public ACE_Refcountable,
00785                                         public ACE_Hashable,
00786                                         public ACE_Recyclable
00787 {
00788 public:
00789   /// Default constructor.
00790   ACE_Refcounted_Hash_Recyclable (void);
00791 
00792   /// Constructor.
00793   ACE_Refcounted_Hash_Recyclable (const T &t,
00794                                   int refcount = 0,
00795                                   ACE_Recyclable_State state = ACE_RECYCLABLE_UNKNOWN);
00796 
00797   /// Destructor
00798   virtual ~ACE_Refcounted_Hash_Recyclable (void);
00799 
00800   /// Compares two instances.
00801   int operator== (const ACE_Refcounted_Hash_Recyclable<T> &rhs) const;
00802   int operator!= (const ACE_Refcounted_Hash_Recyclable<T> &rhs) const;
00803 
00804   T &subject ();
00805 
00806 protected:
00807   /// Computes and returns hash value.
00808   u_long hash_i (void) const;
00809 
00810   T t_;
00811 };
00812 
00813 /**
00814  * @class ACE_Cached_Connect_Strategy
00815  *
00816  * @brief A connection strategy which caches connections to peers
00817  * (represented by <SVC_HANDLER> instances), thereby allowing
00818  * subsequent re-use of unused, but available, connections.
00819  *
00820  * <ACE_Cached_Connect_Strategy> is intended to be used as a
00821  * plug-in connection strategy for <ACE_Strategy_Connector>.
00822  * It's added value is re-use of established connections.
00823  */
00824 template <class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class MUTEX>
00825 class ACE_Cached_Connect_Strategy
00826   : public ACE_Connection_Recycling_Strategy,
00827     public ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
00828 {
00829 public:
00830 
00831   // Useful STL-style traits.
00832   typedef ACE_Creation_Strategy<SVC_HANDLER>
00833           creation_strategy_type;
00834   typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
00835           connect_strategy_type;
00836   typedef ACE_Concurrency_Strategy<SVC_HANDLER>
00837           concurrency_strategy_type;
00838   typedef ACE_Recycling_Strategy<SVC_HANDLER> recycling_strategy_type;
00839 
00840   // = Define some useful (old style) traits.
00841   typedef ACE_Creation_Strategy<SVC_HANDLER>
00842           CREATION_STRATEGY;
00843   typedef ACE_Concurrency_Strategy<SVC_HANDLER>
00844           CONCURRENCY_STRATEGY;
00845   typedef ACE_Recycling_Strategy<SVC_HANDLER>
00846           RECYCLING_STRATEGY;
00847 
00848   // = Super class
00849   typedef ACE_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2>
00850           CONNECT_STRATEGY;
00851 
00852 
00853   typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX> SELF;
00854 
00855   /// Constructor
00856   ACE_Cached_Connect_Strategy (ACE_Creation_Strategy<SVC_HANDLER> *cre_s = 0,
00857                                ACE_Concurrency_Strategy<SVC_HANDLER> *con_s = 0,
00858                                ACE_Recycling_Strategy<SVC_HANDLER> *rec_s = 0,
00859                                MUTEX *mutex = 0,
00860                                int delete_mutex = 0);
00861 
00862   /// Destructor
00863   virtual ~ACE_Cached_Connect_Strategy (void);
00864 
00865   /// This methods allow you to change the strategies used by the
00866   /// cached connector.
00867   virtual int open (ACE_Creation_Strategy<SVC_HANDLER> *cre_s,
00868                     ACE_Concurrency_Strategy<SVC_HANDLER> *con_s,
00869                     ACE_Recycling_Strategy<SVC_HANDLER> *rec_s);
00870 
00871   /// Template method for making a new <svc_handler>
00872   virtual int make_svc_handler (SVC_HANDLER *&sh);
00873 
00874   /// Template method for activating a new <svc_handler>
00875   virtual int activate_svc_handler (SVC_HANDLER *svc_handler);
00876 
00877   /// Template method for setting the recycler information of the
00878   /// svc_handler.
00879   virtual int assign_recycler (SVC_HANDLER *svc_handler,
00880                                ACE_Connection_Recycling_Strategy *recycler,
00881                                const void *recycling_act);
00882 
00883   /// Template method for preparing the svc_handler for recycling.
00884   virtual int prepare_for_recycling (SVC_HANDLER *svc_handler);
00885 
00886   /**
00887    * Checks to see if there is already a <SVC_HANDLER> in the cache
00888    * connected to the <remote_addr>.  If so, we return this pointer.
00889    * Otherwise we establish the connection, put it into the cache, and
00890    * return the <SVC_HANDLER> pointer.  <[NOTE]>: the <{reuse_addr}>
00891    * argument does NOT control re-use of addresses in the cache.
00892    * Rather, if the underlying protocol requires a "dead time" prior
00893    * to re-use of its addresses (TCP is a classic example of this),
00894    * <{and}> the protocol provides a means by which to defeat the dead
00895    * time, setting this argument to non-zero will defeat the dead-time
00896    * requirement.  <{Dev. Note: We might want to consider enhancing
00897    * the interface at some point so that this also controls re-use of
00898    * the cache.}>
00899    */
00900   virtual int connect_svc_handler (SVC_HANDLER *&sh,
00901                                    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00902                                    ACE_Time_Value *timeout,
00903                                    const ACE_PEER_CONNECTOR_ADDR &local_addr,
00904                                    int reuse_addr,
00905                                    int flags,
00906                                    int perms);
00907   virtual int connect_svc_handler (SVC_HANDLER *&sh,
00908                                    SVC_HANDLER *&sh_copy,
00909                                    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00910                                    ACE_Time_Value *timeout,
00911                                    const ACE_PEER_CONNECTOR_ADDR &local_addr,
00912                                    int reuse_addr,
00913                                    int flags,
00914                                    int perms);
00915 
00916   /// Remove from cache.
00917   virtual int purge (const void *recycling_act);
00918 
00919   /// Add to cache.
00920   virtual int cache (const void *recycling_act);
00921 
00922   /// Get/Set <recycle_state>.
00923   virtual int recycle_state (const void *recycling_act,
00924                              ACE_Recyclable_State new_state);
00925   virtual ACE_Recyclable_State recycle_state (const void *recycling_act) const;
00926 
00927   /// Mark as closed.
00928   virtual int mark_as_closed (const void *recycling_act);
00929 
00930   /**
00931    * Mark as closed (non-locking version). This method needs to be public
00932    * as it is used in the cleanup of handlers where teh locked version causes
00933    * a deadlock.
00934    */
00935   virtual int mark_as_closed_i (const void *recycling_act);
00936 
00937   /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
00938   virtual int cleanup_hint (const void *recycling_act,
00939                             void **act_holder = 0);
00940 
00941   // = Typedefs for managing the map
00942   typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>
00943           REFCOUNTED_HASH_RECYCLABLE_ADDRESS;
00944   typedef ACE_Hash_Map_Manager<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, SVC_HANDLER *, ACE_Null_Mutex>
00945           CONNECTION_MAP;
00946   typedef ACE_Hash_Map_Iterator<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, SVC_HANDLER *, ACE_Null_Mutex>
00947           CONNECTION_MAP_ITERATOR;
00948   typedef ACE_Hash_Map_Entry<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, SVC_HANDLER *>
00949           CONNECTION_MAP_ENTRY;
00950 
00951   typedef ACE_Reverse_Lock<MUTEX> REVERSE_MUTEX;
00952 
00953   // = Strategy accessors
00954   virtual ACE_Creation_Strategy<SVC_HANDLER> *creation_strategy (void) const;
00955   virtual ACE_Recycling_Strategy<SVC_HANDLER> *recycling_strategy (void) const;
00956   virtual ACE_Concurrency_Strategy<SVC_HANDLER> *concurrency_strategy (void) const;
00957 
00958 protected:
00959 
00960   /// Creates a new connection.
00961   virtual int new_connection (SVC_HANDLER *&sh,
00962                               const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00963                               ACE_Time_Value *timeout,
00964                               const ACE_PEER_CONNECTOR_ADDR &local_addr,
00965                               int reuse_addr,
00966                               int flags,
00967                               int perms);
00968 
00969   /// Find an idle handle.
00970   int find (ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> &search_addr,
00971             ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, SVC_HANDLER *> *&entry);
00972 
00973   /// Remove from cache (non-locking version).
00974   virtual int purge_i (const void *recycling_act);
00975 
00976   /// Add to cache (non-locking version).
00977   virtual int cache_i (const void *recycling_act);
00978 
00979   /// Set <recycle_state> (non-locking version).
00980   virtual int recycle_state_i (const void *recycling_act,
00981                                ACE_Recyclable_State new_state);
00982 
00983   /// Get <recycle_state> (non-locking version).
00984   virtual ACE_Recyclable_State recycle_state_i (const void *recycling_act) const;
00985 
00986   /// Cleanup hint and reset <*act_holder> to zero if <act_holder != 0>.
00987   virtual int cleanup_hint_i (const void *recycling_act,
00988                               void **act_holder);
00989 
00990   // = Helpers
00991   int check_hint_i (SVC_HANDLER *&sh,
00992                     const ACE_PEER_CONNECTOR_ADDR &remote_addr,
00993                     ACE_Time_Value *timeout,
00994                     const ACE_PEER_CONNECTOR_ADDR &local_addr,
00995                     int reuse_addr,
00996                     int flags,
00997                     int perms,
00998                     ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, SVC_HANDLER *> *&entry,
00999                     int &found);
01000 
01001   int find_or_create_svc_handler_i (SVC_HANDLER *&sh,
01002                                     const ACE_PEER_CONNECTOR_ADDR &remote_addr,
01003                                     ACE_Time_Value *timeout,
01004                                     const ACE_PEER_CONNECTOR_ADDR &local_addr,
01005                                     int reuse_addr,
01006                                     int flags,
01007                                     int perms,
01008                                     ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, SVC_HANDLER *> *&entry,
01009                                     int &found);
01010 
01011   virtual int connect_svc_handler_i (SVC_HANDLER *&sh,
01012                                      const ACE_PEER_CONNECTOR_ADDR &remote_addr,
01013                                      ACE_Time_Value *timeout,
01014                                      const ACE_PEER_CONNECTOR_ADDR &local_addr,
01015                                      int reuse_addr,
01016                                      int flags,
01017                                      int perms,
01018                                      int &found);
01019 
01020   /// Table that maintains the cache of connected <SVC_HANDLER>s.
01021   CONNECTION_MAP connection_map_;
01022 
01023   /// Mutual exclusion for this object.
01024   MUTEX *lock_;
01025 
01026   /// Mutual exclusion for this object.
01027   int delete_lock_;
01028 
01029   /// Reverse lock.
01030   REVERSE_MUTEX *reverse_lock_;
01031 
01032   // = Strategy objects.
01033 
01034   /// Creation strategy for an <Connector>.
01035   CREATION_STRATEGY *creation_strategy_;
01036 
01037   /// 1 if <Connector> created the creation strategy and thus should
01038   /// delete it, else 0.
01039   int delete_creation_strategy_;
01040 
01041   /// Concurrency strategy for an <Connector>.
01042   CONCURRENCY_STRATEGY *concurrency_strategy_;
01043 
01044   /// 1 if <Connector> created the concurrency strategy and thus should
01045   /// delete it, else 0.
01046   int delete_concurrency_strategy_;
01047 
01048   /// Recycling strategy for an <Connector>.
01049   RECYCLING_STRATEGY *recycling_strategy_;
01050 
01051   /// 1 if <Connector> created the recycling strategy and thus should
01052   /// delete it, else 0.
01053   int delete_recycling_strategy_;
01054 };
01055 
01056 #if !defined (ACE_LACKS_INLINE_FUNCTIONS)
01057 #include "ace/Strategies_T.i"
01058 #endif /* ACE_LACKS_INLINE_FUNCTIONS */
01059 
01060 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
01061 #include "ace/Strategies_T.cpp"
01062 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
01063 
01064 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
01065 #pragma implementation ("Strategies_T.cpp")
01066 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
01067 
01068 #include "ace/post.h"
01069 
01070 
01071 #endif /* ACE_STRATEGIES_T_H */

Generated on Mon Jun 16 11:21:28 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002