2019-06-03 14:57:05 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
2019-07-05 21:37:10 +00:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
2019-07-07 21:12:13 +00:00
|
|
|
using Aurora.Proto.Party;
|
|
|
|
using Aurora.Proto.Events;
|
2019-11-04 04:17:34 +00:00
|
|
|
using Aurora.Proto.General;
|
2019-07-15 16:14:38 +00:00
|
|
|
using Aurora.Services.EventManager;
|
2019-11-03 21:04:05 +00:00
|
|
|
using Aurora.Services;
|
|
|
|
using Aurora.Models.Media;
|
|
|
|
using System.Collections;
|
2019-06-03 14:57:05 +00:00
|
|
|
|
2019-07-05 18:17:09 +00:00
|
|
|
namespace Aurora.RemoteImpl
|
2019-06-03 14:57:05 +00:00
|
|
|
{
|
2019-07-05 15:37:44 +00:00
|
|
|
public class RemotePartyServiceImpl : RemotePartyService.RemotePartyServiceBase
|
2019-06-03 14:57:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Dictionary of party members. Key -> ClientId
|
|
|
|
/// </summary>
|
2019-07-05 21:37:10 +00:00
|
|
|
private ObservableCollection<PartyMember> _partyMembers;
|
2019-06-03 14:57:05 +00:00
|
|
|
|
2019-07-05 15:37:44 +00:00
|
|
|
public RemotePartyServiceImpl()
|
2019-06-03 14:57:05 +00:00
|
|
|
{
|
2019-07-05 21:37:10 +00:00
|
|
|
_partyMembers = new ObservableCollection<PartyMember>();
|
2019-06-03 14:57:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 21:37:10 +00:00
|
|
|
public ObservableCollection<PartyMember> PartyMembers
|
2019-06-03 14:57:05 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _partyMembers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task<JoinPartyResponse> JoinParty(JoinPartyRequest request, Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
2019-07-12 15:34:06 +00:00
|
|
|
PartyMember partyMember = new PartyMember("")
|
2019-06-03 14:57:05 +00:00
|
|
|
{
|
2019-07-06 19:52:28 +00:00
|
|
|
UserName = request.UserName,
|
|
|
|
IpAddress = context.Host,
|
2019-07-07 21:12:13 +00:00
|
|
|
};
|
|
|
|
|
2019-07-15 16:14:38 +00:00
|
|
|
Console.WriteLine("SERVER - Client joined party: " + partyMember.Id);
|
|
|
|
|
2019-07-07 21:12:13 +00:00
|
|
|
_partyMembers.Add(partyMember);
|
|
|
|
|
|
|
|
BaseEvent e = new BaseEvent
|
|
|
|
{
|
|
|
|
EventType = EventType.PartyMemberJoined,
|
|
|
|
PartyMemberJoinedEvent = new PartyMemberJoinedEvent
|
|
|
|
{
|
|
|
|
Member = partyMember,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-15 16:14:38 +00:00
|
|
|
EventManager.Instance.FireEvent(e);
|
2019-06-03 14:57:05 +00:00
|
|
|
|
2019-07-12 15:34:06 +00:00
|
|
|
JoinPartyResponse response = new JoinPartyResponse() { Status = PartyJoinedStatusEnum.Connected, ClientId = partyMember.Id };
|
2019-06-03 14:57:05 +00:00
|
|
|
return Task.FromResult(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override Task<LeavePartyResponse> LeaveParty(LeavePartyRequest request, Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
2019-07-07 21:12:13 +00:00
|
|
|
PartyMember partyMember = _partyMembers.Where(e => e.Id == request.ClientId).Single();
|
|
|
|
|
|
|
|
_partyMembers.Remove(partyMember);
|
|
|
|
|
|
|
|
BaseEvent bv = new BaseEvent
|
|
|
|
{
|
|
|
|
EventType = EventType.PartyMemberJoined,
|
|
|
|
PartyMemberLeftEvent = new PartyMemberLeftEvent
|
|
|
|
{
|
|
|
|
Member = partyMember,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-15 16:14:38 +00:00
|
|
|
EventManager.Instance.FireEvent(bv);
|
2019-07-07 21:12:13 +00:00
|
|
|
|
2019-06-03 14:57:05 +00:00
|
|
|
LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected };
|
|
|
|
return Task.FromResult(response);
|
|
|
|
}
|
2019-07-06 19:52:28 +00:00
|
|
|
|
2019-07-07 21:12:13 +00:00
|
|
|
public override Task<MembersResponse> GetPartyMembers(Proto.General.Empty empty, Grpc.Core.ServerCallContext context)
|
2019-07-06 19:52:28 +00:00
|
|
|
{
|
|
|
|
MembersResponse response = new MembersResponse();
|
|
|
|
response.Members.AddRange(_partyMembers);
|
|
|
|
return Task.FromResult(response);
|
|
|
|
}
|
2019-11-03 21:04:05 +00:00
|
|
|
|
|
|
|
public override Task<QueueResponse> GetQueue(Proto.General.Empty empty, Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
|
|
|
//This will change as queuing operation gets solidified
|
|
|
|
//Simply return the hosts library
|
|
|
|
|
|
|
|
ObservableCollection<BaseMedia> queue = LibraryService.Instance.GetLibrary();
|
|
|
|
|
|
|
|
QueueResponse mediaList = new QueueResponse();
|
|
|
|
foreach (BaseMedia media in queue)
|
|
|
|
{
|
|
|
|
AudioMetadata metadata = new AudioMetadata();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (media.Metadata is AudioMetadata)
|
|
|
|
{
|
|
|
|
metadata = media.Metadata as AudioMetadata;
|
|
|
|
|
|
|
|
RemoteMediaData data = new RemoteMediaData();
|
2019-11-04 04:17:34 +00:00
|
|
|
data.Id = media.Id;
|
|
|
|
if (metadata.Title != null)
|
|
|
|
{
|
|
|
|
data.Title = metadata.Title;
|
|
|
|
}
|
2019-11-03 21:04:05 +00:00
|
|
|
if (metadata.Artist != null)
|
|
|
|
{
|
|
|
|
data.Artist = metadata.Artist;
|
|
|
|
}
|
|
|
|
if (metadata.Album != null)
|
|
|
|
{
|
|
|
|
data.Album = metadata.Album;
|
|
|
|
}
|
|
|
|
if (metadata.Duration != null)
|
|
|
|
{
|
|
|
|
data.Duration = metadata.Duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaList.MediaList.Add(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Console.WriteLine(string.Format("Error preparing queue: {0}", ex.Message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.FromResult(mediaList);
|
|
|
|
|
|
|
|
}
|
2019-11-04 04:17:34 +00:00
|
|
|
|
|
|
|
public override async Task GetSongStream(SongRequest request,
|
|
|
|
Grpc.Core.IServerStreamWriter<Chunk> responseStream,
|
|
|
|
Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
|
|
|
BaseMedia song = LibraryService.Instance.GetSong(request.Id);
|
2019-11-04 06:43:26 +00:00
|
|
|
await song.Load();
|
2019-11-04 04:17:34 +00:00
|
|
|
|
|
|
|
//Send stream
|
|
|
|
Console.WriteLine("Begin sending file");
|
|
|
|
byte[] buffer = new byte[2048]; // read in chunks of 2KB
|
|
|
|
int bytesRead;
|
|
|
|
while ((bytesRead = song.DataStream.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
|
{
|
|
|
|
Google.Protobuf.ByteString bufferByteString = Google.Protobuf.ByteString.CopyFrom(buffer);
|
|
|
|
await responseStream.WriteAsync(new Chunk { Content = bufferByteString });
|
|
|
|
}
|
|
|
|
Console.WriteLine("Done sending file");
|
|
|
|
}
|
2019-06-03 14:57:05 +00:00
|
|
|
}
|
|
|
|
}
|