From 9a4bace4c006324fc9aa3d51c48c7ed7c9529957 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Aug 2024 16:46:09 -0500 Subject: add EventBus and events --- src/slskd/Program.cs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index d47d3d91..4a6ab38b 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -553,6 +553,7 @@ namespace slskd services.AddHostedService(p => p.GetRequiredService()); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3 From c818b42bb16bf8ade77b1ad20322cf4b30b43bc5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 11 Aug 2024 17:13:55 -0500 Subject: hack shell service in --- src/slskd/Core/EventBus.cs | 18 ++++++++---- src/slskd/Integrations/Shell/ShellService.cs | 42 ++++++++++++++++++++++++++++ src/slskd/Program.cs | 8 +++++- 3 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/slskd/Integrations/Shell/ShellService.cs (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Core/EventBus.cs b/src/slskd/Core/EventBus.cs index 730fb848..9076f947 100644 --- a/src/slskd/Core/EventBus.cs +++ b/src/slskd/Core/EventBus.cs @@ -40,6 +40,11 @@ using Serilog; /// public class EventBus { + public EventBus() + { + Log.Debug("EventBus initialized"); + } + private ILogger Log { get; } = Serilog.Log.ForContext(); /// @@ -49,7 +54,7 @@ public class EventBus /// Note that the value is a dictionary to prevent multiple subscriptions from the same subscriber, and to /// support unsubscribing. /// - private ConcurrentDictionary>> Subscriptions { get; } = new(); + private ConcurrentDictionary>> Subscriptions { get; } = new(); /// /// Raises an event. @@ -64,6 +69,7 @@ public class EventBus if (!Subscriptions.TryGetValue(typeof(T), out var subscribers)) { Log.Debug("No subscribers for {Type}", typeof(T)); + return; } // we don't care about any of these tasks; contractually we are only obligated to invoke them @@ -94,13 +100,13 @@ public class EventBus Subscriptions.AddOrUpdate( key: typeof(T), - addValue: new ConcurrentDictionary>( - new Dictionary> { [subscriber] = (Func)callback }), + addValue: new ConcurrentDictionary>( + new Dictionary> { [subscriber] = (Func)callback }), updateValueFactory: (_, subscribers) => { subscribers.AddOrUpdate( key: subscriber, - addValue: (Func)callback, + addValue: (Func)callback, updateValueFactory: (_, existingSubscription) => { if (existingSubscription is not null) @@ -108,11 +114,13 @@ public class EventBus Log.Debug("Warning! {Type} subscriber {Name} attempted to create a redundant subscription. The existing subscription was overwritten.", typeof(T), subscriber); } - return (Func)callback; + return (Func)callback; }); return subscribers; }); + + Log.Debug("Subscribed {Name} to {Type}", subscriber, typeof(T)); } /// diff --git a/src/slskd/Integrations/Shell/ShellService.cs b/src/slskd/Integrations/Shell/ShellService.cs new file mode 100644 index 00000000..ddb65a53 --- /dev/null +++ b/src/slskd/Integrations/Shell/ShellService.cs @@ -0,0 +1,42 @@ +// +// 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.Integrations.Shell; + +using System; +using System.Threading.Tasks; +using Serilog; + +public class ShellService +{ + public ShellService(EventBus eventBus) + { + Events = eventBus; + + Events.Subscribe(nameof(ShellService), HandleDownloadFileCompleteEvent); + + Log.Debug("Shell service initialized"); + } + + private ILogger Log { get; } = Serilog.Log.ForContext(); + private EventBus Events { get; } + + private async Task HandleDownloadFileCompleteEvent(DownloadFileCompleteEvent data) + { + Console.WriteLine("here is where we would call an external application!"); + } +} \ No newline at end of file diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 4a6ab38b..4a2b1c51 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -39,7 +39,6 @@ namespace slskd using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; - using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -60,6 +59,8 @@ namespace slskd using slskd.Files; using slskd.Integrations.FTP; using slskd.Integrations.Pushbullet; + using slskd.Integrations.Shell; + using slskd.Messaging; using slskd.Relay; using slskd.Search; @@ -484,6 +485,9 @@ namespace slskd var app = builder.Build(); + // hack: nothing depends on ShellService but we need to initialize it + _ = app.Services.GetService(); + app.ConfigureAspDotNetPipeline(); if (OptionsAtStartup.Flags.NoStart) @@ -592,6 +596,8 @@ namespace slskd services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3 From c541bc60e56369bd2b3e20c79e24efa119f29429 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Aug 2024 17:45:59 -0500 Subject: add events db context to DI --- src/slskd/Program.cs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 4a2b1c51..985095f1 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -56,11 +56,12 @@ namespace slskd using slskd.Configuration; using slskd.Core.API; using slskd.Cryptography; + using slskd.Events; + using slskd.Files; using slskd.Integrations.FTP; using slskd.Integrations.Pushbullet; using slskd.Integrations.Shell; - using slskd.Messaging; using slskd.Relay; using slskd.Search; @@ -563,15 +564,21 @@ namespace slskd if (OptionsAtStartup.Flags.Volatile) { - services.AddDbContext($"Data Source=file:search?mode=memory;Cache=shared;Pooling=True;"); - services.AddDbContext($"Data Source=file:transfers?mode=memory;Cache=shared;Pooling=True;"); - services.AddDbContext($"Data Source=file:messaging?mode=memory;Cache=shared;Pooling=True;"); + static string MemoryConnectionString(string name) => $"Data Source=file:{name}?mode=memory;Cache=shared;Pooling=True;"; + + services.AddDbContext(MemoryConnectionString("search")); + services.AddDbContext(MemoryConnectionString("transfers")); + services.AddDbContext(MemoryConnectionString("messaging")); + services.AddDbContext(MemoryConnectionString("events")); } else { - services.AddDbContext($"Data Source={Path.Combine(DataDirectory, "search.db")};Cache=shared;Pooling=True;"); - services.AddDbContext($"Data Source={Path.Combine(DataDirectory, "transfers.db")};Cache=shared;Pooling=True;"); - services.AddDbContext($"Data Source={Path.Combine(DataDirectory, "messaging.db")};Cache=shared;Pooling=True;"); + static string DiskConnectionString(string name) => $"Data Source={Path.Combine(DataDirectory, $"{name}.db")};Cache=shared;Pooling=True;"; + + services.AddDbContext(DiskConnectionString("search")); + services.AddDbContext(DiskConnectionString("transfers")); + services.AddDbContext(DiskConnectionString("messaging")); + services.AddDbContext(DiskConnectionString("events")); } services.AddSingleton(); -- cgit v1.2.3 From 5296a22f737fb55770835f0e45f9a74b7d96171b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 2 Sep 2024 18:24:41 -0500 Subject: add EventService to DI --- src/slskd/Program.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 985095f1..93734a35 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -558,7 +558,6 @@ namespace slskd services.AddHostedService(p => p.GetRequiredService()); services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); @@ -581,6 +580,9 @@ namespace slskd services.AddDbContext(DiskConnectionString("events")); } + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(_ => new RoomTracker(messageLimit: 250)); -- cgit v1.2.3 From a01994b6c6dd2a798cf2fe23706dacb894c807c1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 22 Sep 2024 16:54:42 -0500 Subject: remove (temporarily) ShellService --- src/slskd/Integrations/Shell/ShellService.cs | 49 ---------------------------- src/slskd/Program.cs | 7 ---- 2 files changed, 56 deletions(-) delete mode 100644 src/slskd/Integrations/Shell/ShellService.cs (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Integrations/Shell/ShellService.cs b/src/slskd/Integrations/Shell/ShellService.cs deleted file mode 100644 index baed28f8..00000000 --- a/src/slskd/Integrations/Shell/ShellService.cs +++ /dev/null @@ -1,49 +0,0 @@ -// -// 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.Integrations.Shell; - -using System; -using System.Threading.Tasks; -using Serilog; -using slskd.Events; - -public class ShellService -{ - public ShellService(EventBus eventBus) - { - Events = eventBus; - - Events.Subscribe(nameof(ShellService), HandleDownloadFileCompleteEvent); - Events.Subscribe(nameof(ShellService), HandleDownloadDirectoryCompleteEvent); - - Log.Debug("Shell service initialized"); - } - - private ILogger Log { get; } = Serilog.Log.ForContext(); - private EventBus Events { get; } - - private async Task HandleDownloadFileCompleteEvent(DownloadFileCompleteEvent data) - { - Console.WriteLine("here is where we would call an external application!"); - } - - private async Task HandleDownloadDirectoryCompleteEvent(DownloadDirectoryCompleteEvent data) - { - Console.WriteLine("wow we downloaded a whole directory"); - } -} \ No newline at end of file diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 93734a35..2d62efd8 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -57,11 +57,9 @@ namespace slskd using slskd.Core.API; using slskd.Cryptography; using slskd.Events; - using slskd.Files; using slskd.Integrations.FTP; using slskd.Integrations.Pushbullet; - using slskd.Integrations.Shell; using slskd.Messaging; using slskd.Relay; using slskd.Search; @@ -486,9 +484,6 @@ namespace slskd var app = builder.Build(); - // hack: nothing depends on ShellService but we need to initialize it - _ = app.Services.GetService(); - app.ConfigureAspDotNetPipeline(); if (OptionsAtStartup.Flags.NoStart) @@ -605,8 +600,6 @@ namespace slskd services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); services.AddSingleton(); -- cgit v1.2.3 From c0e02f9806ed95dd43bbb7bff74d6abe5e66109c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Oct 2024 16:55:58 -0500 Subject: expose X-Total-Count --- src/slskd/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Program.cs') diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 2d62efd8..6392385f 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -640,7 +640,7 @@ namespace slskd .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() - .WithExposedHeaders("X-URL-Base"))); + .WithExposedHeaders("X-URL-Base", "X-Total-Count"))); services.AddSystemMetrics(); using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); -- cgit v1.2.3