Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

downloads.h

Go to the documentation of this file.
00001 /*
00002  * $Id: downloads.h,v 1.23 2005/12/18 06:09:10 cbiere Exp $
00003  *
00004  * Copyright (c) 2001-2003, Raphael Manfredi, Richard Eckart
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00026 #ifndef _if_core_downloads_h_
00027 #define _if_core_downloads_h_
00028 
00029 #include "lib/tm.h"             /* For tm_t */
00030 #include "lib/event.h"          /* For frequency_t */
00031 
00032 #include "if/core/search.h"     /* For gnet_host_vec_t */
00033 
00034 /***
00035  *** Sources (traditionally called "downloads")
00036  ***/
00037 
00038 typedef guint32 gnet_src_t;
00039 
00040 typedef void (*src_listener_t) (gnet_src_t);
00041 typedef enum {
00042     EV_SRC_ADDED = 0,
00043     EV_SRC_REMOVED,
00044     EV_SRC_INFO_CHANGED,
00045     EV_SRC_STATUS_CHANGED,
00046     EV_SRC_RANGES_CHANGED,
00047 
00048     EV_SRC_EVENTS /* Number of events in this domain */
00049 } gnet_src_ev_t;
00050 
00051 #define URN_INDEX   0xffffffff      
00053 /*
00054  * We keep a list of all the downloads queued per GUID+IP:port (host).  Indeed
00055  * some broken clients (e.g. Morpheus) share the same GUID, so we cannot
00056  * fully discriminate on the GUID alone.  So GUID+IP:port forms the "key",
00057  * the `dl_key' structure.
00058  *
00059  * Inside the `dl_server', we keep track all `download' structures and
00060  * other server-related information, which are shared by all downloads
00061  * from this host..
00062  *
00063  * Within a single server, a download can be in either runnning, waiting
00064  * or stopped.  An array of lists is kept, and since the download can be
00065  * in only one of them, it also keeps track of the proper list index.
00066  */
00067 
00068 enum dl_list {
00069     DL_LIST_INVALID = -1,
00070     DL_LIST_RUNNING = 0,
00071     DL_LIST_WAITING = 1,
00072     DL_LIST_STOPPED = 2,
00073     DL_LIST_SZ      = 3
00074 };
00075 
00076 struct vernum {
00077     guint major;
00078     guint minor;
00079 };
00080 
00081 struct dl_key {
00082     gchar *guid;                
00083     host_addr_t addr;           
00084     guint16 port;               
00085 };
00086 
00087 enum dl_server_magic { DL_SERVER_MAGIC = 0x5e45e4ffU };
00088 
00089 struct dl_server {
00090     enum dl_server_magic magic; 
00091     gint refcnt;             
00092     struct dl_key *key;      
00093     GList *list[DL_LIST_SZ]; 
00094     guint count[DL_LIST_SZ]; 
00095     const gchar *vendor;     
00096     const gchar *hostname;   
00097     gint country;            
00098     time_t retry_after;      
00099     time_t dns_lookup;      
00100     struct vernum parq_version; 
00101     guint32 attrs;
00102     GSList *proxies;        
00103     time_t proxies_stamp;   
00104 };
00105 
00106 #define dl_server_valid(x)  ((x) != NULL && (x)->magic == DL_SERVER_MAGIC)
00107 
00112 typedef enum {
00113     GTA_DL_QUEUED           = 1,  
00114     GTA_DL_CONNECTING       = 2,  
00115     GTA_DL_PUSH_SENT        = 3,  
00116     GTA_DL_FALLBACK         = 4,  
00117     GTA_DL_REQ_SENT         = 5,  
00118     GTA_DL_HEADERS          = 6,  
00119     GTA_DL_RECEIVING        = 7,  
00120     GTA_DL_COMPLETED        = 8,  
00121     GTA_DL_ERROR            = 9,  
00122     GTA_DL_ABORTED          = 10, 
00123     GTA_DL_TIMEOUT_WAIT     = 11, 
00124     GTA_DL_REMOVED          = 12, 
00125     GTA_DL_VERIFY_WAIT      = 13, 
00126     GTA_DL_VERIFYING        = 14, 
00127     GTA_DL_VERIFIED         = 15, 
00128     GTA_DL_MOVE_WAIT        = 16, 
00129     GTA_DL_MOVING           = 17, 
00130     GTA_DL_DONE             = 18, 
00131     GTA_DL_SINKING          = 19, 
00132     GTA_DL_ACTIVE_QUEUED    = 20, 
00133     GTA_DL_PASSIVE_QUEUED   = 21, 
00134     GTA_DL_REQ_SENDING      = 22  
00135 } download_status_t;
00136 
00137 typedef struct download download_t;
00138 
00139 struct bio_source;
00140 struct http_buffer;
00141 
00142 enum dl_bufmode {
00143     DL_BUF_READING = 0,
00144     DL_BUF_WRITING,
00145 };
00146 
00152 struct dl_buffers {
00153     enum dl_bufmode mode;       
00154     gint count;                 
00155     gchar **buffers;            
00156     struct iovec *iov;          
00157     struct iovec *iov_cur;      
00158     gint iovcnt;                
00159     size_t size;                
00160     size_t amount;              
00161     size_t held;                
00162 };
00163 
00164 struct download {
00165     gnet_src_t src_handle;      
00167     gchar error_str[256];       
00168     download_status_t status;   
00169     gpointer io_opaque;         
00171     struct bio_source *bio;     
00173     struct dl_server *server;   
00174     enum dl_list list_idx;      
00176     struct dl_file_info *file_info;
00177     guint32 record_index;       
00178     gchar *file_name;           
00179     gchar *escaped_name;        
00180     filesize_t file_size;       
00182     filesize_t size;            
00183     filesize_t skip;            
00184     filesize_t pos;             
00185     filesize_t range_end;       
00187     struct gnutella_socket *socket;
00188     gint file_desc;             
00189     guint32 overlap_size;       
00190     struct http_buffer *req;    
00191     struct dl_buffers *buffers; 
00193     time_t start_date;          
00194     time_t last_update;         
00195     time_t last_gui_update;     
00196     time_t record_stamp;        
00197     time_t retry_after;         
00198     tm_t header_sent;           
00200     guint32 retries;
00201     guint32 timeout_delay;
00202     guint32 served_reqs;        
00204     const gchar *remove_msg;
00205 
00206     gchar *sha1;                
00207     gchar *uri;                 
00209     guint32 last_dmesh;         
00211     GSList *ranges;             
00212     filesize_t ranges_size;     
00213     filesize_t sinkleft;        
00215     guint32 flags;
00216     guint32 cflags;
00217 
00218     gboolean keep_alive;        
00219     gboolean visible;           
00220     gboolean push;              
00221     gboolean always_push;       
00222     gboolean got_giv;           
00223     gboolean unavailable;       
00225     struct cproxy *cproxy;      
00226     gpointer queue_status;      
00227     struct browse_ctx *browse;  
00228 };
00229 
00230 /*
00231  * Download flags.
00232  */
00233 
00234 #define DL_F_URIRES         0x00000001  
00235 #define DL_F_PUSH_IGN       0x00000002  
00236 #define DL_F_OVERLAPPED     0x00000004  
00237 #define DL_F_REPLIED        0x00000008  
00238 #define DL_F_CHUNK_CHOSEN   0x00000010  
00239 #define DL_F_SHRUNK_REPLY   0x00000020  
00240 #define DL_F_SUNK_DATA      0x00000040  
00241 #define DL_F_ACTIVE_QUEUED  0x00000080  
00242 #define DL_F_PASSIVE_QUEUED 0x00000100  
00243 #define DL_F_DNS_LOOKUP     0x00000200  
00244 #define DL_F_BROWSE         0x00000400  
00245 #define DL_F_TRANSIENT      0x20000000  
00246 #define DL_F_SUSPENDED      0x40000000  
00247 #define DL_F_MARK           0x80000000  
00249 /*
00250  * Server attributes.
00251  */
00252 
00253 #define DLS_A_UNUSED_1      0x00000001  
00254 #define DLS_A_PUSH_IGN      0x00000002  
00255 #define DLS_A_UNUSED_2      0x00000004  
00256 #define DLS_A_NO_HTTP_1_1   0x00000008  
00257 #define DLS_A_MINIMAL_HTTP  0x00000010  
00258 #define DLS_A_BANNING       0x00000020  
00259 #define DLS_A_FAKE_G2       0x00000040  
00260 #define DLS_A_DNS_LOOKUP    0x00000080  
00261 #define DLS_A_REMOVED       0x80000000  
00263 /*
00264  * Access macros.
00265  */
00266 
00267 #define download_guid(d)        ((d)->server->key->guid)
00268 #define download_addr(d)        ((d)->server->key->addr)
00269 #define download_port(d)        ((d)->server->key->port)
00270 #define download_vendor(d)      ((d)->server->vendor)
00271 #define download_country(d)     ((d)->server->country)
00272 #define download_hostname(d)    ((d)->server->hostname)
00273 
00274 #define download_vendor_str(d) \
00275     ((d)->server->vendor ? (d)->server->vendor : "")
00276 
00277 #define download_path(d)        ((d)->file_info->path)
00278 #define download_outname(d)     ((d)->file_info->file_name)
00279 #define download_filesize(d)    ((d)->file_info->size)
00280 #define download_filedone(d)    ((d)->file_info->done)
00281 
00282 /*
00283  * State inspection macros.
00284  */
00285 
00286 #define DOWNLOAD_IS_QUEUED(d)  ((d)->status == GTA_DL_QUEUED)
00287 
00288 #define DOWNLOAD_IS_VERIFYING(d)         \
00289     (  (d)->status == GTA_DL_VERIFY_WAIT \
00290     || (d)->status == GTA_DL_VERIFYING   \
00291     || (d)->status == GTA_DL_VERIFIED    )
00292 
00293 #define DOWNLOAD_IS_MOVING(d)           \
00294     (  (d)->status == GTA_DL_MOVE_WAIT  \
00295     || (d)->status == GTA_DL_MOVING     )
00296 
00297 #define DOWNLOAD_IS_STOPPED(d)          \
00298     (  (d)->status == GTA_DL_ABORTED    \
00299     || (d)->status == GTA_DL_ERROR      \
00300     || (d)->status == GTA_DL_COMPLETED  \
00301     || DOWNLOAD_IS_VERIFYING(d)         \
00302     || DOWNLOAD_IS_MOVING(d)            \
00303     || (d)->status == GTA_DL_DONE       )
00304 
00305 #define DOWNLOAD_IS_ACTIVE(d)           \
00306     (  (d)->status == GTA_DL_RECEIVING  )
00307 
00308 #define DOWNLOAD_IS_WAITING(d)          \
00309     (  (d)->status == GTA_DL_TIMEOUT_WAIT)
00310 
00311 #define DOWNLOAD_IS_ESTABLISHING(d)     \
00312     (  (d)->status == GTA_DL_CONNECTING \
00313     || (d)->status == GTA_DL_PUSH_SENT  \
00314     || (d)->status == GTA_DL_FALLBACK   \
00315     || (d)->status == GTA_DL_REQ_SENT   \
00316     || (d)->status == GTA_DL_REQ_SENDING    \
00317     || (d)->status == GTA_DL_ACTIVE_QUEUED  \
00318     || (d)->status == GTA_DL_SINKING    \
00319     || (d)->status == GTA_DL_HEADERS    )
00320 
00321 #define DOWNLOAD_IS_EXPECTING_GIV(d)    \
00322     (  (d)->status == GTA_DL_PUSH_SENT  \
00323     || (d)->status == GTA_DL_FALLBACK   )
00324 
00325 #define DOWNLOAD_IS_RUNNING(d)          \
00326     (   DOWNLOAD_IS_ACTIVE(d)           \
00327     ||  DOWNLOAD_IS_ESTABLISHING(d)     )
00328 
00329 #define DOWNLOAD_IS_IN_PUSH_MODE(d) (d->push)
00330 #define DOWNLOAD_IS_VISIBLE(d)      (d->visible)
00331 
00332 /*
00333  * Public interface, visible only from the bridge.
00334  */
00335 
00336 #ifdef CORE_SOURCES
00337 
00338 /* FIXME: download_index_changed
00339  *        actually needs to be in downloads.h and should be called from
00340  *        search.h and not from search_gui.h.
00341  */
00342 void download_index_changed(const host_addr_t, guint16, gchar *,
00343         guint32, guint32);
00344 
00345 gboolean download_new(gchar *,
00346     filesize_t, guint32, const host_addr_t addr, guint16,
00347     const gchar *, gchar *, gchar *, time_t,
00348     gboolean, struct dl_file_info *, gnet_host_vec_t *, guint32 flags);
00349 gboolean download_new_uri(gchar *file, const gchar *uri, filesize_t size,
00350       const host_addr_t addr, guint16 port, const gchar *guid, gchar *hostname,
00351       gchar *sha1, time_t stamp, gboolean push,
00352       struct dl_file_info *fi, gnet_host_vec_t *proxies, guint32 flags);
00353 void download_auto_new(gchar *,
00354     filesize_t, guint32, const host_addr_t, guint16, const gchar *,
00355     gchar *, gchar *, time_t,
00356     gboolean, gboolean, struct dl_file_info *, gnet_host_vec_t *,
00357     guint32 flags);
00358 
00359 void src_add_listener(src_listener_t, gnet_src_ev_t, frequency_t, guint32);
00360 void src_remove_listener(src_listener_t, gnet_src_ev_t);
00361 struct download *src_get_download(gnet_src_t src_handle);
00362 
00363 const gchar *build_url_from_download(const struct download *d);
00364 gint download_get_http_req_percent(const struct download *d);
00365 void download_fallback_to_push(struct download *, gboolean, gboolean);
00366 gint download_remove_all_from_peer(gchar *guid, host_addr_t addr, guint16 port,
00367     gboolean unavailable);
00368 gint download_remove_all_named(const gchar *name);
00369 gint download_remove_all_with_sha1(const gchar *sha1);
00370 void download_remove_file(struct download *d, gboolean reset);
00371 gboolean download_file_exists(const struct download *d);
00372 void download_requeue(struct download *);
00373 void download_start(struct download *, gboolean);
00374 gboolean download_remove(struct download *);
00375 void download_abort(struct download *);
00376 void download_resume(struct download *);
00377 void download_freeze_queue(void);
00378 void download_thaw_queue(void);
00379 gint download_queue_is_frozen(void);
00380 void download_clear_stopped(gboolean, gboolean, gboolean, gboolean);
00381 gboolean download_new_unknown_size(gchar *file, guint32 record_index,
00382      const host_addr_t addr, guint16 port, const gchar *guid, gchar *hostname,
00383     gchar *sha1, time_t stamp, gboolean push,
00384     struct dl_file_info *fi, gnet_host_vec_t *proxies, guint32 flags);
00385 const gchar *download_get_hostname(const struct download *d);
00386 gdouble download_source_progress(const struct download *);
00387 gdouble download_total_progress(const struct download *);
00388 gboolean download_something_to_clear(void);
00389 struct download *src_get_download(gnet_src_t src_handle);
00390 void src_add_listener(src_listener_t cb, gnet_src_ev_t ev,
00391     frequency_t t, guint32 interval);
00392 void src_remove_listener(src_listener_t cb, gnet_src_ev_t ev);
00393 
00394 #endif /* CORE_SOURCES */
00395 #endif /* _if_core_downloads_h_ */
00396 
00397 /* vi: set ts=4 sw=4 cindent: */

Generated on Sun Feb 12 10:49:56 2006 for Gtk-Gnutella by doxygen 1.3.6