EventManager.cs 5.8 KB

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