jellyfin/MediaBrowser.Server.Implementations/Session/HttpSessionController.cs

210 lines
5.9 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Net;
2013-12-24 01:00:27 +01:00
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Session;
using MediaBrowser.Model.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
2013-12-24 01:00:27 +01:00
using System.Threading;
using System.Threading.Tasks;
2014-05-12 20:04:25 +02:00
namespace MediaBrowser.Server.Implementations.Session
2013-12-24 01:00:27 +01:00
{
public class HttpSessionController : ISessionController, IDisposable
2013-12-24 01:00:27 +01:00
{
private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _json;
private readonly ISessionManager _sessionManager;
2013-12-24 01:00:27 +01:00
public SessionInfo Session { get; private set; }
2014-05-12 20:04:25 +02:00
private readonly string _postUrl;
private Timer _pingTimer;
2014-05-18 21:58:42 +02:00
public HttpSessionController(IHttpClient httpClient,
IJsonSerializer json,
SessionInfo session,
string postUrl, ISessionManager sessionManager)
2013-12-24 01:00:27 +01:00
{
_httpClient = httpClient;
_json = json;
Session = session;
2014-05-12 20:04:25 +02:00
_postUrl = postUrl;
_sessionManager = sessionManager;
_pingTimer = new Timer(PingTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
ResetPingTimer();
2013-12-24 01:00:27 +01:00
}
public bool IsSessionActive
{
get
{
return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
}
}
2014-05-17 20:37:40 +02:00
public bool SupportsMediaControl
{
get { return true; }
}
private async void PingTimerCallback(object state)
2014-05-12 20:04:25 +02:00
{
try
{
await SendMessage("Ping", CancellationToken.None).ConfigureAwait(false);
}
catch
{
ReportSessionEnded();
}
}
2014-05-12 20:04:25 +02:00
private void ReportSessionEnded()
{
try
{
_sessionManager.ReportSessionEnded(Session.Id);
}
catch (Exception ex)
2014-05-12 20:04:25 +02:00
{
}
}
private void ResetPingTimer()
{
2014-05-29 16:19:12 +02:00
if (_pingTimer != null)
{
var period = TimeSpan.FromSeconds(60);
2014-05-29 16:19:12 +02:00
_pingTimer.Change(period, period);
}
2014-05-12 20:04:25 +02:00
}
2014-05-18 21:58:42 +02:00
private Task SendMessage(string name, CancellationToken cancellationToken)
{
2014-05-21 02:56:24 +02:00
return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
2014-05-18 21:58:42 +02:00
}
private async Task SendMessage(string name,
Dictionary<string, string> args,
CancellationToken cancellationToken)
2014-05-18 21:58:42 +02:00
{
2014-05-21 02:56:24 +02:00
var url = _postUrl + "/" + name + ToQueryString(args);
2014-05-18 21:58:42 +02:00
await _httpClient.Post(new HttpRequestOptions
2014-05-21 02:56:24 +02:00
{
Url = url,
CancellationToken = cancellationToken
}).ConfigureAwait(false);
ResetPingTimer();
2014-05-18 21:58:42 +02:00
}
2014-04-06 19:53:23 +02:00
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
2013-12-24 01:00:27 +01:00
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
{
2014-05-18 21:58:42 +02:00
return Task.FromResult(true);
//return SendMessage(new WebSocketMessage<PlayRequest>
//{
// MessageType = "Play",
// Data = command
2013-12-24 01:00:27 +01:00
2014-05-18 21:58:42 +02:00
//}, cancellationToken);
2013-12-24 01:00:27 +01:00
}
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
{
2014-05-18 21:58:42 +02:00
var args = new Dictionary<string, string>();
if (command.Command == PlaystateCommand.Seek)
2013-12-24 01:00:27 +01:00
{
2014-05-18 21:58:42 +02:00
}
return SendMessage(command.Command.ToString(), cancellationToken);
2013-12-24 01:00:27 +01:00
}
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
{
2014-04-06 19:53:23 +02:00
return Task.FromResult(true);
2013-12-24 01:00:27 +01:00
}
2014-05-18 21:58:42 +02:00
public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
2013-12-24 01:00:27 +01:00
{
2014-05-18 21:58:42 +02:00
return SendMessage("RestartRequired", cancellationToken);
2013-12-24 01:00:27 +01:00
}
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
{
2014-04-06 19:53:23 +02:00
return Task.FromResult(true);
2013-12-24 01:00:27 +01:00
}
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
{
2014-05-18 21:58:42 +02:00
return SendMessage("ServerShuttingDown", cancellationToken);
2013-12-24 01:00:27 +01:00
}
public Task SendServerRestartNotification(CancellationToken cancellationToken)
{
2014-05-18 21:58:42 +02:00
return SendMessage("ServerRestarting", cancellationToken);
2013-12-24 01:00:27 +01:00
}
2014-03-31 23:04:22 +02:00
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
2014-03-28 20:58:18 +01:00
{
2014-05-21 02:56:24 +02:00
return SendMessage(command.Name, command.Arguments, cancellationToken);
2014-03-28 20:58:18 +01:00
}
2014-05-18 21:58:42 +02:00
private string ToQueryString(Dictionary<string, string> nvc)
{
var array = (from item in nvc
select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
.ToArray();
2014-05-21 02:56:24 +02:00
var args = string.Join("&", array);
if (string.IsNullOrEmpty(args))
{
return args;
}
return "?" + args;
2014-05-18 21:58:42 +02:00
}
public void Dispose()
{
DisposePingTimer();
}
private void DisposePingTimer()
{
if (_pingTimer != null)
{
_pingTimer.Dispose();
_pingTimer = null;
}
}
2013-12-24 01:00:27 +01:00
}
}