EventManager.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Common.Extensions;
  11. using MediaBrowser.Common.Net;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Dlna.Eventing
  14. {
  15. public class EventManager : IEventManager
  16. {
  17. private readonly ConcurrentDictionary<string, EventSubscription> _subscriptions =
  18. new ConcurrentDictionary<string, EventSubscription>(StringComparer.OrdinalIgnoreCase);
  19. private readonly ILogger _logger;
  20. private readonly IHttpClient _httpClient;
  21. public EventManager(ILogger logger, IHttpClient httpClient)
  22. {
  23. _httpClient = httpClient;
  24. _logger = logger;
  25. }
  26. public EventSubscriptionResponse RenewEventSubscription(string subscriptionId, string notificationType, string requestedTimeoutString, string callbackUrl)
  27. {
  28. var subscription = GetSubscription(subscriptionId, false);
  29. if (subscription != null)
  30. {
  31. subscription.TimeoutSeconds = ParseTimeout(requestedTimeoutString) ?? 300;
  32. int timeoutSeconds = subscription.TimeoutSeconds;
  33. subscription.SubscriptionTime = DateTime.UtcNow;
  34. _logger.LogDebug(
  35. "Renewing event subscription for {0} with timeout of {1} to {2}",
  36. subscription.NotificationType,
  37. timeoutSeconds,
  38. subscription.CallbackUrl);
  39. return GetEventSubscriptionResponse(subscriptionId, requestedTimeoutString, timeoutSeconds);
  40. }
  41. return new EventSubscriptionResponse
  42. {
  43. Content = string.Empty,
  44. ContentType = "text/plain"
  45. };
  46. }
  47. public EventSubscriptionResponse CreateEventSubscription(string notificationType, string requestedTimeoutString, string callbackUrl)
  48. {
  49. var timeout = ParseTimeout(requestedTimeoutString) ?? 300;
  50. var id = "uuid:" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
  51. _logger.LogDebug("Creating event subscription for {0} with timeout of {1} to {2}",
  52. notificationType,
  53. timeout,
  54. callbackUrl);
  55. _subscriptions.TryAdd(id, new EventSubscription
  56. {
  57. Id = id,
  58. CallbackUrl = callbackUrl,
  59. SubscriptionTime = DateTime.UtcNow,
  60. TimeoutSeconds = timeout
  61. });
  62. return GetEventSubscriptionResponse(id, requestedTimeoutString, timeout);
  63. }
  64. private int? ParseTimeout(string header)
  65. {
  66. if (!string.IsNullOrEmpty(header))
  67. {
  68. // Starts with SECOND-
  69. header = header.Split('-').Last();
  70. if (int.TryParse(header, NumberStyles.Integer, _usCulture, out var val))
  71. {
  72. return val;
  73. }
  74. }
  75. return null;
  76. }
  77. public EventSubscriptionResponse CancelEventSubscription(string subscriptionId)
  78. {
  79. _logger.LogDebug("Cancelling event subscription {0}", subscriptionId);
  80. _subscriptions.TryRemove(subscriptionId, out EventSubscription sub);
  81. return new EventSubscriptionResponse
  82. {
  83. Content = string.Empty,
  84. ContentType = "text/plain"
  85. };
  86. }
  87. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  88. private EventSubscriptionResponse GetEventSubscriptionResponse(string subscriptionId, string requestedTimeoutString, int timeoutSeconds)
  89. {
  90. var response = new EventSubscriptionResponse
  91. {
  92. Content = string.Empty,
  93. ContentType = "text/plain"
  94. };
  95. response.Headers["SID"] = subscriptionId;
  96. response.Headers["TIMEOUT"] = string.IsNullOrEmpty(requestedTimeoutString) ? ("SECOND-" + timeoutSeconds.ToString(_usCulture)) : requestedTimeoutString;
  97. return response;
  98. }
  99. public EventSubscription GetSubscription(string id)
  100. {
  101. return GetSubscription(id, false);
  102. }
  103. private EventSubscription GetSubscription(string id, bool throwOnMissing)
  104. {
  105. if (!_subscriptions.TryGetValue(id, out EventSubscription e) && throwOnMissing)
  106. {
  107. throw new ResourceNotFoundException("Event with Id " + id + " not found.");
  108. }
  109. return e;
  110. }
  111. public Task TriggerEvent(string notificationType, IDictionary<string, string> stateVariables)
  112. {
  113. var subs = _subscriptions.Values
  114. .Where(i => !i.IsExpired && string.Equals(notificationType, i.NotificationType, StringComparison.OrdinalIgnoreCase))
  115. .ToList();
  116. var tasks = subs.Select(i => TriggerEvent(i, stateVariables));
  117. return Task.WhenAll(tasks);
  118. }
  119. private async Task TriggerEvent(EventSubscription subscription, IDictionary<string, string> stateVariables)
  120. {
  121. var builder = new StringBuilder();
  122. builder.Append("<?xml version=\"1.0\"?>");
  123. builder.Append("<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">");
  124. foreach (var key in stateVariables.Keys)
  125. {
  126. builder.Append("<e:property>");
  127. builder.Append("<" + key + ">");
  128. builder.Append(stateVariables[key]);
  129. builder.Append("</" + key + ">");
  130. builder.Append("</e:property>");
  131. }
  132. builder.Append("</e:propertyset>");
  133. var options = new HttpRequestOptions
  134. {
  135. RequestContent = builder.ToString(),
  136. RequestContentType = "text/xml",
  137. Url = subscription.CallbackUrl,
  138. BufferContent = false
  139. };
  140. options.RequestHeaders.Add("NT", subscription.NotificationType);
  141. options.RequestHeaders.Add("NTS", "upnp:propchange");
  142. options.RequestHeaders.Add("SID", subscription.Id);
  143. options.RequestHeaders.Add("SEQ", subscription.TriggerCount.ToString(_usCulture));
  144. try
  145. {
  146. using (await _httpClient.SendAsync(options, new HttpMethod("NOTIFY")).ConfigureAwait(false))
  147. {
  148. }
  149. }
  150. catch (OperationCanceledException)
  151. {
  152. }
  153. catch
  154. {
  155. // Already logged at lower levels
  156. }
  157. finally
  158. {
  159. subscription.IncrementTriggerCount();
  160. }
  161. }
  162. }
  163. }