Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendMessage.cs
1using System;
2using System.Collections.Generic;
3using System.Xml;
4using System.Threading.Tasks;
10using Waher.Script;
12using System.IO;
13
15{
20 {
21 private IValue value;
22 private MessageType type;
23 private StringAttribute actor;
24 private StringAttribute to;
25 private StringAttribute subject;
26 private StringAttribute language;
27 private StringAttribute threadId;
28 private StringAttribute parentThreadId;
29
36 : base(Parent, Model)
37 {
38 }
39
43 public override string LocalName => nameof(SendMessage);
44
48 public override string SchemaResource => XmppActor.XmppSchema;
49
53 public override string Namespace => XmppActor.XmppNamespace;
54
62 {
63 return new SendMessage(Parent, Model);
64 }
65
70 public override Task FromXml(XmlElement Definition)
71 {
72 this.actor = new StringAttribute(XML.Attribute(Definition, "actor"));
73 this.to = new StringAttribute(XML.Attribute(Definition, "to"));
74 this.subject = new StringAttribute(XML.Attribute(Definition, "subject"));
75 this.language = new StringAttribute(XML.Attribute(Definition, "language"));
76 this.threadId = new StringAttribute(XML.Attribute(Definition, "threadId"));
77 this.parentThreadId = new StringAttribute(XML.Attribute(Definition, "parentThreadId"));
78 this.type = (MessageType)XML.Attribute(Definition, "type", MessageType.Normal);
79
80 return base.FromXml(Definition);
81 }
82
87 public void Register(IValue Value)
88 {
89 if (this.value is null)
90 this.value = Value;
91 else
92 throw new Exception("Value already registered.");
93 }
94
100 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
101 {
102 string To = await this.to.GetValueAsync(Variables);
103 string Subject = await this.subject.GetValueAsync(Variables);
104 string Language = await this.language.GetValueAsync(Variables);
105 string ThreadId = await this.threadId.GetValueAsync(Variables);
106 string ParentThreadId = await this.parentThreadId.GetValueAsync(Variables);
107 object Content = this.value is null ? string.Empty : await this.value.EvaluateAsync(Variables) ?? string.Empty;
108 string Xml;
109 string Body;
110
111 if (!(await this.GetActorObjectAsync(this.actor, Variables) is XmppActivityObject XmppActor))
112 throw new Exception("Actor not an XMPP client.");
113
114 if (Content is XmlDocument Doc)
115 {
116 Xml = Doc.OuterXml;
117 Body = string.Empty;
118 }
119 else if (Content is XmlElement E)
120 {
121 Xml = E.OuterXml;
122 Body = string.Empty;
123 }
124 else
125 {
126 Xml = string.Empty;
127 Body = Content.ToString();
128 }
129
130 await XmppActor.Client.SendMessage(this.type, To, Xml, Body, Subject, Language, ThreadId, ParentThreadId);
131
132 return null;
133 }
134
141 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
142 {
143 base.ExportPlantUml(Output, Indentation, QuoteChar);
144
145 Indent(Output, Indentation);
146 Output.Write(':');
147 Output.Write(this.actor.Value);
148 Output.Write(".SendMessage");
149 Output.Write("(");
150
151 Indentation++;
152
153 AppendArgument(Output, Indentation, "To", this.to.Value, true, QuoteChar);
154 AppendArgument(Output, Indentation, "Type", this.type.ToString(), false, QuoteChar);
155
156 if (!string.IsNullOrEmpty(this.subject.Value))
157 AppendArgument(Output, Indentation, "Subject", this.subject.Value, true, QuoteChar);
158
159 if (!string.IsNullOrEmpty(this.language.Value))
160 AppendArgument(Output, Indentation, "Language", this.language.Value, true, QuoteChar);
161
162 if (!string.IsNullOrEmpty(this.threadId.Value))
163 AppendArgument(Output, Indentation, "ThreadId", this.threadId.Value, true, QuoteChar);
164
165 if (!string.IsNullOrEmpty(this.parentThreadId.Value))
166 AppendArgument(Output, Indentation, "ParentThreadId", this.parentThreadId.Value, true, QuoteChar);
167
168 if (!(this.value is null))
169 {
170 if (this.value is Xml Xml && !string.IsNullOrEmpty(Xml.RootName))
171 AppendArgument(Output, Indentation, "Content", Xml.RootName, false, QuoteChar);
172 else
173 AppendArgument(Output, Indentation, "Content", this.value, QuoteChar);
174 }
175
176 Output.WriteLine(");");
177 }
178
188 public static void AppendArgument(StreamWriter Output, int Indentation, string Name, string Value, bool Quotes, char QuoteChar)
189 {
190 AppendArgument(Output, Indentation, Name);
191
192 if (Quotes)
193 Eval.ExportPlantUml("\"" + Value.Replace("\"", "\\\"") + "\"", Output, Indentation, QuoteChar, false);
194 else
195 Eval.ExportPlantUml(Value, Output, Indentation, QuoteChar, false);
196 }
197
206 public static void AppendArgument(StreamWriter Output, int Indentation, string Name, IValue Value, char QuoteChar)
207 {
208 AppendArgument(Output, Indentation, Name);
209 Value.ExportPlantUml(Output, Indentation, QuoteChar);
210 }
211
218 public static void AppendArgument(StreamWriter Output, int Indentation, string Name)
219 {
220 Output.WriteLine();
221 Indent(Output, Indentation);
222
223 Output.Write(Name);
224 Output.Write(": ");
225 }
226
227 }
228}
Root node of a simulation model
Definition: Model.cs:49
Abstract base class for activity nodes
Definition: ActivityNode.cs:15
Executes script in an activity.
Definition: Eval.cs:14
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: Eval.cs:88
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
abstract void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
string RootName
Root name
Definition: Xml.cs:34
Sends a custom message to a recipient
Definition: SendMessage.cs:20
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: SendMessage.cs:61
static void AppendArgument(StreamWriter Output, int Indentation, string Name, string Value, bool Quotes, char QuoteChar)
Appends an argument
Definition: SendMessage.cs:188
SendMessage(ISimulationNode Parent, Model Model)
Sends a custom message to a recipient
Definition: SendMessage.cs:35
override string Namespace
XML Namespace where the element is defined.
Definition: SendMessage.cs:53
void Register(IValue Value)
Registers a value for the argument.
Definition: SendMessage.cs:87
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
Definition: SendMessage.cs:48
override string LocalName
Local name of XML element defining contents of class.
Definition: SendMessage.cs:43
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: SendMessage.cs:100
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: SendMessage.cs:141
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: SendMessage.cs:70
static void AppendArgument(StreamWriter Output, int Indentation, string Name, IValue Value, char QuoteChar)
Appends an argument
Definition: SendMessage.cs:206
static void AppendArgument(StreamWriter Output, int Indentation, string Name)
Appends an argument
Definition: SendMessage.cs:218
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
Task SendMessage(MessageType Type, string To, string CustomXml, string Body, string Subject, string Language, string ThreadId, string ParentThreadId)
Sends a simple chat message
Definition: XmppClient.cs:5395
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
MessageType
Type of message received.
Definition: MessageType.cs:7