diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 21 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 4 | ||||
| -rw-r--r-- | src/slskd/Transfers/TransferService.cs | 78 |
3 files changed, 87 insertions, 16 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 3e91e99e..2b6177d6 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -62,14 +62,13 @@ namespace slskd IManagedState<State> state, ISoulseekClient soulseekClient, ITransferTracker transferTracker, + ITransferService transferService, IBrowseTracker browseTracker, IConversationTracker conversationTracker, IRoomTracker roomTracker, IRoomService roomService, ISharedFileCache sharedFileCache, IPushbulletService pushbulletService, - IGovernor governor, - IUploadQueue uploadQueue, IHubContext<ApplicationHub> applicationHub, IHubContext<LogsHub> logHub) { @@ -87,13 +86,11 @@ namespace slskd SharedFileCache.StateMonitor.OnChange(state => SharedFileCacheState_OnChange(state)); TransferTracker = transferTracker; + Transfers = transferService; BrowseTracker = browseTracker; ConversationTracker = conversationTracker; Pushbullet = pushbulletService; - Governor = governor; - UploadQueue = uploadQueue; - RoomService = roomService; ApplicationHub = applicationHub; @@ -130,8 +127,6 @@ namespace slskd private IRoomService RoomService { get; set; } private IBrowseTracker BrowseTracker { get; set; } private IConversationTracker ConversationTracker { get; set; } - private IGovernor Governor { get; set; } - private IUploadQueue UploadQueue { get; set; } private ILogger Log { get; set; } = Serilog.Log.ForContext<Application>(); private ConcurrentDictionary<string, ILogger> Loggers { get; } = new ConcurrentDictionary<string, ILogger>(); private Options Options => OptionsMonitor.CurrentValue; @@ -144,6 +139,7 @@ namespace slskd private DateTime SharesRefreshStarted { get; set; } private IManagedState<State> State { get; } private ITransferTracker TransferTracker { get; set; } + private ITransferService Transfers { get; init; } private IHubContext<ApplicationHub> ApplicationHub { get; set; } private IHubContext<LogsHub> LogHub { get; set; } @@ -561,14 +557,14 @@ namespace slskd if (e.Transfer.State.HasFlag(TransferStates.Queued)) { - UploadQueue.Enqueue(e.Transfer); + Transfers.Uploads.Enqueue(e.Transfer); } }, progressUpdated: (e) => tracker.AddOrUpdate(e, cts), - governor: (tx, req, ct) => Governor.GetBytesAsync(tx, req, ct), - reporter: (tx, att, grant, act) => Governor.ReturnBytes(tx, att, grant, act), - slotAwaiter: (tx, ct) => UploadQueue.StartAsync(tx), - slotReleased: (tx) => UploadQueue.Complete(tx)); + governor: (tx, req, ct) => Transfers.Governor.GetBytesAsync(tx, req, ct), + reporter: (tx, att, grant, act) => Transfers.Governor.ReturnBytes(tx, att, grant, act), + slotAwaiter: (tx, ct) => Transfers.Uploads.AwaitStartAsync(tx), + slotReleased: (tx) => Transfers.Uploads.Complete(tx)); // accept all download requests, and begin the upload immediately. normally there would be an internal queue, and // uploads would be handled separately. @@ -576,7 +572,6 @@ namespace slskd { using var stream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read); await Client.UploadAsync(username, filename, fileInfo.FullName, options: topts, cancellationToken: cts.Token); - }).ContinueWith(t => { Console.WriteLine($"[UPLOAD FAILED] {t.Exception}"); diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index b750ab4d..b1af2482 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -254,14 +254,12 @@ namespace slskd services.AddSingleton<ISearchService, SearchService>(); services.AddSingleton<IUserService, UserService>(); services.AddSingleton<IRoomService, RoomService>(); + services.AddSingleton<ITransferService, TransferService>(); services.AddSingleton<IFTPClientFactory, FTPClientFactory>(); services.AddSingleton<IFTPService, FTPService>(); services.AddSingleton<IPushbulletService, PushbulletService>(); - - services.AddSingleton<IGovernor, Governor>(); - services.AddSingleton<IUploadQueue, UploadQueue>(); } /// <summary> diff --git a/src/slskd/Transfers/TransferService.cs b/src/slskd/Transfers/TransferService.cs new file mode 100644 index 00000000..952e5e38 --- /dev/null +++ b/src/slskd/Transfers/TransferService.cs @@ -0,0 +1,78 @@ +// <copyright file="TransferService.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> + +using Microsoft.Extensions.Options; + +namespace slskd.Transfers +{ + using slskd.Users; + + /// <summary> + /// Manages transfers. + /// </summary> + public interface ITransferService + { + /// <summary> + /// Gets the upload governor. + /// </summary> + IGovernor Governor { get; } + + /// <summary> + /// Gets the upload queue. + /// </summary> + IUploadQueue Uploads { get; } + } + + /// <summary> + /// Manages transfers. + /// </summary> + public class TransferService : ITransferService + { + /// <summary> + /// Initializes a new instance of the <see cref="TransferService"/> class. + /// </summary> + /// <param name="userService">The UserService instance to use.</param> + /// <param name="optionsMonitor">The OptionsMonitor instance to use.</param> + /// <param name="governor">The optional Governor instance to use.</param> + /// <param name="uploadQueue">The optional UploadQueue instance to use.</param> + public TransferService( + IUserService userService, + IOptionsMonitor<Options> optionsMonitor, + IGovernor governor = null, + IUploadQueue uploadQueue = null) + { + Users = userService; + OptionsMonitor = optionsMonitor; + + Governor = governor ?? new Governor(userService: Users, optionsMonitor: OptionsMonitor); + Uploads = uploadQueue ?? new UploadQueue(userService: Users, optionsMonitor: OptionsMonitor); + } + + /// <summary> + /// Gets the upload governor. + /// </summary> + public IGovernor Governor { get; init; } + + /// <summary> + /// Gets the upload queue. + /// </summary> + public IUploadQueue Uploads { get; init; } + + private IOptionsMonitor<Options> OptionsMonitor { get; init; } + private IUserService Users { get; init; } + } +}
\ No newline at end of file |
