SaveLoad Fast Resume

Version 4 (Alan McGovern, 04/20/2009 11:03 PM)

1 1
h1. Save/Load Fast Resume
2 1
3 4 Alan McGovern
This is used so that a full hash check does not have to be performed the first time a torrent is started. Normally all the data for a torrent would be passed through a SHA1 hasher so that data integrity can be verified. With fast resume you can skip this step.
4 4 Alan McGovern
5 3 Alan McGovern
<pre><code class="java">
6 1
		public void SaveFastResume (List <TorrentManager> managers)
7 1
		{
8 1
			// Store the fast resume for each torrent in a list,
9 1
			// then serialise the list to the disk.
10 1
			BEncodedList list = new BEncodedList ();
11 1
			foreach (TorrentManager manager in managers) {
12 1
13 1
				// Get the fast resume data for the torrent
14 1
				FastResume data = manager.SaveFastResume ();
15 1
				
16 1
				// Encode the FastResume data to a BEncodedDictionary.
17 1
				BEncodedDictionary fastResume = data.Encode ();
18 1
19 1
				// Add the FastResume dictionary to the main dictionary using
20 1
				// the torrents infohash as the key
21 1
				list.Add (data);
22 1
			}
23 1
24 1
			// Write all the fast resume data to disk
25 1
			File.WriteAllBytes (fastResumePath, list.Encode ());
26 1
		}
27 1
28 1
		public void LoadFastResume (List <TorrentManager> managers)
29 1
		{
30 1
			// Read the main dictionary from disk and iterate through
31 1
			// all the fast resume items
32 2 Alan McGovern
			BEncodedList list = (BEncodedList) BEncodedValue.Decode (File.ReadAllBytes (fastResumePath));
33 2 Alan McGovern
			foreach (BEncodedDictionary fastResume in list) {
34 1
35 1
				// Decode the FastResume data from the BEncodedDictionary
36 1
				FastResume data = new FastResume (fastResume);
37 1
38 1
				// Find the torrentmanager that the fastresume belongs to
39 1
				// and then load it
40 1
				foreach (TorrentManager manager in managers)
41 1
					if (manager.InfoHash == data.Infohash)
42 1
						manager.LoadFastResume (data);
43 1
			}
44 1
		}
45 1
</code></pre>