Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Request.cs
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using System.Threading.Tasks;
10using Waher.Script;
12using System.IO;
13
15{
19 public enum RequestType
20 {
24 Get,
25
29 Set
30 }
31
36 {
37 private IValue value;
38 private RequestType type;
39 private StringAttribute actor;
40 private StringAttribute to;
41 private string responseVariable;
42
49 : base(Parent, Model)
50 {
51 }
52
56 public override string LocalName => nameof(Request);
57
61 public override string SchemaResource => XmppActor.XmppSchema;
62
66 public override string Namespace => XmppActor.XmppNamespace;
67
75 {
76 return new Request(Parent, Model);
77 }
78
83 public override Task FromXml(XmlElement Definition)
84 {
85 this.actor = new StringAttribute(XML.Attribute(Definition, "actor"));
86 this.to = new StringAttribute(XML.Attribute(Definition, "to"));
87 this.type = (RequestType)XML.Attribute(Definition, "type", RequestType.Get);
88 this.responseVariable = XML.Attribute(Definition, "responseVariable");
89
90 return base.FromXml(Definition);
91 }
92
97 public void Register(IValue Value)
98 {
99 if (this.value is null)
100 this.value = Value;
101 else
102 throw new Exception("Value already registered.");
103 }
104
110 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
111 {
112 string To = await this.to.GetValueAsync(Variables);
113 object Content = this.value is null ? string.Empty : await this.value.EvaluateAsync(Variables) ?? string.Empty;
114 string Xml;
115
116 if (!(await this.GetActorObjectAsync(this.actor, Variables) is XmppActivityObject XmppActor))
117 throw new Exception("Actor not an XMPP client.");
118
119 if (Content is XmlDocument Doc)
120 Xml = Doc.OuterXml;
121 else if (Content is XmlElement E)
122 Xml = E.OuterXml;
123 else
124 throw new Exception("Requests must be XML.");
125
126 TaskCompletionSource<LinkedListNode<IActivityNode>> T = new TaskCompletionSource<LinkedListNode<IActivityNode>>();
127
128 await XmppActor.Client.SendIq(string.Empty, To, Xml, this.type.ToString().ToLower(), (sender, e) =>
129 {
130 if (e.Ok)
131 {
132 Variables[this.responseVariable] = e.FirstElement;
133 T.TrySetResult(null);
134 }
135 else
136 T.TrySetException(new XmppException(string.IsNullOrEmpty(e.ErrorText) ? "Error response returned." : e.ErrorText));
137
138 return Task.CompletedTask;
140
141 return await T.Task;
142 }
143
150 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
151 {
152 base.ExportPlantUml(Output, Indentation, QuoteChar);
153
154 Indent(Output, Indentation);
155 Output.Write(':');
156 Output.Write(this.actor.Value);
157 Output.Write(".Request");
158 Output.Write("(");
159
160 Indentation++;
161
162 SendMessage.AppendArgument(Output, Indentation, "To", this.to.Value, true, QuoteChar);
163 SendMessage.AppendArgument(Output, Indentation, "Type", this.type.ToString(), false, QuoteChar);
164
165 if (!(this.value is null))
166 {
167 if (this.value is Xml Xml && !string.IsNullOrEmpty(Xml.RootName))
168 SendMessage.AppendArgument(Output, Indentation, "Content", Xml.RootName, false, QuoteChar);
169 else
170 SendMessage.AppendArgument(Output, Indentation, "Content", this.value, QuoteChar);
171 }
172
173 Output.WriteLine(");");
174 }
175
176 }
177}
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 request to a recipient
Definition: Request.cs:36
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: Request.cs:74
void Register(IValue Value)
Registers a value for the argument.
Definition: Request.cs:97
override string LocalName
Local name of XML element defining contents of class.
Definition: Request.cs:56
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: Request.cs:110
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: Request.cs:83
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
Definition: Request.cs:61
Request(ISimulationNode Parent, Model Model)
Sends a custom IQ request to a recipient
Definition: Request.cs:48
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: Request.cs:150
override string Namespace
XML Namespace where the element is defined.
Definition: Request.cs:66
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
int DefaultRetryTimeout
Default retry timeout, in milliseconds. This value is used when sending IQ requests wihtout specifyin...
Definition: XmppClient.cs:6778
bool DefaultDropOff
Default Drop-off value. If drop-off is used, the retry timeout is doubled for each retry,...
Definition: XmppClient.cs:6827
int DefaultNrRetries
Default number of retries if results or errors are not returned. This value is used when sending IQ r...
Definition: XmppClient.cs:6795
int DefaultMaxRetryTimeout
Default maximum retry timeout, in milliseconds. This value is used when sending IQ requests wihtout s...
Definition: XmppClient.cs:6811
Task< uint > SendIq(string Id, string To, string Xml, string Type, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ stanza.
Definition: XmppClient.cs:3855
Base class of XMPP exceptions
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
RequestType
Type of IQ request
Definition: Request.cs:20