From d253bc167961a8d46acaef52d5e5184f03f28dd4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 24 Aug 2024 17:46:47 -0500 Subject: move out of core --- src/slskd/Core/EventBus.cs | 141 ----------------------------------------- src/slskd/Core/Types/Events.cs | 39 ------------ 2 files changed, 180 deletions(-) delete mode 100644 src/slskd/Core/EventBus.cs delete mode 100644 src/slskd/Core/Types/Events.cs (limited to 'src') diff --git a/src/slskd/Core/EventBus.cs b/src/slskd/Core/EventBus.cs deleted file mode 100644 index c0952ed8..00000000 --- a/src/slskd/Core/EventBus.cs +++ /dev/null @@ -1,141 +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; - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Serilog; - -/// -/// The event bus supporting interoperability and ancillary functions. -/// -/// -/// -/// This "bus" is meant to mimic an event bus you'd find in a distributed system; fire and forget. If an -/// Exception is encountered while an event is being raised, it will be logged and swallowed. -/// -/// -/// It is also intended to be used *ONLY* for ancillary and/or third party logic that isn't part of the -/// core application. The core application should continue to use regular old C# events and method calls -/// to communicate within and among modules. -/// -/// -public class EventBus -{ - public EventBus() - { - Log.Debug("EventBus initialized"); - } - - private ILogger Log { get; } = Serilog.Log.ForContext(); - - /// - /// Gets the internal list of event subscriptions. - /// - /// - /// Note that the value is a dictionary to prevent multiple subscriptions from the same subscriber, and to - /// support unsubscribing. - /// - private ConcurrentDictionary> Subscriptions { get; } = new(); - - /// - /// Raises an event. - /// - /// The event data. - /// The Type of the event. - public virtual void Raise(T data) - where T : Event - { - Log.Debug("Handling {Type}: {Data}", typeof(T), data); - - if (!Subscriptions.TryGetValue(typeof(T), out var subscribers)) - { - Log.Debug("No subscribers for {Type}", typeof(T)); - return; - } - - Log.Debug("{Count} subscriber(s) for {Type}: {Names}", subscribers.Count, typeof(T), string.Join(", ", subscribers.Keys)); - - // we don't care about any of these tasks; contractually we are only obligated to invoke them - _ = Task.WhenAll(subscribers.Select(subscriber => - Task.Run(() => (subscriber.Value as Func)(data)) - .ContinueWith(task => Log.Error(task.Exception, "Subscriber {Name} for {Type} encountered an error: {Message}", subscriber.Key, typeof(T), task.Exception.Message)))); - } - - /// - /// Subscribes a 's to an event. - /// - /// The unique name of the subscriber. - /// The callback function to execute when an event is raised. - /// The Type of the event. - /// Thrown if the specified is null or whitespace. - /// Thrown if the specified is null. - public virtual void Subscribe(string subscriber, Func callback) - where T : Event - { - ArgumentException.ThrowIfNullOrWhiteSpace(subscriber); - - if (callback is null) - { - throw new ArgumentNullException(nameof(callback)); - } - - Subscriptions.AddOrUpdate( - key: typeof(T), - addValue: new ConcurrentDictionary( - new Dictionary { [subscriber] = callback }), - updateValueFactory: (_, subscribers) => - { - subscribers.AddOrUpdate( - key: subscriber, - addValue: callback, - updateValueFactory: (_, existingSubscription) => - { - if (existingSubscription is not null) - { - Log.Debug("Warning! {Type} subscriber {Name} attempted to create a redundant subscription. The existing subscription was overwritten.", typeof(T), subscriber); - } - - return callback; - }); - - return subscribers; - }); - - Log.Debug("Subscribed {Name} to {Type}", subscriber, typeof(T)); - } - - /// - /// Unsubscribes a from an event. - /// - /// - /// Will not throw if a subscription doesn't exist. - /// - /// The Type of the event. - /// The unique name of the subscriber. - public virtual void Unsubscribe(string subscriber) - { - if (Subscriptions.TryGetValue(typeof(T), out var subscriptions)) - { - subscriptions.TryRemove(subscriber, out _); - } - } -} \ No newline at end of file diff --git a/src/slskd/Core/Types/Events.cs b/src/slskd/Core/Types/Events.cs deleted file mode 100644 index cad97a11..00000000 --- a/src/slskd/Core/Types/Events.cs +++ /dev/null @@ -1,39 +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; - -using slskd.Transfers; - -public abstract record Event -{ -} - -public sealed record DownloadFileCompleteEvent : Event -{ - public string LocalFilename { get; init; } - public string RemoteFilename { get; init; } - public string Username { get; init; } - public Transfer Transfer { get; init; } -} - -public sealed record DownloadDirectoryCompleteEvent : Event -{ - public string LocalDirectoryName { get; init; } - public string RemoteFilename { get; init; } - public string Username { get; init; } -} \ No newline at end of file -- cgit v1.2.3