Strategies_T.h

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

Generated on Thu Nov 9 09:42:05 2006 for ACE by doxygen 1.3.6