Page Speed Optimization Libraries  1.2.24.1
net/instaweb/apache/serf_url_async_fetcher.h
Go to the documentation of this file.
00001 // Copyright 2010 Google Inc.
00017 
00018 #ifndef NET_INSTAWEB_APACHE_SERF_URL_ASYNC_FETCHER_H_
00019 #define NET_INSTAWEB_APACHE_SERF_URL_ASYNC_FETCHER_H_
00020 
00021 #include <vector>
00022 #include "net/instaweb/util/public/basictypes.h"
00023 #include "net/instaweb/util/public/pool.h"
00024 #include "net/instaweb/util/public/string.h"
00025 #include "net/instaweb/util/public/thread_system.h"
00026 #include "net/instaweb/http/public/url_pollable_async_fetcher.h"
00027 
00028 struct apr_pool_t;
00029 struct serf_context_t;
00030 
00031 namespace net_instaweb {
00032 
00033 class AsyncFetch;
00034 class MessageHandler;
00035 class Statistics;
00036 class SerfFetch;
00037 class SerfThreadedFetcher;
00038 class Timer;
00039 class Variable;
00040 
00041 struct SerfStats {
00042   static const char kSerfFetchRequestCount[];
00043   static const char kSerfFetchByteCount[];
00044   static const char kSerfFetchTimeDurationMs[];
00045   static const char kSerfFetchCancelCount[];
00046   static const char kSerfFetchActiveCount[];
00047   static const char kSerfFetchTimeoutCount[];
00048   static const char kSerfFetchFailureCount[];
00049 };
00050 
00057 class SerfUrlAsyncFetcher : public UrlPollableAsyncFetcher {
00058  public:
00059   SerfUrlAsyncFetcher(const char* proxy, apr_pool_t* pool,
00060                       ThreadSystem* thread_system,
00061                       Statistics* statistics, Timer* timer, int64 timeout_ms,
00062                       MessageHandler* handler);
00063   SerfUrlAsyncFetcher(SerfUrlAsyncFetcher* parent, const char* proxy);
00064   virtual ~SerfUrlAsyncFetcher();
00065 
00066   static void InitStats(Statistics* statistics);
00067 
00070   virtual void ShutDown();
00071 
00072   virtual bool SupportsHttps() const { return false; }
00073 
00074   virtual void Fetch(const GoogleString& url,
00075                      MessageHandler* message_handler,
00076                      AsyncFetch* callback);
00077 
00078   virtual int Poll(int64 max_wait_ms);
00079 
00080   enum WaitChoice {
00081     kThreadedOnly,
00082     kMainlineOnly,
00083     kThreadedAndMainline
00084   };
00085 
00086   bool WaitForActiveFetches(int64 max_milliseconds,
00087                             MessageHandler* message_handler,
00088                             WaitChoice wait_choice);
00089 
00092   void FetchComplete(SerfFetch* fetch);
00093   apr_pool_t* pool() const { return pool_; }
00094   serf_context_t* serf_context() const { return serf_context_; }
00095 
00096   void PrintActiveFetches(MessageHandler* handler) const;
00097   virtual int64 timeout_ms() { return timeout_ms_; }
00098   ThreadSystem* thread_system() { return thread_system_; }
00099 
00106   void set_force_threaded(bool x) { force_threaded_ = x; }
00107 
00110   void set_list_outstanding_urls_on_error(bool x);
00111 
00114   bool track_original_content_length() const {
00115     return track_original_content_length_;
00116   }
00117   void set_track_original_content_length(bool x);
00118 
00119  protected:
00120   typedef Pool<SerfFetch> SerfFetchPool;
00121 
00122   void Init(apr_pool_t* parent_pool, const char* proxy);
00123   bool SetupProxy(const char* proxy);
00124 
00129   bool StartFetch(SerfFetch* fetch);
00130 
00135   virtual bool AnyPendingFetches();
00138   int ApproximateNumActiveFetches();
00139 
00140   void CancelActiveFetches();
00141   void CancelActiveFetchesMutexHeld();
00142   bool WaitForActiveFetchesHelper(int64 max_ms,
00143                                   MessageHandler* message_handler);
00144 
00148   void CleanupFetchesWithErrors();
00149 
00151   bool shutdown() const { return shutdown_; }
00152   void set_shutdown(bool s) { shutdown_ = s; }
00153 
00154   apr_pool_t* pool_;
00155   ThreadSystem* thread_system_;
00156   Timer* timer_;
00157 
00159   ThreadSystem::CondvarCapableMutex* mutex_;
00160   serf_context_t* serf_context_;
00161   SerfFetchPool active_fetches_;
00162 
00163   typedef std::vector<SerfFetch*> FetchVector;
00164   SerfFetchPool completed_fetches_;
00165   SerfThreadedFetcher* threaded_fetcher_;
00166 
00169   Variable* active_count_;
00170 
00171  private:
00172   friend class SerfFetch; 
00173 
00174   Variable* request_count_;
00175   Variable* byte_count_;
00176   Variable* time_duration_ms_;
00177   Variable* cancel_count_;
00178   Variable* timeout_count_;
00179   Variable* failure_count_;
00180   const int64 timeout_ms_;
00181   bool force_threaded_;
00182   bool shutdown_;
00183   bool list_outstanding_urls_on_error_;
00184   bool track_original_content_length_;
00185   MessageHandler* message_handler_;
00186 
00187   DISALLOW_COPY_AND_ASSIGN(SerfUrlAsyncFetcher);
00188 };
00189 
00190 }  
00191 
00192 #endif  ///< NET_INSTAWEB_APACHE_SERF_URL_ASYNC_FETCHER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines