EventManager.cs 7.0 KB

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