Skip to content

Commit

Permalink
Adapt internal request logic
Browse files Browse the repository at this point in the history
Change the logic such that a provider response is not expected for all requests.

References #8
  • Loading branch information
andreashuber-lawo committed Jan 5, 2016
1 parent 7f1ea9b commit fa82879
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
13 changes: 10 additions & 3 deletions Lawo.EmberPlusSharp/Model/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,14 @@ private async Task<bool> SendRequestAsync()
// Of course, in the latter case the assumption is that the provider will at some point send an
// answer to our previous getDirectory request. If it doesn't, the timeout will take care of things.
MemoryStream stream;
WriteRequest(this.root, out stream);

if (!WriteRequest(this.root, out stream))
{
// If no answer is expected from the provider due to the request, we need to update the request
// state again to see whether there's still something missing.
rootRequestState = this.root.UpdateRequestState(false);
}

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

Expand Down Expand Up @@ -410,13 +417,13 @@ private void ApplyChange(MessageReceivedEventArgs args)
}
}

private static void WriteRequest(TRoot root, out MemoryStream stream)
private static bool WriteRequest(TRoot root, out MemoryStream stream)
{
// TODO: Reuse MemoryStream and EmberWriter for all outgoing messages.
using (stream = new MemoryStream())
using (var writer = new EmberWriter(stream))
{
root.WriteRequest(writer);
return root.WriteRequest(writer);
}
}

Expand Down
5 changes: 4 additions & 1 deletion Lawo.EmberPlusSharp/Model/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,14 @@ internal virtual RequestState UpdateRequestState(bool throwForMissingRequiredChi

/// <summary>Writes the payload of a message that contains an appropriate requests for all elements that require
/// one.</summary>
/// <returns><c>true</c> if a provider response is expected due to the request; otherwise <c>false</c>.
/// </returns>
/// <remarks>Recursively visits all children with a state equal to <see cref="RequestState.None"/>, writes
/// a getDirectory command for nodes that do not yet have children or a subscribe command for stream parameters
/// and changes their state accordingly.</remarks>
internal virtual void WriteRequest(EmberWriter writer)
internal virtual bool WriteRequest(EmberWriter writer)
{
return false;
}

internal abstract void WriteChanges(EmberWriter writer, IInvocationCollection invocationCollection);
Expand Down
20 changes: 16 additions & 4 deletions Lawo.EmberPlusSharp/Model/NodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,24 @@ internal void ReadChild(EmberReader reader, ElementType actualType)
this.ReadChild(reader, actualType, reader.AssertAndReadContentsAsInt32());
}

internal void WriteCommandCollection(EmberWriter writer)
internal bool WriteCommandCollection(EmberWriter writer)
{
if (this.children.Count == 0)
{
this.WriteCommandCollection(writer, GlowCommandNumber.GetDirectory, RequestState.RequestSent);
return true;
}
else
{
var result = false;

foreach (var child in this.children.Values)
{
child.WriteRequest(writer);
// We want to avoid short-circuit logic, which is why we use | rather than ||.
result |= child.WriteRequest(writer);
}

return result;
}
}

Expand Down Expand Up @@ -206,7 +212,7 @@ internal sealed override RequestState UpdateRequestState(bool throwForMissingReq
return this.RequestState;
}

internal override void WriteRequest(EmberWriter writer)
internal override bool WriteRequest(EmberWriter writer)
{
if (this.RequestState.Equals(RequestState.None))
{
Expand All @@ -221,13 +227,19 @@ internal override void WriteRequest(EmberWriter writer)
GlowQualifiedNode.Children.OuterId, GlowElementCollection.InnerNumber);
}

this.WriteCommandCollection(writer);
var result = this.WriteCommandCollection(writer);

if (isEmpty)
{
writer.WriteEndContainer();
writer.WriteEndContainer();
}

return result;
}
else
{
return false;
}
}

Expand Down
5 changes: 3 additions & 2 deletions Lawo.EmberPlusSharp/Model/Root.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ 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 void WriteRequest(EmberWriter writer)
internal sealed override bool WriteRequest(EmberWriter writer)
{
writer.WriteStartApplicationDefinedType(GlowGlobal.Root.OuterId, GlowRootElementCollection.InnerNumber);
this.WriteCommandCollection(writer);
var result = this.WriteCommandCollection(writer);
writer.WriteEndContainer();
return result;
}

[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")]
Expand Down

0 comments on commit fa82879

Please sign in to comment.