Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,97 +1,58 @@
using AudioPlayerBackend.AudioLibrary.LibraryRepo.OwnTcp.Extensions;
using AudioPlayerBackend.Communication;
using AudioPlayerBackend.Communication.Base;
using AudioPlayerBackend.OwnTcp;
using AudioPlayerBackend.Player;
using System;
using System.Threading.Tasks;

namespace AudioPlayerBackend.AudioLibrary.LibraryRepo.OwnTcp
{
internal class OwnTcpLibraryRepo : ILibraryRepo
internal class OwnTcpLibraryRepo : OwnTcpBaseService, ILibraryRepo
{
private readonly IClientCommunicator clientCommunicator;

public event EventHandler<AudioLibraryChangeArgs<PlaybackState>> PlayStateChanged;
public event EventHandler<AudioLibraryChangeArgs<double>> VolumeChanged;
public event EventHandler<AudioLibraryChangeArgs<Guid?>> CurrentPlaylistIdChanged;
public event EventHandler<AudioLibraryChangeArgs<DateTime?>> FoldersLastUpdatedChanged;

public OwnTcpLibraryRepo(IClientCommunicator clientCommunicator)
{
this.clientCommunicator = clientCommunicator;
}

public Task Start()
{
clientCommunicator.Received += ClientCommunicator_Received;
return Task.CompletedTask;
}

public Task Stop()
public OwnTcpLibraryRepo(IClientCommunicator clientCommunicator) : base(nameof(ILibraryRepo), clientCommunicator)
{
clientCommunicator.Received -= ClientCommunicator_Received;
return Task.CompletedTask;
}

public Task Dispose()
protected override Task<byte[]> OnMessageReceived(string subTopic, byte[] payload)
{
return Stop();
return Task.FromResult(OnMessageReceived(subTopic, payload));
}

private void ClientCommunicator_Received(object sender, ReceivedEventArgs e)
private byte[] OnMessageReceived(string subTopic, ByteQueue payload)
{
TaskCompletionSource<byte[]> anwser = null;
try
{
string[] parts = e.Topic.Split('.');
if (parts.Length != 2 || parts[0] != nameof(ILibraryRepo)) return;

anwser = e.StartAnwser();

ByteQueue payload = e.Payload;
switch (parts[1])
{
case nameof(PlayStateChanged):
PlaybackState playState = payload.DequeuePlaybackState();
PlayStateChanged?.Invoke(this, new AudioLibraryChangeArgs<PlaybackState>(playState));
anwser.SetResult(null);
break;

case nameof(VolumeChanged):
double volume = payload.DequeueDouble();
VolumeChanged?.Invoke(this, new AudioLibraryChangeArgs<double>(volume));
anwser.SetResult(null);
break;

case nameof(CurrentPlaylistIdChanged):
Guid? currentPlaylistId = payload.DequeueGuidNullable();
CurrentPlaylistIdChanged?.Invoke(this, new AudioLibraryChangeArgs<Guid?>(currentPlaylistId));
anwser.SetResult(null);
break;

case nameof(FoldersLastUpdatedChanged):
DateTime? foldersLastUpdated = payload.DequeueDateTimeNullable();
FoldersLastUpdatedChanged?.Invoke(this, new AudioLibraryChangeArgs<DateTime?>(foldersLastUpdated));
anwser.SetResult(null);
break;

default:
anwser.SetException(new NotSupportedException($"Received action is not supported: {parts[2]}"));
break;
}
}
catch (Exception exception)
switch (subTopic)
{
if (anwser != null) anwser.SetException(exception);
else throw;
case nameof(PlayStateChanged):
PlaybackState playState = payload.DequeuePlaybackState();
PlayStateChanged?.Invoke(this, new AudioLibraryChangeArgs<PlaybackState>(playState));
return null;

case nameof(VolumeChanged):
double volume = payload.DequeueDouble();
VolumeChanged?.Invoke(this, new AudioLibraryChangeArgs<double>(volume));
return null;

case nameof(CurrentPlaylistIdChanged):
Guid? currentPlaylistId = payload.DequeueGuidNullable();
CurrentPlaylistIdChanged?.Invoke(this, new AudioLibraryChangeArgs<Guid?>(currentPlaylistId));
return null;

case nameof(FoldersLastUpdatedChanged):
DateTime? foldersLastUpdated = payload.DequeueDateTimeNullable();
FoldersLastUpdatedChanged?.Invoke(this, new AudioLibraryChangeArgs<DateTime?>(foldersLastUpdated));
return null;

default:
throw new NotSupportedException($"Received action is not supported: {subTopic}");
}
}

public Task<byte[]> SendAsync(string funcName, byte[] payload = null)
{
return clientCommunicator.SendAsync($"{nameof(ILibraryRepo)}.{funcName}", payload);
}

public async Task<Library> GetLibrary()
{
ByteQueue result = await SendAsync(nameof(GetLibrary));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
using AudioPlayerBackend.AudioLibrary.LibraryRepo.OwnTcp.Extensions;
using AudioPlayerBackend.Communication;
using AudioPlayerBackend.Communication.Base;
using AudioPlayerBackend.OwnTcp;
using AudioPlayerBackend.Player;
using System;
using System.Threading.Tasks;

namespace AudioPlayerBackend.AudioLibrary.LibraryRepo.OwnTcp
{
internal class OwnTcpServerLibraryRepoConnector : IServerLibraryRepoConnector
internal class OwnTcpServerLibraryRepoConnector : OwnTcpBaseServerConnector, IServerLibraryRepoConnector
{
private readonly ILibraryRepo libraryRepo;
private readonly IServerCommunicator serverCommunicator;

public OwnTcpServerLibraryRepoConnector(ILibraryRepo libraryRepo, IServerCommunicator serverCommunicator)
: base(nameof(ILibraryRepo), serverCommunicator)
{
this.libraryRepo = libraryRepo;
this.serverCommunicator = serverCommunicator;
}

public Task<byte[]> SendAsync(string funcName, byte[] payload = null)
{
return serverCommunicator.SendAsync($"{nameof(ILibraryRepo)}.{funcName}", payload);
}

public Task Start()
protected override void SubscribeToService()
{
libraryRepo.PlayStateChanged += OnPlayStateChanged;
libraryRepo.VolumeChanged += OnVolumeChanged;
libraryRepo.CurrentPlaylistIdChanged += OnCurrentPlaylistIdChanged;
libraryRepo.FoldersLastUpdatedChanged += OnFoldersLastUpdatedChanged;

serverCommunicator.Received += OnReceived;

return Task.CompletedTask;
}

public Task Stop()
protected override void UnsubscribeFromService()
{
libraryRepo.PlayStateChanged -= OnPlayStateChanged;
libraryRepo.VolumeChanged -= OnVolumeChanged;
libraryRepo.CurrentPlaylistIdChanged -= OnCurrentPlaylistIdChanged;
libraryRepo.FoldersLastUpdatedChanged -= OnFoldersLastUpdatedChanged;

serverCommunicator.Received += OnReceived;

return Task.CompletedTask;
}

private async void OnPlayStateChanged(object sender, AudioLibraryChangeArgs<PlaybackState> e)
Expand Down Expand Up @@ -75,66 +61,39 @@ private async void OnFoldersLastUpdatedChanged(object sender, AudioLibraryChange
await SendAsync(nameof(libraryRepo.FoldersLastUpdatedChanged), payload);
}

private async void OnReceived(object sender, ReceivedEventArgs e)
protected override async Task<byte[]> OnMessageReceived(string subTopic, byte[] payload)
{
TaskCompletionSource<byte[]> anwser = null;
try
ByteQueue queue = payload;
switch (subTopic)
{
string[] parts = e.Topic.Split('.');
if (parts.Length != 2 || parts[0] != nameof(ILibraryRepo)) return;

anwser = e.StartAnwser();

ByteQueue payload = e.Payload;
ByteQueue result;
switch (parts[1])
{
case nameof(libraryRepo.GetLibrary):
Library library = await libraryRepo.GetLibrary();
result = new ByteQueue()
.Enqueue(library);
anwser.SetResult(result);
break;

case nameof(libraryRepo.SetPlayState):
PlaybackState playState = payload.DequeuePlaybackState();
await libraryRepo.SetPlayState(playState);
anwser.SetResult(null);
break;

case nameof(libraryRepo.SetVolume):
double volume = payload.DequeueDouble();
await libraryRepo.SetVolume(volume);
anwser.SetResult(null);
break;

case nameof(libraryRepo.SetCurrentPlaylistId):
Guid? currentPlaylistId = payload.DequeueGuidNullable();
await libraryRepo.SetCurrentPlaylistId(currentPlaylistId);
anwser.SetResult(null);
break;

case nameof(libraryRepo.SetFoldersLastUpdated):
DateTime? foldersLastUpdated = payload.DequeueDateTimeNullable();
await libraryRepo.SetFoldersLastUpdated(foldersLastUpdated);
anwser.SetResult(null);
break;

default:
anwser.SetException(new NotSupportedException($"Received action is not supported: {parts[1]}"));
break;
}
case nameof(libraryRepo.GetLibrary):
Library library = await libraryRepo.GetLibrary();
return new ByteQueue()
.Enqueue(library);

case nameof(libraryRepo.SetPlayState):
PlaybackState playState = queue.DequeuePlaybackState();
await libraryRepo.SetPlayState(playState);
return null;

case nameof(libraryRepo.SetVolume):
double volume = queue.DequeueDouble();
await libraryRepo.SetVolume(volume);
return null;

case nameof(libraryRepo.SetCurrentPlaylistId):
Guid? currentPlaylistId = queue.DequeueGuidNullable();
await libraryRepo.SetCurrentPlaylistId(currentPlaylistId);
return null;

case nameof(libraryRepo.SetFoldersLastUpdated):
DateTime? foldersLastUpdated = queue.DequeueDateTimeNullable();
await libraryRepo.SetFoldersLastUpdated(foldersLastUpdated);
return null;

default:
throw new NotSupportedException($"Received action is not supported: {subTopic}");
}
catch (Exception exception)
{
if (anwser != null) anwser.SetException(exception);
else throw;
}
}

public async Task Dispose()
{
await Stop();
}
}
}
Loading