diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Core/EventBus.cs | 18 | ||||
| -rw-r--r-- | src/slskd/Integrations/Shell/ShellService.cs | 42 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 8 |
3 files changed, 62 insertions, 6 deletions
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; /// </remarks> public class EventBus { + public EventBus() + { + Log.Debug("EventBus initialized"); + } + private ILogger Log { get; } = Serilog.Log.ForContext<EventBus>(); /// <summary> @@ -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. /// </remarks> - private ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<Event, Task>>> Subscriptions { get; } = new(); + private ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, Task>>> Subscriptions { get; } = new(); /// <summary> /// 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<string, Func<Event, Task>>( - new Dictionary<string, Func<Event, Task>> { [subscriber] = (Func<Event, Task>)callback }), + addValue: new ConcurrentDictionary<string, Func<object, Task>>( + new Dictionary<string, Func<object, Task>> { [subscriber] = (Func<object, Task>)callback }), updateValueFactory: (_, subscribers) => { subscribers.AddOrUpdate( key: subscriber, - addValue: (Func<Event, Task>)callback, + addValue: (Func<object, Task>)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<Event, Task>)callback; + return (Func<object, Task>)callback; }); return subscribers; }); + + Log.Debug("Subscribed {Name} to {Type}", subscriber, typeof(T)); } /// <summary> 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 file="ShellService.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.Integrations.Shell; + +using System; +using System.Threading.Tasks; +using Serilog; + +public class ShellService +{ + public ShellService(EventBus eventBus) + { + Events = eventBus; + + Events.Subscribe<DownloadFileCompleteEvent>(nameof(ShellService), HandleDownloadFileCompleteEvent); + + Log.Debug("Shell service initialized"); + } + + private ILogger Log { get; } = Serilog.Log.ForContext<ShellService>(); + 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<ShellService>(); + app.ConfigureAspDotNetPipeline(); if (OptionsAtStartup.Flags.NoStart) @@ -592,6 +596,8 @@ namespace slskd services.AddSingleton<IRelayService, RelayService>(); + services.AddSingleton<ShellService>(); + services.AddSingleton<IFTPClientFactory, FTPClientFactory>(); services.AddSingleton<IFTPService, FTPService>(); |
