Author: alanmc
Date: 2006-11-07 16:08:07 -0500 (Tue, 07 Nov 2006)
New Revision: 67495
Added:
trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
Modified:
trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
Log:
Initial version of End game mode implemented. It's not great, and needs a
refactor. But it works.
Modified: trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2006-11-07
21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/ClientEngine.cs 2006-11-07
21:08:07 UTC (rev 67495)
@@ -192,13 +192,20 @@
/// </summary>
public WaitHandle[] Stop()
{
- int i = 0;
- WaitHandle[] waitHandles = new WaitHandle[this.torrents.Count];
+ List<WaitHandle> waitHandles = new
List<WaitHandle>(this.torrents.Count);
+ List<TorrentManager> managers = new
List<TorrentManager>(this.torrents.Count);
foreach (KeyValuePair<string, TorrentManager> keypair in
this.torrents)
if (keypair.Value.State != TorrentState.Stopped)
- waitHandles[i++] = Stop(keypair.Value);
+ managers.Add(keypair.Value);
- return waitHandles;
+ for (int i = 0; i < managers.Count; i++)
+ {
+ WaitHandle h = managers[i].Stop();
+ if (h != null)
+ waitHandles.Add(h);
+ }
+
+ return waitHandles.ToArray();
}
Added: trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
2006-11-07 21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
2006-11-07 21:08:07 UTC (rev 67495)
@@ -0,0 +1,133 @@
+//
+// PieceManager.cs
+//
+// Authors:
+// Alan McGovern [EMAIL PROTECTED]
+//
+// Copyright (C) 2006 Alan McGovern
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+
+
+using System;
+using System.Collections.Generic;
+using MonoTorrent.Client.PeerMessages;
+using MonoTorrent.Common;
+using MonoTorrent.Client;
+
+namespace MonoTorrent.Client
+{
+ /// <summary>
+ /// Contains the logic for choosing what piece to download next
+ /// </summary>
+ public class PieceManager
+ {
+ #region Events
+ /// <summary>
+ /// Event that's fired every time a Piece changes
+ /// </summary>
+ internal event EventHandler<PieceEventArgs> OnPieceChanged;
+ #endregion
+
+
+ #region Member Variables
+ private IPiecePicker piecePicker;
+ #endregion
+
+
+ #region Constructors
+ public PieceManager(BitField bitfield, TorrentFile[] files)
+ {
+ this.piecePicker = new StandardPicker(bitfield, files);
+ }
+ #endregion
+
+
+ #region Methods
+ internal bool IsInteresting(PeerConnectionID id)
+ {
+ lock (this.piecePicker)
+ return this.piecePicker.IsInteresting(id);
+ }
+
+
+ public bool InEndGameMode
+ {
+ get
+ {
+ lock (this.piecePicker)
+ return this.piecePicker is EndGamePicker;
+ }
+ }
+
+
+ internal BitField MyBitField
+ {
+ get
+ {
+ lock (this.piecePicker)
+ return this.piecePicker.MyBitField;
+ }
+ }
+
+
+ internal int CurrentRequestCount()
+ {
+ lock (this.piecePicker)
+ return this.piecePicker.CurrentRequestCount();
+ }
+
+
+ internal IPeerMessageInternal PickPiece(PeerConnectionID id, Peers
otherPeers)
+ {
+ lock (this.piecePicker)
+ {
+ if ((this.MyBitField.Length - this.MyBitField.TrueCount < 15)
&& this.piecePicker is StandardPicker)
+ this.piecePicker = new EndGamePicker(this.MyBitField,
id.TorrentManager.Torrent);
+
+ return this.piecePicker.PickPiece(id, otherPeers);
+ }
+ }
+
+
+ internal void ReceivedRejectRequest(PeerConnectionID id,
RejectRequestMessage msg)
+ {
+ lock (this.piecePicker)
+ this.piecePicker.ReceivedRejectRequest(id, msg);
+ }
+
+
+ internal void RemoveRequests(PeerConnectionID id)
+ {
+ lock (this.piecePicker)
+ this.piecePicker.RemoveRequests(id);
+ }
+
+
+ internal void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer,
int dataOffset, int writeIndex, int blockLength, PieceMessage message)
+ {
+ lock (this.piecePicker)
+ this.piecePicker.ReceivedPieceMessage(id, buffer, dataOffset,
writeIndex, blockLength, message);
+ }
+ #endregion
+ }
+}
Property changes on:
trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
2006-11-07 21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/TorrentManager.cs
2006-11-07 21:08:07 UTC (rev 67495)
@@ -456,15 +456,10 @@
Debug.WriteLine("ReChoking: " + this.uploadingTo);
}
- if ((DateTime.Now -
id.Peer.Connection.LastMessageReceived) > new TimeSpan(0, 2, 0) &&
id.Peer.Connection.AmRequestingPiecesCount > 0)
- {
- ClientEngine.ConnectionManager.CleanupSocket(id);
- continue;
- }
while (!id.Peer.Connection.IsChoking &&
id.Peer.Connection.AmRequestingPiecesCount < 6 &&
id.Peer.Connection.AmInterested)
{
if (pieceManager.InEndGameMode)// In endgame we
only want to queue 2 pieces
- if (id.Peer.Connection.AmRequestingPiecesCount
> 2)
+ if (id.Peer.Connection.AmRequestingPiecesCount
> 6)
break;
msg = this.pieceManager.PickPiece(id,
this.connectedPeers);
@@ -475,6 +470,13 @@
id.Peer.Connection.AmRequestingPiecesCount++;
}
+ if ((DateTime.Now -
id.Peer.Connection.LastMessageReceived) > new TimeSpan(0, 0, 30) &&
id.Peer.Connection.AmRequestingPiecesCount > 0)
+ {
+ this.pieceManager.RemoveRequests(id);
+ ClientEngine.ConnectionManager.CleanupSocket(id);
+ continue;
+ }
+
if (!(id.Peer.Connection.ProcessingQueue) &&
id.Peer.Connection.QueueLength > 0)
ClientEngine.ConnectionManager.ProcessQueue(id);
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
2006-11-07 21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/MonoTorrent.Client.csproj
2006-11-07 21:08:07 UTC (rev 67495)
@@ -89,7 +89,7 @@
<Compile Include="Managers\ConnectionManager.cs" />
<Compile Include="Managers\ConnectionMonitor.cs" />
<Compile Include="Managers\FileManager.cs" />
- <Compile Include="PiecePicking\PieceManager.cs" />
+ <Compile Include="Managers\PieceManager.cs" />
<Compile Include="Managers\TorrentManager.cs" />
<Compile Include="PeerConnections\PeerConnectionBase.cs" />
<Compile Include="PeerConnections\PeerConnectionId.cs" />
Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
2006-11-07 21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/EndGamePicker.cs
2006-11-07 21:08:07 UTC (rev 67495)
@@ -2,12 +2,164 @@
using System.Collections.Generic;
using System.Text;
using MonoTorrent.Client.PeerMessages;
+using MonoTorrent.Common;
namespace MonoTorrent.Client
{
- internal class EndGamePieceManager
+ internal class EndGamePicker : IPiecePicker
{
- /*
+ #region Member Variables
+ private object requestsLocker = new object();
+ private List<Piece> pieces;
+ private List<Block> blocks;
+ private List<PeerConnectionID> requesters;
+ #endregion
+
+
+ #region Constructors
+ public EndGamePicker(BitField myBitfield, Torrent torrent)
+ {
+ this.myBitfield = myBitfield;
+ this.pieces = new List<Piece>(this.myBitfield.Length -
this.myBitfield.TrueCount);
+ for (int i = 0; i < this.myBitfield.Length; i++)
+ if (!this.myBitfield[i])
+ this.pieces.Add(new Piece(i, torrent));
+
+ this.blocks = new List<Block>(this.pieces.Count *
this.pieces[0].Blocks.Length);
+ for (int i = 0; i < this.pieces.Count; i++)
+ for (int j = 0; j < this.pieces[i].Blocks.Length; j++)
+ this.blocks.Add(this.pieces[i][j]);
+ }
+ #endregion
+
+
+ #region Methods
+ public bool IsInteresting(PeerConnectionID id)
+ {
+ lock (this.requestsLocker)
+ {
+ for(int i=0; i<this.pieces.Count;i++)
+ if (!id.Peer.Connection.BitField[pieces[i].Index])
+ return true;
+
+ return false;
+ }
+ }
+
+ public RequestMessage PickPiece(PeerConnectionID id, Peers otherPeers)
+ {
+ lock (this.requestsLocker)
+ {
+ for (int i = 0; i < this.blocks.Count; i++)
+ {
+ if
(!id.Peer.Connection.BitField[this.blocks[i].PieceIndex])
+ continue;
+
+ Block b = this.blocks[i];
+ this.blocks.RemoveAt(i);
+ this.blocks.Add(b);
+ b.Requested = true;
+ return b.CreateRequest();
+ }
+
+ return null;
+ }
+ }
+
+ public BitField MyBitField
+ {
+ get { return this.myBitfield; }
+ }
+ private BitField myBitfield;
+
+
+ public List<Piece> CurrentPieces()
+ {
+ return this.pieces;
+ }
+
+
+ public int CurrentRequestCount()
+ {
+ return this.pieces.Count;
+ }
+
+
+ public void ReceivedRejectRequest(PeerConnectionID id,
RejectRequestMessage message)
+ {
+ // FIXME: Remove a request
+ }
+
+
+ public void RemoveRequests(PeerConnectionID id)
+ {
+ // In End Game mode requests aren't tracked... they should be
though
+ }
+
+
+ public void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer,
int dataOffset, long writeIndex, int blockLength, PieceMessage message)
+ {
+ lock (this.requestsLocker)
+ {
+ Piece p = null;
+ Block b = null;
+ for (int i = 0; i < this.pieces.Count; i++)
+ if (this.pieces[i].Index == message.PieceIndex)
+ p = this.pieces[i];
+
+ if (p == null)
+ return;
+
+ for (int i = 0; i < p.Blocks.Length; i++)
+ if (p[i].StartOffset == message.StartOffset)
+ b = p[i];
+
+ if (b == null)
+ return;
+
+ if (message.BlockLength != b.RequestLength)
+ throw new Exception("Request length should match block
length");
+
+ if(!b.Received)
+ id.TorrentManager.FileManager.Write(buffer, dataOffset,
writeIndex, blockLength);
+ b.Received = true;
+ id.Peer.Connection.AmRequestingPiecesCount--;
+
+ if (!p.AllBlocksReceived)
+ return;
+
+ bool result =
ToolBox.ByteMatch(id.TorrentManager.Torrent.Pieces[p.Index],
id.TorrentManager.FileManager.GetHash(p.Index));
+ this.myBitfield[message.PieceIndex] = result;
+
+ id.TorrentManager.HashedPiece(new
PieceHashedEventArgs(p.Index, result));
+
+ if (result)
+ {
+ id.Peer.Connection.IsInterestingToMe =
this.IsInteresting(id);
+ id.TorrentManager.PieceCompleted(p.Index);
+ for (int i = 0; i < p.Blocks.Length; i++)
+ this.blocks.Remove(p[i]);
+
+ this.pieces.Remove(p);
+ }
+ else
+ {
+ for (int i = 0; i < p.Blocks.Length; i++)
+ {
+ p.Blocks[i].Requested = false;
+ p.Blocks[i].Received = false;
+ }
+ }
+ }
+ }
+ #endregion
+
+
+ }
+}
+
+#region crap
+/*
List<Piece> endGamePieces;
private RequestMessage EndGamePicker(PeerConnectionID id, Peers
otherPeers)
{
@@ -108,5 +260,5 @@
}
}
*/
- }
-}
+
+#endregion
\ No newline at end of file
Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
2006-11-07 21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/IPiecePicker.cs
2006-11-07 21:08:07 UTC (rev 67495)
@@ -13,6 +13,6 @@
int CurrentRequestCount();
void ReceivedRejectRequest(PeerConnectionID id, RejectRequestMessage
message);
void RemoveRequests(PeerConnectionID id);
- void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer, int
dataOffset, int writeIndex, int blockLength, PieceMessage message);
+ void ReceivedPieceMessage(PeerConnectionID id, byte[] buffer, int
dataOffset, long writeIndex, int blockLength, PieceMessage message);
}
}
Modified: trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
2006-11-07 21:05:17 UTC (rev 67494)
+++ trunk/bitsharp/src/MonoTorrent.Client/PiecePicking/StandardPicker.cs
2006-11-07 21:08:07 UTC (rev 67495)
@@ -8,8 +8,6 @@
{
internal class StandardPicker : IPiecePicker
{
-
-
#region Member Variables
private int[] priorities;
@@ -252,7 +250,7 @@
/// <param name="writeIndex"></param>
/// <param name="p"></param>
- public void ReceivedPieceMessage(PeerConnectionID id, byte[]
recieveBuffer, int offset, int writeIndex, int p, PieceMessage message)
+ public void ReceivedPieceMessage(PeerConnectionID id, byte[]
recieveBuffer, int offset, long writeIndex, int p, PieceMessage message)
{
lock (this.requests)
{
@@ -351,5 +349,17 @@
}
}
#endregion
+
+ public List<Piece> CurrentPieces()
+ {
+ List<Piece> pieces = new List<Piece>(this.requests.Count * 2);
+ lock (this.requests)
+ {
+ foreach (KeyValuePair<PeerConnectionID, List<Piece>> keypair
in this.requests)
+ for (int i = 0; i < keypair.Value.Count; i++)
+ pieces.Add(keypair.Value[i]);
+ }
+ return pieces;
+ }
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches