diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-05-22 16:15:38 -0500 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-05-22 16:15:38 -0500 |
| commit | 5c295ee32ebc604b47866f58c44c741a22e82e08 (patch) | |
| tree | a9d1286eeb42e7602c72b9439dd96463d21ddda2 /src/slskd/Trackers | |
| parent | 33a168d67950f9e6a5fb0071ba059f6faaace479 (diff) | |
| download | slskd-5c295ee32ebc604b47866f58c44c741a22e82e08.tar.gz | |
split things into feature folders
Diffstat (limited to 'src/slskd/Trackers')
| -rw-r--r-- | src/slskd/Trackers/BrowseTracker.cs | 57 | ||||
| -rw-r--r-- | src/slskd/Trackers/ConversationTracker.cs | 63 | ||||
| -rw-r--r-- | src/slskd/Trackers/IBrowseTracker.cs | 54 | ||||
| -rw-r--r-- | src/slskd/Trackers/IConversationTracker.cs | 56 | ||||
| -rw-r--r-- | src/slskd/Trackers/IRoomTracker.cs | 76 | ||||
| -rw-r--r-- | src/slskd/Trackers/ISearchTracker.cs | 52 | ||||
| -rw-r--r-- | src/slskd/Trackers/ITransferTracker.cs | 57 | ||||
| -rw-r--r-- | src/slskd/Trackers/RoomTracker.cs | 113 | ||||
| -rw-r--r-- | src/slskd/Trackers/SearchTracker.cs | 62 | ||||
| -rw-r--r-- | src/slskd/Trackers/TransferTracker.cs | 192 |
10 files changed, 0 insertions, 782 deletions
diff --git a/src/slskd/Trackers/BrowseTracker.cs b/src/slskd/Trackers/BrowseTracker.cs deleted file mode 100644 index 9eeb7561..00000000 --- a/src/slskd/Trackers/BrowseTracker.cs +++ /dev/null @@ -1,57 +0,0 @@ -// <copyright file="BrowseTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using Soulseek; - - /// <summary> - /// Tracks browse operations. - /// </summary> - public class BrowseTracker : IBrowseTracker - { - /// <summary> - /// Tracked browse operations. - /// </summary> - public ConcurrentDictionary<string, BrowseProgressUpdatedEventArgs> Browses { get; } = new ConcurrentDictionary<string, BrowseProgressUpdatedEventArgs>(); - - /// <summary> - /// Adds or updates a tracked browse operation. - /// </summary> - /// <param name="username"></param> - /// <param name="progress"></param> - public void AddOrUpdate(string username, BrowseProgressUpdatedEventArgs progress) - => Browses.AddOrUpdate(username, progress, (user, oldprogress) => progress); - - /// <summary> - /// Removes a tracked browse operation for the specified user. - /// </summary> - /// <param name="username"></param> - public void TryRemove(string username) - => Browses.TryRemove(username, out _); - - /// <summary> - /// Gets the browse progress for the specified user. - /// </summary> - /// <param name="username"></param> - /// <param name="progress"></param> - /// <returns></returns> - public bool TryGet(string username, out BrowseProgressUpdatedEventArgs progress) - => Browses.TryGetValue(username, out progress); - } -} diff --git a/src/slskd/Trackers/ConversationTracker.cs b/src/slskd/Trackers/ConversationTracker.cs deleted file mode 100644 index b39ba0e6..00000000 --- a/src/slskd/Trackers/ConversationTracker.cs +++ /dev/null @@ -1,63 +0,0 @@ -// <copyright file="ConversationTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using System.Collections.Generic; - using slskd.Entities; - - /// <summary> - /// Tracks private message conversations. - /// </summary> - public class ConversationTracker : IConversationTracker - { - /// <summary> - /// Tracked private message conversations. - /// </summary> - public ConcurrentDictionary<string, IList<PrivateMessage>> Conversations { get; } = new ConcurrentDictionary<string, IList<PrivateMessage>>(); - - /// <summary> - /// Adds a private message conversation and appends the specified <paramref name="message"/>, or just appends the - /// message if the conversation exists. - /// </summary> - /// <param name="username"></param> - /// <param name="message"></param> - public void AddOrUpdate(string username, PrivateMessage message) - { - Conversations.AddOrUpdate(username, new List<PrivateMessage>() { message }, (_, messageList) => - { - messageList.Add(message); - return messageList; - }); - } - - /// <summary> - /// Returns the list of private messages for the specified <paramref name="username"/>, if any exist. - /// </summary> - /// <param name="username"></param> - /// <param name="messages"></param> - /// <returns></returns> - public bool TryGet(string username, out IList<PrivateMessage> messages) => Conversations.TryGetValue(username, out messages); - - /// <summary> - /// Removes a tracked private message conversation for the specified user. - /// </summary> - /// <param name="username"></param> - public void TryRemove(string username) => Conversations.TryRemove(username, out _); - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/IBrowseTracker.cs b/src/slskd/Trackers/IBrowseTracker.cs deleted file mode 100644 index f7607e2b..00000000 --- a/src/slskd/Trackers/IBrowseTracker.cs +++ /dev/null @@ -1,54 +0,0 @@ -// <copyright file="IBrowseTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using Soulseek; - - /// <summary> - /// Tracks browse operations. - /// </summary> - public interface IBrowseTracker - { - /// <summary> - /// Tracked browse operations. - /// </summary> - ConcurrentDictionary<string, BrowseProgressUpdatedEventArgs> Browses { get; } - - /// <summary> - /// Adds or updates a tracked browse operation. - /// </summary> - /// <param name="username"></param> - /// <param name="progress"></param> - void AddOrUpdate(string username, BrowseProgressUpdatedEventArgs progress); - - /// <summary> - /// Gets the browse progress for the specified user. - /// </summary> - /// <param name="username"></param> - /// <param name="progress"></param> - /// <returns></returns> - bool TryGet(string username, out BrowseProgressUpdatedEventArgs progress); - - /// <summary> - /// Removes a tracked browse operation for the specified user. - /// </summary> - /// <param name="username"></param> - void TryRemove(string username); - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/IConversationTracker.cs b/src/slskd/Trackers/IConversationTracker.cs deleted file mode 100644 index 3da20b9e..00000000 --- a/src/slskd/Trackers/IConversationTracker.cs +++ /dev/null @@ -1,56 +0,0 @@ -// <copyright file="IConversationTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using System.Collections.Generic; - using slskd.Entities; - - /// <summary> - /// Tracks private message conversations. - /// </summary> - public interface IConversationTracker - { - /// <summary> - /// Tracked private message conversations. - /// </summary> - ConcurrentDictionary<string, IList<PrivateMessage>> Conversations { get; } - - /// <summary> - /// Adds a private message conversation and appends the specified <paramref name="message"/>, or just appends the - /// message if the conversation exists. - /// </summary> - /// <param name="username"></param> - /// <param name="message"></param> - void AddOrUpdate(string username, PrivateMessage message); - - /// <summary> - /// Returns the list of private messages for the specified <paramref name="username"/>, if any exist. - /// </summary> - /// <param name="username"></param> - /// <param name="messages"></param> - /// <returns></returns> - bool TryGet(string username, out IList<PrivateMessage> messages); - - /// <summary> - /// Removes a tracked private message conversation for the specified user. - /// </summary> - /// <param name="username"></param> - void TryRemove(string username); - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/IRoomTracker.cs b/src/slskd/Trackers/IRoomTracker.cs deleted file mode 100644 index bbeb8255..00000000 --- a/src/slskd/Trackers/IRoomTracker.cs +++ /dev/null @@ -1,76 +0,0 @@ -// <copyright file="IRoomTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using slskd.Entities; - using Soulseek; - - /// <summary> - /// Tracks rooms. - /// </summary> - public interface IRoomTracker - { - /// <summary> - /// Gets tracked rooms. - /// </summary> - ConcurrentDictionary<string, Room> Rooms { get; } - - /// <summary> - /// Adds a room and appends the specified <paramref name="message"/>, or just appends the message if the room exists. - /// </summary> - /// <param name="roomName"></param> - /// <param name="message"></param> - void AddOrUpdateMessage(string roomName, RoomMessage message); - - /// <summary> - /// Adds the specified room to the tracker. - /// </summary> - /// <param name="roomName"></param> - /// <param name="room"></param> - void TryAdd(string roomName, Room room); - - /// <summary> - /// Adds the specified <paramref name="userData"/> to the specified room. - /// </summary> - /// <param name="roomName"></param> - /// <param name="userData"></param> - void TryAddUser(string roomName, UserData userData); - - /// <summary> - /// Returns the list of messages for the specified <paramref name="roomName"/>, if it is tracked. - /// </summary> - /// <param name="roomName"></param> - /// <param name="room"></param> - /// <returns></returns> - bool TryGet(string roomName, out Room room); - - /// <summary> - /// Removes a tracked room. - /// </summary> - /// <param name="roomName"></param> - void TryRemove(string roomName); - - /// <summary> - /// Removes the specified <paramref name="username"/> from the specified room. - /// </summary> - /// <param name="roomName"></param> - /// <param name="username"></param> - void TryRemoveUser(string roomName, string username); - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/ISearchTracker.cs b/src/slskd/Trackers/ISearchTracker.cs deleted file mode 100644 index 648b6179..00000000 --- a/src/slskd/Trackers/ISearchTracker.cs +++ /dev/null @@ -1,52 +0,0 @@ -// <copyright file="ISearchTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System; - using System.Collections.Concurrent; - using Soulseek; - - /// <summary> - /// Tracks active searches. - /// </summary> - public interface ISearchTracker - { - /// <summary> - /// Gets active searches. - /// </summary> - ConcurrentDictionary<Guid, Search> Searches { get; } - - /// <summary> - /// Adds or updates a tracked search. - /// </summary> - /// <param name="id"></param> - /// <param name="args"></param> - void AddOrUpdate(Guid id, SearchEventArgs args); - - /// <summary> - /// Removes all tracked searches. - /// </summary> - void Clear(); - - /// <summary> - /// Removes a tracked search. - /// </summary> - /// <param name="id"></param> - void TryRemove(Guid id); - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/ITransferTracker.cs b/src/slskd/Trackers/ITransferTracker.cs deleted file mode 100644 index b16311f8..00000000 --- a/src/slskd/Trackers/ITransferTracker.cs +++ /dev/null @@ -1,57 +0,0 @@ -// <copyright file="ITransferTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using System.Threading; - using Soulseek; - - /// <summary> - /// Tracks transfers. - /// </summary> - public interface ITransferTracker - { - /// <summary> - /// Tracked transfers. - /// </summary> - ConcurrentDictionary<TransferDirection, ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>>> Transfers { get; } - - /// <summary> - /// Adds or updates a tracked transfer. - /// </summary> - /// <param name="args"></param> - /// <param name="cancellationTokenSource"></param> - void AddOrUpdate(TransferEventArgs args, CancellationTokenSource cancellationTokenSource); - - /// <summary> - /// Removes a tracked transfer. - /// </summary> - /// <remarks>Omitting an id will remove ALL transfers associated with the specified username.</remarks> - void TryRemove(TransferDirection direction, string username, string id = null); - - /// <summary> - /// Gets the specified transfer. - /// </summary> - /// <param name="direction"></param> - /// <param name="username"></param> - /// <param name="id"></param> - /// <param name="transfer"></param> - /// <returns></returns> - bool TryGet(TransferDirection direction, string username, string id, out (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource) transfer); - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/RoomTracker.cs b/src/slskd/Trackers/RoomTracker.cs deleted file mode 100644 index d20f2444..00000000 --- a/src/slskd/Trackers/RoomTracker.cs +++ /dev/null @@ -1,113 +0,0 @@ -// <copyright file="RoomTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using System.Collections.Generic; - using System.Linq; - using slskd.Entities; - using Soulseek; - - /// <summary> - /// Tracks rooms. - /// </summary> - public class RoomTracker : IRoomTracker - { - /// <summary> - /// Initializes a new instance of the <see cref="RoomTracker"/> class. - /// </summary> - /// <param name="messageLimit"></param> - public RoomTracker(int messageLimit = 25) - { - MessageLimit = messageLimit; - } - - /// <summary> - /// Tracked rooms. - /// </summary> - public ConcurrentDictionary<string, Room> Rooms { get; } = new ConcurrentDictionary<string, Room>(); - - private int MessageLimit { get; } - - /// <summary> - /// Adds a room and appends the specified <paramref name="message"/>, or just appends the message if the room exists. - /// </summary> - /// <param name="roomName"></param> - /// <param name="message"></param> - public void AddOrUpdateMessage(string roomName, RoomMessage message) - { - Rooms.AddOrUpdate(roomName, new Room() { Messages = new List<RoomMessage>() { message } }, (_, room) => - { - if (room.Messages.Count >= MessageLimit) - { - room.Messages = room.Messages.TakeLast(MessageLimit - 1).ToList(); - } - - room.Messages.Add(message); - return room; - }); - } - - /// <summary> - /// Adds the specified room to the tracker. - /// </summary> - /// <param name="roomName"></param> - /// <param name="room"></param> - public void TryAdd(string roomName, Room room) => Rooms.TryAdd(roomName, room); - - /// <summary> - /// Adds the specified <paramref name="userData"/> to the specified room. - /// </summary> - /// <param name="roomName"></param> - /// <param name="userData"></param> - public void TryAddUser(string roomName, UserData userData) - { - if (Rooms.TryGetValue(roomName, out var room)) - { - room.Users.Add(userData); - } - } - - /// <summary> - /// Removes a tracked room. - /// </summary> - /// <param name="roomName"></param> - /// <param name="room"></param> - /// <returns></returns> - public bool TryGet(string roomName, out Room room) => Rooms.TryGetValue(roomName, out room); - - /// <summary> - /// Returns the list of messages for the specified <paramref name="roomName"/>, if it is tracked. - /// </summary> - /// <param name="roomName"></param> - public void TryRemove(string roomName) => Rooms.TryRemove(roomName, out _); - - /// <summary> - /// Removes the specified <paramref name="username"/> from the specified room. - /// </summary> - /// <param name="roomName"></param> - /// <param name="username"></param> - public void TryRemoveUser(string roomName, string username) - { - if (Rooms.TryGetValue(roomName, out var room)) - { - room.Users = room.Users.Where(u => u.Username != username).ToList(); - } - } - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/SearchTracker.cs b/src/slskd/Trackers/SearchTracker.cs deleted file mode 100644 index 35c34ae6..00000000 --- a/src/slskd/Trackers/SearchTracker.cs +++ /dev/null @@ -1,62 +0,0 @@ -// <copyright file="SearchTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System; - using System.Collections.Concurrent; - using Soulseek; - - /// <summary> - /// Tracks active searches. - /// </summary> - public class SearchTracker : ISearchTracker - { - /// <summary> - /// Gets active searches. - /// </summary> - public ConcurrentDictionary<Guid, Search> Searches { get; private set; } = - new ConcurrentDictionary<Guid, Search>(); - - /// <summary> - /// Adds or updates a tracked search. - /// </summary> - /// <param name="id"></param> - /// <param name="args"></param> - public void AddOrUpdate(Guid id, SearchEventArgs args) - { - Searches.AddOrUpdate(id, args.Search, (token, search) => args.Search); - } - - /// <summary> - /// Removes all tracked searches. - /// </summary> - public void Clear() - { - Searches.Clear(); - } - - /// <summary> - /// Removes a tracked search. - /// </summary> - /// <param name="id"></param> - public void TryRemove(Guid id) - { - Searches.TryRemove(id, out _); - } - } -}
\ No newline at end of file diff --git a/src/slskd/Trackers/TransferTracker.cs b/src/slskd/Trackers/TransferTracker.cs deleted file mode 100644 index e89cb594..00000000 --- a/src/slskd/Trackers/TransferTracker.cs +++ /dev/null @@ -1,192 +0,0 @@ -// <copyright file="TransferTracker.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Trackers -{ - using System.Collections.Concurrent; - using System.Linq; - using System.Threading; - using Soulseek; - - /// <summary> - /// Transfer extensions. - /// </summary> - public static class TransferTrackerExtensions - { - /// <summary> - /// Filters a Transfer collection by user. - /// </summary> - /// <param name="directedTransfers"></param> - /// <param name="username"></param> - /// <returns></returns> - public static ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)> FromUser( - this ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>> directedTransfers, - string username) - { - directedTransfers.TryGetValue(username, out var transfers); - return transfers ?? new ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>(); - } - - /// <summary> - /// Maps a Transfer collection to a serializable object. - /// </summary> - /// <param name="directedTransfers"></param> - /// <returns></returns> - public static object ToMap( - this ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>> 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) }), - }); - } - - /// <summary> - /// Maps a Transfer collection to a serializable object. - /// </summary> - /// <param name="userTransfers"></param> - /// <returns></returns> - public static object ToMap( - this ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)> userTransfers) - { - return userTransfers.Values - .GroupBy(f => f.Transfer.Filename.DirectoryName()) - .Select(d => new { Directory = d.Key, Files = d.Select(r => r.Transfer) }); - } - - /// <summary> - /// Filters a Transfer collection by direction. - /// </summary> - /// <param name="allTransfers"></param> - /// <param name="direction"></param> - /// <returns></returns> - public static ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>> WithDirection( - this ConcurrentDictionary<TransferDirection, ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>>> allTransfers, - TransferDirection direction) - { - allTransfers.TryGetValue(direction, out var transfers); - return transfers ?? new ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>>(); - } - - /// <summary> - /// Retrieves a Transfer from a Transfer collection by id. - /// </summary> - /// <param name="userTransfers"></param> - /// <param name="id"></param> - /// <returns></returns> - public static (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource) WithId( - this ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)> userTransfers, - string id) - { - userTransfers.TryGetValue(id, out var transfer); - return transfer; - } - } - - /// <summary> - /// Tracks transfers. - /// </summary> - public class TransferTracker : ITransferTracker - { - /// <summary> - /// Initializes a new instance of the <see cref="TransferTracker"/> class. - /// </summary> - public TransferTracker() - { - Transfers.TryAdd(TransferDirection.Download, new ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>>()); - Transfers.TryAdd(TransferDirection.Upload, new ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>>()); - } - - /// <summary> - /// Gets tracked transfers. - /// </summary> - public ConcurrentDictionary<TransferDirection, ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>>> Transfers { get; private set; } = - new ConcurrentDictionary<TransferDirection, ConcurrentDictionary<string, ConcurrentDictionary<string, (API.DTO.Transfer, CancellationTokenSource)>>>(); - - /// <summary> - /// Adds or updates a tracked transfer. - /// </summary> - /// <param name="args"></param> - /// <param name="cancellationTokenSource"></param> - 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.DTO.Transfer.FromSoulseekTransfer(args.Transfer); - dict.AddOrUpdate(transfer.Id, (transfer, cancellationTokenSource), (id, record) => (transfer, cancellationTokenSource)); - return dict; - }); - } - - /// <summary> - /// Gets the specified transfer. - /// </summary> - /// <param name="direction"></param> - /// <param name="username"></param> - /// <param name="id"></param> - /// <param name="transfer"></param> - /// <returns></returns> - public bool TryGet(TransferDirection direction, string username, string id, out (API.DTO.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; - } - - /// <summary> - /// Removes a tracked transfer. - /// </summary> - /// <remarks>Omitting an id will remove ALL transfers associated with the specified username.</remarks> - 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<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)> GetNewDictionaryForUser(TransferEventArgs args, CancellationTokenSource cancellationTokenSource) - { - var r = new ConcurrentDictionary<string, (API.DTO.Transfer Transfer, CancellationTokenSource CancellationTokenSource)>(); - var transfer = API.DTO.Transfer.FromSoulseekTransfer(args.Transfer); - r.AddOrUpdate(transfer.Id, (transfer, cancellationTokenSource), (id, record) => (transfer, record.CancellationTokenSource)); - return r; - } - } -}
\ No newline at end of file |
