From da9bcc5fb3a9544e2f9bf9fa3ccbd03837d060ed Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Thu, 13 Aug 2020 15:47:31 -0400 Subject: Rename IEventManager to IDlnaEventManager --- Emby.Dlna/Emby.Dlna.csproj | 2 +- Emby.Dlna/Eventing/DlnaEventManager.cs | 200 +++++++++++++++++++++++ Emby.Dlna/Eventing/EventManager.cs | 200 ----------------------- Emby.Dlna/IConnectionManager.cs | 2 +- Emby.Dlna/IContentDirectory.cs | 2 +- Emby.Dlna/IDlnaEventManager.cs | 23 +++ Emby.Dlna/IEventManager.cs | 23 --- Emby.Dlna/IMediaReceiverRegistrar.cs | 2 +- Emby.Dlna/Service/BaseService.cs | 12 +- Jellyfin.Api/Controllers/DlnaServerController.cs | 8 +- 10 files changed, 237 insertions(+), 237 deletions(-) create mode 100644 Emby.Dlna/Eventing/DlnaEventManager.cs delete mode 100644 Emby.Dlna/Eventing/EventManager.cs create mode 100644 Emby.Dlna/IDlnaEventManager.cs delete mode 100644 Emby.Dlna/IEventManager.cs diff --git a/Emby.Dlna/Emby.Dlna.csproj b/Emby.Dlna/Emby.Dlna.csproj index 42a5f95c1..1eaa16f6b 100644 --- a/Emby.Dlna/Emby.Dlna.csproj +++ b/Emby.Dlna/Emby.Dlna.csproj @@ -20,7 +20,7 @@ netstandard2.1 false true - true + true diff --git a/Emby.Dlna/Eventing/DlnaEventManager.cs b/Emby.Dlna/Eventing/DlnaEventManager.cs new file mode 100644 index 000000000..782295290 --- /dev/null +++ b/Emby.Dlna/Eventing/DlnaEventManager.cs @@ -0,0 +1,200 @@ +#pragma warning disable CS1591 + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using MediaBrowser.Common.Extensions; +using MediaBrowser.Common.Net; +using Microsoft.Extensions.Logging; + +namespace Emby.Dlna.Eventing +{ + public class DlnaEventManager : IDlnaEventManager + { + private readonly ConcurrentDictionary _subscriptions = + new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + + private readonly ILogger _logger; + private readonly IHttpClient _httpClient; + + public DlnaEventManager(ILogger logger, IHttpClient httpClient) + { + _httpClient = httpClient; + _logger = logger; + } + + public EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string requestedTimeoutString, string callbackUrl) + { + var subscription = GetSubscription(subscriptionId, false); + if (subscription != null) + { + subscription.TimeoutSeconds = ParseTimeout(requestedTimeoutString) ?? 300; + int timeoutSeconds = subscription.TimeoutSeconds; + subscription.SubscriptionTime = DateTime.UtcNow; + + _logger.LogDebug( + "Renewing event subscription for {0} with timeout of {1} to {2}", + subscription.NotificationType, + timeoutSeconds, + subscription.CallbackUrl); + + return GetEventSubscriptionResponse(subscriptionId, requestedTimeoutString, timeoutSeconds); + } + + return new EventSubscriptionResponse + { + Content = string.Empty, + ContentType = "text/plain" + }; + } + + public EventSubscriptionResponse CreateEventSubscription(string notificationType, string requestedTimeoutString, string callbackUrl) + { + var timeout = ParseTimeout(requestedTimeoutString) ?? 300; + var id = "uuid:" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); + + _logger.LogDebug("Creating event subscription for {0} with timeout of {1} to {2}", + notificationType, + timeout, + callbackUrl); + + _subscriptions.TryAdd(id, new EventSubscription + { + Id = id, + CallbackUrl = callbackUrl, + SubscriptionTime = DateTime.UtcNow, + TimeoutSeconds = timeout + }); + + return GetEventSubscriptionResponse(id, requestedTimeoutString, timeout); + } + + private int? ParseTimeout(string header) + { + if (!string.IsNullOrEmpty(header)) + { + // Starts with SECOND- + header = header.Split('-').Last(); + + if (int.TryParse(header, NumberStyles.Integer, _usCulture, out var val)) + { + return val; + } + } + + return null; + } + + public EventSubscriptionResponse CancelEventSubscription(string subscriptionId) + { + _logger.LogDebug("Cancelling event subscription {0}", subscriptionId); + + _subscriptions.TryRemove(subscriptionId, out EventSubscription sub); + + return new EventSubscriptionResponse + { + Content = string.Empty, + ContentType = "text/plain" + }; + } + + private readonly CultureInfo _usCulture = new CultureInfo("en-US"); + private EventSubscriptionResponse GetEventSubscriptionResponse(string subscriptionId, string requestedTimeoutString, int timeoutSeconds) + { + var response = new EventSubscriptionResponse + { + Content = string.Empty, + ContentType = "text/plain" + }; + + response.Headers["SID"] = subscriptionId; + response.Headers["TIMEOUT"] = string.IsNullOrEmpty(requestedTimeoutString) ? ("SECOND-" + timeoutSeconds.ToString(_usCulture)) : requestedTimeoutString; + + return response; + } + + public EventSubscription GetSubscription(string id) + { + return GetSubscription(id, false); + } + + private EventSubscription GetSubscription(string id, bool throwOnMissing) + { + if (!_subscriptions.TryGetValue(id, out EventSubscription e) && throwOnMissing) + { + throw new ResourceNotFoundException("Event with Id " + id + " not found."); + } + + return e; + } + + public Task TriggerEvent(string notificationType, IDictionary stateVariables) + { + var subs = _subscriptions.Values + .Where(i => !i.IsExpired && string.Equals(notificationType, i.NotificationType, StringComparison.OrdinalIgnoreCase)) + .ToList(); + + var tasks = subs.Select(i => TriggerEvent(i, stateVariables)); + + return Task.WhenAll(tasks); + } + + private async Task TriggerEvent(EventSubscription subscription, IDictionary stateVariables) + { + var builder = new StringBuilder(); + + builder.Append(""); + builder.Append(""); + foreach (var key in stateVariables.Keys) + { + builder.Append("") + .Append('<') + .Append(key) + .Append('>') + .Append(stateVariables[key]) + .Append("') + .Append(""); + } + + builder.Append(""); + + var options = new HttpRequestOptions + { + RequestContent = builder.ToString(), + RequestContentType = "text/xml", + Url = subscription.CallbackUrl, + BufferContent = false + }; + + options.RequestHeaders.Add("NT", subscription.NotificationType); + options.RequestHeaders.Add("NTS", "upnp:propchange"); + options.RequestHeaders.Add("SID", subscription.Id); + options.RequestHeaders.Add("SEQ", subscription.TriggerCount.ToString(_usCulture)); + + try + { + using (await _httpClient.SendAsync(options, new HttpMethod("NOTIFY")).ConfigureAwait(false)) + { + } + } + catch (OperationCanceledException) + { + } + catch + { + // Already logged at lower levels + } + finally + { + subscription.IncrementTriggerCount(); + } + } + } +} diff --git a/Emby.Dlna/Eventing/EventManager.cs b/Emby.Dlna/Eventing/EventManager.cs deleted file mode 100644 index 7d02f5e96..000000000 --- a/Emby.Dlna/Eventing/EventManager.cs +++ /dev/null @@ -1,200 +0,0 @@ -#pragma warning disable CS1591 - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; -using MediaBrowser.Common.Extensions; -using MediaBrowser.Common.Net; -using Microsoft.Extensions.Logging; - -namespace Emby.Dlna.Eventing -{ - public class EventManager : IEventManager - { - private readonly ConcurrentDictionary _subscriptions = - new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); - - private readonly ILogger _logger; - private readonly IHttpClient _httpClient; - - public EventManager(ILogger logger, IHttpClient httpClient) - { - _httpClient = httpClient; - _logger = logger; - } - - public EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string requestedTimeoutString, string callbackUrl) - { - var subscription = GetSubscription(subscriptionId, false); - if (subscription != null) - { - subscription.TimeoutSeconds = ParseTimeout(requestedTimeoutString) ?? 300; - int timeoutSeconds = subscription.TimeoutSeconds; - subscription.SubscriptionTime = DateTime.UtcNow; - - _logger.LogDebug( - "Renewing event subscription for {0} with timeout of {1} to {2}", - subscription.NotificationType, - timeoutSeconds, - subscription.CallbackUrl); - - return GetEventSubscriptionResponse(subscriptionId, requestedTimeoutString, timeoutSeconds); - } - - return new EventSubscriptionResponse - { - Content = string.Empty, - ContentType = "text/plain" - }; - } - - public EventSubscriptionResponse CreateEventSubscription(string notificationType, string requestedTimeoutString, string callbackUrl) - { - var timeout = ParseTimeout(requestedTimeoutString) ?? 300; - var id = "uuid:" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); - - _logger.LogDebug("Creating event subscription for {0} with timeout of {1} to {2}", - notificationType, - timeout, - callbackUrl); - - _subscriptions.TryAdd(id, new EventSubscription - { - Id = id, - CallbackUrl = callbackUrl, - SubscriptionTime = DateTime.UtcNow, - TimeoutSeconds = timeout - }); - - return GetEventSubscriptionResponse(id, requestedTimeoutString, timeout); - } - - private int? ParseTimeout(string header) - { - if (!string.IsNullOrEmpty(header)) - { - // Starts with SECOND- - header = header.Split('-').Last(); - - if (int.TryParse(header, NumberStyles.Integer, _usCulture, out var val)) - { - return val; - } - } - - return null; - } - - public EventSubscriptionResponse CancelEventSubscription(string subscriptionId) - { - _logger.LogDebug("Cancelling event subscription {0}", subscriptionId); - - _subscriptions.TryRemove(subscriptionId, out EventSubscription sub); - - return new EventSubscriptionResponse - { - Content = string.Empty, - ContentType = "text/plain" - }; - } - - private readonly CultureInfo _usCulture = new CultureInfo("en-US"); - private EventSubscriptionResponse GetEventSubscriptionResponse(string subscriptionId, string requestedTimeoutString, int timeoutSeconds) - { - var response = new EventSubscriptionResponse - { - Content = string.Empty, - ContentType = "text/plain" - }; - - response.Headers["SID"] = subscriptionId; - response.Headers["TIMEOUT"] = string.IsNullOrEmpty(requestedTimeoutString) ? ("SECOND-" + timeoutSeconds.ToString(_usCulture)) : requestedTimeoutString; - - return response; - } - - public EventSubscription GetSubscription(string id) - { - return GetSubscription(id, false); - } - - private EventSubscription GetSubscription(string id, bool throwOnMissing) - { - if (!_subscriptions.TryGetValue(id, out EventSubscription e) && throwOnMissing) - { - throw new ResourceNotFoundException("Event with Id " + id + " not found."); - } - - return e; - } - - public Task TriggerEvent(string notificationType, IDictionary stateVariables) - { - var subs = _subscriptions.Values - .Where(i => !i.IsExpired && string.Equals(notificationType, i.NotificationType, StringComparison.OrdinalIgnoreCase)) - .ToList(); - - var tasks = subs.Select(i => TriggerEvent(i, stateVariables)); - - return Task.WhenAll(tasks); - } - - private async Task TriggerEvent(EventSubscription subscription, IDictionary stateVariables) - { - var builder = new StringBuilder(); - - builder.Append(""); - builder.Append(""); - foreach (var key in stateVariables.Keys) - { - builder.Append("") - .Append('<') - .Append(key) - .Append('>') - .Append(stateVariables[key]) - .Append("') - .Append(""); - } - - builder.Append(""); - - var options = new HttpRequestOptions - { - RequestContent = builder.ToString(), - RequestContentType = "text/xml", - Url = subscription.CallbackUrl, - BufferContent = false - }; - - options.RequestHeaders.Add("NT", subscription.NotificationType); - options.RequestHeaders.Add("NTS", "upnp:propchange"); - options.RequestHeaders.Add("SID", subscription.Id); - options.RequestHeaders.Add("SEQ", subscription.TriggerCount.ToString(_usCulture)); - - try - { - using (await _httpClient.SendAsync(options, new HttpMethod("NOTIFY")).ConfigureAwait(false)) - { - } - } - catch (OperationCanceledException) - { - } - catch - { - // Already logged at lower levels - } - finally - { - subscription.IncrementTriggerCount(); - } - } - } -} diff --git a/Emby.Dlna/IConnectionManager.cs b/Emby.Dlna/IConnectionManager.cs index 7b4a33a98..9f643a9e6 100644 --- a/Emby.Dlna/IConnectionManager.cs +++ b/Emby.Dlna/IConnectionManager.cs @@ -2,7 +2,7 @@ namespace Emby.Dlna { - public interface IConnectionManager : IEventManager, IUpnpService + public interface IConnectionManager : IDlnaEventManager, IUpnpService { } } diff --git a/Emby.Dlna/IContentDirectory.cs b/Emby.Dlna/IContentDirectory.cs index 83ef09c66..10f4d6386 100644 --- a/Emby.Dlna/IContentDirectory.cs +++ b/Emby.Dlna/IContentDirectory.cs @@ -2,7 +2,7 @@ namespace Emby.Dlna { - public interface IContentDirectory : IEventManager, IUpnpService + public interface IContentDirectory : IDlnaEventManager, IUpnpService { } } diff --git a/Emby.Dlna/IDlnaEventManager.cs b/Emby.Dlna/IDlnaEventManager.cs new file mode 100644 index 000000000..b535c3bde --- /dev/null +++ b/Emby.Dlna/IDlnaEventManager.cs @@ -0,0 +1,23 @@ +#pragma warning disable CS1591 + +namespace Emby.Dlna +{ + public interface IDlnaEventManager + { + /// + /// Cancels the event subscription. + /// + /// The subscription identifier. + EventSubscriptionResponse CancelEventSubscription(string subscriptionId); + + /// + /// Renews the event subscription. + /// + EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string requestedTimeoutString, string callbackUrl); + + /// + /// Creates the event subscription. + /// + EventSubscriptionResponse CreateEventSubscription(string notificationType, string requestedTimeoutString, string callbackUrl); + } +} diff --git a/Emby.Dlna/IEventManager.cs b/Emby.Dlna/IEventManager.cs deleted file mode 100644 index 287203389..000000000 --- a/Emby.Dlna/IEventManager.cs +++ /dev/null @@ -1,23 +0,0 @@ -#pragma warning disable CS1591 - -namespace Emby.Dlna -{ - public interface IEventManager - { - /// - /// Cancels the event subscription. - /// - /// The subscription identifier. - EventSubscriptionResponse CancelEventSubscription(string subscriptionId); - - /// - /// Renews the event subscription. - /// - EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string requestedTimeoutString, string callbackUrl); - - /// - /// Creates the event subscription. - /// - EventSubscriptionResponse CreateEventSubscription(string notificationType, string requestedTimeoutString, string callbackUrl); - } -} diff --git a/Emby.Dlna/IMediaReceiverRegistrar.cs b/Emby.Dlna/IMediaReceiverRegistrar.cs index b0376b6a9..43e934b53 100644 --- a/Emby.Dlna/IMediaReceiverRegistrar.cs +++ b/Emby.Dlna/IMediaReceiverRegistrar.cs @@ -2,7 +2,7 @@ namespace Emby.Dlna { - public interface IMediaReceiverRegistrar : IEventManager, IUpnpService + public interface IMediaReceiverRegistrar : IDlnaEventManager, IUpnpService { } } diff --git a/Emby.Dlna/Service/BaseService.cs b/Emby.Dlna/Service/BaseService.cs index 8794ec26a..2950eede2 100644 --- a/Emby.Dlna/Service/BaseService.cs +++ b/Emby.Dlna/Service/BaseService.cs @@ -6,9 +6,9 @@ using Microsoft.Extensions.Logging; namespace Emby.Dlna.Service { - public class BaseService : IEventManager + public class BaseService : IDlnaEventManager { - protected IEventManager EventManager; + protected IDlnaEventManager _dlnaEventManager; protected IHttpClient HttpClient; protected ILogger Logger; @@ -17,22 +17,22 @@ namespace Emby.Dlna.Service Logger = logger; HttpClient = httpClient; - EventManager = new EventManager(logger, HttpClient); + _dlnaEventManager = new DlnaEventManager(logger, HttpClient); } public EventSubscriptionResponse CancelEventSubscription(string subscriptionId) { - return EventManager.CancelEventSubscription(subscriptionId); + return _dlnaEventManager.CancelEventSubscription(subscriptionId); } public EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string timeoutString, string callbackUrl) { - return EventManager.RenewEventSubscription(subscriptionId, notificationType, timeoutString, callbackUrl); + return _dlnaEventManager.RenewEventSubscription(subscriptionId, notificationType, timeoutString, callbackUrl); } public EventSubscriptionResponse CreateEventSubscription(string notificationType, string timeoutString, string callbackUrl) { - return EventManager.CreateEventSubscription(notificationType, timeoutString, callbackUrl); + return _dlnaEventManager.CreateEventSubscription(notificationType, timeoutString, callbackUrl); } } } diff --git a/Jellyfin.Api/Controllers/DlnaServerController.cs b/Jellyfin.Api/Controllers/DlnaServerController.cs index ef507f2ed..d5cfcdf3f 100644 --- a/Jellyfin.Api/Controllers/DlnaServerController.cs +++ b/Jellyfin.Api/Controllers/DlnaServerController.cs @@ -230,7 +230,7 @@ namespace Jellyfin.Api.Controllers }); } - private EventSubscriptionResponse ProcessEventRequest(IEventManager eventManager) + private EventSubscriptionResponse ProcessEventRequest(IDlnaEventManager dlnaEventManager) { var subscriptionId = Request.Headers["SID"]; if (string.Equals(Request.Method, "subscribe", StringComparison.OrdinalIgnoreCase)) @@ -241,17 +241,17 @@ namespace Jellyfin.Api.Controllers if (string.IsNullOrEmpty(notificationType)) { - return eventManager.RenewEventSubscription( + return dlnaEventManager.RenewEventSubscription( subscriptionId, notificationType, timeoutString, callback); } - return eventManager.CreateEventSubscription(notificationType, timeoutString, callback); + return dlnaEventManager.CreateEventSubscription(notificationType, timeoutString, callback); } - return eventManager.CancelEventSubscription(subscriptionId); + return dlnaEventManager.CancelEventSubscription(subscriptionId); } } } -- cgit v1.2.3