Author: alanmc
Date: 2008-02-17 18:55:46 -0500 (Sun, 17 Feb 2008)
New Revision: 96017

Modified:
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/DiskManager.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/PeerManager.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/TrackerTier.cs
   trunk/bitsharp/src/MonoTorrent/MonoTorrent.Common/ToolBox.cs
Log:
Cosmetic changes

Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs   
2008-02-17 23:36:58 UTC (rev 96016)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs   
2008-02-17 23:55:46 UTC (rev 96017)
@@ -255,12 +255,9 @@
         {
             if (torrent == null)
                 return false;
-            using (new ReaderLock(this.torrentsLock))
-                for (int i = 0; i < this.torrents.Count; i++)
-                    if (Toolbox.ByteMatch(this.torrents[i].Torrent.infoHash, 
torrent.infoHash))
-                        return true;
 
-            return false;
+            using (new ReaderLock(this.torrentsLock))
+                return torrents.Exists(delegate(TorrentManager m) { return 
Toolbox.ByteMatch(m.Torrent.infoHash, torrent.infoHash); });
         }
 
 
@@ -326,10 +323,7 @@
         {
             lock (asyncCompletionLock)
                 using (new ReaderLock(this.torrentsLock))
-                    for (int i = 0; i < torrents.Count; i++)
-                        if (torrents[i].State == TorrentState.Downloading ||
-                            torrents[i].State == TorrentState.Seeding)
-                            torrents[i].Pause();
+                    torrents.ForEach(delegate(TorrentManager m) { m.Pause(); 
});
         }
 
 
@@ -359,9 +353,7 @@
         {
             lock (asyncCompletionLock)
                 using (new ReaderLock(this.torrentsLock))
-                    for (int i = 0; i < torrents.Count; i++)
-                        if (torrents[i].State == TorrentState.Stopped || 
torrents[i].State == TorrentState.Paused)
-                            torrents[i].Start();
+                    torrents.ForEach(delegate(TorrentManager m) { m.Start(); 
});
         }
 
 
@@ -373,9 +365,7 @@
             List<WaitHandle> handles = new List<WaitHandle>();
             lock (asyncCompletionLock)
                 using (new ReaderLock(this.torrentsLock))
-                    for (int i = 0; i < torrents.Count; i++)
-                        if (torrents[i].State != TorrentState.Stopped)
-                            handles.Add(torrents[i].Stop());
+                    torrents.ForEach(delegate(TorrentManager m) { 
handles.Add(m.Stop()); });
 
             return handles.ToArray();
         }
@@ -388,12 +378,8 @@
         {
             get
             {
-                double speed = 0;
                 using (new ReaderLock(torrentsLock))
-                    for (int i = 0; i < torrents.Count; i++)
-                        speed += torrents[i].Monitor.DownloadSpeed;
-
-                return speed;
+                    return Toolbox.Accumulate<TorrentManager>(torrents, 
delegate(TorrentManager m) { return (int)m.Monitor.DownloadSpeed; });
             }
         }
 
@@ -405,12 +391,8 @@
         {
             get
             {
-                double speed = 0;
                 using (new ReaderLock(torrentsLock))
-                    for (int i = 0; i < torrents.Count; i++)
-                        speed += torrents[i].Monitor.UploadSpeed;
-
-                return speed;
+                    return Toolbox.Accumulate<TorrentManager>(torrents, 
delegate(TorrentManager m) { return (int)m.Monitor.UploadSpeed; });
             }
         }
 
@@ -502,12 +484,9 @@
         {
             lock (asyncCompletionLock)
             {
+                // If all the torrents are stopped, stop ticking
                 using (new ReaderLock(this.torrentsLock))
-                    for (int i = 0; i < this.torrents.Count; i++)
-                        if (this.torrents[i].State != TorrentState.Stopped)
-                            return;
-
-                timer.Enabled = false;              // All the torrents are 
stopped, so stop ticking
+                    timer.Enabled = torrents.Exists(delegate(TorrentManager m) 
{ return m.State != TorrentState.Stopped; });
             }
         }
 

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/DiskManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/DiskManager.cs   
2008-02-17 23:36:58 UTC (rev 96016)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/DiskManager.cs   
2008-02-17 23:55:46 UTC (rev 96017)
@@ -20,7 +20,7 @@
 
         private ConnectionMonitor monitor;
         internal RateLimiter rateLimiter;
-               private PieceWriter writer;
+        private PieceWriter writer;
 
         #endregion Member Variables
 
@@ -73,7 +73,7 @@
 
         #region Constructors
 
-               internal DiskManager(ClientEngine engine, PieceWriter writer)
+        internal DiskManager(ClientEngine engine, PieceWriter writer)
         {
             this.bufferedReads = new Queue<BufferedFileRead>();
             this.bufferedWrites = new Queue<PieceData>();

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs   
2008-02-17 23:36:58 UTC (rev 96016)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/FileManager.cs   
2008-02-17 23:55:46 UTC (rev 96017)
@@ -208,10 +208,10 @@
                     do
                     {
                         bytesToRead = (PieceLength - totalRead) > 
hashBuffer.Count ? hashBuffer.Count : (PieceLength - totalRead);
-                                               bytesToRead = 
Math.Min(bytesToRead, Piece.BlockSize);
+                        bytesToRead = Math.Min(bytesToRead, Piece.BlockSize);
 
                         if ((pieceStartIndex + bytesToRead + totalRead) > 
this.fileSize)
-                                                       bytesToRead = 
(int)(fileSize - (pieceStartIndex + totalRead));
+                            bytesToRead = (int)(fileSize - (pieceStartIndex + 
totalRead));
 
                         if (asynchronous)
                         {

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/PeerManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/PeerManager.cs   
2008-02-17 23:36:58 UTC (rev 96016)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/PeerManager.cs   
2008-02-17 23:55:46 UTC (rev 96017)
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Text;
+using MonoTorrent.Common;
 
 namespace MonoTorrent.Client
 {
@@ -60,14 +61,8 @@
         {
             get
             {
-                int leechs = 0;
                 lock (this.manager.listLock)
-                    for (int i = 0; i < this.activePeers.Count; i++)
-                        lock (this.activePeers[i])
-                            if (!this.activePeers[i].IsSeeder)
-                                leechs++;
-
-                return leechs;
+                    return Toolbox.Count<Peer>(activePeers, delegate(Peer p) { 
return !p.IsSeeder; });
             }
         }
 
@@ -79,13 +74,8 @@
         {
             get
             {
-                int seeds = 0;
                 lock (this.manager.listLock)
-                    for (int i = 0; i < this.activePeers.Count; i++)
-                        lock (this.activePeers[i])
-                            if (this.activePeers[i].IsSeeder)
-                                seeds++;
-                return seeds;
+                    return Toolbox.Count<Peer>(activePeers, delegate(Peer p) { 
return p.IsSeeder; });
             }
         }
 

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs    
    2008-02-17 23:36:58 UTC (rev 96016)
+++ 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs    
    2008-02-17 23:55:46 UTC (rev 96017)
@@ -404,6 +404,9 @@
             lock (this.engine.asyncCompletionLock)
                 lock (this.listLock)
                 {
+                    if (state != TorrentState.Downloading && state != 
TorrentState.Seeding)
+                        return;
+
                     // By setting the state to "paused", peers will not be 
dequeued from the either the
                     // sending or receiving queues, so no traffic will be 
allowed.
                     UpdateState(TorrentState.Paused);
@@ -439,11 +442,10 @@
                     return;
                 }
 
-
                 if (this.state == TorrentState.Seeding || this.state == 
TorrentState.Downloading)
-                    throw new TorrentException("Torrent is already running");
+                    return;
 
-                if (TrackerManager.CurrentTracker.CanScrape)
+                if (TrackerManager.CurrentTracker != null && 
TrackerManager.CurrentTracker.CanScrape)
                     this.TrackerManager.Scrape();
 
                 this.trackerManager.Announce(TorrentEvent.Started); // Tell 
server we're starting
@@ -465,13 +467,13 @@
         /// </summary>
         public WaitHandle Stop()
         {
-                       WaitHandle handle;
+            WaitHandle handle;
             lock (this.engine.asyncCompletionLock)
             {
-                               if (this.state == TorrentState.Stopped)
-                                       return new ManualResetEvent(true);
+                if (this.state == TorrentState.Stopped)
+                    return new ManualResetEvent(true);
 
-                               UpdateState(TorrentState.Stopped);
+                UpdateState(TorrentState.Stopped);
 
                 handle = this.trackerManager.Announce(TorrentEvent.Stopped);
                 lock (this.listLock)
@@ -721,8 +723,8 @@
         internal void RaisePieceHashed(PieceHashedEventArgs 
pieceHashedEventArgs)
         {
             int index = pieceHashedEventArgs.PieceIndex;
-                       TorrentFile[] files = this.torrent.Files;
-                       
+            TorrentFile[] files = this.torrent.Files;
+            
             for (int i = 0; i < files.Length; i++)
                 if (index >= files[i].StartPieceIndex && index <= 
files[i].EndPieceIndex)
                     files[i].BitField[index - files[i].StartPieceIndex] = 
pieceHashedEventArgs.HashPassed;

Modified: 
trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/TrackerTier.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/TrackerTier.cs    
2008-02-17 23:36:58 UTC (rev 96016)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Client/Tracker/TrackerTier.cs    
2008-02-17 23:55:46 UTC (rev 96017)
@@ -71,11 +71,7 @@
 
         internal int IndexOf(Tracker tracker)
         {
-            for (int i = 0; i < this.trackers.Length; i++)
-                if (this.trackers[i].Equals(tracker))
-                    return i;
-
-            return -1;
+                       return Array.IndexOf<Tracker>(trackers, tracker);
         }
 
 

Modified: trunk/bitsharp/src/MonoTorrent/MonoTorrent.Common/ToolBox.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent/MonoTorrent.Common/ToolBox.cs        
2008-02-17 23:36:58 UTC (rev 96016)
+++ trunk/bitsharp/src/MonoTorrent/MonoTorrent.Common/ToolBox.cs        
2008-02-17 23:55:46 UTC (rev 96017)
@@ -35,10 +35,33 @@
 
 namespace MonoTorrent.Common
 {
-    public class Toolbox
+       public delegate int Action<T>(T target);
+
+    public static class Toolbox
     {
         private static Random r = new Random();
+               public static int Count<T>(IEnumerable<T> enumerable, 
Predicate<T> predicate)
+               {
+                       int count = 0;
 
+                       foreach (T t in enumerable)
+                               if (predicate(t))
+                                       count++;
+
+                       return count;
+               }
+
+               public static int Accumulate<T>(IEnumerable<T> enumerable, 
Action<T> action)
+               {
+                       int count = 0;
+
+                       foreach (T t in enumerable)
+                               count += action(t);
+               
+                       return count;
+               }
+
+
         /// <summary>
         /// Randomizes the contents of the array
         /// </summary>

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

Reply via email to