Naanou.ResourceExchange
Class Downloader

Manages the download of one particular resource

Field Summary
System.Threading.ReaderWriterLock bdRwl
long bytesDownloaded
         Do not set directly
Naanou.ResourceExchange.ResourceAvailRangeComparer comp
System.Collections.ArrayList doneRanges
         Array list of ByteRanges. Gets updated by StartImpl() and OnWorkerComplete handler
string downloadElapsed
         Set when status is updated
long downloadStartTicks
         Gets set by Start to DateTime.Now.Ticks
long fileLength
         Expected length of file
bool hadError
string hash
Naanou.ResourceExchange.WorkStatusArgs lastStatus
Naanou.ResourceExchange.Source.Manifest[] likelySourcesQ
System.Collections.Hashtable likelySourcesUQ
         Sources that may have resource, but we don't know any more than their id and url. Keyed by Naanou Id, value is where we found them from
Naanou.ResourceExchange.IMonitorForm monitorForm
System.Threading.ManualResetEvent mutex
string pathForEndFile
         Full path (including filename) to use for file when downloaded
System.Collections.ArrayList pool
Naanou.ResourceExchange.Downloader.Priority priority
System.Random random
System.Threading.ReaderWriterLock rwl
System.Collections.ArrayList searchProgressTemp
System.Collections.ArrayList sourcesToUse
         Array List of Source.Manifest. Gets updated by StartImpl and QuerySources
Naanou.ResourceExchange.Downloader.State state
System.Threading.ReaderWriterLock statsLock
System.Threading.Thread thread
System.Collections.Hashtable workerStatus

Constructor Summary
Downloader(string hash, Naanou.ResourceExchange.Downloader.Priority priority)
         Downloader for a file of unknown length
Downloader(string hash, long fileLength, Naanou.ResourceExchange.Downloader.Priority priority)
         Constructor

Property Summary
string ElapsedTime
         Gets the elapsed time for download
long FileLength
         Gets the total file length of the file this downloader is getting
string HashSerial
         Gets the serialised hash of resource
bool IsClosed
         Has the downloader closed (or is otherwise aborted, complete or in an error state)
Naanou.ResourceExchange.WorkStatusArgs LastStatus
         Gets the last recorded status
Naanou.ResourceExchange.Source.Manifest[] LikelySourcesQueried
         Gets or sets likely sources (and what ranges they have) for content (gathered from search results for instance)
Naanou.ResourceExchange.IMonitorForm MonitorForm
         Gets or sets the monitor form
string PathForEndFile
         Gets path to use for saving the file
string Progress
         Gets progress
string StateText
         Gets the current state of this downloader in string form
int Threads
         Gets the number of threads in use
bool WorkersAvailable
         Are there any workers available?

Method Summary
void Abort()
         Aborts download
void AddCombinedRange(System.Collections.ArrayList combined, System.Collections.Hashtable combinedHash, Naanou.ResourceExchange.ByteRange newRange, Naanou.ResourceExchange.Source.Manifest source, Naanou.ResourceExchange.ResourceAvailRange existing, bool mergeExisting, bool isFilled)
void AddLikelyUQSource(Naanou.ResourceExchange.Source source, string findMethod)
         Adds a likely, but unqueried source for this downloader
bool AssembleFile(string location)
         Assembles all the blocks we've downloaded into a single file
void BlockDownloadedOk(Naanou.ResourceExchange.Block b)
long BytesDownloaded()
System.Collections.ArrayList CombineRanges(System.Collections.ArrayList sources, System.Collections.ArrayList doneRanges, long totalSize)
         Combines the available ranges from all sources into one big list, sorted in scarcity order (0 = scarcist)
void GetStats()
         Gets the latest stats, and updates any monitorForm downloader has. Only allows one GetStats to run at a time
void GetStats(System.Drawing.Graphics g, System.Drawing.Rectangle rect)
Naanou.ResourceExchange.IWorker[] GetWorkerPool()
         Gets a copy of all the workers currently in the pool for this downloader.
Naanou.ResourceExchange.IWorker[] GetWorkers(Naanou.ResourceExchange.WorkStatusArgs[] statuses)
         Gets a copy of all workers that the Downloader is receiving stats for and their statuses
void HandleShutdown(bool errorWhileRunning)
void LocateSources()
         Attempt to locate sources for resource
void OnSearchProgress(Naanou.Common.Search.SearchResultSet rs, bool complete)
void OnWorkerComplete(Naanou.ResourceExchange.IWorker sender, Naanou.ResourceExchange.WorkCompleteArgs args)
         Notification by a worker that it has completed
void OnWorkerStatus(Naanou.ResourceExchange.IWorker worker, Naanou.ResourceExchange.WorkStatusArgs args)
void QuerySources(System.Collections.ArrayList sources, Naanou.ResourceExchange.Downloader.QueryDepth queryDepth)
void SetState(Naanou.ResourceExchange.Downloader.State state)
void Start(string savePath)
         Starts download process in a new thread
void StartImpl()
void StartWorker(Naanou.ResourceExchange.IWorker worker)
         Starts a worker if there are threads available, or waits
void StartWrapper()
void WaitForWorkers(bool abort)
void WaitForWorkerSlot()

Methods inherited from class System.Object
Equals, Finalize, GetHashCode, GetType, MemberwiseClone, ToString

Event Summary
Naanou.ResourceExchange.TransferFinishedEventHandler TransferFinished
         Indicates when a transfer (i.e. a swarmed download) has finished
Naanou.ResourceExchange.TransferStartedEventHandler TransferStarted
         Indicates when a transfer (i.e. a swarmed download) has stated


Field Detail

bdRwl

private System.Threading.ReaderWriterLock bdRwl


bytesDownloaded

private long bytesDownloaded

Do not set directly


comp

private Naanou.ResourceExchange.ResourceAvailRangeComparer comp


doneRanges

private System.Collections.ArrayList doneRanges

Array list of ByteRanges. Gets updated by StartImpl() and OnWorkerComplete handler


downloadElapsed

private string downloadElapsed

Set when status is updated


downloadStartTicks

private long downloadStartTicks

Gets set by Start to DateTime.Now.Ticks


fileLength

private long fileLength

Expected length of file


hadError

private bool hadError


hash

private string hash


lastStatus

private Naanou.ResourceExchange.WorkStatusArgs lastStatus


likelySourcesQ

private Naanou.ResourceExchange.Source.Manifest[] likelySourcesQ


likelySourcesUQ

private System.Collections.Hashtable likelySourcesUQ

Sources that may have resource, but we don't know any more than their id and url. Keyed by Naanou Id, value is where we found them from


monitorForm

private Naanou.ResourceExchange.IMonitorForm monitorForm


mutex

private System.Threading.ManualResetEvent mutex


pathForEndFile

private string pathForEndFile

Full path (including filename) to use for file when downloaded


pool

private System.Collections.ArrayList pool


priority

private Naanou.ResourceExchange.Downloader.Priority priority


random

private System.Random random


rwl

private System.Threading.ReaderWriterLock rwl


searchProgressTemp

private System.Collections.ArrayList searchProgressTemp


sourcesToUse

private System.Collections.ArrayList sourcesToUse

Array List of Source.Manifest. Gets updated by StartImpl and QuerySources


state

private Naanou.ResourceExchange.Downloader.State state


statsLock

private System.Threading.ReaderWriterLock statsLock


thread

private System.Threading.Thread thread


workerStatus

private System.Collections.Hashtable workerStatus

Constructor Detail

Downloader

internal Downloader(string hash,
                    Naanou.ResourceExchange.Downloader.Priority priority)

Downloader for a file of unknown length

Parameters:
hash - Resource to get's hash
pathForFile - Path for file
priority - Priority

Downloader

internal Downloader(string hash,
                    long fileLength,
                    Naanou.ResourceExchange.Downloader.Priority priority)

Constructor

Parameters:
hash - Resource's hash
fileLength - Length of file
pathForFile - Where to save file
priority - Priority
Property Detail

ElapsedTime

public string ElapsedTime

Gets the elapsed time for download


FileLength

public long FileLength

Gets the total file length of the file this downloader is getting


HashSerial

public string HashSerial

Gets the serialised hash of resource


IsClosed

public bool IsClosed

Has the downloader closed (or is otherwise aborted, complete or in an error state)


LastStatus

public Naanou.ResourceExchange.WorkStatusArgs LastStatus

Gets the last recorded status


LikelySourcesQueried

public Naanou.ResourceExchange.Source.Manifest[] LikelySourcesQueried

Gets or sets likely sources (and what ranges they have) for content (gathered from search results for instance)


MonitorForm

public Naanou.ResourceExchange.IMonitorForm MonitorForm

Gets or sets the monitor form


PathForEndFile

public string PathForEndFile

Gets path to use for saving the file


Progress

public string Progress

Gets progress


StateText

public string StateText

Gets the current state of this downloader in string form


Threads

public int Threads

Gets the number of threads in use


WorkersAvailable

public bool WorkersAvailable

Are there any workers available?

Method Detail

Abort

public void Abort()

Aborts download


AddCombinedRange

private void AddCombinedRange(System.Collections.ArrayList combined,
                              System.Collections.Hashtable combinedHash,
                              Naanou.ResourceExchange.ByteRange newRange,
                              Naanou.ResourceExchange.Source.Manifest source,
                              Naanou.ResourceExchange.ResourceAvailRange existing,
                              bool mergeExisting,
                              bool isFilled)


AddLikelyUQSource

public void AddLikelyUQSource(Naanou.ResourceExchange.Source source,
                              string findMethod)

Adds a likely, but unqueried source for this downloader

Parameters:
source - Source
findMethod - How was source discovered

AssembleFile

private bool AssembleFile(string location)

Assembles all the blocks we've downloaded into a single file

Parameters:
location - Full path to save blocks to
Returns:
True if file was written alright

BlockDownloadedOk

private void BlockDownloadedOk(Naanou.ResourceExchange.Block b)


BytesDownloaded

private long BytesDownloaded()


CombineRanges

public System.Collections.ArrayList CombineRanges(System.Collections.ArrayList sources,
                                                  System.Collections.ArrayList doneRanges,
                                                  long totalSize)

Combines the available ranges from all sources into one big list, sorted in scarcity order (0 = scarcist)

Parameters:
sources - ArrayList of ResourceSources
Returns:
ArrayList of ResourceAvailRanges

GetStats

private void GetStats()

Gets the latest stats, and updates any monitorForm downloader has. Only allows one GetStats to run at a time


GetStats

private void GetStats(System.Drawing.Graphics g,
                      System.Drawing.Rectangle rect)


GetWorkerPool

public Naanou.ResourceExchange.IWorker[] GetWorkerPool()

Gets a copy of all the workers currently in the pool for this downloader.

Returns:
Workers currently in the pool

GetWorkers

public Naanou.ResourceExchange.IWorker[] GetWorkers(Naanou.ResourceExchange.WorkStatusArgs[] statuses)

Gets a copy of all workers that the Downloader is receiving stats for and their statuses

Parameters:
statuses -
Returns:
Workers that the Downloader is receiving stats for

HandleShutdown

private void HandleShutdown(bool errorWhileRunning)


LocateSources

private void LocateSources()

Attempt to locate sources for resource

Returns:
True if sources were located

OnSearchProgress

private void OnSearchProgress(Naanou.Common.Search.SearchResultSet rs,
                              bool complete)


OnWorkerComplete

private void OnWorkerComplete(Naanou.ResourceExchange.IWorker sender,
                              Naanou.ResourceExchange.WorkCompleteArgs args)

Notification by a worker that it has completed

Parameters:
sender - Worker
args - Completion details

OnWorkerStatus

private void OnWorkerStatus(Naanou.ResourceExchange.IWorker worker,
                            Naanou.ResourceExchange.WorkStatusArgs args)


QuerySources

private void QuerySources(System.Collections.ArrayList sources,
                          Naanou.ResourceExchange.Downloader.QueryDepth queryDepth)


SetState

private void SetState(Naanou.ResourceExchange.Downloader.State state)


Start

public void Start(string savePath)

Starts download process in a new thread


StartImpl

private void StartImpl()


StartWorker

internal void StartWorker(Naanou.ResourceExchange.IWorker worker)

Starts a worker if there are threads available, or waits

Parameters:
rg -

StartWrapper

private void StartWrapper()


WaitForWorkers

private void WaitForWorkers(bool abort)


WaitForWorkerSlot

private void WaitForWorkerSlot()

Event Detail

TransferFinished

public Naanou.ResourceExchange.TransferFinishedEventHandler TransferFinished

Indicates when a transfer (i.e. a swarmed download) has finished


TransferStarted

public Naanou.ResourceExchange.TransferStartedEventHandler TransferStarted

Indicates when a transfer (i.e. a swarmed download) has stated