Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RoomSource.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
7using Waher.Script;
12
14{
18 public class RoomSource : IDataSource
19 {
20 private readonly MucRoom room;
21 private readonly string sourceName;
22 private readonly bool containsWhiteSpace;
23
29 public RoomSource(MucRoom Room, string SourceName)
30 {
31 this.room = Room;
32 this.sourceName = SourceName;
33
34 this.containsWhiteSpace = false;
35
36 foreach (char ch in SourceName)
37 {
38 if (char.IsWhiteSpace(ch))
39 {
40 this.containsWhiteSpace = true;
41 break;
42 }
43 }
44 }
45
49 public string CollectionName => throw InvalidOperation();
50
54 public string TypeName => throw InvalidOperation();
55
59 public string Name => string.Empty;
60
61 private static Exception InvalidOperation()
62 {
63 return new InvalidOperationException("Operation not permitted on MUC room sources.");
64 }
65
71 public bool IsSource(string Name)
72 {
73 return Name == this.room.RoomId || Name == this.room.Jid;
74 }
75
81 public Task<bool> IsLabel(string Label)
82 {
83 return Task.FromResult(false);
84 }
85
97 public Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
98 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
99 {
100 StringBuilder sql = new StringBuilder();
101
102 sql.Append("SELECT");
103
104 if (Top < int.MaxValue)
105 {
106 sql.Append(" TOP ");
107 sql.Append(Top.ToString());
108 }
109
110 sql.Append(" GENERIC * FROM ");
111
112 if (this.containsWhiteSpace)
113 {
114 sql.Append('"');
115 sql.Append(this.sourceName.Replace("\"", "\\\""));
116 sql.Append('"');
117 }
118 else
119 sql.Append(this.sourceName.Replace("\"", "\\\""));
120
121 if (!(Where is null))
122 {
123 sql.Append(" WHERE ");
124 sql.Append(Where.SubExpression);
125 }
126
127 if (!(Order is null))
128 {
129 bool First = true;
130
131 foreach (KeyValuePair<VariableReference, bool> P in Order)
132 {
133 if (First)
134 {
135 First = false;
136 sql.Append(" ORDER BY ");
137 }
138 else
139 sql.Append(", ");
140
141 sql.Append(P.Key.VariableName);
142
143 if (!P.Value)
144 sql.Append(" DESC");
145 }
146 }
147
148 if (Offset > 0)
149 {
150 sql.Append(" OFFSET ");
151 sql.Append(Offset.ToString());
152 }
153
154 ConsolidationState State = new ConsolidationState()
155 {
156 ThreadId = Guid.NewGuid().ToString(),
157 N = XmppServerModule.Instance.GetNrOccupants(this.room.Jid, this.room.RoomId, this.room.Domain, true, true)
158 };
159 ScriptConsolidator Consolidator = new ScriptConsolidator(State.ThreadId, State.N < 12 ? 12 : State.N)
160 {
161 Tag = State
162 };
163
164 Consolidator.Added += this.Consolidator_Updated;
165 Consolidator.Updated += this.Consolidator_Updated;
166 Consolidator.Disposed += this.Consolidator_Disposed;
167
168 XmppServerModule.Instance.RegisterConsolidator(State.ThreadId, Consolidator);
169
170 Gateway.XmppClient.SendMessage(Networking.XMPP.MessageType.GroupChat, this.room.Jid, string.Empty, sql.ToString(),
171 string.Empty, string.Empty, State.ThreadId, string.Empty);
172
173 return State.CompletionSource.Task;
174 }
175
176 private class ConsolidationState
177 {
178 public TaskCompletionSource<IResultSetEnumerator> CompletionSource = new TaskCompletionSource<IResultSetEnumerator>();
179 public string ThreadId;
180 public int N;
181 public bool Reported = false;
182 }
183
184 private Task Consolidator_Disposed(object Sender, EventArgs e)
185 {
186 if (Sender is ScriptConsolidator Consolidator &&
187 Consolidator.Tag is ConsolidationState State)
188 {
189 this.Report(Consolidator, State);
190 }
191
192 return Task.CompletedTask;
193 }
194
195 private async Task Consolidator_Updated(object Sender, SourceEventArgs e)
196 {
197 if (Sender is ScriptConsolidator Consolidator &&
198 Consolidator.Tag is ConsolidationState State &&
199 await Consolidator.GetNrReportedSources() == State.N)
200 {
201 this.Report(Consolidator, State);
202 }
203 }
204
205 private void Report(ScriptConsolidator Consolidator, ConsolidationState State)
206 {
207 if (!State.Reported)
208 {
209 State.Reported = true;
210 IVector V = Consolidator.GetResult();
211 State.CompletionSource.TrySetResult(new SynchEnumerator(V.VectorElements.GetEnumerator()));
212 }
213 }
214
220 public Task CreateIndex(string Name, string[] Fields)
221 {
222 throw InvalidOperation();
223 }
224
228 public Task DropCollection()
229 {
230 throw InvalidOperation();
231 }
232
237 public Task<bool> DropIndex(string Name)
238 {
239 throw InvalidOperation();
240 }
241
253 public Task<int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
254 {
255 throw InvalidOperation();
256 }
257
263 public Task Insert(bool Lazy, object Object)
264 {
265 throw InvalidOperation();
266 }
267
273 public Task Update(bool Lazy, IEnumerable<object> Objects)
274 {
275 throw InvalidOperation();
276 }
277 }
278}
279
Consolidates Markdown from multiple sources, sharing the same thread.
Definition: Consolidator.cs:18
object Tag
External tag object that can be tagged to the object by its owner.
async Task< int > GetNrReportedSources()
Number of sources that have reported content.
Definition: Consolidator.cs:69
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
string SubExpression
Sub-expression defining the node.
Definition: ScriptNode.cs:183
Collection of variables.
Definition: Variables.cs:25
MUC Room, for script access to remote sources.
Definition: MucRoom.cs:7
string TypeName
Name of corresponding type.
Definition: RoomSource.cs:54
string CollectionName
Name of corresponding collection.
Definition: RoomSource.cs:49
RoomSource(MucRoom Room, string SourceName)
MUC Room Data source.
Definition: RoomSource.cs:29
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Definition: RoomSource.cs:273
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
Definition: RoomSource.cs:220
string Name
Collection name or alias.
Definition: RoomSource.cs:59
Task< IResultSetEnumerator > Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds objects matching filter conditions in Where .
Definition: RoomSource.cs:97
Task< bool > DropIndex(string Name)
Drops an index from the source.
Definition: RoomSource.cs:237
bool IsSource(string Name)
Checks if the name refers to the source.
Definition: RoomSource.cs:71
Task< int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds and Deletes a set of objects.
Definition: RoomSource.cs:253
Task Insert(bool Lazy, object Object)
Inserts an object.
Definition: RoomSource.cs:263
Task DropCollection()
Drops the collection from the source.
Definition: RoomSource.cs:228
Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
Definition: RoomSource.cs:81
Consolidates responses from occupants in a MUC room.
Service Module hosting the XMPP broker and its components.
Basic interface for vectors.
Definition: IVector.cs:9
ICollection< IElement > VectorElements
An enumeration of vector elements.
Definition: IVector.cs:22
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13