//
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see https://www.gnu.org/licenses/.
//
namespace slskd.Transfer
{
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using Soulseek;
///
/// Transfer extensions.
///
public static class TransferTrackerExtensions
{
///
/// Filters a Transfer collection by user.
///
///
///
///
public static ConcurrentDictionary FromUser(
this ConcurrentDictionary> directedTransfers,
string username)
{
directedTransfers.TryGetValue(username, out var transfers);
return transfers ?? new ConcurrentDictionary();
}
///
/// Maps a Transfer collection to a serializable object.
///
///
///
public static object ToMap(
this ConcurrentDictionary> directedTransfers)
{
return directedTransfers.Select(u => new
{
Username = u.Key,
Directories = u.Value.Values
.GroupBy(f => f.Transfer.Filename.DirectoryName())
.Select(d => new { Directory = d.Key, Files = d.Select(r => r.Transfer) }),
});
}
///
/// Maps a Transfer collection to a serializable object.
///
///
///
public static object ToMap(
this ConcurrentDictionary userTransfers)
{
return userTransfers.Values
.GroupBy(f => f.Transfer.Filename.DirectoryName())
.Select(d => new { Directory = d.Key, Files = d.Select(r => r.Transfer) });
}
///
/// Filters a Transfer collection by direction.
///
///
///
///
public static ConcurrentDictionary> WithDirection(
this ConcurrentDictionary>> allTransfers,
TransferDirection direction)
{
allTransfers.TryGetValue(direction, out var transfers);
return transfers ?? new ConcurrentDictionary>();
}
///
/// Retrieves a Transfer from a Transfer collection by id.
///
///
///
///
public static (API.Transfer Transfer, CancellationTokenSource CancellationTokenSource) WithId(
this ConcurrentDictionary userTransfers,
string id)
{
userTransfers.TryGetValue(id, out var transfer);
return transfer;
}
}
///
/// Tracks transfers.
///
public class TransferTracker : ITransferTracker
{
///
/// Initializes a new instance of the class.
///
public TransferTracker()
{
Transfers.TryAdd(TransferDirection.Download, new ConcurrentDictionary>());
Transfers.TryAdd(TransferDirection.Upload, new ConcurrentDictionary>());
}
///
/// Gets tracked transfers.
///
public ConcurrentDictionary>> Transfers { get; private set; } =
new ConcurrentDictionary>>();
///
/// Adds or updates a tracked transfer.
///
///
///
public void AddOrUpdate(TransferEventArgs args, CancellationTokenSource cancellationTokenSource)
{
Transfers.TryGetValue(args.Transfer.Direction, out var direction);
direction.AddOrUpdate(args.Transfer.Username, GetNewDictionaryForUser(args, cancellationTokenSource), (user, dict) =>
{
var transfer = API.Transfer.FromSoulseekTransfer(args.Transfer);
dict.AddOrUpdate(transfer.Id, (transfer, cancellationTokenSource), (id, record) => (transfer, cancellationTokenSource));
return dict;
});
}
///
/// Gets the specified transfer.
///
///
///
///
///
///
public bool TryGet(TransferDirection direction, string username, string id, out (API.Transfer Transfer, CancellationTokenSource CancellationTokenSource) transfer)
{
transfer = default;
if (Transfers.TryGetValue(direction, out var transfers) && transfers.TryGetValue(username, out var user) && user.TryGetValue(id, out transfer))
{
return true;
}
return false;
}
///
/// Removes a tracked transfer.
///
/// Omitting an id will remove ALL transfers associated with the specified username.
public void TryRemove(TransferDirection direction, string username, string id = null)
{
Transfers.TryGetValue(direction, out var directionDict);
if (string.IsNullOrEmpty(id))
{
directionDict.TryRemove(username, out _);
}
else
{
directionDict.TryGetValue(username, out var userDict);
userDict.TryRemove(id, out _);
if (userDict.IsEmpty)
{
directionDict.TryRemove(username, out _);
}
}
}
private ConcurrentDictionary GetNewDictionaryForUser(TransferEventArgs args, CancellationTokenSource cancellationTokenSource)
{
var r = new ConcurrentDictionary();
var transfer = API.Transfer.FromSoulseekTransfer(args.Transfer);
r.AddOrUpdate(transfer.Id, (transfer, cancellationTokenSource), (id, record) => (transfer, record.CancellationTokenSource));
return r;
}
}
}