From e4935b167eb0e19e05765bbc44055c697cae712c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 26 Jul 2022 09:55:27 -0500 Subject: stub EnqueueAsync --- src/slskd/Application.cs | 2 +- src/slskd/Transfers/Uploads/IUploadService.cs | 15 ++++ src/slskd/Transfers/Uploads/UploadService.cs | 124 +++++++++++++++++++++++--- 3 files changed, 129 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 249376fd..9239f4f6 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -599,7 +599,7 @@ namespace slskd { // remote clients might sometimes re-request downloads to check the status. don't try to add the download again // if it is already tracked. - if (tracker.Contains(TransferDirection.Upload, username, filename)) + if (await Transfers.Uploads.ExistsAsync(t => t.Username == username && t.Filename == filename)) { return; } diff --git a/src/slskd/Transfers/Uploads/IUploadService.cs b/src/slskd/Transfers/Uploads/IUploadService.cs index 12a2edaf..cfa980e8 100644 --- a/src/slskd/Transfers/Uploads/IUploadService.cs +++ b/src/slskd/Transfers/Uploads/IUploadService.cs @@ -37,6 +37,14 @@ namespace slskd.Transfers.Uploads /// IUploadQueue Queue { get; } + /// + /// Enqueues the requested file. + /// + /// The username of the requesting user. + /// The local filename of the requested file. + /// The operation context. + Task EnqueueAsync(string username, string filename); + /// /// Finds a single upload matching the specified . /// @@ -66,5 +74,12 @@ namespace slskd.Transfers.Uploads /// The unique identifier for the upload. /// A value indicating whether the upload was successfully cancelled. bool TryCancel(Guid id); + + /// + /// Returns a value indicating whether an upload matching the specified exists. + /// + /// The expression used to match uploads. + /// A value indicating whether an upload matching the specified expression exists. + Task ExistsAsync(Expression> expression); } } \ No newline at end of file diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index c21e107d..78542d6d 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -15,7 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // - using Microsoft.Extensions.Options; using Soulseek; @@ -23,14 +22,16 @@ namespace slskd.Transfers.Uploads { using System; using System.Collections.Concurrent; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; - using slskd.Users; using Serilog; - using System.Linq; - using System.Linq.Expressions; - using System.Collections.Generic; + using slskd.Shares; + using slskd.Users; /// /// Manages uploads. @@ -39,20 +40,20 @@ namespace slskd.Transfers.Uploads { public UploadService( IUserService userService, + ISoulseekClient soulseekClient, IOptionsMonitor optionsMonitor, + IShareService shareService, IDbContextFactory contextFactory) { + Users = userService; + Client = soulseekClient; + Shares = shareService; ContextFactory = contextFactory; Governor = new UploadGovernor(userService, optionsMonitor); Queue = new UploadQueue(userService, optionsMonitor); } - private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); - private IDbContextFactory ContextFactory { get; } - private ILogger Log { get; } = Serilog.Log.ForContext(); - - /// /// Gets the upload governor. /// @@ -63,6 +64,97 @@ namespace slskd.Transfers.Uploads /// public IUploadQueue Queue { get; init; } + private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); + private IDbContextFactory ContextFactory { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + private IShareService Shares { get; set; } + private IUserService Users { get; set; } + private ISoulseekClient Client { get; set; } + + /// + /// Enqueues the requested file. + /// + /// The username of the requesting user. + /// The local filename of the requested file. + /// The operation context. + public async Task EnqueueAsync(string username, string filename) + { + // remote clients might sometimes re-request downloads to check the status. don't try to add the download again + // if it is already tracked. + if (await ExistsAsync(t => t.Username == username && t.Filename == filename)) + { + return; + } + + string localFilename; + FileInfo fileInfo = default; + + Console.WriteLine($"[UPLOAD REQUESTED] [{username}/{filename}]"); + + try + { + localFilename = (await Shares.ResolveFilenameAsync(filename)).ToLocalOSPath(); + + fileInfo = new FileInfo(localFilename); + + if (!fileInfo.Exists) + { + throw new NotFoundException(); + } + } + catch (NotFoundException) + { + Console.WriteLine($"[UPLOAD REJECTED] File {filename} not found."); + throw new DownloadEnqueueException($"File not shared."); + } + + // todo: create record + + // create a new cancellation token source so that we can cancel the upload from the UI. + var cts = new CancellationTokenSource(); + var topts = new TransferOptions( + stateChanged: (e) => + { + // todo: update record + // todo: broadcast + tracker.AddOrUpdate(e.Transfer, cts); + + if (e.Transfer.State.HasFlag(TransferStates.Queued)) + { + Queue.Enqueue(e.Transfer.Username, e.Transfer.Filename); + } + }, + progressUpdated: (e) => + { + // todo: update record + // todo: broadcast + tracker.AddOrUpdate(e.Transfer, cts); + }, + governor: (tx, req, ct) => Governor.GetBytesAsync(tx.Username, req, ct), + reporter: (tx, att, grant, act) => Governor.ReturnBytes(tx.Username, att, grant, act), + slotAwaiter: (tx, ct) => Queue.AwaitStartAsync(tx.Username, tx.Filename), + slotReleased: (tx) => Queue.Complete(tx.Username, tx.Filename)); + + // accept all download requests, and begin the upload immediately. normally there would be an internal queue, and + // uploads would be handled separately. + _ = Task.Run(async () => + { + // users with uploads must be watched so that we can keep informed of their + // online status, privileges, and statistics. this is so that we can accurately + // determine their effective group. + if (!Users.IsWatched(username)) + { + await Users.WatchAsync(username); + } + + 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}"); + }, TaskContinuationOptions.NotOnRanToCompletion); // fire and forget + } + /// /// Finds a single upload matching the specified . /// @@ -162,5 +254,15 @@ namespace slskd.Transfers.Uploads return false; } + + /// + /// Returns a value indicating whether an upload matching the specified exists. + /// + /// The expression used to match uploads. + /// A value indicating whether an upload matching the specified expression exists. + public async Task ExistsAsync(Expression> expression) + { + return (await FindAsync(expression)) != default; + } } -} +} \ No newline at end of file -- cgit v1.2.3