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