Click here to Skip to main content
15,881,898 members
Articles / Multimedia / GDI+

MyDownloader: A Multi-thread C# Segmented Download Manager

Rate me:
Please Sign up or sign in to vote.
4.95/5 (418 votes)
12 Feb 2008CPOL10 min read 2.1M   124.7K   1.3K  
Sample application that manages multiple segmented downloads and supports HTTP, FTP and YouTube video downloads
using System;
using System.Collections.Generic;
using System.Text;
using MyDownloader.Spider.Parsers.Html;

namespace MyDownloader.Spider
{
    public delegate void ResourceCompletedEventHandler(SpiderContext context, ISpiderResource resource);

    public delegate void CompletedEventHandler(SpiderContext context);

    public class SpiderContext
    {
        private bool aborted;
        private string baseDirectory;
        private string baseLocation;

        private List<INamingRule> namingRules = new List<INamingRule>();
        private List<Uri> visitedLocations = new List<Uri>();
        private List<ISpiderResource> completedResources = new List<ISpiderResource>();

        public SpiderContext()
        {
        }

        public SpiderContext(string baseDirectory, string baseLocation)
        {
            this.baseDirectory = baseDirectory;
            this.baseLocation = baseLocation;
        }

        #region Properties

        public string BaseLocation
        {
            get
            {
                return baseLocation;
            }
            set
            {
                baseLocation = value;
            }
        }

        public string BaseDirectory
        {
            get
            {
                return baseDirectory;
            }
            set
            {
                baseDirectory = value;
            }
        }

        public List<INamingRule> NamingRules
        {
            get
            {
                return namingRules;
            }
        }

        public List<Uri> VisitedLocations
        {
            get
            {
                return visitedLocations;
            }
        }

        public List<ISpiderResource> CompletedResources
        {
            get
            {
                return completedResources;
            }
        } 

        #endregion

        #region Events

        public event ResourceCompletedEventHandler ResourceCompleted;

        public event CompletedEventHandler Completed;

        protected internal virtual void OnResourceCompleted(ISpiderResource spider)
        {
            if (ResourceCompleted != null)
            {
                ResourceCompleted(this, spider);
            }
        } 

        protected internal virtual void OnCompleted()
        {
            if (Completed != null)
            {
                Completed(this);
            }
        } 

        #endregion

        private bool ShouldAdd(Uri location, ISpiderResource parentSpider)
        {
            #region Aborted
            if (aborted)
            {
                return false;
            } 
            #endregion

            #region Naming rules
            for (int j = 0; j < namingRules.Count; j++)
            {
                if (!namingRules[j].Accept(location, this, parentSpider)) return false;
            } 
            #endregion

            return true;
        }

        public void Abort()
        {
            aborted = true;
        }

        public void NotifyCompletion(ISpiderResource spider)
        {
            lock (completedResources)
            {
                if (!completedResources.Contains(spider))
                {
                    completedResources.Add(spider);
                }
            }

            OnResourceCompleted(spider);

            if (completedResources.Count == VisitedLocations.Count)
            {
                OnCompleted();
            }
        }

        public bool AddURIToVisit(Uri uri, ISpiderResource parentSpider)
        {
            if (ShouldAdd(uri, parentSpider))
            {
                lock (visitedLocations)
                {
                    if (visitedLocations.Contains(uri))
                    {
                        return false;
                    }

                    visitedLocations.Add(uri);

                    return true;
                }
            }
            else
            {
                return false;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions