HttpSessionController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Session;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Serialization;
  5. using MediaBrowser.Model.Session;
  6. using MediaBrowser.Model.System;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Server.Implementations.Session
  15. {
  16. public class HttpSessionController : ISessionController, IDisposable
  17. {
  18. private readonly IHttpClient _httpClient;
  19. private readonly IJsonSerializer _json;
  20. private readonly ISessionManager _sessionManager;
  21. public SessionInfo Session { get; private set; }
  22. private readonly string _postUrl;
  23. public HttpSessionController(IHttpClient httpClient,
  24. IJsonSerializer json,
  25. SessionInfo session,
  26. string postUrl, ISessionManager sessionManager)
  27. {
  28. _httpClient = httpClient;
  29. _json = json;
  30. Session = session;
  31. _postUrl = postUrl;
  32. _sessionManager = sessionManager;
  33. }
  34. public void OnActivity()
  35. {
  36. }
  37. private string PostUrl
  38. {
  39. get
  40. {
  41. return string.Format("http://{0}{1}", Session.RemoteEndPoint, _postUrl);
  42. }
  43. }
  44. public bool IsSessionActive
  45. {
  46. get
  47. {
  48. return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
  49. }
  50. }
  51. public bool SupportsMediaControl
  52. {
  53. get { return true; }
  54. }
  55. private Task SendMessage(string name, CancellationToken cancellationToken)
  56. {
  57. return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
  58. }
  59. private async Task SendMessage(string name,
  60. Dictionary<string, string> args,
  61. CancellationToken cancellationToken)
  62. {
  63. var url = PostUrl + "/" + name + ToQueryString(args);
  64. await _httpClient.Post(new HttpRequestOptions
  65. {
  66. Url = url,
  67. CancellationToken = cancellationToken
  68. }).ConfigureAwait(false);
  69. }
  70. public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  71. {
  72. return Task.FromResult(true);
  73. }
  74. public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  75. {
  76. return Task.FromResult(true);
  77. }
  78. public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
  79. {
  80. return Task.FromResult(true);
  81. }
  82. public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
  83. {
  84. var dict = new Dictionary<string, string>();
  85. dict["ItemIds"] = string.Join(",", command.ItemIds);
  86. if (command.StartPositionTicks.HasValue)
  87. {
  88. dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  89. }
  90. return SendMessage(command.PlayCommand.ToString(), dict, cancellationToken);
  91. }
  92. public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
  93. {
  94. var args = new Dictionary<string, string>();
  95. if (command.Command == PlaystateCommand.Seek)
  96. {
  97. if (!command.SeekPositionTicks.HasValue)
  98. {
  99. throw new ArgumentException("SeekPositionTicks cannot be null");
  100. }
  101. args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  102. }
  103. return SendMessage(command.Command.ToString(), args, cancellationToken);
  104. }
  105. public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
  106. {
  107. return Task.FromResult(true);
  108. }
  109. public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
  110. {
  111. return SendMessage("RestartRequired", cancellationToken);
  112. }
  113. public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
  114. {
  115. return Task.FromResult(true);
  116. }
  117. public Task SendServerShutdownNotification(CancellationToken cancellationToken)
  118. {
  119. return SendMessage("ServerShuttingDown", cancellationToken);
  120. }
  121. public Task SendServerRestartNotification(CancellationToken cancellationToken)
  122. {
  123. return SendMessage("ServerRestarting", cancellationToken);
  124. }
  125. public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
  126. {
  127. return SendMessage(command.Name, command.Arguments, cancellationToken);
  128. }
  129. public Task SendMessage<T>(string name, T data, CancellationToken cancellationToken)
  130. {
  131. // Not supported or needed right now
  132. return Task.FromResult(true);
  133. }
  134. private string ToQueryString(Dictionary<string, string> nvc)
  135. {
  136. var array = (from item in nvc
  137. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  138. .ToArray();
  139. var args = string.Join("&", array);
  140. if (string.IsNullOrEmpty(args))
  141. {
  142. return args;
  143. }
  144. return "?" + args;
  145. }
  146. public void Dispose()
  147. {
  148. }
  149. }
  150. }