1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2025-07-01 08:47:36 +00:00

MpdResponse refactored (partial)

This commit is contained in:
glucaci
2016-12-06 12:53:49 +01:00
parent ec24b3f01a
commit 9212021be6
7 changed files with 199 additions and 352 deletions

View File

@ -0,0 +1,24 @@
namespace LibMpc
{
public interface IMpdMessage
{
IMpdRequest Request { get; }
IMpdResponse Response { get; }
}
public class MpdMessage : IMpdMessage
{
public MpdMessage(IMpcCommand command)
{
Request = new MpdRequest(command);
}
public void AddResponse(IMpdResponse response)
{
Response = response;
}
public IMpdRequest Request { get; }
public IMpdResponse Response { get; private set; }
}
}

View File

@ -0,0 +1,17 @@
namespace LibMpc
{
public interface IMpdRequest
{
IMpcCommand Command { get; }
}
public class MpdRequest : IMpdRequest
{
public MpdRequest(IMpcCommand command)
{
Command = command;
}
public IMpcCommand Command { get; }
}
}

View File

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace LibMpc
{
public interface IMpdResponse
{
IEnumerable<string> Response { get; }
IReadOnlyDictionary<string, string> Values { get; }
IMpdResponseState State { get; }
}
public class MpdResponse : IMpdResponse
{
private static readonly Regex LinePattern = new Regex("^(?<key>[A-Za-z]*):[ ]{0,1}(?<value>.*)$");
public MpdResponse(ICollection<string> response)
{
response.CheckNotNull();
var endLine = response.Take(response.Count - 2).Single();
Response = response.Take(response.Count - 2).ToList();
State = new MpdResponseState(endLine);
Values = GetValuesFromResponse();
}
public IMpdResponseState State { get; }
public IEnumerable<string> Response { get; }
public IReadOnlyDictionary<string, string> Values { get; }
private IReadOnlyDictionary<string, string> GetValuesFromResponse()
{
var result = new Dictionary<string, string>();
foreach (var line in Response)
{
var match = LinePattern.Match(line);
if (match.Success)
{
var mpdKey = match.Result("${key}");
if (!string.IsNullOrEmpty(mpdKey))
{
var mpdValue = match.Result("${value}");
if (string.IsNullOrEmpty(mpdValue))
{
result.Add(mpdKey, mpdValue);
}
}
}
}
return result;
}
}
public static class CheckNotNullExtension
{
public static void CheckNotNull(this object toBeChecked)
{
if (toBeChecked == null)
{
throw new ArgumentNullException(nameof(toBeChecked));
}
}
}
}

View File

@ -0,0 +1,63 @@
using System.Text.RegularExpressions;
namespace LibMpc
{
public interface IMpdResponseState
{
string Status { get; }
string ErrorMessage { get; }
string MpdError { get; }
bool IsError { get; }
}
public class MpdResponseState : IMpdResponseState
{
private static readonly Regex ErrorPattern = new Regex("^ACK \\[(?<code>[0-9]*)@(?<nr>[0-9]*)] \\{(?<command>[a-z]*)} (?<message>.*)$");
private readonly string _endLine;
public MpdResponseState(string endLine)
{
_endLine = endLine;
if (!string.IsNullOrEmpty(_endLine))
{
if (_endLine.Equals(Constants.Ok))
{
Status = _endLine;
IsError = false;
}
else
{
ParseErrorResponse();
}
}
}
public bool IsError { get; private set; } = true;
public string Status { get; private set; } = "UNKNOWN";
public string ErrorMessage { get; private set; } = string.Empty;
public string MpdError { get; private set; } = string.Empty;
private void ParseErrorResponse()
{
Status = "ERROR";
MpdError = _endLine;
var match = ErrorPattern.Match(_endLine);
if (match.Groups.Count != 5)
{
ErrorMessage = "Unexpected response from server.";
}
else
{
var errorCode = match.Result("${code}");
var commandListItem = match.Result("${nr}");
var commandFailed = match.Result("${command}");
var errorMessage = match.Result("${message}");
ErrorMessage = $"ErrorCode: { errorCode }, CommandListItem: { commandListItem }, CommandFailed: { commandFailed }, ErrorMessage: { errorMessage }";
}
}
}
}