Refactoring controllers to use publicly accessible CursorLists

This commit is contained in:
Brandon Watson 2021-04-12 21:54:21 -04:00
parent 4acf091511
commit 46173f4f99
6 changed files with 47 additions and 49 deletions

View File

@ -1,7 +0,0 @@
namespace Aurora.Services.Signal
{
public partial class SignalService : Signal.SignalBase
{
}
}

View File

@ -105,13 +105,14 @@ message LeavePartyResponse {
message Member { message Member {
//Resource name of the party member to be returned (Added by server) //Resource name of the party member to be returned (Added by server)
string name = 1; string name = 1;
string userName = 2; string id = 2;
string userName = 3;
//Added by server //Added by server
string ipAddress = 3; string ipAddress = 4;
//Added by server //Added by server
google.protobuf.Timestamp addedOn = 4; google.protobuf.Timestamp addedOn = 5;
} }
message ListMembersRequest { message ListMembersRequest {

View File

@ -1,11 +1,12 @@
using System; using System;
using Aurora.Cursor;
namespace Aurora.Proto.Party namespace Aurora.Proto.Party
{ {
/// <summary> /// <summary>
/// Partial PartyMember class with a constructor that generates a new id /// Partial PartyMember class with a constructor that generates a new id
/// </summary> /// </summary>
public partial class Member public partial class Member : ICursorObject
{ {
public Member(string id) public Member(string id)
{ {

View File

@ -7,6 +7,7 @@ using Aurora.Services.Settings;
using Aurora.Models.Media; using Aurora.Models.Media;
using Aurora.Services.EventManager; using Aurora.Services.EventManager;
using Aurora.Utils; using Aurora.Utils;
using Aurora.Cursor;
namespace Aurora.Services.Controllers namespace Aurora.Services.Controllers
{ {
@ -24,7 +25,7 @@ namespace Aurora.Services.Controllers
this._startDateTime = DateTime.UtcNow; this._startDateTime = DateTime.UtcNow;
this._displayName = partyName; this._displayName = partyName;
this._description = description; this._description = description;
this._memberList = new SortedList<string, Member>(); this._memberList = new CursorList<Member>();
this._mediaList = new SortedList<string, Media>(); this._mediaList = new SortedList<string, Media>();
_libraryService = libraryService; _libraryService = libraryService;

View File

@ -6,40 +6,50 @@ using Aurora.Proto.General;
using Aurora.Utils; using Aurora.Utils;
using Grpc.Core; using Grpc.Core;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
using Aurora.Cursor;
namespace Aurora.Services.Controllers namespace Aurora.Services.Controllers
{ {
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{ {
private SortedList<string, Member> _memberList; private CursorList<Member> _memberList;
public CursorList<Member> MemberList
{
get
{
return this._memberList;
}
set
{
if(this._memberList != value)
{
_memberList = value;
}
}
}
public override Task<ListMembersResponse> ListMembers(ListMembersRequest request, Grpc.Core.ServerCallContext context) public override Task<ListMembersResponse> ListMembers(ListMembersRequest request, Grpc.Core.ServerCallContext context)
{ {
Cursor<Member> cursor = new Cursor<Member>(ref this._memberList);
Aurora.Cursor.SortDirection direction = Aurora.Cursor.SortDirection.Asc;
CursorResult<Member> res = cursor
.WithNextPageToken(request.PageToken)
.WithSize(request.PageSize)
.GetNextPage();
//Ignoring parent field because there is only one instance of the party //Ignoring parent field because there is only one instance of the party
ListMembersResponse resp = new ListMembersResponse(); ListMembersResponse resp = new ListMembersResponse();
//Determine start idx resp.Members.AddRange(res.Result.ConvertAll(member => new Member()
int startIdx = 0;
if (!string.IsNullOrEmpty(request.PageToken))
{ {
startIdx = _memberList.IndexOfKey(request.PageToken) + 1; Name = member.Name,
} UserName = member.UserName,
IpAddress = member.IpAddress,
int pageSize = request.PageSize; AddedOn = member.AddedOn
}));
//Assign pageSize
if (pageSize > _memberList.Count)
{
pageSize = _memberList.Count;
}
//Gather page
List<Member> members = new List<Member>(_memberList.Values);
resp.Members.AddRange(members.GetRange(startIdx, pageSize));
//Set next page token
resp.NextPageToken = resp.Members[resp.Members.Count - 1].Name;
return Task.FromResult(resp); return Task.FromResult(resp);
} }
@ -76,7 +86,7 @@ namespace Aurora.Services.Controllers
request.Member.AddedOn = Timestamp.FromDateTime(DateTime.UtcNow); request.Member.AddedOn = Timestamp.FromDateTime(DateTime.UtcNow);
request.Member.IpAddress = context.Host; request.Member.IpAddress = context.Host;
_memberList.Add(resourceName, request.Member); _memberList.Add(request.Member);
BaseEvent @event = new BaseEvent BaseEvent @event = new BaseEvent
{ {

View File

@ -22,7 +22,7 @@ namespace Aurora.Design.Views.Party
private ILibraryService _libraryService; private ILibraryService _libraryService;
private Grpc.Core.Server _server; private Grpc.Core.Server _server;
private RemotePartyService.RemotePartyServiceClient _remotePartyClient; private RemotePartyController _remotePartyController;
private Channel _channel; private Channel _channel;
private int _port = 8080; private int _port = 8080;
private bool _isServerStarted = false; private bool _isServerStarted = false;
@ -48,7 +48,9 @@ namespace Aurora.Design.Views.Party
this._hostname = IpUtil.GetLocalIPAddress(); this._hostname = IpUtil.GetLocalIPAddress();
this.StartServer("test", "test description"); this.StartServer("test", "test description");
this.StartClient();
// TODO assign members
// TODO assign songList
// Register commands // Register commands
PlayCommand = new Command(OnDoubleClickCommandExecute, CanDoubleClickCommandExecute); PlayCommand = new Command(OnDoubleClickCommandExecute, CanDoubleClickCommandExecute);
@ -84,7 +86,6 @@ namespace Aurora.Design.Views.Party
this._hostname = IpUtil.GetLocalIPAddress(); this._hostname = IpUtil.GetLocalIPAddress();
this.StartServer("test", "test description"); this.StartServer("test", "test description");
this.StartClient();
} }
return Task.FromResult<object>(null); return Task.FromResult<object>(null);
} }
@ -198,7 +199,7 @@ namespace Aurora.Design.Views.Party
}; };
//Construct implementations //Construct implementations
RemotePartyController remotePartyController = new RemotePartyController( this._remotePartyController = new RemotePartyController(
partyName, partyName,
description, description,
this._libraryService, this._libraryService,
@ -206,7 +207,7 @@ namespace Aurora.Design.Views.Party
_eventManager); _eventManager);
// Register grpc RemoteService with singleton server service // Register grpc RemoteService with singleton server service
RemotePartyService.BindService(remotePartyController); RemotePartyService.BindService(this._remotePartyController);
_server.Start(); _server.Start();
@ -218,15 +219,6 @@ namespace Aurora.Design.Views.Party
Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message)); Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
} }
} }
private void StartClient()
{
_channel = new Channel(string.Format("{0}:{1}", this._hostname, this._port), ChannelCredentials.Insecure);
_remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel);
}
#endregion Private Methods #endregion Private Methods
} }