Example #1
0
        /// <summary>
        /// Synchronously connects to the remote host.
        /// </summary>
        /// <returns>True if the connection attempt completed successfully, otherwise false.</returns>
        public bool Connect()
        {
            Close();

            lock (_sync)
            {
                State      = GlowEndPointState.Connecting;
                _tcpClient = new TcpClient();

                try
                {
                    _tcpClient.Connect(HostName, TcpPort);

                    _glowReader        = new GlowReader(GlowReader_RootReady, GlowReader_KeepAliveRequestReceived);
                    _glowReader.Error += GlowReader_Error;

                    var stream = _tcpClient.GetStream();
                    stream.BeginRead(_buffer, 0, _buffer.Length, ReceiveCallback, stream);

                    State = GlowEndPointState.ProtocolProbing;

                    var glow = GlowRootElementCollection.CreateRoot();
                    glow.Insert(new GlowCommand(GlowCommandType.GetDirectory));
                    Write(glow);
                    return(true);
                }
                catch (Exception ex)
                {
                    State = GlowEndPointState.Error;
                    Console.WriteLine(ex.Message);
                }
            }

            return(false);
        }
Example #2
0
            protected override void OnParameter(GlowParameterBase glow, int[] path)
            {
                var glowValue = glow.Value;

                if (glowValue != null)
                {
                    var item = OpenItem(path, true);

                    if (item.IsValue)
                    {
                        var valueKind = item.Parent.Key.GetValueKind(item.Name);

                        switch (valueKind)
                        {
                        case RegistryValueKind.DWord:
                            item.Parent.Key.SetValue(item.Name, (int)glowValue.Integer);
                            break;

                        case RegistryValueKind.QWord:
                            item.Parent.Key.SetValue(item.Name, glowValue.Integer);
                            break;

                        case RegistryValueKind.String:
                            item.Parent.Key.SetValue(item.Name, glowValue.String);
                            break;
                        }

                        var glowRoot = GlowRootElementCollection.CreateRoot();
                        CreateGlowParameter(item, path, null, GlowFieldFlags.Value, glowRoot);
                        _client.Write(glowRoot);
                    }

                    CloseItem(item);
                }
            }
Example #3
0
        static GlowRootElementCollection BuildGlowTree(Element local, GlowElement glow)
        {
            while (local.Type != ElementType.Root)
            {
                GlowElement glowParent;

                if (local.Type == ElementType.Parameter)
                {
                    var param = new GlowParameter(local.Number);
                    param.Children = new GlowElementCollection(GlowTags.Parameter.Children);
                    param.Children.Insert(glow);
                    glowParent = param;
                }
                else
                {
                    var node = new GlowNode(local.Number);
                    node.Children = new GlowElementCollection(GlowTags.Node.Children);
                    node.Children.Insert(glow);
                    glowParent = node;
                }

                glow  = glowParent;
                local = local.Parent;
            }

            var root = GlowRootElementCollection.CreateRoot();

            root.Insert(glow);
            return(root);
        }
        public void NotifyParameterValueChanged(int[] parameterPath, GlowValue value)
        {
            var glowParam = new GlowQualifiedParameter(parameterPath)
            {
                Value = value,
            };

            var glow = GlowRootElementCollection.CreateRoot();

            glow.Insert(glowParam);

            OnGlowRootReady(new GlowRootReadyArgs(glow, null));
        }
Example #5
0
            public GlowContainer Visit(GlowRootElementCollection glow, object state)
            {
                var root = GlowRootElementCollection.CreateRoot();

                Walk(glow, RemapParameterStreamIdentifier, RemapMatrixParameterPaths);

                foreach (var element in glow.Elements)
                {
                    root.Insert(element.Accept(this, null));
                }

                return(root);
            }
Example #6
0
        void EndPoint_StateChanged(object sender, EventArgs e)
        {
            var glow     = GlowRootElementCollection.CreateRoot();
            var ep       = (Consumer.GlowEndPoint)sender;
            var glowNode = new GlowNode(ep.LocalNumber)
            {
                Identifier = ep.HostName + ":" + ep.TcpPort,
                IsOnline   = ep.State == Consumer.GlowEndPointState.Connected,
            };

            glow.Insert(glowNode);
            OnGlowFromProviderReady(new GlowFromProviderReadyArgs(glow));
        }
Example #7
0
            protected override void OnCommand(GlowCommand glow, int[] path)
            {
                if (glow.Number == GlowCommandType.GetDirectory)
                {
                    var glowRoot = GlowRootElementCollection.CreateRoot();
                    var fields   = glow.DirFieldMask ?? GlowFieldFlags.All;
                    var item     = OpenItem(path, false);

                    if (item.IsValue)
                    {
                        CreateGlowParameter(item, path, null, fields, glowRoot);
                    }
                    else
                    {
                        var children = item.Children;

                        if (children == null)
                        {
                            children      = GetSubItems(item);
                            item.Children = children;
                        }

                        if (children.Any())
                        {
                            var number = 1;

                            foreach (var child in children)
                            {
                                if (child.IsValue)
                                {
                                    CreateGlowParameter(child, path, number, fields, glowRoot);
                                }
                                else
                                {
                                    CreateGlowNode(child, path, number, fields, glowRoot);
                                }

                                number++;
                            }
                        }
                        else
                        {
                            CreateGlowNode(item, path, null, fields, glowRoot);
                        }
                    }

                    _client.Write(glowRoot);
                }
            }
        public void NotifyParameterValueChanged(ParameterBase parameter)
        {
            var options = new ElementToGlowOptions
            {
                DirFieldMask = GlowFieldFlags.Value
            };

            var glowParam = ElementToGlow(parameter, options);

            var glow = GlowRootElementCollection.CreateRoot();

            glow.Insert(glowParam);

            OnGlowRootReady(new GlowRootReadyArgs(glow, null));
        }
Example #9
0
            public IEnumerable <GlowContainer> Visit(GlowRootElementCollection glow, object state)
            {
                var root = GlowRootElementCollection.CreateRoot();

                foreach (var element in glow.Elements)
                {
                    var newElements = element.Accept(this, null);

                    foreach (var newElement in newElements)
                    {
                        root.Insert(newElement);
                    }
                }

                yield return(root);
            }
Example #10
0
        public void DispatchGlow(GlowContainer glow, Provider.Client source)
        {
            var visitor = new ConsumerToProviderTranslator(_endPoints);
            var newGlow = glow.Accept(visitor, null).FirstOrDefault();

            if (newGlow != null)
            {
                if (visitor.EndPointNumber != null)
                {
                    // message to provider
                    var endPoint = (from ep in _endPoints
                                    where ep.LocalNumber == visitor.EndPointNumber.Value
                                    select ep)
                                   .FirstOrDefault();

                    if (endPoint.State == Consumer.GlowEndPointState.Connected ||
                        endPoint.State == Consumer.GlowEndPointState.ProtocolProbing)
                    {
                        endPoint.Write(newGlow);
                    }
                    else
                    {
                        // if endpoint is offline, and a dir command has been issued
                        // on the endpoint node (top-level node) -> send empty top-level node
                        if (newGlow is GlowRootElementCollection)
                        {
                            var command = newGlow.FirstOrDefault() as GlowCommand;

                            if (command != null &&
                                command.Number == GlowCommandType.GetDirectory)
                            {
                                var root = GlowRootElementCollection.CreateRoot();
                                root.Insert(new GlowNode(endPoint.LocalNumber));
                                source.Write(root);
                            }
                        }
                    }
                }
                else
                {
                    // message to proxy - usually command at root level
                    source.Write(newGlow);
                }
            }
        }
        GlowContainer ConvertRoot(XElement xml)
        {
            switch (xml.Attribute("type").Value)
            {
            case "ElementCollection":
                var glow = GlowRootElementCollection.CreateRoot();
                FillElementCollection(glow, xml);
                return(glow);

            case "StreamCollection":
                return(ConvertStreamCollection(xml));

            case "InvocationResult":
                return(ConvertInvocationResult(xml));
            }

            return(null);
        }
        public void NotifyMatrixConnection(Matrix matrix, Signal target, object state)
        {
            var glow           = GlowRootElementCollection.CreateRoot();
            var glowMatrix     = new GlowQualifiedMatrix(matrix.Path);
            var glowConnection = new GlowConnection(target.Number)
            {
                Sources     = target.ConnectedSources.Select(signal => signal.Number).ToArray(),
                Disposition = GlowConnectionDisposition.Modified,
            };

            glowMatrix.EnsureConnections().Insert(glowConnection);

            glow.Insert(glowMatrix);

            OnGlowRootReady(new GlowRootReadyArgs(glow, null)
            {
                Matrix = matrix
            });
        }
Example #13
0
        static GlowRootElementCollection BuildQualified(Element local, GlowElement glow)
        {
            var qualified = null as GlowElement;

            if (local.Type != ElementType.Root)
            {
                var path = local.BuildPath();

                if (local.Type == ElementType.Parameter)
                {
                    var qparam = new GlowQualifiedParameter(path);
                    qparam.Children = new GlowElementCollection(GlowTags.QualifiedParameter.Children);
                    qparam.Children.Insert(glow);
                    qualified = qparam;
                }
                else if (local.Type == ElementType.Node)
                {
                    var qnode = new GlowQualifiedNode(path);
                    qnode.Children = new GlowElementCollection(GlowTags.QualifiedNode.Children);
                    qnode.Children.Insert(glow);
                    qualified = qnode;
                }
            }
            else
            {
                qualified = glow;
            }

            if (qualified != null)
            {
                var root = GlowRootElementCollection.CreateRoot();
                root.Insert(qualified);
                return(root);
            }

            return(null);
        }
        void IDynamicPathHandler.HandleCommand(GlowCommand command, int[] path, Client source)
        {
            if (command.Number == GlowCommandType.GetDirectory)
            {
                var offset = Path.Length;

                if (path.Length == offset + 4 &&
                    path[offset + 0] == ParametersSubIdentifier &&
                    path[offset + 1] == 3) // connections
                {
                    Dictionary <int, XpointParams> dict;

                    if (_xpointParameters.TryGetValue(path[offset + 2], out dict)) // target
                    {
                        XpointParams xpointParams;

                        if (dict.TryGetValue(path[offset + 3], out xpointParams)) // source
                        {
                            var gainPath = path.Concat(new[] { 1 }).ToArray();

                            var glow = new GlowQualifiedParameter(gainPath)
                            {
                                Identifier = "dynamicGain",
                                Value      = new GlowValue(xpointParams.Gain),
                                Minimum    = new GlowMinMax(XpointParams.MinimumGain),
                                Maximum    = new GlowMinMax(XpointParams.MaximumGain),
                            };

                            var root = GlowRootElementCollection.CreateRoot();
                            root.Insert(glow);
                            source.Write(root);
                        }
                    }
                }
            }
        }
            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);
                    }
                }
            }
Example #16
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);
        }