using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Extensions.Json;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Session;
namespace Emby.Server.Implementations.Session;
///
/// Stands in for the websocket of a session another instance holds: messages are forwarded to that
/// instance, which writes them to the connection it owns.
///
public sealed class RemoteSessionController : ISessionController
{
private readonly IPodMessageBus _bus;
private readonly string _ownerPod;
private readonly string _sessionId;
///
/// Initializes a new instance of the class.
///
/// The cross-instance bus.
/// The instance holding the connection.
/// The session identifier.
/// Whether the owner reported the session as active.
/// Whether the owner reported the session as controllable.
public RemoteSessionController(IPodMessageBus bus, string ownerPod, string sessionId, bool isSessionActive, bool supportsMediaControl)
{
_bus = bus;
_ownerPod = ownerPod;
_sessionId = sessionId;
IsSessionActive = isSessionActive;
SupportsMediaControl = supportsMediaControl;
}
///
public bool IsSessionActive { get; }
///
public bool SupportsMediaControl { get; }
///
public Task SendMessage(SessionMessageType name, Guid messageId, T data, CancellationToken cancellationToken)
{
var routed = new RoutedSessionMessage
{
SessionId = _sessionId,
MessageType = name,
MessageId = messageId,
Data = JsonSerializer.Serialize(data, JsonDefaults.Options)
};
_bus.Publish(
_ownerPod,
new PodMessage
{
Kind = RoutedSessionMessage.Kind,
Payload = JsonSerializer.Serialize(routed, JsonDefaults.Options)
});
return Task.CompletedTask;
}
}