jigdo API
Last update by Admin on 2010-05-23
net/download.hh
Go to the documentation of this file.00001 /* $Id: download.hh,v 1.8 2003/08/17 15:37:07 atterer Exp $ -*- C++ -*- 00002 __ _ 00003 |_) /| Copyright (C) 2001-2003 | richard@ 00004 | \/¯| Richard Atterer | atterer.net 00005 ¯ '` ¯ 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License, version 2. See 00008 the file COPYING for details. 00009 00010 */ 00019 #ifndef DOWNLOAD_HH 00020 #define DOWNLOAD_HH 00021 00022 #include <config.h> 00023 00024 #include <glib.h> 00025 #include <string> 00026 00027 // #include <curl.h> 00028 typedef void CURL; 00029 typedef void CURLSH; 00030 struct curl_slist; 00031 00032 #ifdef ERROR 00033 # undef ERROR /* Windows... */ 00034 #endif 00035 //______________________________________________________________________ 00036 00044 class Download { 00045 public: 00047 class Output; 00048 00050 static void init(); 00052 static void cleanup(); 00053 00054 Download(const string& uri, Output* o /*= 0*/); 00055 ~Download(); 00056 00061 inline void setResumeOffset(uint64 offset); 00063 inline uint64 resumeOffset() const; 00064 00069 // void setPragmaNoCache(bool pragmaNoCache); 00070 00076 void run(); 00077 00078 inline const string& uri() const; 00079 00083 /*inline*/ void pause(); 00085 void cont(); 00088 inline bool paused() const; 00090 //inline bool pausedSoon() const; 00092 inline bool failed() const; 00095 inline bool succeeded() const; 00097 inline bool interrupted() const; 00098 00108 void stop(); 00109 00111 inline Output* output() const; 00113 inline void setOutput(Output* o); 00114 00115 private: 00116 enum State { 00117 CREATED, // but not run() yet 00118 RUNNING, // downloading 00119 //PAUSE_SCHEDULED, // will switch to pause next time data arrives 00120 PAUSED, // socket no longer being polled, we'll get no more data 00121 INTERRUPTED, // like ERROR, but will try resuming the download 00122 ERROR, SUCCEEDED 00123 }; 00124 00125 // Called by libcurl when data is received from the net 00126 static size_t curlWriter(void* data, size_t size, size_t nmemb, 00127 void* selfPtr); 00128 // Called by glibcurl after curl_multi_perform() 00129 static void glibcurlCallback(void*); 00130 00131 // Unregister request from glibwww event loop 00132 // void pauseNow(); 00133 // Call output->error() with appropriate string taken from request object 00134 void generateError(State newState = ERROR, int cc = -1); 00135 /* A callback function which is registered if the download needs to be 00136 stopped. It'll get executed the next time the main glib loop is 00137 executed. This delayed execution is necessary because libwww doesn't 00138 like Download::stop() being called from download_newData(). */ 00139 static gboolean stopLater_callback(gpointer data); 00140 00141 // static CURLSH* shHandle; // Handle of curl_shared object 00142 CURL* handle; // Handle of curl_easy object 00143 char* curlError; // Curl error string buffer 00144 00145 string uriVal; // Careful: Includes a trailing null byte! 00146 string uriValWithoutNull; 00147 uint64 resumeOffsetVal; 00148 uint64 currentSize; 00149 Output* outputVal; // Usually points to a Job::SingleUrl 00150 State state; 00151 00152 unsigned stopLaterId; // glib idle function id, or 0 if none 00153 00154 static string userAgent; 00155 static struct curl_slist* extraHeaders; 00156 bool insideNewData; 00157 }; 00158 //______________________________________________________________________ 00159 00162 class Download::Output { 00163 public: 00164 00166 virtual ~Output() { } 00167 00168 /* Called by the Download when it is deleted. If the Output object 00169 considers itself owned by its Download, it can delete itself. */ 00170 //virtual void download_deleted() = 0; 00171 00176 virtual void download_dataSize(uint64 n) = 0; 00177 00182 virtual void download_data(const byte* data, unsigned size, 00183 uint64 currentSize) = 0; 00184 00190 virtual void download_succeeded() = 0; 00191 00195 virtual void download_failed(string* message) = 0; 00196 00200 virtual void download_message(string* message) = 0; 00201 00202 }; 00203 //______________________________________________________________________ 00204 00205 const string& Download::uri() const { 00206 return uriValWithoutNull; 00207 } 00208 00209 Download::Output* Download::output() const { return outputVal; } 00210 void Download::setOutput(Download::Output* o) { outputVal = o; } 00211 00212 // void Download::pause() { 00213 // if (state == RUNNING) state = PAUSE_SCHEDULED; 00214 // } 00215 bool Download::paused() const { return state == PAUSED; } 00216 // bool Download::pausedSoon() const { 00217 // return state == PAUSED || state == PAUSE_SCHEDULED; } 00218 bool Download::failed() const { return state == ERROR; } 00219 bool Download::succeeded() const { return state == SUCCEEDED; } 00220 bool Download::interrupted() const { return state == INTERRUPTED; } 00221 00222 uint64 Download::resumeOffset() const { return resumeOffsetVal; } 00223 void Download::setResumeOffset(uint64 resumeOffset) { 00224 resumeOffsetVal = resumeOffset; 00225 } 00226 00227 #endif
Generated on Tue Sep 23 14:27:41 2008 for jigdo by
