Author: alanmc
Date: 2007-05-23 18:34:10 -0400 (Wed, 23 May 2007)
New Revision: 77874

Modified:
   trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs
   trunk/bitsharp/src/MonoTorrent/Common/Torrent.cs
   trunk/bitsharp/src/MonoTorrent/Tracker/Frontend/global.asax.cs
   trunk/bitsharp/src/MonoTorrent/Tracker/TrackerEngine.cs
Log:
Torrent.Load is now a static method. Much neater design

Modified: trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs       2007-05-23 
21:54:11 UTC (rev 77873)
+++ trunk/bitsharp/src/MonoTorrent/Client/ClientEngine.cs       2007-05-23 
22:34:10 UTC (rev 77874)
@@ -335,27 +335,30 @@
         /// <returns>A TorrentManager used to control the torrent</returns>
         public TorrentManager LoadTorrent(string path, string savePath, 
TorrentSettings torrentSettings)
         {
-            Torrent torrent = new Torrent();
             try
             {
-                torrent.LoadTorrent(path);
+                Torrent torrent = Torrent.Load(path);
+                return LoadTorrent(torrent, savePath, torrentSettings);
             }
             catch (Exception ex)
             {
                 throw new TorrentLoadException("Could not load the torrent", 
ex);
             }
+        }
 
-            if (this.ContainsTorrent(BitConverter.ToString(torrent.InfoHash)))
-                throw new TorrentException("The torrent is already in the 
engine");
+               public TorrentManager LoadTorrent(Torrent torrent, string 
savePath, TorrentSettings torrentSettings)
+               {
+                       if 
(this.ContainsTorrent(BitConverter.ToString(torrent.InfoHash)))
+                               throw new TorrentException("The torrent is 
already in the engine");
 
-            TorrentManager manager = new TorrentManager(torrent, savePath, 
torrentSettings, this.settings);
-            this.torrents.Add(manager);
+                       TorrentManager manager = new TorrentManager(torrent, 
savePath, torrentSettings, this.settings);
+                       this.torrents.Add(manager);
 
-            if (File.Exists(torrent.TorrentPath + ".fresume"))
-                manager.HashChecked = LoadFastResume(manager);
+                       if (File.Exists(torrent.TorrentPath + ".fresume"))
+                               manager.HashChecked = LoadFastResume(manager);
 
-            return (manager);
-        }
+                       return (manager);
+               }
 
         private bool ContainsTorrent(string p)
         {

Modified: trunk/bitsharp/src/MonoTorrent/Common/Torrent.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Common/Torrent.cs    2007-05-23 21:54:11 UTC 
(rev 77873)
+++ trunk/bitsharp/src/MonoTorrent/Common/Torrent.cs    2007-05-23 22:34:10 UTC 
(rev 77874)
@@ -34,6 +34,7 @@
 using System.Text;
 using System.Security.Cryptography;
 using MonoTorrent.BEncoding;
+using System.Collections;
 
 namespace MonoTorrent.Common
 {
@@ -260,7 +261,7 @@
 
         #region Constructors
 
-        public Torrent()
+        private Torrent()
         {
                        this.announceUrls = new List<List<string>>();
             this.comment = string.Empty;
@@ -480,11 +481,7 @@
             if (torrent == null)
                 return false;
 
-            for (int i = 0; i < this.infoHash.Length; i++)
-                if (this.infoHash[i] != torrent.infoHash[i])
-                    return false;
-
-            return true;
+            return ToolBox.ByteMatch(this.infoHash, torrent.infoHash);
         }
 
 
@@ -494,7 +491,11 @@
         /// <returns>int</returns>
         public override int GetHashCode()
         {
-            return this.infoHash.GetHashCode();
+            int result = 0;
+            for (int i = 0; i < infoHash.Length; i++)
+                result ^= infoHash[i];
+
+            return result;
         }
 
 
@@ -502,19 +503,18 @@
         /// This method loads a .torrent file from the specified path.
         /// </summary>
         /// <param name="path">The path to load the .torrent file from</param>
-        public void LoadTorrent(string path)
+        public static Torrent Load(string path)
         {
             if (String.IsNullOrEmpty(path))
                 throw new ArgumentNullException("path");
 
             using (BinaryReader reader = new BinaryReader(new FileStream(path, 
FileMode.Open, FileAccess.Read, FileShare.Read), System.Text.Encoding.UTF8))
             {
-                this.torrentPath = path;
-                BEncodedDictionary dict = null;
                 try
                 {
-                    dict = (BEncodedDictionary)BEncode.Decode(reader);
-                    LoadTorrent(dict);
+                    Torrent t = 
Torrent.Load((BEncodedDictionary)BEncode.Decode(reader));
+                    t.torrentPath = path;
+                    return t;
                 }
                 catch (BEncodingException ex)
                 {
@@ -528,59 +528,60 @@
         /// This method loads the torrent information from a BEncoded 
dictionary
         /// </summary>
         /// <param name="torrentInformation">The dictionary from a decoded 
.torrent file</param>
-        public void LoadTorrent(BEncodedDictionary torrentInformation)
+        public static Torrent Load(BEncodedDictionary torrentInformation)
         {
+            Torrent t = new Torrent();
             foreach (KeyValuePair<BEncodedString, IBEncodedValue> keypair in 
torrentInformation)
             {
                 switch (keypair.Key.Text)
                 {
                     case ("announce"):
-                        this.announceUrls.Add(new List<string>(1));
-                                               
this.announceUrls[0].Add(keypair.Value.ToString());
+                        t.announceUrls.Add(new List<string>(1));
+                        t.announceUrls[0].Add(keypair.Value.ToString());
                         break;
 
                     case ("creation date"):
-                        this.creationDate = 
this.creationDate.AddSeconds(long.Parse(keypair.Value.ToString()));
+                        t.creationDate = 
t.creationDate.AddSeconds(long.Parse(keypair.Value.ToString()));
                         break;
 
                     case ("nodes"):
-                        this.nodes = (BEncodedList)keypair.Value;
+                        t.nodes = (BEncodedList)keypair.Value;
                         break;
 
                     case ("comment.utf-8"):
                         if (keypair.Value.ToString().Length != 0)
-                            this.comment = keypair.Value.ToString();        // 
Always take the UTF-8 version
+                            t.comment = keypair.Value.ToString();        // 
Always take the UTF-8 version
                         break;                                              // 
even if there's an existing value
 
                     case ("comment"):
-                        if (String.IsNullOrEmpty(this.comment))
-                            this.comment = keypair.Value.ToString();
+                        if (String.IsNullOrEmpty(t.comment))
+                            t.comment = keypair.Value.ToString();
                         break;
 
                     case ("publisher-url.utf-8"):                           // 
Always take the UTF-8 version
-                        this.publisherUrl = keypair.Value.ToString();       // 
even if there's an existing value
+                        t.publisherUrl = keypair.Value.ToString();       // 
even if there's an existing value
                         break;
 
                     case ("publisher-url"):
-                        if (String.IsNullOrEmpty(this.publisherUrl))
-                            this.publisherUrl = keypair.Value.ToString();
+                        if (String.IsNullOrEmpty(t.publisherUrl))
+                            t.publisherUrl = keypair.Value.ToString();
                         break;
 
                     case ("azureus_properties"):
-                        this.azureusProperties = keypair.Value;
+                        t.azureusProperties = keypair.Value;
                         break;
 
                     case ("created by"):
-                        this.createdBy = keypair.Value.ToString();
+                        t.createdBy = keypair.Value.ToString();
                         break;
 
                     case ("encoding"):
-                        this.encoding = keypair.Value.ToString();
+                        t.encoding = keypair.Value.ToString();
                         break;
 
                     case ("info"):
-                        this.infoHash = new 
SHA1Managed().ComputeHash(keypair.Value.Encode());
-                        this.ProcessInfo(((BEncodedDictionary)keypair.Value));
+                        t.infoHash = new 
SHA1Managed().ComputeHash(keypair.Value.Encode());
+                        t.ProcessInfo(((BEncodedDictionary)keypair.Value));
                         break;
 
                     case ("name"):                                             
  // Handled elsewhere
@@ -588,7 +589,7 @@
 
                     case ("announce-list"):
                         BEncodedList announces = (BEncodedList)keypair.Value;
-                                               this.announceUrls = new 
List<List<string>>(announces.Count);
+                        t.announceUrls = new 
List<List<string>>(announces.Count);
 
                                                for (int j = 0; j < 
announces.Count; j++)
                                                {
@@ -599,7 +600,7 @@
                                                                
tier.Add(bencodedTier[k].ToString());
 
                                                        
ToolBox.Randomize<string>(tier);
-                                                       
this.announceUrls.Add(tier);
+                            t.announceUrls.Add(tier);
                                                }
                         break;
 
@@ -610,28 +611,15 @@
 
             int i = 0;
             long totalSize = 0;
-            foreach (TorrentFile file in this.torrentFiles)
+            foreach (TorrentFile file in t.torrentFiles)
             {
                 file.StartPieceIndex = i;
                 totalSize += file.Length;
-                file.EndPieceIndex = (int)(totalSize / this.pieceLength + 
(totalSize % this.pieceLength > 0 ? 0 : -1));
+                file.EndPieceIndex = (int)(totalSize / t.pieceLength + 
(totalSize % t.pieceLength > 0 ? 0 : -1));
                 i = file.EndPieceIndex;
             }
-        }
 
-
-        /// <summary>
-        /// This method saves this .torrent file to the specified location
-        /// </summary>
-        /// <param name="path">The location to save the .torrent file 
to</param>
-        /// <param name="overwrite">If true, any existing file will be 
overwritten</param>
-        public void SaveTorrent(string path, bool overwrite)
-        {
-            if (!path.ToLower().EndsWith(".torrent"))      // make sure it 
ends with .torrent
-                path += ".torrent";
-
-            throw new NotImplementedException();
-            // This method should be used to save this torrent to the disk
+            return t;
         }
 
 
@@ -640,7 +628,7 @@
         /// contained within this Torrent
         /// </summary>
         /// <returns></returns>
-        public System.Collections.IEnumerator GetEnumerator()
+        public IEnumerator GetEnumerator()
         {
             return this.torrentFiles.GetEnumerator();
         }

Modified: trunk/bitsharp/src/MonoTorrent/Tracker/Frontend/global.asax.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Tracker/Frontend/global.asax.cs      
2007-05-23 21:54:11 UTC (rev 77873)
+++ trunk/bitsharp/src/MonoTorrent/Tracker/Frontend/global.asax.cs      
2007-05-23 22:34:10 UTC (rev 77874)
@@ -53,8 +53,7 @@
                 Debug.WriteLine("after engine.Start");
                 Application.Add("engine", engine);
                 Application.Add("tracker", Tracker.Instance);
-                Torrent test = new Torrent();
-                test.LoadTorrent("bin/torrentcreator.torrent");
+                Torrent test = Torrent.Load("bin/torrentcreator.torrent");
                 Tracker.Instance.AddTorrent(test);
             } catch (Exception e) {
                 Console.WriteLine(e);

Modified: trunk/bitsharp/src/MonoTorrent/Tracker/TrackerEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/Tracker/TrackerEngine.cs     2007-05-23 
21:54:11 UTC (rev 77873)
+++ trunk/bitsharp/src/MonoTorrent/Tracker/TrackerEngine.cs     2007-05-23 
22:34:10 UTC (rev 77874)
@@ -205,8 +205,7 @@
         {
             try
             {
-                Torrent t = new Torrent();
-                t.LoadTorrent(e.TorrentPath);
+                Torrent t = Torrent.Load(e.TorrentPath);
                 this.Tracker.AddTorrent(t);
             }
             catch (BEncodingException ex)

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

Reply via email to