Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Zexuz committed Apr 17, 2019
1 parent 01bd133 commit fdb28dd
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 105 deletions.
4 changes: 2 additions & 2 deletions Mpv.JsonIpc/Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public async Task PlayMedia(string path)

public async Task<TimeSpan> GetCurrentPosition()
{
var result = await _ipc.GetProperty<string>(Property.Position);
return TimeSpan.Zero;
var result = await _ipc.GetProperty<double>(Property.Position);
return TimeSpan.FromSeconds(result.Data);
}
}
}
2 changes: 1 addition & 1 deletion Mpv.JsonIpc/IManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mpv.JsonIpc
{
public interface IManager
{
Task<Response<T>> Execute<T>(Request message);
Task<Response<T>> Execute<T>(Request request);
void Dispose();
}
}
55 changes: 34 additions & 21 deletions Mpv.JsonIpc/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,57 @@ public sealed class Manager : IDisposable, IManager
{
private readonly NamedPipeClientStream _pipe;

private StreamReader _pipeIn;
private StreamWriter _pipeOut;
private StreamReader _pipeReader;
private StreamWriter _pipeWriter;

public Manager(INamedPipeFactory pipeFactory)
{
_pipe = pipeFactory.CreateNamedPipe();
}

public async Task<Response<T>> Execute<T>(Request message)
//If we ever want to listen to random MPV actions, we can always just create a sepperate pipe for that.
//So one pipe for sending/receiving RPC call, and one just to read/oberser events from properties
public async Task<Response<T>> Execute<T>(Request request)
{
if (!_pipe.IsConnected)
{
_pipe.Connect();
_pipeIn = new StreamReader(_pipe);
_pipeOut = new StreamWriter(_pipe) {AutoFlush = true};

_pipeReader = new StreamReader(_pipe);
_pipeWriter = new StreamWriter(_pipe);
}

var messageToSend = JsonConvert.SerializeObject(message);
Console.WriteLine(messageToSend);
_pipeOut.WriteLine(messageToSend);
var messageToSend = JsonConvert.SerializeObject(request);
await _pipeWriter.WriteLineAsync(messageToSend);
await _pipeWriter.FlushAsync();

var readLineTask = _pipeIn.ReadLineAsync();
var counter = 0;
while (true)
{
await Task.Delay(25);
counter++;

if (counter > 1000)
{
throw new Exception("Retries maxed out");
}

const int timeout = 1000;
if (await Task.WhenAny(readLineTask, Task.Delay(timeout)) != readLineTask)
{
return new Response<T>

var messageReceived = await _pipeReader.ReadLineAsync();
if (string.IsNullOrWhiteSpace(messageReceived))
{
Data = default(T),
Error = $"Got a timeout after {timeout}ms",
RequestId = -1
};
}
continue;
}

var response = JsonConvert.DeserializeObject<Response<T>>(messageReceived);

var messageReceived = readLineTask.Result;
Console.WriteLine(messageReceived);
return JsonConvert.DeserializeObject<Response<T>>(messageReceived);
if (response.RequestId != request.RequestId)
{
continue;
}

return response;
}
}

public void Dispose()
Expand Down
4 changes: 4 additions & 0 deletions Mpv.JsonIpc/Mpv.JsonIpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SnackTime.Core\SnackTime.Core.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions SnackTime.Core/Database/DatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public LiteDatabase GetDatabase()
{
return new LiteDatabase(ConnectionString);
}

public LiteRepository GetRepository()
{
return new LiteRepository(GetDatabase());
}
}
}
2 changes: 1 addition & 1 deletion SnackTime.Core/DependencyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void Load(ContainerBuilder builder)

builder.RegisterType<TimeService>().AsSelf();
builder.RegisterType<SessionService>().AsSelf();
builder.RegisterType<SessionQueue>().AsSelf().SingleInstance();
builder.RegisterType<Queue<Item>>().AsSelf().SingleInstance();

builder.RegisterType<EpisodeFileLookupProvider>().AsSelf();
builder.RegisterType<EpisodeProvider>().AsSelf();
Expand Down
9 changes: 9 additions & 0 deletions SnackTime.Core/Session/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace SnackTime.Core.Session
{
public class Item
{
public string MediaId { get; set; }
public string Path { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace SnackTime.Core.Session
{
public class SessionQueue
public class Queue<T>
{
private readonly object _lock;
private readonly IList<Item> _queue;
private readonly object _lock;
private readonly IList<T> _queue;

public SessionQueue()
public Queue()
{
_lock = new object();
_queue = new List<Item>();
_queue = new List<T>();
}

public void AddToQueue(Item request)
public void AddToQueue(T request)
{
lock (_lock)
{
Expand All @@ -29,7 +29,7 @@ public bool HasItems()
}
}

public Item Pop()
public T Pop()
{
lock (_lock)
{
Expand All @@ -38,11 +38,5 @@ public Item Pop()
return item;
}
}

public class Item
{
public string MediaId { get; set; }
public string Path { get; set; }
}
}
}
8 changes: 4 additions & 4 deletions SnackTime.Core/Session/SessionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ public MediaServer.Storage.ProtoGenerated.Session CreateNewSession(string mediaI
Id = sessionId,
MediaId = mediaId,
Duration = duration,
StartUTC = _timeService.GetCurrentTimeAsString(),
EndUTC = _timeService.GetCurrentTimeAsString(),
StartUTC = _timeService.GetCurrentTimeAsUnixSeconds(),
EndUTC = _timeService.GetCurrentTimeAsUnixSeconds(),
};
}

public void UpsertSession(MediaServer.Storage.ProtoGenerated.Session session)
{
using (var db = new LiteRepository(_databaseFactory.GetDatabase()))
using (var db = _databaseFactory.GetRepository())
{
db.Upsert(session);
}
}

public object GetAll()
{
using (var db = new LiteRepository(_databaseFactory.GetDatabase()))
using (var db = _databaseFactory.GetRepository())
{
return db.Fetch<MediaServer.Storage.ProtoGenerated.Session>();
}
Expand Down
9 changes: 2 additions & 7 deletions SnackTime.Core/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ namespace SnackTime.Core
{
public class TimeService
{
public static DateTimeOffset Parse(string timeString)
{
return DateTimeOffset.Parse(timeString, CultureInfo.InvariantCulture);
}

public DateTimeOffset GetCurrentTime()
{
return DateTimeOffset.UtcNow;
}

public string GetCurrentTimeAsString()
public long GetCurrentTimeAsUnixSeconds()
{
return GetCurrentTime().ToString(CultureInfo.InvariantCulture);
return GetCurrentTime().ToUnixTimeSeconds();
}
}
}
52 changes: 26 additions & 26 deletions SnackTime.MediaServer.Proto/csharp/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ static StorageReflection() {
"ChNwcm90by9zdG9yYWdlLnByb3RvEhFzbmFja3RpbWUuc3RvcmFnZSJfCglN",
"ZWRpYUZpbGUSEAoIZmlsZU5hbWUYASABKAkSEQoJbWVkaWFOYW1lGAIgASgJ",
"EhUKDWRvd25sb2FkZWRVVEMYAyABKAkSFgoObGFzdFdhdGNoZWRVVEMYBCAB",
"KAkidwoHU2Vzc2lvbhIKCgJpZBgBIAEoCRIQCghzdGFydFVUQxgCIAEoCRIO",
"CgZlbmRVVEMYAyABKAkSDwoHbWVkaWFJZBgEIAEoCRItCghkdXJhdGlvbhgF",
"KAkidwoHU2Vzc2lvbhIKCgJpZBgBIAEoCRIQCghzdGFydFVUQxgCIAEoAxIO",
"CgZlbmRVVEMYAyABKAMSDwoHbWVkaWFJZBgEIAEoCRItCghkdXJhdGlvbhgF",
"IAEoCzIbLnNuYWNrdGltZS5zdG9yYWdlLkR1cmF0aW9uIj4KCER1cmF0aW9u",
"EhkKEXN0YXJ0UG9zdGlvbkluU2VjGAEgASgBEhcKD2VuZFBvc3Rpb25JblNl",
"YxgCIAEoAUIvqgIsU25hY2tUaW1lLk1lZGlhU2VydmVyLlN0b3JhZ2UuUHJv",
Expand Down Expand Up @@ -309,26 +309,26 @@ public string Id {

/// <summary>Field number for the "startUTC" field.</summary>
public const int StartUTCFieldNumber = 2;
private string startUTC_ = "";
private long startUTC_;
/// <summary>
/// TODO add device id / name?
/// </summary>
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string StartUTC {
public long StartUTC {
get { return startUTC_; }
set {
startUTC_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
startUTC_ = value;
}
}

/// <summary>Field number for the "endUTC" field.</summary>
public const int EndUTCFieldNumber = 3;
private string endUTC_ = "";
private long endUTC_;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public string EndUTC {
public long EndUTC {
get { return endUTC_; }
set {
endUTC_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
endUTC_ = value;
}
}

Expand Down Expand Up @@ -379,8 +379,8 @@ public bool Equals(Session other) {
public override int GetHashCode() {
int hash = 1;
if (Id.Length != 0) hash ^= Id.GetHashCode();
if (StartUTC.Length != 0) hash ^= StartUTC.GetHashCode();
if (EndUTC.Length != 0) hash ^= EndUTC.GetHashCode();
if (StartUTC != 0L) hash ^= StartUTC.GetHashCode();
if (EndUTC != 0L) hash ^= EndUTC.GetHashCode();
if (MediaId.Length != 0) hash ^= MediaId.GetHashCode();
if (duration_ != null) hash ^= Duration.GetHashCode();
if (_unknownFields != null) {
Expand All @@ -400,13 +400,13 @@ public void WriteTo(pb::CodedOutputStream output) {
output.WriteRawTag(10);
output.WriteString(Id);
}
if (StartUTC.Length != 0) {
output.WriteRawTag(18);
output.WriteString(StartUTC);
if (StartUTC != 0L) {
output.WriteRawTag(16);
output.WriteInt64(StartUTC);
}
if (EndUTC.Length != 0) {
output.WriteRawTag(26);
output.WriteString(EndUTC);
if (EndUTC != 0L) {
output.WriteRawTag(24);
output.WriteInt64(EndUTC);
}
if (MediaId.Length != 0) {
output.WriteRawTag(34);
Expand All @@ -427,11 +427,11 @@ public int CalculateSize() {
if (Id.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(Id);
}
if (StartUTC.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(StartUTC);
if (StartUTC != 0L) {
size += 1 + pb::CodedOutputStream.ComputeInt64Size(StartUTC);
}
if (EndUTC.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(EndUTC);
if (EndUTC != 0L) {
size += 1 + pb::CodedOutputStream.ComputeInt64Size(EndUTC);
}
if (MediaId.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(MediaId);
Expand All @@ -453,10 +453,10 @@ public void MergeFrom(Session other) {
if (other.Id.Length != 0) {
Id = other.Id;
}
if (other.StartUTC.Length != 0) {
if (other.StartUTC != 0L) {
StartUTC = other.StartUTC;
}
if (other.EndUTC.Length != 0) {
if (other.EndUTC != 0L) {
EndUTC = other.EndUTC;
}
if (other.MediaId.Length != 0) {
Expand All @@ -483,12 +483,12 @@ public void MergeFrom(pb::CodedInputStream input) {
Id = input.ReadString();
break;
}
case 18: {
StartUTC = input.ReadString();
case 16: {
StartUTC = input.ReadInt64();
break;
}
case 26: {
EndUTC = input.ReadString();
case 24: {
EndUTC = input.ReadInt64();
break;
}
case 34: {
Expand Down
4 changes: 2 additions & 2 deletions SnackTime.MediaServer.Proto/proto/storage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ message MediaFile {
message Session{
string id = 1;
// TODO add device id / name?
string startUTC = 2;
string endUTC = 3;
int64 startUTC = 2;
int64 endUTC = 3;
string mediaId = 4;
Duration duration = 5;
}
Expand Down
Loading

0 comments on commit fdb28dd

Please sign in to comment.