Managing Torrents

Version 15 (Alan McGovern, 07/30/2009 01:08 AM) → Version 16/18 (Alan McGovern, 07/30/2009 01:17 AM)


h1. Managing Torrents

[[Managing Torrents#Example-1|Example 1]] shows how to add a torrent to the engine and start the download
[[Managing Torrents#Example-2|Example 2]] shows how to hash check a torrent and show a progress indicator
[[Managing Torrents#Example-3|Example 3]] shows how to stop a torrent


h2. Example 1

<pre><code class="java">
class MainClass
{
ClientEngine engine;
string savePath;

// savePath is the directory where downloads will be stored
public MainClass(string savePath)
{
// Create a basic ClientEngine without changing any settings
this.engine = new ClientEngine(new EngineSettings());
this.savePath = savePath;
}

public void DownloadTorrent(string path)
{
// Open the .torrent file
Torrent torrent = Torrent.Load(path);

// Create the manager which will download the torrent to savePath
// using the default settings.
TorrentManager manager = new TorrentManager(torrent, savePath, new TorrentSettings());

// Register the manager with the engine
engine.Register(manager);

// Begin the download. It is not necessary to call HashCheck on the manager
// before starting the download. If a hash check has not been performed, the
// manager will enter the Hashing state and perform a hash check before it
// begins downloading.

// If the torrent is fully downloaded already, calling 'Start' will place
// the manager in the Seeding state.
manager.Start();
}
}
</code></pre>

h2. Example 2

<pre><code class="java">
public void HashTorrent(TorrentManager manager)
{
// Note: The manager must be in the 'Stopped' state in order to perform
// a hash check. Also, to make the sample easier the event handler
// is not unregistered. In your application be careful you don't
// accidentally attach a new event handler every time the torrent is hashed
manager.PieceHashed += delegate (object o, PieceHashedEventArgs e) {
int pieceIndex = e.PieceIndex;
int totalPieces = e.TorrentManager.Torrent.Pieces.Count;
double progress = (double) pieceIndex / totalPieces * 100.0;
if (e.HashPassed)
Console.WriteLine("Piece {0} of {1} is complete", pieceIndex, totalPieces);
else
Console.WriteLine("Piece {0} of {1} is corrupt or incomplete ", pieceIndex, totalPieces);

// This shows how complete the hashing is.
Console.WriteLine("Total progress is: {0}%", progress);

// This shows the percentage completion of the download. This value
// is updated as the torrent is hashed or new pieces are downloaded
Console.WriteLine("{0}% of the torrent is complete");
};

// If 'true' is passed, the torrent will automatically go to the 'Downloading' or 'Seeding'
// state once the hash check is finished. Otherwise it will return to the 'Stopped' state.
manager.HashCheck(false);
}
</code></pre>

h2. Example 3


<pre><code class="java">
public void StopTorrent(TorrentManager manager)
{
// When stopping a torrent, certain cleanup tasks need to


ClientEngine engine = new ClientEngine(new EngineSettings(downloadsPath, port));

//DHT
DhtListener dhtListner = new UdpListener (new IPEndPoint (IPAddress.Any, port));
DhtEngine dht = new DhtEngine (dhtListner);
engine.RegisterDht(dht);
dhtListner.Start();
//byte array of dht nodes can
be perfomed
// such as flushing unwritten data
null if you have never connect to disk and informing DHT before today!
engine.DhtEngine.Start(nodes);

torrent = Torrent.Load("test.torrent");
TorrentManager manager = new TorrentManager(torrent, downloadsPath, torrentDefaults);

//FastResume code need a BencodedDictionnary
//If you jsut start
the tracker
// the client is
torrent, you have no longer downloading/seeding the torrent. To allow for
fast resume
// this, when Stop is called the manager enters but if you have download a 'Stopping' state. Once
// all
part of the tasks are completed the manager will enter the 'Stopped' state.

manager.TorrentStateChanged += delegate (object o, TorrentStateChangedEventArgs e) {
if (e.NewState == TorrentState.Stopping)
{
Console.WriteLine("Torrent {0} has begun stopping", e.TorrentManager.Torrent.Name);
}
else if (e.NewState == TorrentState.Stopped)
{
// It is now safe to unregister the
torrent from the engine you can save fast resume data
//(manager.SaveFastResume())
and dispose of restore it (if required)
engine.Unregister(manager);
manager.Dispose();

Console.WriteLine("Torrent {0} has stopped", e.TorrentManager.Torrent.Name);
}
};
}
later to do a quicker load
</code></pre> manager.LoadFastResume(new FastResume (BEncDictFastResume));

engine.Register(manager);
manager.Start ();

</code>
</pre>