object IGlowVisitor <XmlWriter, object> .Visit(GlowCommand glow, XmlWriter state) { state.WriteStartElement("Command"); state.WriteAttributeString("number", XmlConvert.ToString(glow.Number)); glow.DirFieldMask.Do(value => state.WriteElementString("dirFieldMask", XmlConvert.ToString(value))); var invocation = glow.Invocation; if (invocation != null) { state.WriteStartElement("invocation"); invocation.InvocationId.Do(value => state.WriteElementString("invocationId", XmlConvert.ToString(value))); var arguments = invocation.ArgumentValues; if (arguments != null) { state.WriteStartElement("arguments"); foreach (var value in arguments) { ConvertValue("Value", value, state); } state.WriteEndElement(); } state.WriteEndElement(); } state.WriteEndElement(); return(null); }
GlowCommand ConvertCommand(XElement xml) { var glow = new GlowCommand(XmlConvert.ToInt32(xml.Attribute("number").Value)); xml.Element("dirFieldMask").Do(value => glow.DirFieldMask = XmlConvert.ToInt32(value)); var invocationXml = xml.Element("invocation"); if (invocationXml != null) { var glowInvocation = new GlowInvocation(GlowTags.Command.Invocation); invocationXml.Element("invocationId").Do(value => glowInvocation.InvocationId = XmlConvert.ToInt32(value)); var argumentsXml = invocationXml.Element("arguments"); if (argumentsXml != null) { glowInvocation.ArgumentValues = from valueXml in argumentsXml.Elements("Value") let glowValue = ConvertValue(valueXml) where glowValue != null select glowValue; } glow.Invocation = glowInvocation; } return(glow); }
/// <summary> /// Creates an ember/glow tree which can be used to issue /// the "dir" command to the remote host. /// </summary> /// <returns>The GlowContainer object that is the root of /// a glow tree that mirrors the element tree up this element, /// having the GlowCommand object as the single leaf.</returns> public GlowContainer GetDirectory() { var glow = new GlowCommand(GlowCommandType.GetDirectory); // this builds a QualifiedNode or QualifiedParameter, // containing the GetDirectory Command return(BuildQualified(this, glow)); // this builds a complete glow tree using the Node and // Parameter types (more verbose). //return BuildGlowTree(this, glow); }
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); } }
// - handle Command at top level (dir on root: return all endpoint nodes) public IEnumerable <GlowContainer> Visit(GlowCommand glow, object state) { // return top-level nodes (describing endpoints) var nodes = from ep in EndPoints select new GlowNode(ep.LocalNumber) { Identifier = ep.HostName + ":" + ep.TcpPort, Description = ep.Description, IsOnline = ep.State == Consumer.GlowEndPointState.Connected, }; foreach (var node in nodes) { yield return(node); } }
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); } } } } }
void GlowCommandObserver.NewGlowCommandAvail(GlowCommand cmd) { NewGlowCommandAvailEvent(cmd);//pass it up }
bool IGlowVisitor <object, bool> .Visit(GlowCommand glow, object state) { return(false); }
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); } } }
public void NewGlowCommandAvail(GlowCommand cmd) { if(NewGlowCmdAvailEvent != null) NewGlowCmdAvailEvent(cmd);//pass it up }
private void SendCommand(GlowCommand cmd) { if(NewGlowCommandAvailEvent != null) { NewGlowCommandAvailEvent(cmd); } }
/// <summary> /// Handle a NewGlowCommandAvail event /// </summary> /// <param name="command"></param> public void NewGlowCommandAvail(GlowCommand command) { command.ExecuteCommand(this); }
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); } }
/// <summary> /// Pass up any observed GlowCommands /// </summary> /// <param name="command"></param> public void NewGlowCommandAvail(GlowCommand command) { if(NewGlowCommandAvailEvent != null) { NewGlowCommandAvailEvent(command); } }
public GlowContainer Visit(GlowCommand glow, object state) { return(WrapWithEndPointNode(glow)); }
GlowCommand ConvertCommand(XElement xml) { var glow = new GlowCommand(XmlConvert.ToInt32(xml.Attribute("number").Value)); xml.Element("dirFieldMask").Do(value => glow.DirFieldMask = XmlConvert.ToInt32(value)); var invocationXml = xml.Element("invocation"); if(invocationXml != null) { var glowInvocation = new GlowInvocation(GlowTags.Command.Invocation); invocationXml.Element("invocationId").Do(value => glowInvocation.InvocationId = XmlConvert.ToInt32(value)); var argumentsXml = invocationXml.Element("arguments"); if(argumentsXml != null) { glowInvocation.ArgumentValues = from valueXml in argumentsXml.Elements("Value") let glowValue = ConvertValue(valueXml) where glowValue != null select glowValue; } glow.Invocation = glowInvocation; } return glow; }
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); }
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); }