From 91b112fbe3492969179bf1abc75bc6b5a6401aa3 Mon Sep 17 00:00:00 2001 From: andreashuber-lawo Date: Sat, 19 Dec 2015 15:31:51 +0100 Subject: [PATCH] Rename ChildrenState to RequestState References #8 --- .../Glow/GlowLogInterpreter.cs | 2 +- .../Lawo.EmberPlusSharp.csproj | 2 +- Lawo.EmberPlusSharp/Model/ChildrenState.cs | 77 ------------- Lawo.EmberPlusSharp/Model/CollectionNode.cs | 6 +- Lawo.EmberPlusSharp/Model/Consumer.cs | 20 ++-- Lawo.EmberPlusSharp/Model/DynamicNodeBase.cs | 4 +- .../Model/DynamicNodeHelper.cs | 8 +- Lawo.EmberPlusSharp/Model/DynamicRoot.cs | 4 +- Lawo.EmberPlusSharp/Model/Element.cs | 48 ++++---- Lawo.EmberPlusSharp/Model/Element1.cs | 4 +- Lawo.EmberPlusSharp/Model/EnumParameter.cs | 2 +- .../Model/EnumParameterImpl.cs | 4 +- Lawo.EmberPlusSharp/Model/FieldNode.cs | 10 +- Lawo.EmberPlusSharp/Model/FunctionBase.cs | 4 +- Lawo.EmberPlusSharp/Model/Matrix.cs | 4 +- Lawo.EmberPlusSharp/Model/MetaElement.cs | 2 +- Lawo.EmberPlusSharp/Model/MetaElement1.cs | 4 +- Lawo.EmberPlusSharp/Model/Node.cs | 8 +- Lawo.EmberPlusSharp/Model/NodeBase.cs | 108 +++++++++--------- .../Model/NullableEnumParameter.cs | 2 +- Lawo.EmberPlusSharp/Model/Parameter.cs | 2 +- Lawo.EmberPlusSharp/Model/ParameterBase.cs | 4 +- Lawo.EmberPlusSharp/Model/RequestState.cs | 86 ++++++++++++++ Lawo.EmberPlusSharp/Model/Root.cs | 2 +- Lawo.EmberPlusSharp/Model/ShadowNode.cs | 2 +- 25 files changed, 214 insertions(+), 205 deletions(-) delete mode 100644 Lawo.EmberPlusSharp/Model/ChildrenState.cs create mode 100644 Lawo.EmberPlusSharp/Model/RequestState.cs diff --git a/Lawo.EmberPlusSharp/Glow/GlowLogInterpreter.cs b/Lawo.EmberPlusSharp/Glow/GlowLogInterpreter.cs index 7671e55f..b65bd23f 100644 --- a/Lawo.EmberPlusSharp/Glow/GlowLogInterpreter.cs +++ b/Lawo.EmberPlusSharp/Glow/GlowLogInterpreter.cs @@ -72,7 +72,7 @@ public void ApplyPayload() { this.root.Read(emberReader, this.pendingInvocations); this.root.SetComplete(); - this.root.UpdateChildrenState(true); + this.root.UpdateRequestState(true); } } } diff --git a/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj b/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj index 26ad03a1..4af8b5ab 100644 --- a/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj +++ b/Lawo.EmberPlusSharp/Lawo.EmberPlusSharp.csproj @@ -105,7 +105,7 @@ - + diff --git a/Lawo.EmberPlusSharp/Model/ChildrenState.cs b/Lawo.EmberPlusSharp/Model/ChildrenState.cs deleted file mode 100644 index 8b43f3e8..00000000 --- a/Lawo.EmberPlusSharp/Model/ChildrenState.cs +++ /dev/null @@ -1,77 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// Copyright 2012-2015 Lawo AG (http://www.lawo.com). -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -namespace Lawo.EmberPlusSharp.Model -{ - using System; - - /// Represents the state of a node with regards to its children. - /// A parameter is always in the state . The state of an element is always propagated - /// up the chain of its direct and indirect parents such that a node always assumes the state of the child with the - /// lowest state. - internal struct ChildrenState : IEquatable - { - private static readonly ChildrenState NoneField = new ChildrenState(); - private static readonly ChildrenState GetDirectorySentField = new ChildrenState(1); - private static readonly ChildrenState CompleteField = new ChildrenState(2); - private static readonly ChildrenState VerifiedField = new ChildrenState(3); - - private readonly int state; - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - /// Returns the lowest state of and . - /// Note that this is a generalization of the boolean & operator to more than two states. Also - /// known as Fuzzy Logic. - public static ChildrenState operator &(ChildrenState left, ChildrenState right) - { - return new ChildrenState(Math.Min(left.state, right.state)); - } - - public bool Equals(ChildrenState other) - { - return this.state == other.state; - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - /// Gets the state used when a getDirectory request needs to be issued for a node. - /// This is the lowest state. - internal static ChildrenState None - { - get { return NoneField; } - } - - /// Gets the state used when a getDirectory request has been sent for a node. - /// This is the second lowest state. - internal static ChildrenState GetDirectorySent - { - get { return GetDirectorySentField; } - } - - /// Gets the state used when the provider has sent an empty children collection or we have received - /// only children that are either complete or ones we're not interested in. - /// This is the second highest state. - internal static ChildrenState Complete - { - get { return CompleteField; } - } - - /// Gets the state used when we have verified that all required children are present. - /// This is the highest state. - internal static ChildrenState Verified - { - get { return VerifiedField; } - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - private ChildrenState(int state) - { - this.state = state; - } - } -} diff --git a/Lawo.EmberPlusSharp/Model/CollectionNode.cs b/Lawo.EmberPlusSharp/Model/CollectionNode.cs index c61274fc..3d137d4f 100644 --- a/Lawo.EmberPlusSharp/Model/CollectionNode.cs +++ b/Lawo.EmberPlusSharp/Model/CollectionNode.cs @@ -48,7 +48,7 @@ public TElement this[int number] [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", Justification = "These are actual class names.")] internal sealed override Element ReadNewChildContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState) { if (ReadContentsCallback == null) { @@ -57,7 +57,7 @@ internal sealed override Element ReadNewChildContents( throw new ModelException(string.Format(CultureInfo.InvariantCulture, Format, this.GetPath())); } - return ReadContentsCallback(reader, actualType, context, out childChildrenState); + return ReadContentsCallback(reader, actualType, context, out childRequestState); } [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] @@ -93,6 +93,6 @@ private static ReadContentsMethod GetReadContentsMethod() } private delegate Element ReadContentsMethod( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState); + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState); } } diff --git a/Lawo.EmberPlusSharp/Model/Consumer.cs b/Lawo.EmberPlusSharp/Model/Consumer.cs index 34e88d91..e1aea631 100644 --- a/Lawo.EmberPlusSharp/Model/Consumer.cs +++ b/Lawo.EmberPlusSharp/Model/Consumer.cs @@ -229,7 +229,7 @@ private async Task QueryChildrenAsync() if ((await Task.WhenAny(queryChildrenTask, Task.Delay(this.queryChildrenTimeout))) != queryChildrenTask) { - this.root.UpdateChildrenState(this.root.ChildrenState.Equals(ChildrenState.Complete)); + this.root.UpdateRequestState(this.root.RequestState.Equals(RequestState.Complete)); var firstIncompleteNode = this.root.GetFirstIncompleteChild(); var message = firstIncompleteNode == null ? "The provider failed to send all requested elements within the specified timeout." : @@ -353,23 +353,23 @@ private async Task DelayAutoSend() private async Task SendChildrenQuery() { - var rootChildrenState = this.root.UpdateChildrenState(false); + var rootRequestState = this.root.UpdateRequestState(false); - if (rootChildrenState.Equals(ChildrenState.None)) + if (rootRequestState.Equals(RequestState.None)) { // There is no guarantee that the response we received is actually an answer to the previously sent - // getDirectory request. It could just as well be a parameter value update. ChildrenState indicates - // whether the former or the latter happened. If ChildrenState == ChildrenState.None, we have - // received at least one new node. If ChildrenState != ChildrenState.None, no new node without + // getDirectory request. It could just as well be a parameter value update. RequestState indicates + // whether the former or the latter happened. If RequestState == RequestState.None, we have + // received at least one new node. If RequestState != RequestState.None, no new node without // children has been received and consequently no new getDirectory request needs to be sent. // 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; - WriteChildrenQuery(this.root, out stream); + WriteRequest(this.root, out stream); await this.client.SendMessageAsync(this.emberDataMessage, stream.ToArray()); } - return !rootChildrenState.Equals(ChildrenState.Verified); + return !rootRequestState.Equals(RequestState.Verified); } private void ApplyChange(MessageReceivedEventArgs args) @@ -410,13 +410,13 @@ private void ApplyChange(MessageReceivedEventArgs args) } } - private static void WriteChildrenQuery(TRoot root, out MemoryStream stream) + private static void 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.WriteChildrenQuery(writer); + root.WriteRequest(writer); } } diff --git a/Lawo.EmberPlusSharp/Model/DynamicNodeBase.cs b/Lawo.EmberPlusSharp/Model/DynamicNodeBase.cs index 602a61ec..f5df3181 100644 --- a/Lawo.EmberPlusSharp/Model/DynamicNodeBase.cs +++ b/Lawo.EmberPlusSharp/Model/DynamicNodeBase.cs @@ -25,9 +25,9 @@ internal DynamicNodeBase() [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] internal sealed override Element ReadNewDynamicChildContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState) { - return DynamicNodeHelper.ReadDynamicChildContents(reader, actualType, context, out childChildrenState); + return DynamicNodeHelper.ReadDynamicChildContents(reader, actualType, context, out childRequestState); } } } diff --git a/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs b/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs index 1bb1ccce..0965a933 100644 --- a/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs +++ b/Lawo.EmberPlusSharp/Model/DynamicNodeHelper.cs @@ -17,16 +17,16 @@ internal static Element ReadDynamicChildContents( EmberReader reader, ElementType actualType, Context context, - out ChildrenState childChildrenState) + out RequestState childRequestState) { switch (actualType) { case ElementType.Parameter: - return DynamicParameter.ReadContents(reader, actualType, context, out childChildrenState); + return DynamicParameter.ReadContents(reader, actualType, context, out childRequestState); case ElementType.Node: - return DynamicNode.ReadContents(reader, actualType, context, out childChildrenState); + return DynamicNode.ReadContents(reader, actualType, context, out childRequestState); default: - return DynamicFunction.ReadContents(reader, actualType, context, out childChildrenState); + return DynamicFunction.ReadContents(reader, actualType, context, out childRequestState); } } diff --git a/Lawo.EmberPlusSharp/Model/DynamicRoot.cs b/Lawo.EmberPlusSharp/Model/DynamicRoot.cs index 513e63f9..adbe6d2a 100644 --- a/Lawo.EmberPlusSharp/Model/DynamicRoot.cs +++ b/Lawo.EmberPlusSharp/Model/DynamicRoot.cs @@ -36,9 +36,9 @@ public ReadOnlyObservableCollection DynamicChildren //////////////////////////////////////////////////////////////////////////////////////////////////////////////// internal sealed override Element ReadNewDynamicChildContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState) { - return DynamicNodeHelper.ReadDynamicChildContents(reader, actualType, context, out childChildrenState); + return DynamicNodeHelper.ReadDynamicChildContents(reader, actualType, context, out childRequestState); } internal sealed override bool ChangeOnlineStatus(IElement child) diff --git a/Lawo.EmberPlusSharp/Model/Element.cs b/Lawo.EmberPlusSharp/Model/Element.cs index 4482467a..dcd8bf23 100644 --- a/Lawo.EmberPlusSharp/Model/Element.cs +++ b/Lawo.EmberPlusSharp/Model/Element.cs @@ -193,38 +193,38 @@ internal void SetConsumerValue(ref T field, T newValue, [CallerMemberName] st } } - internal virtual void SetChildrenState(bool isEmpty, ref ChildrenState newChildrenState) + internal virtual void SetRequestState(bool isEmpty, ref RequestState newRequestState) { - newChildrenState = ChildrenState.Complete; + newRequestState = RequestState.Complete; } - internal abstract ChildrenState ReadContents(EmberReader reader, ElementType actualType); + internal abstract RequestState ReadContents(EmberReader reader, ElementType actualType); /// Recursively reads the children of an element as they appear in the message payload and returns /// the state of this element. /// /// Nodes for which an empty children collection is received are marked with - /// . The same happens if the collection only contains children we're not + /// . The same happens if the collection only contains children we're not /// interested in. In all other cases, the state of the node is lowered to the lowest state of the /// interesting children appearing in the payload. /// This approach ensures that any node for which incomplete interesting children have been received will - /// be visited by . This is necessary because some providers send messages with + /// be visited by . This is necessary because some providers send messages with /// payloads where the same node appears multiple times. For example, the first time the state of the node may - /// be set to , due to the fact that there are indirect children for which a - /// getDirectory request needs to be sent. The second time the node appears only with direct and indirect - /// children that have the state . Now the state of the node cannot be set - /// to because then no getDirectory requests would be issued for the children - /// that appeared the first time. + /// be set to , due to the fact that there are indirect children for which a + /// getDirectory request needs to be sent. The second time the node appears only with direct and + /// indirect children that have the state . Now the state of the node cannot + /// be set to because then no getDirectory requests would be + /// issued for the children that appeared the first time. /// It follows that the state of a node cannot be set to its definitive value while its children are read. /// Instead there needs to be a second step that visits all affected nodes and updates their state, which is - /// implemented by . - internal virtual ChildrenState ReadChildren(EmberReader reader) + /// implemented by . + internal virtual RequestState ReadChildren(EmberReader reader) { reader.Skip(); - return ChildrenState.Complete; + return RequestState.Complete; } - internal virtual ChildrenState ReadQualifiedChild( + internal virtual RequestState ReadQualifiedChild( EmberReader reader, ElementType actualType, int[] path, int index) { const string Format = @@ -238,21 +238,21 @@ internal virtual void ReadAdditionalFields(EmberReader reader) } /// Recursively updates the state of all children and returns the state of this element. - /// Only the children with a state not equal to are visited. + /// Only the children with a state not equal to are visited. /// The state of a node is set to the lowest state of its children. If a node without children has the state - /// and does not require children, the state is lifted to - /// . In all other cases, the state is left as is. - internal virtual ChildrenState UpdateChildrenState(bool throwForMissingRequiredChildren) + /// and does not require children, the state is lifted to + /// . In all other cases, the state is left as is. + internal virtual RequestState UpdateRequestState(bool throwForMissingRequiredChildren) { - return ChildrenState.Verified; + return RequestState.Verified; } - /// Writes the payload of a message that contains a getDirectory request for all nodes that require + /// Writes the payload of a message that contains an appropriate requests for all elements that require /// one. - /// Recursively visits all children with a state equal to , writes - /// a getDirectory request for nodes that do not yet have children and marks them with - /// . - internal virtual void WriteChildrenQuery(EmberWriter writer) + /// Recursively visits all children with a state equal to , writes + /// a getDirectory request for nodes that do not yet have children or a subscribe request for stream parameters + /// and changes their state accordingly. + internal virtual void WriteRequest(EmberWriter writer) { } diff --git a/Lawo.EmberPlusSharp/Model/Element1.cs b/Lawo.EmberPlusSharp/Model/Element1.cs index e65f8724..ecafc06e 100644 --- a/Lawo.EmberPlusSharp/Model/Element1.cs +++ b/Lawo.EmberPlusSharp/Model/Element1.cs @@ -81,10 +81,10 @@ internal static TMostDerived Construct(Context context) } internal static TMostDerived ReadContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState requestState) { var result = Construct(context); - childrenState = result.ReadContents(reader, actualType); + requestState = result.ReadContents(reader, actualType); return result; } diff --git a/Lawo.EmberPlusSharp/Model/EnumParameter.cs b/Lawo.EmberPlusSharp/Model/EnumParameter.cs index b7e2b289..5827c13d 100644 --- a/Lawo.EmberPlusSharp/Model/EnumParameter.cs +++ b/Lawo.EmberPlusSharp/Model/EnumParameter.cs @@ -30,7 +30,7 @@ internal sealed override IReadOnlyList> EnumMapCore set { this.impl.EnumMapCore = value; } } - internal sealed override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal sealed override RequestState ReadContents(EmberReader reader, ElementType actualType) { return this.impl.ReadContents(base.ReadContents, reader, actualType); } diff --git a/Lawo.EmberPlusSharp/Model/EnumParameterImpl.cs b/Lawo.EmberPlusSharp/Model/EnumParameterImpl.cs index fcfd5f5d..3d008b3e 100644 --- a/Lawo.EmberPlusSharp/Model/EnumParameterImpl.cs +++ b/Lawo.EmberPlusSharp/Model/EnumParameterImpl.cs @@ -72,8 +72,8 @@ internal IReadOnlyList> EnumMapCore } [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", Justification = "Official Glow name.")] - internal ChildrenState ReadContents( - Func baseImpl, EmberReader reader, ElementType actualType) + internal RequestState ReadContents( + Func baseImpl, EmberReader reader, ElementType actualType) { if (!IsEnum) { diff --git a/Lawo.EmberPlusSharp/Model/FieldNode.cs b/Lawo.EmberPlusSharp/Model/FieldNode.cs index eb3e989b..f87fb703 100644 --- a/Lawo.EmberPlusSharp/Model/FieldNode.cs +++ b/Lawo.EmberPlusSharp/Model/FieldNode.cs @@ -51,9 +51,9 @@ public abstract partial class FieldNode : Node //////////////////////////////////////////////////////////////////////////////////////////////////////////////// internal virtual Element ReadNewDynamicChildContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState) { - return base.ReadNewChildContents(reader, actualType, context, out childChildrenState); + return base.ReadNewChildContents(reader, actualType, context, out childRequestState); } [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] @@ -73,12 +73,12 @@ internal override bool ChangeOnlineStatus(IElement child) [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] internal sealed override Element ReadNewChildContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState) { MetaElement metaChild; return MetaChildren.TryGetValue(context.Identifier, out metaChild) ? - metaChild.ReadContents(reader, actualType, context, out childChildrenState) : - this.ReadNewDynamicChildContents(reader, actualType, context, out childChildrenState); + metaChild.ReadContents(reader, actualType, context, out childRequestState) : + this.ReadNewDynamicChildContents(reader, actualType, context, out childRequestState); } internal sealed override bool AreRequiredChildrenAvailable(bool throwIfMissing) diff --git a/Lawo.EmberPlusSharp/Model/FunctionBase.cs b/Lawo.EmberPlusSharp/Model/FunctionBase.cs index c7e4a630..5371aa20 100644 --- a/Lawo.EmberPlusSharp/Model/FunctionBase.cs +++ b/Lawo.EmberPlusSharp/Model/FunctionBase.cs @@ -70,7 +70,7 @@ internal Task InvokeCoreAsync( return invokeResult.Task; } - internal override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal override RequestState ReadContents(EmberReader reader, ElementType actualType) { this.AssertElementType(ElementType.Function, actualType); var argumentsRead = false; @@ -102,7 +102,7 @@ internal override ChildrenState ReadContents(EmberReader reader, ElementType act throw this.CreateSignatureMismatchException(); } - return ChildrenState.Complete; + return RequestState.Complete; } internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection invocationCollection) diff --git a/Lawo.EmberPlusSharp/Model/Matrix.cs b/Lawo.EmberPlusSharp/Model/Matrix.cs index f11143f6..00ba5e38 100644 --- a/Lawo.EmberPlusSharp/Model/Matrix.cs +++ b/Lawo.EmberPlusSharp/Model/Matrix.cs @@ -107,7 +107,7 @@ INode IMatrix.Parameters get { return this.Parameters; } } - internal override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal override RequestState ReadContents(EmberReader reader, ElementType actualType) { ////this.AssertElementType(ElementType.Parameter, actualType); @@ -185,7 +185,7 @@ internal override ChildrenState ReadContents(EmberReader reader, ElementType act ////} ////this.SetFinalTytpe(valueType, enumType, typeType); - return ChildrenState.Complete; + return RequestState.Complete; } internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection invocationCollection) diff --git a/Lawo.EmberPlusSharp/Model/MetaElement.cs b/Lawo.EmberPlusSharp/Model/MetaElement.cs index 536a7960..4a784e7a 100644 --- a/Lawo.EmberPlusSharp/Model/MetaElement.cs +++ b/Lawo.EmberPlusSharp/Model/MetaElement.cs @@ -42,7 +42,7 @@ internal string Identifier } internal abstract Element ReadContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childrenState); + EmberReader reader, ElementType actualType, Context context, out RequestState requestState); internal abstract bool IsAvailable(IParent parent, bool throwIfMissing); diff --git a/Lawo.EmberPlusSharp/Model/MetaElement1.cs b/Lawo.EmberPlusSharp/Model/MetaElement1.cs index 36458cfe..95a56748 100644 --- a/Lawo.EmberPlusSharp/Model/MetaElement1.cs +++ b/Lawo.EmberPlusSharp/Model/MetaElement1.cs @@ -38,9 +38,9 @@ internal MetaElement(PropertyInfo property) : base(property) [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] internal sealed override Element ReadContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState requestState) { - return Element.ReadContents(reader, actualType, context, out childrenState); + return Element.ReadContents(reader, actualType, context, out requestState); } [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", Justification = "Method is not public, CA bug?")] diff --git a/Lawo.EmberPlusSharp/Model/Node.cs b/Lawo.EmberPlusSharp/Model/Node.cs index 3fb22bc8..93e3cfb2 100644 --- a/Lawo.EmberPlusSharp/Model/Node.cs +++ b/Lawo.EmberPlusSharp/Model/Node.cs @@ -85,7 +85,7 @@ internal override bool ChangeOnlineStatus(IElement child) return base.ChangeOnlineStatus(child); } - internal sealed override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal sealed override RequestState ReadContents(EmberReader reader, ElementType actualType) { this.AssertElementType(ElementType.Node, actualType); @@ -101,8 +101,8 @@ internal sealed override ChildrenState ReadContents(EmberReader reader, ElementT break; case GlowNodeContents.IsOnline.OuterNumber: this.IsOnline = reader.AssertAndReadContentsAsBoolean(); - var newChildrenState = this.ChildrenState & ChildrenState.Complete; - this.SetChildrenState(false, ref newChildrenState); + var newRequestState = this.RequestState & RequestState.Complete; + this.SetRequestState(false, ref newRequestState); break; case GlowNodeContents.SchemaIdentifiers.OuterNumber: this.ReadSchemaIdentifiers(reader); @@ -113,7 +113,7 @@ internal sealed override ChildrenState ReadContents(EmberReader reader, ElementT } } - return this.ChildrenState; + return this.RequestState; } internal override void WriteChanges(EmberWriter writer, IInvocationCollection invocationCollection) diff --git a/Lawo.EmberPlusSharp/Model/NodeBase.cs b/Lawo.EmberPlusSharp/Model/NodeBase.cs index c51b6f26..022bd8a8 100644 --- a/Lawo.EmberPlusSharp/Model/NodeBase.cs +++ b/Lawo.EmberPlusSharp/Model/NodeBase.cs @@ -24,13 +24,13 @@ public abstract class NodeBase : ElementWithSchemas, { private readonly SortedDictionary children = new SortedDictionary(); - /// See for more information. - /// This field and its sibling are modified by the following + /// See for more information. + /// This field and its sibling are modified by the following /// methods, which are directly or indirectly called from /// : /// - /// - /// + /// + /// /// /// /// @@ -45,8 +45,8 @@ public abstract class NodeBase : ElementWithSchemas, /// are changed at once in a large tree. /// /// - private ChildrenState onlineChildrenState; - private ChildrenState offlineChildrenState = ChildrenState.Complete; + private RequestState onlineRequestState; + private RequestState offlineRequestState = RequestState.Complete; //////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -74,22 +74,22 @@ internal NodeBase() { } - internal ChildrenState ChildrenState + internal RequestState RequestState { get { - return this.IsOnline ? this.onlineChildrenState : this.offlineChildrenState; + return this.IsOnline ? this.onlineRequestState : this.offlineRequestState; } private set { if (this.IsOnline) { - this.onlineChildrenState = value; + this.onlineRequestState = value; } else { - this.offlineChildrenState = value; + this.offlineRequestState = value; } } } @@ -112,13 +112,13 @@ internal void WriteChildrenQueryCollection(EmberWriter writer) writer.WriteStartApplicationDefinedType(GlowElementCollection.Element.OuterId, GlowCommand.InnerNumber); writer.WriteValue(GlowCommand.Number.OuterId, 32); writer.WriteEndContainer(); - this.ChildrenState = ChildrenState.GetDirectorySent; + this.RequestState = RequestState.RequestSent; } else { foreach (var child in this.children.Values) { - child.WriteChildrenQuery(writer); + child.WriteRequest(writer); } } } @@ -132,10 +132,10 @@ internal void WriteChangesCollection(EmberWriter writer, IInvocationCollection i } internal virtual Element ReadNewChildContents( - EmberReader reader, ElementType actualType, Context context, out ChildrenState childChildrenState) + EmberReader reader, ElementType actualType, Context context, out RequestState childRequestState) { reader.SkipToEndContainer(); - childChildrenState = ChildrenState.Complete; + childRequestState = RequestState.Complete; return null; } @@ -176,27 +176,27 @@ internal virtual bool ChangeOnlineStatus(IElement child) return false; } - internal sealed override void SetChildrenState(bool isEmpty, ref ChildrenState newChildrenState) + internal sealed override void SetRequestState(bool isEmpty, ref RequestState newRequestState) { if (isEmpty) { - base.SetChildrenState(isEmpty, ref newChildrenState); + base.SetRequestState(isEmpty, ref newRequestState); } - this.ChildrenState = newChildrenState; + this.RequestState = newRequestState; } - internal sealed override ChildrenState ReadChildren(EmberReader reader) + internal sealed override RequestState ReadChildren(EmberReader reader) { if (this.ReadChildrenCore(reader)) { - this.ChildrenState = ChildrenState.Complete; + this.RequestState = RequestState.Complete; } - return this.ChildrenState; + return this.RequestState; } - internal sealed override ChildrenState ReadQualifiedChild( + internal sealed override RequestState ReadQualifiedChild( EmberReader reader, ElementType actualType, int[] path, int index) { if (index == path.Length - 1) @@ -213,41 +213,41 @@ internal sealed override ChildrenState ReadQualifiedChild( } else { - this.ChildrenState &= child.ReadQualifiedChild(reader, actualType, path, index + 1); + this.RequestState &= child.ReadQualifiedChild(reader, actualType, path, index + 1); } } - return this.ChildrenState; + return this.RequestState; } - internal sealed override ChildrenState UpdateChildrenState(bool throwForMissingRequiredChildren) + internal sealed override RequestState UpdateRequestState(bool throwForMissingRequiredChildren) { if (!this.IsOnline) { - this.ChildrenState = ChildrenState.Verified; + this.RequestState = RequestState.Verified; } else if (this.children.Count == 0) { - if (this.ChildrenState.Equals(ChildrenState.Complete) && + if (this.RequestState.Equals(RequestState.Complete) && this.AreRequiredChildrenAvailable(throwForMissingRequiredChildren)) { - this.ChildrenState = ChildrenState.Verified; + this.RequestState = RequestState.Verified; } } else { - if (!this.ChildrenState.Equals(ChildrenState.Verified)) + if (!this.RequestState.Equals(RequestState.Verified)) { - var accumulatedChildChildrenState = ChildrenState.Verified; + var accumulatedChildRequestState = RequestState.Verified; foreach (var child in this.children.Values) { - var childChildrenState = child.UpdateChildrenState(throwForMissingRequiredChildren); - accumulatedChildChildrenState &= childChildrenState; + var childRequestState = child.UpdateRequestState(throwForMissingRequiredChildren); + accumulatedChildRequestState &= childRequestState; if (child.IsOnlineChangeStatus != IsOnlineChangeStatus.Unchanged) { - if ((child.IsOnline && childChildrenState.Equals(ChildrenState.Verified)) || !child.IsOnline) + if ((child.IsOnline && childRequestState.Equals(RequestState.Verified)) || !child.IsOnline) { child.IsOnlineChangeStatus = IsOnlineChangeStatus.Unchanged; this.ChangeOnlineStatus(child); @@ -255,17 +255,17 @@ internal sealed override ChildrenState UpdateChildrenState(bool throwForMissingR } } - this.ChildrenState = - this.GetChildrenState(throwForMissingRequiredChildren, accumulatedChildChildrenState); + this.RequestState = + this.GetRequestState(throwForMissingRequiredChildren, accumulatedChildRequestState); } } - return this.ChildrenState; + return this.RequestState; } - internal override void WriteChildrenQuery(EmberWriter writer) + internal override void WriteRequest(EmberWriter writer) { - if (this.ChildrenState.Equals(ChildrenState.None)) + if (this.RequestState.Equals(RequestState.None)) { var isEmpty = this.children.Count == 0; @@ -295,12 +295,12 @@ internal sealed override void SetComplete() child.SetComplete(); } - this.ChildrenState = ChildrenState.Complete; + this.RequestState = RequestState.Complete; } internal sealed override IParent GetFirstIncompleteChild() { - if (this.ChildrenState.Equals(ChildrenState.GetDirectorySent)) + if (this.RequestState.Equals(RequestState.RequestSent)) { return this.children.Count == 0 ? this : this.children.Values.Select(c => c.GetFirstIncompleteChild()).FirstOrDefault(c => c != null); @@ -315,7 +315,7 @@ internal sealed override IParent GetFirstIncompleteChild() private void ReadChild(EmberReader reader, ElementType actualType, int number) { - var childChildrenState = ChildrenState.Complete; + var childRequestState = RequestState.Complete; Element child; this.children.TryGetValue(number, out child); var isEmpty = true; @@ -327,7 +327,7 @@ private void ReadChild(EmberReader reader, ElementType actualType, int number) if (contextSpecificOuterNumber == GlowNode.Contents.OuterNumber) { - this.ReadChildContents(reader, actualType, number, ref child, out childChildrenState); + this.ReadChildContents(reader, actualType, number, ref child, out childRequestState); } else { @@ -340,7 +340,7 @@ private void ReadChild(EmberReader reader, ElementType actualType, int number) if (contextSpecificOuterNumber == GlowNode.Children.OuterNumber) { reader.AssertInnerNumber(GlowElementCollection.InnerNumber); - childChildrenState = child.ReadChildren(reader); + childRequestState = child.ReadChildren(reader); } else { @@ -352,11 +352,11 @@ private void ReadChild(EmberReader reader, ElementType actualType, int number) if (child != null) { - child.SetChildrenState(isEmpty, ref childChildrenState); + child.SetRequestState(isEmpty, ref childRequestState); } - this.ChildrenState = - (this.children.Count == 0 ? ChildrenState.Complete : this.ChildrenState) & childChildrenState; + this.RequestState = + (this.children.Count == 0 ? RequestState.Complete : this.RequestState) & childRequestState; } private void ReadChildContents( @@ -364,13 +364,13 @@ private void ReadChildContents( ElementType actualType, int number, ref Element child, - out ChildrenState childChildrenState) + out RequestState childRequestState) { reader.AssertInnerNumber(InnerNumber.Set); if (child != null) { - childChildrenState = child.ReadContents(reader, actualType); + childRequestState = child.ReadContents(reader, actualType); } else { @@ -379,7 +379,7 @@ private void ReadChildContents( if (reader.CanReadContents && (reader.OuterId == GlowNodeContents.Identifier.OuterId)) { var context = new Context(this, number, reader.AssertAndReadContentsAsString()); - child = this.ReadNewChildContents(reader, actualType, context, out childChildrenState); + child = this.ReadNewChildContents(reader, actualType, context, out childRequestState); if (child != null) { @@ -389,23 +389,23 @@ private void ReadChildContents( else { reader.SkipToEndContainer(); - childChildrenState = ChildrenState.Complete; + childRequestState = RequestState.Complete; child = null; } } } - private ChildrenState GetChildrenState( - bool throwForMissingRequiredChildren, ChildrenState accumulatedChildChildrenState) + private RequestState GetRequestState( + bool throwForMissingRequiredChildren, RequestState accumulatedChildRequestState) { - if (accumulatedChildChildrenState.Equals(ChildrenState.Verified)) + if (accumulatedChildRequestState.Equals(RequestState.Verified)) { return this.AreRequiredChildrenAvailable(throwForMissingRequiredChildren) ? - ChildrenState.Verified : ChildrenState.Complete; + RequestState.Verified : RequestState.Complete; } else { - return accumulatedChildChildrenState; + return accumulatedChildRequestState; } } } diff --git a/Lawo.EmberPlusSharp/Model/NullableEnumParameter.cs b/Lawo.EmberPlusSharp/Model/NullableEnumParameter.cs index d9423283..9b9cd427 100644 --- a/Lawo.EmberPlusSharp/Model/NullableEnumParameter.cs +++ b/Lawo.EmberPlusSharp/Model/NullableEnumParameter.cs @@ -31,7 +31,7 @@ internal sealed override IReadOnlyList> EnumMapCore set { this.impl.EnumMapCore = value; } } - internal sealed override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal sealed override RequestState ReadContents(EmberReader reader, ElementType actualType) { return this.impl.ReadContents(base.ReadContents, reader, actualType); } diff --git a/Lawo.EmberPlusSharp/Model/Parameter.cs b/Lawo.EmberPlusSharp/Model/Parameter.cs index d664ec82..8b611f3c 100644 --- a/Lawo.EmberPlusSharp/Model/Parameter.cs +++ b/Lawo.EmberPlusSharp/Model/Parameter.cs @@ -23,7 +23,7 @@ internal Parameter() { } - internal override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal override RequestState ReadContents(EmberReader reader, ElementType actualType) { var result = base.ReadContents(reader, actualType); diff --git a/Lawo.EmberPlusSharp/Model/ParameterBase.cs b/Lawo.EmberPlusSharp/Model/ParameterBase.cs index 6c0af205..bed5c4b8 100644 --- a/Lawo.EmberPlusSharp/Model/ParameterBase.cs +++ b/Lawo.EmberPlusSharp/Model/ParameterBase.cs @@ -220,7 +220,7 @@ internal virtual TValue AssertValueType(object value) } } - internal override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal override RequestState ReadContents(EmberReader reader, ElementType actualType) { this.AssertElementType(ElementType.Parameter, actualType); @@ -298,7 +298,7 @@ internal override ChildrenState ReadContents(EmberReader reader, ElementType act } this.SetFinalTytpe(valueType, enumType, typeType); - return ChildrenState.Complete; + return RequestState.Complete; } internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollection invocationCollection) diff --git a/Lawo.EmberPlusSharp/Model/RequestState.cs b/Lawo.EmberPlusSharp/Model/RequestState.cs new file mode 100644 index 00000000..54b9f73f --- /dev/null +++ b/Lawo.EmberPlusSharp/Model/RequestState.cs @@ -0,0 +1,86 @@ +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Copyright 2012-2015 Lawo AG (http://www.lawo.com). +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace Lawo.EmberPlusSharp.Model +{ + using System; + + /// Represents the state of an element with regards to whether a request needs to be sent to the provider + /// and whether all expected information has been received. + /// The state of an element is always propagated up the chain of its direct and indirect parents such that + /// a node always assumes the state of the child with the lowest state. + internal struct RequestState : IEquatable + { + private static readonly RequestState NoneField = new RequestState(); + private static readonly RequestState RequestSentField = new RequestState(1); + private static readonly RequestState CompleteField = new RequestState(2); + private static readonly RequestState VerifiedField = new RequestState(3); + + private readonly int state; + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + /// Returns the lowest state of and . + /// Note that this is a generalization of the boolean & operator to more than two states. Also + /// known as Fuzzy Logic. + public static RequestState operator &(RequestState left, RequestState right) + { + return new RequestState(Math.Min(left.state, right.state)); + } + + public bool Equals(RequestState other) + { + return this.state == other.state; + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + /// Gets the state used when a request needs to be issued for an element. + /// This is the lowest state. + internal static RequestState None + { + get { return NoneField; } + } + + /// Gets the state used when a request has been sent for a node. + /// This is the second lowest state. + internal static RequestState RequestSent + { + get { return RequestSentField; } + } + + /// Gets the state used for subscribed stream parameters and nodes for which the provider has sent a + /// response. + /// + /// This state is used for the following two elements: + /// + /// Parameter: When a stream subscription has been sent. + /// Node: When the provider has sent an empty children collection or only children that are either + /// complete or ones we're not interested in. + /// + /// This is the second highest state. + /// + internal static RequestState Complete + { + get { return CompleteField; } + } + + /// Gets the state used when we have verified that all required children are present for a node. + /// + /// This is the highest state. + internal static RequestState Verified + { + get { return VerifiedField; } + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + private RequestState(int state) + { + this.state = state; + } + } +} diff --git a/Lawo.EmberPlusSharp/Model/Root.cs b/Lawo.EmberPlusSharp/Model/Root.cs index 980f1155..8ec225a2 100644 --- a/Lawo.EmberPlusSharp/Model/Root.cs +++ b/Lawo.EmberPlusSharp/Model/Root.cs @@ -126,7 +126,7 @@ 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 WriteChildrenQuery(EmberWriter writer) + internal sealed override void WriteRequest(EmberWriter writer) { writer.WriteStartApplicationDefinedType(GlowGlobal.Root.OuterId, GlowRootElementCollection.InnerNumber); this.WriteChildrenQueryCollection(writer); diff --git a/Lawo.EmberPlusSharp/Model/ShadowNode.cs b/Lawo.EmberPlusSharp/Model/ShadowNode.cs index 900dc63f..7330fedc 100644 --- a/Lawo.EmberPlusSharp/Model/ShadowNode.cs +++ b/Lawo.EmberPlusSharp/Model/ShadowNode.cs @@ -20,7 +20,7 @@ internal ShadowNode() } [SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", Justification = "Official Glow name.")] - internal sealed override ChildrenState ReadContents(EmberReader reader, ElementType actualType) + internal sealed override RequestState ReadContents(EmberReader reader, ElementType actualType) { throw new ModelException("Unexpected contents for intermediate node in basePath."); }