comparison src/transfer/download.c @ 0:d39e1d0d75b6

initial add
author paulo@hit-nxdomain.opendns.com
date Sat, 20 Feb 2010 21:18:28 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7dcf604c13f1
1 /*
2 * $Id: download.c,v 1.2 2004/04/17 06:06:46 hipnod Exp $
3 *
4 * Copyright (C) 2004 giFT project (gift.sourceforge.net)
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17 #include "gt_gnutella.h"
18 #include "transfer/download.h"
19
20 /*****************************************************************************/
21
22 static Dataset *gt_downloads;
23
24 /*****************************************************************************/
25
26 void gt_download_add (Transfer *transfer, Source *source)
27 {
28 Dataset *d;
29
30 d = dataset_lookup (gt_downloads, &transfer, sizeof(transfer));
31 dataset_insert (&d, &source, sizeof(source), source, 0);
32
33 dataset_insert (&gt_downloads, &transfer, sizeof(transfer), d, 0);
34 }
35
36 void gt_download_remove (Transfer *transfer, Source *source)
37 {
38 Dataset *d;
39
40 d = dataset_lookup (gt_downloads, &transfer, sizeof(transfer));
41 dataset_remove (d, &source, sizeof(source));
42
43 if (dataset_length (d) == 0)
44 {
45 dataset_clear (d);
46 dataset_remove (gt_downloads, &transfer, sizeof(transfer));
47 }
48
49 if (dataset_length (gt_downloads) == 0)
50 {
51 dataset_clear (gt_downloads);
52 gt_downloads = NULL;
53 }
54 }
55
56 /*****************************************************************************/
57
58 static int ds_find_hash (ds_data_t *key, ds_data_t *value, void *udata)
59 {
60 Array *a = udata;
61 char *sha1;
62 Source **ret;
63 Source *src = value->data;
64 int n;
65
66 n = array_list (&a, &sha1, &ret, NULL);
67 assert (n == 2);
68
69 if (!src->hash)
70 return DS_CONTINUE;
71
72 /* NOTE: the hash is prefixed with giftd's hash here */
73 if (strcmp (src->hash, sha1) == 0)
74 {
75 *ret = src;
76 return DS_BREAK;
77 }
78
79 return DS_CONTINUE;
80 }
81
82 static int ds_traverse_transfer (ds_data_t *key, ds_data_t *value, void *udata)
83 {
84 Dataset *d = value->data;
85
86 dataset_foreach_ex (d, ds_find_hash, udata);
87 return DS_CONTINUE;
88 }
89
90 Source *gt_download_lookup (const char *sha1)
91 {
92 Array *a;
93 Source *ret = NULL;
94
95 a = array_new ((void *)sha1, &ret, NULL);
96
97 if (!a)
98 return NULL;
99
100 dataset_foreach_ex (gt_downloads, ds_traverse_transfer, a);
101 array_unset (&a);
102
103 return ret;
104 }