Skip to content

Commit

Permalink
Simplify lazy children retrieval
Browse files Browse the repository at this point in the history
References #21
  • Loading branch information
andreashuber-lawo committed Apr 28, 2016
1 parent b5c670e commit c0a0a4c
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 47 deletions.
3 changes: 0 additions & 3 deletions Lawo.EmberPlusSharp/Glow/GlowLogInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ public void ApplyPayload()
using (var dummyWriter = new EmberWriter(Stream.Null))
{
this.root.Read(emberReader, this.pendingInvocations, this.streamedParameters);
dummyWriter.WriteStartApplicationDefinedType(
GlowGlobal.Root.OuterId, GlowRootElementCollection.InnerNumber);
this.root.WriteRequest(dummyWriter, this.streamedParameters);
dummyWriter.WriteEndContainer();
this.root.SetComplete();
this.root.UpdateRetrievalState(true);
}
Expand Down
16 changes: 10 additions & 6 deletions Lawo.EmberPlusSharp/Model/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Lawo.EmberPlusSharp.Model
using System.Threading.Tasks;

using Ember;
using Glow;
using IO;
using S101;

Expand All @@ -41,6 +40,7 @@ public sealed class Consumer<TRoot> : IMonitoredConnection
private int autoSendInterval = 100;
private CancellationTokenSource autoSendDelayCancellationSource;
private TaskCompletionSource<bool> hasChangesSetSource;
private TaskCompletionSource<bool> isVerifiedSource;
private bool disposed;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -109,7 +109,11 @@ public async Task SendAsync()
}

await this.client.SendMessageAsync(this.emberDataMessage, stream.ToArray());
await this.RetrieveChildrenAsync();
}

if (await this.SendRequestAsync())
{
await this.isVerifiedSource.Task;
}
}

Expand Down Expand Up @@ -290,6 +294,7 @@ private async void SendReceiveLoop()
Exception exception = null;
this.autoSendDelayCancellationSource = new CancellationTokenSource();
this.hasChangesSetSource = new TaskCompletionSource<bool>();
this.isVerifiedSource = new TaskCompletionSource<bool>();
this.root.HasChangesSet += this.OnHasChangesSet;

try
Expand All @@ -310,6 +315,8 @@ private async void SendReceiveLoop()
await providerTask;
this.ApplyProviderChanges();
await this.RetrieveChildrenAsync();
this.isVerifiedSource.SetResult(false);
this.isVerifiedSource = new TaskCompletionSource<bool>();
providerTask = this.WaitForProviderChangesAsync();
}
}
Expand Down Expand Up @@ -467,10 +474,7 @@ private bool WriteRequest(out MemoryStream stream)
using (stream = new MemoryStream())
using (var writer = new EmberWriter(stream))
{
writer.WriteStartApplicationDefinedType(GlowGlobal.Root.OuterId, GlowRootElementCollection.InnerNumber);
var result = this.root.WriteRequest(writer, this.streamedParameters);
writer.WriteEndContainer();
return result;
return this.root.WriteRequest(writer, this.streamedParameters);
}
}

Expand Down
12 changes: 11 additions & 1 deletion Lawo.EmberPlusSharp/Model/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ internal virtual RetrievalState RetrievalState
set { } // Intentionally empty
}

internal void ResetRetrievalState()
{
this.RetrievalState = RetrievalState.None;

if (this.parent != null)
{
this.parent.ResetRetrievalState();
}
}

internal virtual void SetContext(Context context)
{
this.parent = context.Parent;
Expand Down Expand Up @@ -265,7 +275,7 @@ internal virtual bool WriteRequest(EmberWriter writer, IStreamedParameterCollect
return false;
}

internal abstract RetrievalState WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations);
internal abstract void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations);

internal virtual void SetComplete()
{
Expand Down
5 changes: 1 addition & 4 deletions Lawo.EmberPlusSharp/Model/FunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ internal override RetrievalState ReadContents(EmberReader reader, ElementType ac
return RetrievalState.Complete;
}

internal sealed override RetrievalState WriteChanges(
EmberWriter writer, IInvocationCollection pendingInvocations)
internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations)
{
if (this.HasChanges)
{
Expand All @@ -121,8 +120,6 @@ internal sealed override RetrievalState WriteChanges(
writer.WriteEndContainer();
this.HasChanges = false;
}

return RetrievalState.Complete;
}

internal abstract KeyValuePair<string, ParameterType>[] ReadTupleDescription(
Expand Down
2 changes: 2 additions & 0 deletions Lawo.EmberPlusSharp/Model/IParent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal interface IParent

void SetHasChanges();

void ResetRetrievalState();

string GetPath();

void AppendPath(StringBuilder builder);
Expand Down
5 changes: 1 addition & 4 deletions Lawo.EmberPlusSharp/Model/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ internal override RetrievalState ReadContents(EmberReader reader, ElementType ac
return this.RetrievalState;
}

internal sealed override RetrievalState WriteChanges(
EmberWriter writer, IInvocationCollection pendingInvocations)
internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations)
{
////if (this.HasChanges)
////{
Expand All @@ -213,8 +212,6 @@ internal sealed override RetrievalState WriteChanges(
//// writer.WriteEndContainer();
//// this.HasChanges = false;
////}

return this.RetrievalState;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 1 addition & 4 deletions Lawo.EmberPlusSharp/Model/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ internal sealed override RetrievalState ReadContents(EmberReader reader, Element
return this.RetrievalState;
}

internal override RetrievalState WriteChanges(
EmberWriter writer, IInvocationCollection pendingInvocations)
internal override void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations)
{
if (this.HasChanges)
{
this.WriteChangesCollection(writer, pendingInvocations);
this.HasChanges = false;
}

return this.RetrievalState;
}

internal sealed override IElement GetElement(string[] pathElements, int index)
Expand Down
21 changes: 8 additions & 13 deletions Lawo.EmberPlusSharp/Model/NodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public ChildrenRetrievalPolicy ChildrenRetrievalPolicy

if (this.RetrieveDetailsChangeStatus != RetrieveDetailsChangeStatus.Unchanged)
{
this.HasChanges = true;
this.ResetRetrievalState();
}
}
}
Expand All @@ -75,6 +75,11 @@ void IParent.SetHasChanges()
this.HasChanges = true;
}

void IParent.ResetRetrievalState()
{
this.ResetRetrievalState();
}

void IParent.AppendPath(StringBuilder builder)
{
this.AppendPath(builder);
Expand Down Expand Up @@ -137,19 +142,9 @@ internal bool WriteCommandCollection(EmberWriter writer, IStreamedParameterColle

internal void WriteChangesCollection(EmberWriter writer, IInvocationCollection pendingInvocations)
{
if (this.children.Count == 0)
{
if (this.RetrieveDetails && (this.RetrieveDetailsChangeStatus != RetrieveDetailsChangeStatus.Unchanged))
{
this.WriteRequest(writer, null);
}
}
else
foreach (var child in this.children.Values)
{
foreach (var child in this.children.Values)
{
this.RetrievalState &= child.WriteChanges(writer, pendingInvocations);
}
child.WriteChanges(writer, pendingInvocations);
}
}

Expand Down
5 changes: 1 addition & 4 deletions Lawo.EmberPlusSharp/Model/ParameterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ internal override RetrievalState ReadContents(EmberReader reader, ElementType ac
return this.RetrievalState;
}

internal sealed override RetrievalState WriteChanges(
EmberWriter writer, IInvocationCollection pendingInvocations)
internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations)
{
if (this.HasChanges)
{
Expand All @@ -343,8 +342,6 @@ internal sealed override RetrievalState WriteChanges(
writer.WriteEndContainer();
this.HasChanges = false;
}

return this.RetrievalState;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
10 changes: 5 additions & 5 deletions Lawo.EmberPlusSharp/Model/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ internal sealed override bool ReadChildrenCore(EmberReader reader)
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")]
internal sealed override bool WriteRequest(EmberWriter writer, IStreamedParameterCollection streamedParameters)
{
return this.WriteCommandCollection(writer, streamedParameters);
writer.WriteStartApplicationDefinedType(GlowGlobal.Root.OuterId, GlowRootElementCollection.InnerNumber);
var result = this.WriteCommandCollection(writer, streamedParameters);
writer.WriteEndContainer();
return result;
}

[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")]
internal sealed override RetrievalState WriteChanges(
EmberWriter writer, IInvocationCollection pendingInvocations)
internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations)
{
if (this.HasChanges)
{
Expand All @@ -149,8 +151,6 @@ internal sealed override RetrievalState WriteChanges(
writer.WriteEndContainer();
this.HasChanges = false;
}

return this.RetrievalState;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 1 addition & 3 deletions Lawo.EmberPlusSharp/Model/ShadowNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ internal sealed override RetrievalState ReadContents(EmberReader reader, Element
throw new ModelException("Unexpected contents for intermediate node in basePath.");
}

internal sealed override RetrievalState WriteChanges(
EmberWriter writer, IInvocationCollection pendingInvocations)
internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection pendingInvocations)
{
this.HasChanges = false;
return this.RetrievalState;
}
}
}

0 comments on commit c0a0a4c

Please sign in to comment.