2013-09-24 12:54:42 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-10-02 18:22:50 -07:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-09-24 12:54:42 -07:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2013-10-02 18:22:50 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-09-24 08:42:30 -07:00
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
2013-10-02 18:22:50 -07:00
|
|
|
|
using MediaBrowser.Model.System;
|
2013-09-24 08:42:30 -07:00
|
|
|
|
using System;
|
2013-10-02 18:22:50 -07:00
|
|
|
|
using System.Collections.Generic;
|
2013-09-24 08:42:30 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.Session
|
|
|
|
|
{
|
2013-10-02 12:08:58 -07:00
|
|
|
|
public class WebSocketController : ISessionController
|
2013-09-24 08:42:30 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public SessionInfo Session { get; private set; }
|
|
|
|
|
public List<IWebSocketConnection> Sockets { get; private set; }
|
|
|
|
|
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
|
|
|
|
|
public WebSocketController(SessionInfo session, IServerApplicationHost appHost)
|
2013-09-24 08:42:30 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
Session = session;
|
|
|
|
|
_appHost = appHost;
|
|
|
|
|
Sockets = new List<IWebSocketConnection>();
|
2013-09-24 08:42:30 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public bool SupportsMediaRemoteControl
|
2013-09-24 08:42:30 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Sockets.Any(i => i.State == WebSocketState.Open);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-24 08:42:30 -07:00
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public bool IsSessionActive
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Sockets.Any(i => i.State == WebSocketState.Open);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IWebSocketConnection GetActiveSocket()
|
|
|
|
|
{
|
|
|
|
|
var socket = Sockets
|
|
|
|
|
.OrderByDescending(i => i.LastActivityDate)
|
|
|
|
|
.FirstOrDefault(i => i.State == WebSocketState.Open);
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
if (socket == null)
|
2013-09-24 08:42:30 -07:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("The requested session does not have an open web socket.");
|
|
|
|
|
}
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public Task SendSystemCommand(SystemCommand command, CancellationToken cancellationToken)
|
2013-09-24 12:54:42 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
var socket = GetActiveSocket();
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<string>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "SystemCommand",
|
|
|
|
|
Data = command.ToString()
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public Task SendMessageCommand(MessageCommand command, CancellationToken cancellationToken)
|
2013-09-24 12:54:42 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
var socket = GetActiveSocket();
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<MessageCommand>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "MessageCommand",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
|
2013-09-24 12:54:42 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
var socket = GetActiveSocket();
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<PlayRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Play",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public Task SendBrowseCommand(BrowseRequest command, CancellationToken cancellationToken)
|
2013-09-24 12:54:42 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
var socket = GetActiveSocket();
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<BrowseRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Browse",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 18:22:50 -07:00
|
|
|
|
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
|
2013-09-24 12:54:42 -07:00
|
|
|
|
{
|
2013-10-02 18:22:50 -07:00
|
|
|
|
var socket = GetActiveSocket();
|
2013-09-24 12:54:42 -07:00
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<PlaystateRequest>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Playstate",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
2013-09-24 08:42:30 -07:00
|
|
|
|
}
|
2013-10-02 18:22:50 -07:00
|
|
|
|
|
|
|
|
|
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<LibraryUpdateInfo>
|
|
|
|
|
{
|
2013-10-06 11:48:04 -07:00
|
|
|
|
MessageType = "LibraryChanged",
|
2013-10-02 18:22:50 -07:00
|
|
|
|
Data = info
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the restart required message.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-10-03 11:02:23 -07:00
|
|
|
|
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
|
2013-10-02 18:22:50 -07:00
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<SystemInfo>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "RestartRequired",
|
|
|
|
|
Data = _appHost.GetSystemInfo()
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2013-10-03 11:02:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the user data change info.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">The info.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<UserDataChangeInfo>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "UserDataChanged",
|
|
|
|
|
Data = info
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the server shutdown notification.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<string>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "ServerShuttingDown",
|
|
|
|
|
Data = string.Empty
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the server restart notification.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendServerRestartNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<string>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "ServerRestarting",
|
|
|
|
|
Data = string.Empty
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2014-03-28 12:58:18 -07:00
|
|
|
|
|
|
|
|
|
public Task SendGenericCommand(GenericCommand command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(new WebSocketMessage<GenericCommand>
|
|
|
|
|
{
|
|
|
|
|
MessageType = "Command",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2013-09-24 08:42:30 -07:00
|
|
|
|
}
|
|
|
|
|
}
|