Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Respond.cs
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using System.Threading.Tasks;
10using Waher.Script;
11using System.IO;
12
14{
19 {
20 private IValue value;
21 private StringAttribute actor;
22 private StringAttribute to;
23 private StringAttribute requestId;
24
31 : base(Parent, Model)
32 {
33 }
34
38 public override string LocalName => nameof(Respond);
39
43 public override string SchemaResource => XmppActor.XmppSchema;
44
48 public override string Namespace => XmppActor.XmppNamespace;
49
57 {
58 return new Respond(Parent, Model);
59 }
60
65 public override Task FromXml(XmlElement Definition)
66 {
67 this.actor = new StringAttribute(XML.Attribute(Definition, "actor"));
68 this.to = new StringAttribute(XML.Attribute(Definition, "to"));
69 this.requestId = new StringAttribute(XML.Attribute(Definition, "requestId"));
70
71 return base.FromXml(Definition);
72 }
73
78 public void Register(IValue Value)
79 {
80 if (this.value is null)
81 this.value = Value;
82 else
83 throw new Exception("Value already registered.");
84 }
85
91 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
92 {
93 string To = await this.to.GetValueAsync(Variables);
94 string RequestId = await this.requestId.GetValueAsync(Variables);
95
96 if (!(await this.GetActorObjectAsync(this.actor, Variables) is XmppActivityObject XmppActor))
97 throw new Exception("Actor not an XMPP client.");
98
99 object Content = this.value is null ? string.Empty : await this.value.EvaluateAsync(Variables) ?? string.Empty;
100 string Xml;
101
102 if (Content is XmlDocument Doc)
103 Xml = Doc.OuterXml;
104 else if (Content is XmlElement E)
105 Xml = E.OuterXml;
106 else if (Content is null)
107 Xml = null;
108 else
109 throw new Exception("Responses must be XML, or empty.");
110
111 await XmppActor.Client.SendIqResult(RequestId, To, Xml);
112
113 return null;
114 }
115
122 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
123 {
124 base.ExportPlantUml(Output, Indentation, QuoteChar);
125
126 Indent(Output, Indentation);
127 Output.Write(':');
128 Output.Write(this.actor.Value);
129 Output.Write(".Respond");
130 Output.Write("(");
131
132 Indentation++;
133
134 SendMessage.AppendArgument(Output, Indentation, "Id", this.requestId.Value, true, QuoteChar);
135 SendMessage.AppendArgument(Output, Indentation, "To", this.to.Value, true, QuoteChar);
136
137 if (!(this.value is null))
138 {
139 if (this.value is Xml Xml && !string.IsNullOrEmpty(Xml.RootName))
140 SendMessage.AppendArgument(Output, Indentation, "Content", Xml.RootName, false, QuoteChar);
141 else
142 SendMessage.AppendArgument(Output, Indentation, "Content", this.value, QuoteChar);
143 }
144
145 Output.WriteLine(");");
146 }
147
148 }
149}
Root node of a simulation model
Definition: Model.cs:49
Abstract base class for activity nodes
Definition: ActivityNode.cs:15
async Task< object > GetActorObjectAsync(StringAttribute Actor, Variables Variables)
Gets an actor object, given a string representation, possibly containing script, of the actor.
static void Indent(StreamWriter Output, int Indentation)
Adds indentation to the current row.
Contains the value of a string attribute, possibly with embedded script.
async Task< string > GetValueAsync(Variables Variables)
Gets the value of the attribute.
string Value
String value, from definition
Abstract base class for values
Definition: Value.cs:11
string RootName
Root name
Definition: Xml.cs:34
Sends a custom IQ response to a recipient
Definition: Respond.cs:19
override string LocalName
Local name of XML element defining contents of class.
Definition: Respond.cs:38
override string Namespace
XML Namespace where the element is defined.
Definition: Respond.cs:48
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
Definition: Respond.cs:43
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: Respond.cs:56
void Register(IValue Value)
Registers a value for the argument.
Definition: Respond.cs:78
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: Respond.cs:122
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: Respond.cs:65
Respond(ISimulationNode Parent, Model Model)
Sends a custom IQ response to a recipient
Definition: Respond.cs:30
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: Respond.cs:91
Sends a custom message to a recipient
Definition: SendMessage.cs:20
static void AppendArgument(StreamWriter Output, int Indentation, string Name, string Value, bool Quotes, char QuoteChar)
Appends an argument
Definition: SendMessage.cs:188
Object used in simulation activities.
Abstract base class for XMPP actors.
Definition: XmppActor.cs:23
XmppClient Client
XMPP Client
Definition: XmppActor.cs:110
const string XmppSchema
TAG.Simulator.XMPP.Schema.ComSimXmpp.xsd
Definition: XmppActor.cs:32
const string XmppNamespace
http://lab.tagroot.io/Schema/ComSim/XMPP.xsd
Definition: XmppActor.cs:27
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Collection of variables.
Definition: Variables.cs:25
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Task< object > EvaluateAsync(Variables Variables)
Evaluates the value.
Interface for nodes holding a value node