Exemplo n.º 1
0
        public GlowInvocationResult Invoke(GlowInvocation invocation)
        {
            GlowValue[] arguments;
            int?        invocationId;

            if (invocation == null)
            {
                if (Arguments != null)
                {
                    throw new ArgumentException("Function with parameters called without arguments!");
                }

                arguments    = null;
                invocationId = null;
            }
            else
            {
                var argumentValues = invocation.ArgumentValues;

                arguments = argumentValues != null
                        ? argumentValues.ToArray()
                        : null;

                invocationId = invocation.InvocationId;

                AssertValueTypes(arguments, Arguments);
            }

            if (invocationId == null && HasResult)
            {
                throw new ArgumentException("Function with result called without invocation id!");
            }

            var result = _coreFunc(arguments);

            AssertValueTypes(result, Result);

            if (invocationId != null)
            {
                var invocationResult = GlowInvocationResult.CreateRoot(invocationId.Value);

                if (result != null)
                {
                    invocationResult.ResultValues = result;
                }

                return(invocationResult);
            }

            return(null);
        }
        GlowContainer ConvertInvocationResult(XElement xml)
        {
            var glow = GlowInvocationResult.CreateRoot(0);

            glow.InvocationId = XmlConvert.ToInt32(xml.Attribute("invocationId").Value);
            xml.Element("success").Do(value => glow.Success = XmlConvert.ToBoolean(value));

            var resultXml = xml.Element("result");

            if (resultXml != null)
            {
                glow.ResultValues = from valueXml in resultXml.Elements("Value")
                                    let glowValue = ConvertValue(valueXml)
                                                    where glowValue != null
                                                    select glowValue;
            }

            return(glow);
        }
        object IGlowVisitor <XmlWriter, object> .Visit(GlowInvocationResult glow, XmlWriter state)
        {
            state.WriteStartElement("Root");
            state.WriteAttributeString("type", "InvocationResult");
            state.WriteAttributeString("invocationId", XmlConvert.ToString(glow.InvocationId));

            glow.Success.Do(value => state.WriteElementString("success", XmlConvert.ToString(value)));

            var result = glow.ResultValues;

            if (result != null)
            {
                state.WriteStartElement("result");
                foreach (var value in result)
                {
                    ConvertValue("Value", value, state);
                }
                state.WriteEndElement();
            }

            state.WriteEndElement();
            return(null);
        }
Exemplo n.º 4
0
 bool IGlowVisitor <object, bool> .Visit(GlowInvocationResult glow, object state)
 {
     return(false);
 }
 protected override void OnInvocationResult(GlowInvocationResult glow)
 {
 }
            protected override void OnCommand(GlowCommand glow, int[] path)
            {
                IDynamicPathHandler dynamicPathHandler;
                var parent = _dispatcher.Root.ResolveChild(path, out dynamicPathHandler);

                if (parent != null)
                {
                    if (glow.Number == GlowCommandType.GetDirectory)
                    {
                        var glowRoot = GlowRootElementCollection.CreateRoot();
                        var options  = new ElementToGlowOptions {
                            DirFieldMask = glow.DirFieldMask ?? GlowFieldFlags.All
                        };

                        var visitor = new InlineElementVisitor(
                            node =>
                        {
                            // "dir" in node
                            if (node.ChildrenCount == 0)
                            {
                                glowRoot.Insert(new GlowQualifiedNode(node.Path));
                            }
                            else
                            {
                                var glowChildren = from element in node.Children select _dispatcher.ElementToGlow(element, options);
                                foreach (var glowChild in glowChildren)
                                {
                                    glowRoot.Insert(glowChild);
                                }
                            }
                        },
                            parameter =>
                        {
                            // "dir" in parameter
                            glowRoot.Insert(_dispatcher.ElementToGlow(parameter, options));
                        },
                            matrix =>
                        {
                            // "dir" in matrix
                            options.IsCompleteMatrixEnquired = true;
                            glowRoot.Insert(_dispatcher.ElementToGlow(matrix, options));
                            _source.SubscribeToMatrix(matrix, subscribe: true);
                        },
                            function =>
                        {
                            // "dir" in function
                            glowRoot.Insert(_dispatcher.ElementToGlow(function, options));
                        });

                        parent.Accept(visitor, null); // run inline visitor against parent

                        _source.Write(glowRoot);
                    }
                    else if (glow.Number == GlowCommandType.Unsubscribe)
                    {
                        var visitor = new InlineElementVisitor(onMatrix: matrix => _source.SubscribeToMatrix(matrix, subscribe: false));
                        parent.Accept(visitor, null); // run inline visitor against parent
                    }
                    else if (glow.Number == GlowCommandType.Invoke)
                    {
                        var visitor = new InlineElementVisitor(
                            onFunction: async function =>
                        {
                            var invocation       = glow.Invocation;
                            var invocationResult = null as GlowInvocationResult;

                            try
                            {
                                invocationResult = await function.Invoke(invocation);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine($"Exception: Dispatcher/OnCommand/Invoke: {ex.Message}");
                                if (invocation != null && invocation.InvocationId != null)
                                {
                                    invocationResult         = GlowInvocationResult.CreateRoot(invocation.InvocationId.Value);
                                    invocationResult.Success = false;
                                }
                            }

                            if (invocationResult != null)
                            {
                                _source.Write(invocationResult);
                            }
                        });

                        parent.Accept(visitor, null); // run inline visitor against parent
                    }
                }
                else
                {
                    if (dynamicPathHandler != null)
                    {
                        dynamicPathHandler.HandleCommand(glow, path, _source);
                    }
                }
            }
Exemplo n.º 7
0
 public IEnumerable <GlowContainer> Visit(GlowInvocationResult glow, object state)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 8
0
 public GlowContainer Visit(GlowInvocationResult glow, object state)
 {
     return(glow);
 }
Exemplo n.º 9
0
        void Test_Functions()
        {
            Action <EmberSequence, string> fillTupleDescription =
                (sequence, namePrefix) =>
            {
                sequence.Insert(new GlowTupleItemDescription(GlowParameterType.Integer)
                {
                    Name = namePrefix + "1:integer"
                });
                sequence.Insert(new GlowTupleItemDescription(GlowParameterType.Boolean)
                {
                    Name = namePrefix + "2:boolean"
                });
                sequence.Insert(new GlowTupleItemDescription(GlowParameterType.Octets)
                {
                    Name = namePrefix + "3:octets"
                });
                sequence.Insert(new GlowTupleItemDescription(GlowParameterType.Real)
                {
                    Name = namePrefix + "4:real"
                });
                sequence.Insert(new GlowTupleItemDescription(GlowParameterType.String)
                {
                    Name = namePrefix + "5:string"
                });
            };

            // --------------- Invocation Command
            var glowCommand = new GlowCommand(GlowCommandType.Invoke)
            {
                Invocation = new GlowInvocation(GlowTags.Command.Invocation)
                {
                    InvocationId = 123,
                },
            };

            var argsTuple = glowCommand.Invocation.EnsureArguments();

            argsTuple.Insert(new IntegerEmberLeaf(GlowTags.CollectionItem, 456));
            argsTuple.Insert(new BooleanEmberLeaf(GlowTags.CollectionItem, true));
            argsTuple.Insert(new OctetStringEmberLeaf(GlowTags.CollectionItem, new byte[] { 250, 251, 253 }));
            argsTuple.Insert(new RealEmberLeaf(GlowTags.CollectionItem, 123.321));
            argsTuple.Insert(new StringEmberLeaf(GlowTags.CollectionItem, "hallo"));

            AssertCodecSanity(glowCommand);
            Console.WriteLine(GetGlowXml(glowCommand));

            // --------------- Function
            var glowFunction = new GlowFunction(100)
            {
                Identifier  = "testFunction",
                Description = "Test Function",
            };

            fillTupleDescription(glowFunction.EnsureArguments(), "arg");
            fillTupleDescription(glowFunction.EnsureResult(), "res");
            glowFunction.EnsureChildren().Insert(glowCommand);

            AssertCodecSanity(glowFunction);
            Console.WriteLine(GetXml(glowFunction));

            // --------------- QualifiedFunction
            var glowQualifiedFunction = new GlowQualifiedFunction(new[] { 1, 2, 3 })
            {
                Identifier  = "testFunction",
                Description = "Test Function",
            };

            fillTupleDescription(glowQualifiedFunction.EnsureArguments(), "arg");
            fillTupleDescription(glowQualifiedFunction.EnsureResult(), "res");
            glowQualifiedFunction.EnsureChildren().Insert(glowCommand);

            AssertCodecSanity(glowQualifiedFunction);
            Console.WriteLine(GetXml(glowQualifiedFunction));

            var glowRoot = GlowRootElementCollection.CreateRoot();

            glowRoot.Insert(glowQualifiedFunction);
            AssertGlowXmlSanity(glowRoot);

            // --------------- InvocationResult
            var glowInvocationResult = GlowInvocationResult.CreateRoot(glowCommand.Invocation.InvocationId.Value);
            var resTuple             = glowInvocationResult.EnsureResult();

            resTuple.Insert(new IntegerEmberLeaf(GlowTags.CollectionItem, 456));
            resTuple.Insert(new BooleanEmberLeaf(GlowTags.CollectionItem, true));
            resTuple.Insert(new OctetStringEmberLeaf(GlowTags.CollectionItem, new byte[] { 250, 251, 253 }));
            resTuple.Insert(new RealEmberLeaf(GlowTags.CollectionItem, 123.321));
            resTuple.Insert(new StringEmberLeaf(GlowTags.CollectionItem, "hallo"));

            AssertCodecSanity(glowInvocationResult);
            Console.WriteLine(GetXml(glowInvocationResult));

            AssertGlowXmlSanity(glowInvocationResult);
        }
Exemplo n.º 10
0
        public async Task <GlowInvocationResult> Invoke(GlowInvocation invocation)
        {
            GlowValue[] arguments;
            int?        invocationId;

            if (invocation == null)
            {
                if (Arguments != null)
                {
                    throw new ArgumentException("Function with parameters called without arguments!");
                }

                arguments    = null;
                invocationId = null;
            }
            else
            {
                var argumentValues = invocation.ArgumentValues;

                arguments = argumentValues != null
                            ? argumentValues.ToArray()
                            : null;

                // **********************************************
                // Added code
                // Functions without parameters can be defined in two (2) ways. Either 'args=null' or 'args=empty list'
                // - EmberViewer v1.6.2 sends args=null when function parameter is missing
                // - EmberViewer v2.4.0 sends args=empty list when function parameter is missing
                // So that both models will work, we create an empty list if args=null
                // and we create args=null if args=empty list
                if (Arguments != null && Arguments.Length == 0 && arguments == null)
                {
                    arguments = new GlowValue[0];
                }
                if (Arguments == null && arguments != null && arguments.Length == 0)
                {
                    arguments = null;
                }
                // ************************************************

                invocationId = invocation.InvocationId;

                AssertValueTypes(arguments, Arguments);
            }

            if (invocationId == null && HasResult)
            {
                throw new ArgumentException("Function with result called without invocation id!");
            }

            Debug.WriteLine($"Invoking function {IdentifierPath}");

            var result = await _coreFunc(arguments);

            AssertValueTypes(result, Result);

            if (invocationId != null)
            {
                var invocationResult = GlowInvocationResult.CreateRoot(invocationId.Value);

                if (result != null)
                {
                    invocationResult.ResultValues = result;
                }

                return(invocationResult);
            }

            return(null);
        }
Exemplo n.º 11
0
        public async Task <GlowInvocationResult> Invoke(GlowInvocation invocation)
        {
            GlowValue[] arguments;
            int?        invocationId;

            if (invocation == null)
            {
                if (Arguments != null)
                {
                    throw new ArgumentException("Function with parameters called without arguments!");
                }

                arguments    = null;
                invocationId = null;
            }
            else
            {
                var argumentValues = invocation.ArgumentValues;

                arguments = argumentValues != null
                            ? argumentValues.ToArray()
                            : null;

                // ******* Tillagd kod ****************************
                // Funktioner utan  parametrar kan definieras på två sätt. Antingen args=null eller args=tom lista.
                // Men EmberViewer v1.6.2 skickar args=null då funktionsparametrar saknas
                // och EmberViewer v2.4.0 skickar args=tom lista då funktionsparametrar saknas
                // För att båda modellerna ska fungera skapar vi tom lista om arg=null men förväntas vara tom lista
                // och argumenten sätts till null om argumenten förväntas vara null men är tom lista.
                if (Arguments != null && Arguments.Length == 0 && arguments == null)
                {
                    arguments = new GlowValue[0];
                }
                if (Arguments == null && arguments != null && arguments.Length == 0)
                {
                    arguments = null;
                }
                // ************************************************

                invocationId = invocation.InvocationId;

                AssertValueTypes(arguments, Arguments);
            }

            if (invocationId == null && HasResult)
            {
                throw new ArgumentException("Function with result called without invocation id!");
            }

            Log.Debug($"Invoking function {IdentifierPath}");

            var result = await _coreFunc(arguments);

            AssertValueTypes(result, Result);

            if (invocationId != null)
            {
                var invocationResult = GlowInvocationResult.CreateRoot(invocationId.Value);

                if (result != null)
                {
                    invocationResult.ResultValues = result;
                }

                return(invocationResult);
            }

            return(null);
        }
Exemplo n.º 12
0
 protected override void OnInvocationResult(GlowInvocationResult glow)
 {
 }