Author: alanmc
Date: 2008-02-03 17:46:22 -0500 (Sun, 03 Feb 2008)
New Revision: 94725

Added:
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/FastResume/
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/FastResume/FastResume.cs
Modified:
   trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs
Log:
Added new fast resume code - not tested fully yet ;)

Added: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/FastResume/FastResume.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/FastResume/FastResume.cs  
2008-02-03 22:45:54 UTC (rev 94724)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/FastResume/FastResume.cs  
2008-02-03 22:46:22 UTC (rev 94725)
@@ -0,0 +1,92 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using MonoTorrent.Client;
+using MonoTorrent.Common;
+using System.IO;
+using MonoTorrent.BEncoding;
+
+namespace MonoTorrent.Client
+{
+    public class FastResume
+    {
+        private static readonly BEncodedString VersionKey = 
(BEncodedString)"version";
+        private static readonly BEncodedString InfoHashKey = 
(BEncodedString)"infohash";
+        private static readonly BEncodedString BitfieldKey = 
(BEncodedString)"bitfield";
+        private static readonly BEncodedString BitfieldLengthKey = 
(BEncodedString)"bitfield_length";
+
+        private BitField bitfield;
+        private byte[] infoHash;
+        private List<Peer> peers;
+
+        public BitField Bitfield
+        {
+            get { return bitfield; }
+        }
+
+        public byte[] InfoHash
+        {
+            get { return infoHash; }
+        }
+
+        public FastResume()
+        {
+        }
+
+        public FastResume(byte[] infoHash, BitField bitfield, List<Peer> peers)
+        {
+            if (infoHash==null)
+                throw new ArgumentNullException("infoHash");
+            if(bitfield == null)
+                throw new ArgumentNullException("bitfield");
+            if(peers == null)
+                throw new ArgumentNullException("peers");
+
+            this.infoHash = infoHash;
+            this.bitfield = bitfield;
+            this.peers = peers;
+        }
+
+        public FastResume(BEncodedDictionary dict)
+        {
+            CheckContent(dict, VersionKey, (BEncodedNumber)1);
+            CheckContent(dict, InfoHashKey);
+            CheckContent(dict, BitfieldKey);
+            CheckContent(dict, BitfieldLengthKey);
+
+            infoHash = ((BEncodedString)dict[InfoHashKey]).TextBytes;
+            bitfield = new 
BitField((int)((BEncodedNumber)dict[BitfieldLengthKey]).Number);
+            byte[] data = ((BEncodedString)dict[BitfieldKey]).TextBytes;
+            bitfield.FromArray(data, 0, data.Length);
+        }
+
+        private void CheckContent(BEncodedDictionary dict, BEncodedString key, 
BEncodedNumber value)
+        {
+            CheckContent(dict, key);
+            if (!dict[key].Equals(value))
+                throw new TorrentException(string.Format("Invalid FastResume 
data. The value of '{0}' was '{1}' instead of '{2}'", key, dict[key], value));
+        }
+
+        private void CheckContent(BEncodedDictionary dict, BEncodedString key)
+        {
+            if (!dict.ContainsKey(key))
+                throw new TorrentException(string.Format("Invalid FastResume 
data. Key '{0}' was not present", key));
+        }
+
+        public BEncodedDictionary Encode()
+        {
+            BEncodedDictionary dict = new BEncodedDictionary();
+            dict.Add(VersionKey, (BEncodedNumber)1);
+            dict.Add(InfoHashKey, new BEncodedString(infoHash));
+            dict.Add(BitfieldKey, new BEncodedString(bitfield.ToByteArray()));
+            dict.Add(BitfieldLengthKey, (BEncodedNumber)bitfield.Length);
+            return dict;
+        }
+
+        public void Encode(Stream s)
+        {
+            byte[] data = Encode().Encode();
+            s.Write(data, 0, data.Length);
+        }
+    }
+}


Property changes on: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/FastResume/FastResume.cs
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs   
2008-02-03 22:45:54 UTC (rev 94724)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs   
2008-02-03 22:46:22 UTC (rev 94725)
@@ -247,7 +247,7 @@
         /// </summary>
         /// <param name="manager">The manager to load fastresume data 
for</param>
         /// <returns></returns>
-        internal static bool LoadFastResume(TorrentManager manager)
+        /*internal static bool LoadFastResume(TorrentManager manager)
         {
             try
             {
@@ -276,7 +276,7 @@
                 manager.PieceManager.MyBitField.SetAll(false);
                 return false;
             }
-        }
+        }*/
 
 
         /// <summary>

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs    
    2008-02-03 22:45:54 UTC (rev 94724)
+++ 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs    
    2008-02-03 22:46:22 UTC (rev 94725)
@@ -271,11 +271,21 @@
         /// <param name="savePath">The directory to save downloaded files 
to</param>
         /// <param name="settings">The settings to use for controlling 
connections</param>
         public TorrentManager(Torrent torrent, string savePath, 
TorrentSettings settings)
-            : this(torrent, savePath, settings, torrent.Files.Length == 1 ? "" 
: torrent.Name)
+            : this(torrent, savePath, settings, torrent.Files.Length == 1 ? "" 
: torrent.Name, null)
         {
 
         }
 
+        public TorrentManager(Torrent torrent, string savePath, 
TorrentSettings settings, FastResume fastResumeData)
+            : this(torrent, savePath, settings, torrent.Files.Length == 1 ? "" 
: torrent.Name, fastResumeData)
+        {
+
+        }
+
+        public TorrentManager(Torrent torrent, string savePath, 
TorrentSettings settings, string baseDirectory)
+            : this(torrent, savePath, settings, baseDirectory, null)
+        {
+        }
         /// <summary>
         /// Creates a new TorrentManager instance.
         /// </summary>
@@ -283,7 +293,7 @@
         /// <param name="savePath">The directory to save downloaded files 
to</param>
         /// <param name="settings">The settings to use for controlling 
connections</param>
         /// <param name="baseDirectory">In the case of a multi-file torrent, 
the name of the base directory containing the files. Defaults to 
Torrent.Name</param>
-        public TorrentManager(Torrent torrent, string savePath, 
TorrentSettings settings, string baseDirectory)
+        public TorrentManager(Torrent torrent, string savePath, 
TorrentSettings settings, string baseDirectory, FastResume fastResumeData)
         {
             if (torrent == null)
                 throw new ArgumentNullException("torrent");
@@ -306,8 +316,12 @@
             this.pieceManager = new PieceManager(bitfield, torrent.Files);
             this.torrent = torrent;
             this.trackerManager = new TrackerManager(this);
+
+            if (fastResumeData != null)
+                LoadFastResume(fastResumeData);
         }
 
+
         #endregion
 
 
@@ -315,8 +329,7 @@
 
         public void Dispose()
         {
-            // FIXME: Dispose of the streams when this is reached
-            //this.fileManager.Dispose();
+            // Do nothing?
         }
 
 
@@ -802,7 +815,9 @@
                 bool filesExist = fileManager.CheckFilesExist();
 
                 // We only load fast resume if one (or more) of the files 
exist on disk.
-                bool loadedFastResume = filesExist && 
FileManager.LoadFastResume(this);
+                // FIXME: We now never load fastresume from the disk as it's 
passed into the constructor
+                //bool loadedFastResume = filesExist && 
FileManager.LoadFastResume(this);
+                bool loadedFastResume = filesExist && false;
 
                 // If we are performing a forced scan OR we aren't forcing a 
full scan but can't load the fast resume data
                 // perform a full scan.
@@ -885,19 +900,28 @@
         //}
 
 
+        private void LoadFastResume(FastResume fastResumeData)
+        {
+            if (fastResumeData == null)
+                throw new ArgumentNullException ("fastResumeData");
+            if (!Toolbox.ByteMatch(torrent.infoHash, fastResumeData.InfoHash) 
|| torrent.Pieces.Count != fastResumeData.Bitfield.Length)
+                throw new ArgumentException("The fast resume data does not 
match this torrent", "fastResumeData");
+
+            this.bitfield = fastResumeData.Bitfield;
+            this.hashChecked = true;
+        }
+
+
         /// <summary>
         /// Saves data to allow fastresumes to the disk
         /// </summary>
-        private void SaveFastResume()
+        private FastResume SaveFastResume()
         {
             // Do not create fast-resume data if we do not support it for this 
TorrentManager object
             if (!Settings.FastResumeEnabled || 
string.IsNullOrEmpty(this.torrent.TorrentPath))
-                return;
+                return null;
 
-            XmlSerializer fastResume = new XmlSerializer(typeof(int[]));
-
-            using (FileStream file = File.Open(this.torrent.TorrentPath + 
".fresume", FileMode.Create))
-                fastResume.Serialize(file, this.pieceManager.MyBitField.Array);
+            return new FastResume(this.torrent.infoHash, this.bitfield, new 
List<Peer>());
         }
 
 

Modified: trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj       
2008-02-03 22:45:54 UTC (rev 94724)
+++ trunk/bitsharp/src/MonoTorrent.Tests/MonoTorrent.Tests.csproj       
2008-02-03 22:46:22 UTC (rev 94725)
@@ -47,6 +47,10 @@
       <Project>{F48B95BE-0C36-48A3-838F-83FB0E4D0936}</Project>
       <Name>MonoTorrent</Name>
     </ProjectReference>
+    <ProjectReference Include="..\SampleClient\SampleClient.csproj">
+      <Project>{6842B47D-8D85-4B56-BFBF-F97DD6D4D0B6}</Project>
+      <Name>SampleClient</Name>
+    </ProjectReference>
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to