aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-08-10 19:29:51 -0500
committerJP Dillingham <jp@dillingham.me>2024-08-10 19:29:51 -0500
commit621973f393ac0e18f854baeec1ac203bd4805ac1 (patch)
tree639aefac1370b3a7b0246a1559319ef615bedcef /src
parent49d2a78b0edc5237411f770bb8584b96c361d9c1 (diff)
downloadslskd-621973f393ac0e18f854baeec1ac203bd4805ac1.tar.gz
wip
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Core/EventBus.cs51
-rw-r--r--src/slskd/Tasks/TaskService.cs0
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs6
-rw-r--r--src/slskd/Transfers/Types/Transfer.cs4
-rw-r--r--src/slskd/Transfers/Types/Upload.cs2
5 files changed, 54 insertions, 9 deletions
diff --git a/src/slskd/Core/EventBus.cs b/src/slskd/Core/EventBus.cs
index b90e3fc2..ee4254ca 100644
--- a/src/slskd/Core/EventBus.cs
+++ b/src/slskd/Core/EventBus.cs
@@ -15,19 +15,32 @@
// along with this program. If not, see https://www.gnu.org/licenses/.
// </copyright>
+namespace slskd;
+
+using System;
+using System.Collections.Concurrent;
+using System.Linq;
using System.Threading.Tasks;
+using Serilog;
+using slskd.Transfers;
-namespace slskd;
+public record Event
+{
+}
-public enum EventType
+public record DownloadFileCompleteEvent : Event
{
- NOOP,
- DOWNLOAD_FILE_COMPLETE,
- DOWNLOAD_DIRECTORY_COMPLETE,
+ public string LocalFilename { get; init; }
+ public string RemoteFilename { get; init; }
+ public string Username { get; init; }
+ public Transfer Transfer { get; init; }
}
-public class EventData
+public record DownloadDirectoryCompleteEvent : Event
{
+ public string LocalDirectoryName { get; init; }
+ public string RemoteFilename { get; init; }
+ public string Username { get; init; }
}
/// <summary>
@@ -35,8 +48,32 @@ public class EventData
/// </summary>
public class EventBus
{
- public async Task RaiseAsync<T>(EventType type, T data)
+ private ILogger Log { get; } = Serilog.Log.ForContext<EventBus>();
+ private ConcurrentDictionary<Type, ConcurrentBag<Func<Event, Task>>> Subscriptions { get; } = new();
+
+ public async Task RaiseAsync<T>(T data)
+ where T : Event
{
+ Log.Debug("Handling {Type}: {Data}", typeof(T), data);
+
+ if (!Subscriptions.TryGetValue(typeof(T), out var callbacks))
+ {
+ Log.Debug("No subscribers for {Type}", typeof(T));
+ }
+ // fire and forget on a few levels! there's not much we can (or want to)
+ // do here, just rely on the continuation to log the error
+ _ = callbacks.Select(callback => Task.Run(() =>
+ {
+ _ = callback(data);
+ }).ContinueWith((task, obj) =>
+ {
+ Log.Error(task?.Exception, "Error handling {Type}: {Message}", typeof(T), task?.Exception?.Message);
+ }, TaskContinuationOptions.OnlyOnFaulted));
+ }
+
+ public void Subscribe<T>(Func<T, Task> callback)
+ where T : Event
+ {
}
} \ No newline at end of file
diff --git a/src/slskd/Tasks/TaskService.cs b/src/slskd/Tasks/TaskService.cs
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/src/slskd/Tasks/TaskService.cs
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 69e2d2c4..32493422 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -122,7 +122,8 @@ namespace slskd.Transfers.Downloads
IDbContextFactory<TransfersDbContext> contextFactory,
FileService fileService,
IRelayService relayService,
- IFTPService ftpClient)
+ IFTPService ftpClient,
+ EventBus eventBus)
{
Client = soulseekClient;
OptionsMonitor = optionsMonitor;
@@ -130,6 +131,7 @@ namespace slskd.Transfers.Downloads
Files = fileService;
FTP = ftpClient;
Relay = relayService;
+ Events = eventBus;
}
private ConcurrentDictionary<Guid, CancellationTokenSource> CancellationTokens { get; } = new ConcurrentDictionary<Guid, CancellationTokenSource>();
@@ -140,6 +142,7 @@ namespace slskd.Transfers.Downloads
private IRelayService Relay { get; }
private ILogger Log { get; } = Serilog.Log.ForContext<DownloadService>();
private IOptionsMonitor<Options> OptionsMonitor { get; }
+ private EventBus Events { get; }
/// <summary>
/// Adds the specified <paramref name="transfer"/>. Supersedes any existing record for the same file and username.
@@ -332,6 +335,7 @@ namespace slskd.Transfers.Downloads
}
// todo: raise DOWNLOAD_FILE_COMPLETE
+ Events.Downloads.FileComplete?.Invoke()
// todo: determine if there are any pending transfers in this same directory, and if not raise DOWNLOAD_DIRECTORY_COMPLETE
if (OptionsMonitor.CurrentValue.Integration.Ftp.Enabled)
diff --git a/src/slskd/Transfers/Types/Transfer.cs b/src/slskd/Transfers/Types/Transfer.cs
index 1797adcd..ddb3e017 100644
--- a/src/slskd/Transfers/Types/Transfer.cs
+++ b/src/slskd/Transfers/Types/Transfer.cs
@@ -29,6 +29,10 @@ namespace slskd.Transfers
public Guid Id { get; init; }
public string Username { get; init; }
public TransferDirection Direction { get; init; }
+
+ /// <summary>
+ /// Gets the remote filename.
+ /// </summary>
public string Filename { get; init; }
public long Size { get; set; }
public long StartOffset { get; init; }
diff --git a/src/slskd/Transfers/Types/Upload.cs b/src/slskd/Transfers/Types/Upload.cs
index bc2b69b1..9dbd6696 100644
--- a/src/slskd/Transfers/Types/Upload.cs
+++ b/src/slskd/Transfers/Types/Upload.cs
@@ -30,4 +30,4 @@ namespace slskd.Transfers
public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource();
public string Username { get; set; }
}
-}
+} \ No newline at end of file