diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/transfer/download.c	Sat Feb 20 21:18:28 2010 -0800
     1.3 @@ -0,0 +1,104 @@
     1.4 +/*
     1.5 + * $Id: download.c,v 1.2 2004/04/17 06:06:46 hipnod Exp $
     1.6 + *
     1.7 + * Copyright (C) 2004 giFT project (gift.sourceforge.net)
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or modify it
    1.10 + * under the terms of the GNU General Public License as published by the
    1.11 + * Free Software Foundation; either version 2, or (at your option) any
    1.12 + * later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful, but
    1.15 + * WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 + * General Public License for more details.
    1.18 + */
    1.19 +
    1.20 +#include "gt_gnutella.h"
    1.21 +#include "transfer/download.h"
    1.22 +
    1.23 +/*****************************************************************************/
    1.24 +
    1.25 +static Dataset *gt_downloads;
    1.26 +
    1.27 +/*****************************************************************************/
    1.28 +
    1.29 +void gt_download_add (Transfer *transfer, Source *source)
    1.30 +{
    1.31 +	Dataset *d;
    1.32 +
    1.33 +	d = dataset_lookup (gt_downloads, &transfer, sizeof(transfer));
    1.34 +	dataset_insert (&d, &source, sizeof(source), source, 0);
    1.35 +
    1.36 +	dataset_insert (&gt_downloads, &transfer, sizeof(transfer), d, 0);
    1.37 +}
    1.38 +
    1.39 +void gt_download_remove (Transfer *transfer, Source *source)
    1.40 +{
    1.41 +	Dataset *d;
    1.42 +
    1.43 +	d = dataset_lookup (gt_downloads, &transfer, sizeof(transfer));
    1.44 +	dataset_remove (d, &source, sizeof(source));
    1.45 +
    1.46 +	if (dataset_length (d) == 0)
    1.47 +	{
    1.48 +		dataset_clear (d);
    1.49 +		dataset_remove (gt_downloads, &transfer, sizeof(transfer));
    1.50 +	}
    1.51 +
    1.52 +	if (dataset_length (gt_downloads) == 0)
    1.53 +	{
    1.54 +		dataset_clear (gt_downloads);
    1.55 +		gt_downloads = NULL;
    1.56 +	}
    1.57 +}
    1.58 +
    1.59 +/*****************************************************************************/
    1.60 +
    1.61 +static int ds_find_hash (ds_data_t *key, ds_data_t *value, void *udata)
    1.62 +{
    1.63 +	Array      *a    = udata;
    1.64 +	char       *sha1;
    1.65 +	Source    **ret;
    1.66 +	Source     *src  = value->data;
    1.67 +	int         n;
    1.68 +
    1.69 +	n = array_list (&a, &sha1, &ret, NULL);
    1.70 +	assert (n == 2);
    1.71 +
    1.72 +	if (!src->hash)
    1.73 +		return DS_CONTINUE;
    1.74 +
    1.75 +	/* NOTE: the hash is prefixed with giftd's hash here */
    1.76 +	if (strcmp (src->hash, sha1) == 0)
    1.77 +	{
    1.78 +		*ret = src;
    1.79 +		return DS_BREAK;
    1.80 +	}
    1.81 +
    1.82 +	return DS_CONTINUE;
    1.83 +}
    1.84 +
    1.85 +static int ds_traverse_transfer (ds_data_t *key, ds_data_t *value, void *udata)
    1.86 +{
    1.87 +	Dataset *d = value->data;
    1.88 +
    1.89 +	dataset_foreach_ex (d, ds_find_hash, udata);
    1.90 +	return DS_CONTINUE;
    1.91 +}
    1.92 +
    1.93 +Source *gt_download_lookup (const char *sha1)
    1.94 +{
    1.95 +	Array  *a;
    1.96 +	Source *ret = NULL;
    1.97 +
    1.98 +	a = array_new ((void *)sha1, &ret, NULL);
    1.99 +
   1.100 +	if (!a)
   1.101 +		return NULL;
   1.102 +
   1.103 +	dataset_foreach_ex (gt_downloads, ds_traverse_transfer, a);
   1.104 +	array_unset (&a);
   1.105 +
   1.106 +	return ret;
   1.107 +}