EventManager.cs 6.5 KB

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