Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Refresh the 'AmInterested' status regularly #601

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public DownloadMode (TorrentManager manager, DiskManager diskManager, Connection
}
}

public override void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
=> RefreshAmInterestedStatusForAllPeers ();

public override bool ShouldConnect (Peer peer)
{
return !(peer.IsSeeder && Manager.HasMetadata && Manager.Complete);
Expand Down
5 changes: 5 additions & 0 deletions src/MonoTorrent.Client/MonoTorrent.Client.Modes/ErrorMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public ErrorMode (TorrentManager manager, ConnectionManager connectionManager)
public void Dispose ()
=> Cancellation.Cancel ();

public void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
{
// Nothing
}

public void HandleMessage (PeerId id, PeerMessage message, PeerMessage.Releaser releaser)
=> throw new NotSupportedException ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public HashingMode (TorrentManager manager, DiskManager diskManager)
PausedCompletionSource.SetResult (null);
}

public void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
{
// Nothing
}

public void Pause ()
{
if (State == TorrentState.HashingPaused)
Expand Down
1 change: 1 addition & 0 deletions src/MonoTorrent.Client/MonoTorrent.Client.Modes/IMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface IMode : IDisposable
TorrentState State { get; }
CancellationToken Token { get; }

void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority);
void HandleMessage (PeerId id, PeerMessage message, PeerMessage.Releaser releaser);
void HandlePeerConnected (PeerId id);
void HandlePeerDisconnected (PeerId id);
Expand Down
31 changes: 22 additions & 9 deletions src/MonoTorrent.Client/MonoTorrent.Client.Modes/Mode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ abstract class Mode : IMode
static readonly SHA1 AllowedFastHasher = SHA1.Create ();

bool hashingPendingFiles;

ValueStopwatch lastSendHaveMessage;
ValueStopwatch lastRefreshAllPeers;

protected CancellationTokenSource Cancellation { get; }
protected ConnectionManager ConnectionManager { get; }
Expand All @@ -80,6 +81,11 @@ protected Mode (TorrentManager manager, DiskManager diskManager, ConnectionManag
Unchoker = unchoker ?? new ChokeUnchokeManager (new TorrentManagerUnchokeable (manager));
}

public virtual void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
{

}

public void HandleMessage (PeerId id, PeerMessage message, PeerMessage.Releaser releaser)
{
if (!CanHandleMessages)
Expand Down Expand Up @@ -577,7 +583,16 @@ protected virtual void AppendBitfieldMessage (PeerId id, MessageBundle bundle)
protected void PreLogicTick (int counter)
{
SendAnnounces ();
CloseConnectionsForStalePeers ();

// The 'AmInterested' status is dependent on whether or not the set of IPiecePicker's
// associated with the TorrentManager determine if any pieces are ready to be requested.
// There's no event which will be raised each time this occurs, so just periodically
// refresh peers.
if (!lastRefreshAllPeers.IsRunning || lastRefreshAllPeers.Elapsed > TimeSpan.FromSeconds (5)) {
lastRefreshAllPeers = ValueStopwatch.StartNew ();
RefreshAmInterestedStatusForAllPeers ();
CloseConnectionsForStalePeers ();
}
Manager.Peers.UpdatePeerCounts ();

//Execute initial logic for individual peers
Expand Down Expand Up @@ -611,8 +626,6 @@ void PostLogicTick (int counter)

for (int i = 0; i < Manager.Peers.ConnectedPeers.Count; i++) {
var id = Manager.Peers.ConnectedPeers[i];
if (id.Connection == null)
continue;

if (!id.LastPeerExchangeReview.IsRunning || id.LastPeerExchangeReview.Elapsed > TimeSpan.FromMinutes (1)) {
id.PeerExchangeManager?.OnTick ();
Expand All @@ -629,8 +642,6 @@ void PostLogicTick (int counter)

for (int i = 0; i < Manager.Peers.ConnectedPeers.Count; i++) {
var id = Manager.Peers.ConnectedPeers[i];
if (id.Connection == null)
continue;

ConnectionManager.TryProcessQueue (Manager, id);

Expand Down Expand Up @@ -778,8 +789,6 @@ internal async ReusableTask TryHashPendingFilesAsync ()
}
}

ValueStopwatch lastSendHaveMessage;

void SendHaveMessagesToAll ()
{
if (Manager.finishedPieces.Count == 0 || (lastSendHaveMessage.IsRunning && lastSendHaveMessage.ElapsedMilliseconds < 5000))
Expand All @@ -799,11 +808,15 @@ void SendHaveMessagesToAll ()
peer.MessageQueue.Enqueue (bundle, releaser);
}

Manager.finishedPieces.Clear ();
}

protected void RefreshAmInterestedStatusForAllPeers ()
{
foreach (PeerId peer in Manager.Peers.ConnectedPeers) {
bool isInteresting = Manager.PieceManager.IsInteresting (peer);
SetAmInterestedStatus (peer, isInteresting);
}
Manager.finishedPieces.Clear ();
}

public void Dispose ()
Expand Down
3 changes: 3 additions & 0 deletions src/MonoTorrent.Client/MonoTorrent.Client.Modes/PausedMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public PausedMode (TorrentManager manager, DiskManager diskManager, ConnectionMa
// does not need to do anything special.
}

public override void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
=> RefreshAmInterestedStatusForAllPeers ();

public override void Tick (int counter)
{
// TODO: In future maybe this can be made smarter by refactoring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public StartingMode (TorrentManager manager, DiskManager diskManager, Connection
public void Dispose ()
=> Cancellation.Cancel ();

public void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
{
// Nothing
}

public void HandleMessage (PeerId id, PeerMessage message, PeerMessage.Releaser releaser)
=> throw new NotSupportedException ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public StoppedMode ()
public void Dispose ()
=> Cancellation.Cancel ();

public void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
{
// Nothing
}

public void HandleMessage (PeerId id, PeerMessage message, PeerMessage.Releaser releaser)
=> throw new NotSupportedException ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public StoppingMode (TorrentManager manager, DiskManager diskManager, Connection
public void Dispose ()
=> Cancellation.Dispose ();

public void HandleFilePriorityChanged (ITorrentManagerFile file, Priority oldPriority)
{
// Nothing
}

public void HandleMessage (PeerId id, PeerMessage message, PeerMessage.Releaser releaser)
=> throw new NotSupportedException ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public async Task SetFilePriorityAsync (ITorrentManagerFile file, Priority prior

// If the old priority, or new priority, is 'DoNotDownload' then the selector needs to be refreshed
bool needsToUpdateSelector = file.Priority == Priority.DoNotDownload || priority == Priority.DoNotDownload;
var oldPriority = file.Priority;
((TorrentFileInfo) file).Priority = priority;

if (needsToUpdateSelector) {
Expand All @@ -109,6 +110,8 @@ public async Task SetFilePriorityAsync (ITorrentManagerFile file, Priority prior
PartialProgressSelector.SetTrue ((f.StartPieceIndex, f.EndPieceIndex));
}
}

Mode.HandleFilePriorityChanged (file, oldPriority);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,21 @@ public async Task PartialProgress_RelatedDownloaded_FileAdded ()
Assert.AreEqual (TorrentState.Downloading, newState, "#7");
}

[Test]
public async Task SettingPriorityRefreshesAmInterestedStatus ()
{
Manager.OnPieceHashed (0, true);

var mode = new DownloadMode (Manager, DiskManager, ConnectionManager, Settings);
Manager.Mode = mode;

var peer = PeerId.CreateNull (Manager.Bitfield.Length, true, true, true, Manager.InfoHashes.V1OrV2);
Manager.Peers.ConnectedPeers.Add (peer);
foreach (var file in Manager.Files)
await Manager.SetFilePriorityAsync (file, Priority.DoNotDownload);
Assert.IsFalse (peer.AmInterested);
}

[Test]
public async Task PartialProgress_UnrelatedDownloaded_AllDoNotDownload ()
{
Expand Down