Rename GroupController into Group

pull/3194/head
Ionut Andrei Oanca 4 years ago
parent 7169c0a22d
commit b7eb4da04e

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.SyncPlay;
@ -19,17 +16,17 @@ using Microsoft.Extensions.Logging;
namespace Emby.Server.Implementations.SyncPlay
{
/// <summary>
/// Class GroupController.
/// Class Group.
/// </summary>
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
public class GroupController : IGroupController, IGroupStateContext
public class Group : IGroupStateContext
{
/// <summary>
/// The logger.
/// </summary>
private readonly ILogger<GroupController> _logger;
private readonly ILogger<Group> _logger;
/// <summary>
/// The logger factory.
@ -63,13 +60,13 @@ namespace Emby.Server.Implementations.SyncPlay
private IGroupState _state;
/// <summary>
/// Initializes a new instance of the <see cref="GroupController" /> class.
/// Initializes a new instance of the <see cref="Group" /> class.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="sessionManager">The session manager.</param>
/// <param name="libraryManager">The library manager.</param>
public GroupController(
public Group(
ILoggerFactory loggerFactory,
IUserManager userManager,
ISessionManager sessionManager,
@ -79,7 +76,7 @@ namespace Emby.Server.Implementations.SyncPlay
_userManager = userManager;
_sessionManager = sessionManager;
_libraryManager = libraryManager;
_logger = loggerFactory.CreateLogger<GroupController>();
_logger = loggerFactory.CreateLogger<Group>();
_state = new IdleGroupState(loggerFactory);
}
@ -235,10 +232,18 @@ namespace Emby.Server.Implementations.SyncPlay
return !usersWithNoAccess.Any();
}
/// <inheritdoc />
/// <summary>
/// Checks if the group is empty.
/// </summary>
/// <returns><c>true</c> if the group is empty, <c>false</c> otherwise.</returns>
public bool IsGroupEmpty() => _participants.Count == 0;
/// <inheritdoc />
/// <summary>
/// Initializes the group with the session's info.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
{
GroupName = request.GroupName;
@ -273,7 +278,12 @@ namespace Emby.Server.Implementations.SyncPlay
_logger.LogInformation("Session {SessionId} created group {GroupId}.", session.Id, GroupId.ToString());
}
/// <inheritdoc />
/// <summary>
/// Adds the session to the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
{
AddSession(session);
@ -289,21 +299,12 @@ namespace Emby.Server.Implementations.SyncPlay
_logger.LogInformation("Session {SessionId} joined group {GroupId}.", session.Id, GroupId.ToString());
}
/// <inheritdoc />
public void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
{
var updateSession = NewSyncPlayGroupUpdate(GroupUpdateType.GroupJoined, GetInfo());
SendGroupUpdate(session, SyncPlayBroadcastType.CurrentSession, updateSession, cancellationToken);
var updateOthers = NewSyncPlayGroupUpdate(GroupUpdateType.UserJoined, session.UserName);
SendGroupUpdate(session, SyncPlayBroadcastType.AllExceptCurrentSession, updateOthers, cancellationToken);
_state.SessionJoined(this, _state.Type, session, cancellationToken);
_logger.LogInformation("Session {SessionId} re-joined group {GroupId}.", session.Id, GroupId.ToString());
}
/// <inheritdoc />
/// <summary>
/// Removes the session from the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void SessionLeave(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken)
{
_state.SessionLeaving(this, _state.Type, session, cancellationToken);
@ -319,7 +320,12 @@ namespace Emby.Server.Implementations.SyncPlay
_logger.LogInformation("Session {SessionId} left group {GroupId}.", session.Id, GroupId.ToString());
}
/// <inheritdoc />
/// <summary>
/// Handles the requested action by the session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The requested action.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
{
// The server's job is to maintain a consistent state for clients to reference
@ -329,14 +335,21 @@ namespace Emby.Server.Implementations.SyncPlay
request.Apply(this, _state, session, cancellationToken);
}
/// <inheritdoc />
/// <summary>
/// Gets the info about the group for the clients.
/// </summary>
/// <returns>The group info for the clients.</returns>
public GroupInfoDto GetInfo()
{
var participants = _participants.Values.Select(session => session.Session.UserName).Distinct().ToList();
return new GroupInfoDto(GroupId, GroupName, _state.Type, participants, DateTime.UtcNow);
}
/// <inheritdoc />
/// <summary>
/// Checks if a user has access to all content in the play queue.
/// </summary>
/// <param name="user">The user.</param>
/// <returns><c>true</c> if the user can access the play queue; <c>false</c> otherwise.</returns>
public bool HasAccessToPlayQueue(User user)
{
var items = PlayQueue.GetPlaylist().Select(item => item.ItemId).ToList();

@ -44,20 +44,20 @@ namespace Emby.Server.Implementations.SyncPlay
/// <summary>
/// The map between sessions and groups.
/// </summary>
private readonly Dictionary<string, IGroupController> _sessionToGroupMap =
new Dictionary<string, IGroupController>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Group> _sessionToGroupMap =
new Dictionary<string, Group>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// The groups.
/// </summary>
private readonly Dictionary<Guid, IGroupController> _groups =
new Dictionary<Guid, IGroupController>();
private readonly Dictionary<Guid, Group> _groups =
new Dictionary<Guid, Group>();
/// <summary>
/// Lock used for accessing any group.
/// </summary>
/// <remarks>
/// Always lock before <see cref="_mapsLock"/> and before locking on any <see cref="IGroupController"/>.
/// Always lock before <see cref="_mapsLock"/> and before locking on any <see cref="Group"/>.
/// </remarks>
private readonly object _groupsLock = new object();
@ -65,7 +65,7 @@ namespace Emby.Server.Implementations.SyncPlay
/// Lock used for accessing the session-to-group map.
/// </summary>
/// <remarks>
/// Always lock after <see cref="_groupsLock"/> and before locking on any <see cref="IGroupController"/>.
/// Always lock after <see cref="_groupsLock"/> and before locking on any <see cref="Group"/>.
/// </remarks>
private readonly object _mapsLock = new object();
@ -115,7 +115,7 @@ namespace Emby.Server.Implementations.SyncPlay
LeaveGroup(session, leaveGroupRequest, cancellationToken);
}
var group = new GroupController(_loggerFactory, _userManager, _sessionManager, _libraryManager);
var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager);
_groups[group.GroupId] = group;
AddSessionToGroup(session, group);
@ -132,7 +132,7 @@ namespace Emby.Server.Implementations.SyncPlay
// Locking required to access list of groups.
lock (_groupsLock)
{
_groups.TryGetValue(request.GroupId, out IGroupController group);
_groups.TryGetValue(request.GroupId, out Group group);
if (group == null)
{
@ -162,7 +162,8 @@ namespace Emby.Server.Implementations.SyncPlay
{
if (FindJoinedGroupId(session).Equals(request.GroupId))
{
group.SessionRestore(session, request, cancellationToken);
// Restore session.
group.SessionJoin(session, request, cancellationToken);
return;
}
@ -240,7 +241,7 @@ namespace Emby.Server.Implementations.SyncPlay
/// <inheritdoc />
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
{
IGroupController group;
Group group;
lock (_mapsLock)
{
group = FindJoinedGroup(session);
@ -255,7 +256,7 @@ namespace Emby.Server.Implementations.SyncPlay
return;
}
// Group lock required as GroupController is not thread-safe.
// Group lock required as Group is not thread-safe.
lock (group)
{
group.HandleRequest(session, request, cancellationToken);
@ -317,7 +318,7 @@ namespace Emby.Server.Implementations.SyncPlay
/// </remarks>
/// <param name="session">The session.</param>
/// <returns>The group.</returns>
private IGroupController FindJoinedGroup(SessionInfo session)
private Group FindJoinedGroup(SessionInfo session)
{
_sessionToGroupMap.TryGetValue(session.Id, out var group);
return group;
@ -345,7 +346,7 @@ namespace Emby.Server.Implementations.SyncPlay
/// <param name="session">The session.</param>
/// <param name="group">The group.</param>
/// <exception cref="InvalidOperationException">Thrown when the user is in another group already.</exception>
private void AddSessionToGroup(SessionInfo session, IGroupController group)
private void AddSessionToGroup(SessionInfo session, Group group)
{
if (session == null)
{
@ -369,7 +370,7 @@ namespace Emby.Server.Implementations.SyncPlay
/// <param name="session">The session.</param>
/// <param name="group">The group.</param>
/// <exception cref="InvalidOperationException">Thrown when the user is not found in the specified group.</exception>
private void RemoveSessionFromGroup(SessionInfo session, IGroupController group)
private void RemoveSessionFromGroup(SessionInfo session, Group group)
{
if (session == null)
{

@ -1,87 +0,0 @@
using System;
using System.Threading;
using Jellyfin.Data.Entities;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.SyncPlay.Queue;
using MediaBrowser.Controller.SyncPlay.Requests;
using MediaBrowser.Model.SyncPlay;
namespace MediaBrowser.Controller.SyncPlay
{
/// <summary>
/// Interface IGroupController.
/// </summary>
public interface IGroupController
{
/// <summary>
/// Gets the group identifier.
/// </summary>
/// <value>The group identifier.</value>
Guid GroupId { get; }
/// <summary>
/// Gets the play queue.
/// </summary>
/// <value>The play queue.</value>
PlayQueueManager PlayQueue { get; }
/// <summary>
/// Checks if the group is empty.
/// </summary>
/// <returns><c>true</c> if the group is empty, <c>false</c> otherwise.</returns>
bool IsGroupEmpty();
/// <summary>
/// Initializes the group with the session's info.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void CreateGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken);
/// <summary>
/// Adds the session to the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void SessionJoin(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken);
/// <summary>
/// Restores the state of a session that already joined the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void SessionRestore(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken);
/// <summary>
/// Removes the session from the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void SessionLeave(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken);
/// <summary>
/// Handles the requested action by the session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The requested action.</param>
/// <param name="cancellationToken">The cancellation token.</param>
void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken);
/// <summary>
/// Gets the info about the group for the clients.
/// </summary>
/// <returns>The group info for the clients.</returns>
GroupInfoDto GetInfo();
/// <summary>
/// Checks if a user has access to all content in the play queue.
/// </summary>
/// <param name="user">The user.</param>
/// <returns><c>true</c> if the user can access the play queue; <c>false</c> otherwise.</returns>
bool HasAccessToPlayQueue(User user);
}
}
Loading…
Cancel
Save