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;
3using System.Text;
4using System.Threading.Tasks;
8using Waher.Script;
13
15{
19 public class RoomSource : IDataSource
20 {
21 private readonly MucRoom room;
22 private readonly string sourceName;
23 private readonly bool containsWhiteSpace;
24
30 public RoomSource(MucRoom Room, string SourceName)
31 {
32 this.room = Room;
33 this.sourceName = SourceName;
34
35 this.containsWhiteSpace = false;
36
37 foreach (char ch in SourceName)
38 {
39 if (char.IsWhiteSpace(ch))
40 {
41 this.containsWhiteSpace = true;
42 break;
43 }
44 }
45 }
46
50 public string CollectionName => throw InvalidOperation();
51
55 public string TypeName => throw InvalidOperation();
56
60 public string Name => string.Empty;
61
62 private static Exception InvalidOperation()
63 {
64 return new InvalidOperationException("Operation not permitted on MUC room sources.");
65 }
66
72 public bool IsSource(string Name)
73 {
74 return Name == this.room.RoomId || Name == this.room.Jid;
75 }
76
82 public Task<bool> IsLabel(string Label)
83 {
84 return Task.FromResult(false);
85 }
86
98 public Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
99 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
100 {
101 StringBuilder sql = new StringBuilder();
102
103 sql.Append("SELECT");
104
105 if (Top < int.MaxValue)
106 {
107 sql.Append(" TOP ");
108 sql.Append(Top.ToString());
109 }
110
111 sql.Append(" GENERIC * FROM ");
112
113 if (this.containsWhiteSpace)
114 {
115 sql.Append('"');
116 sql.Append(this.sourceName.Replace("\"", "\\\""));
117 sql.Append('"');
118 }
119 else
120 sql.Append(this.sourceName.Replace("\"", "\\\""));
121
122 if (!(Where is null))
123 {
124 sql.Append(" WHERE ");
125 sql.Append(Where.SubExpression);
126 }
127
128 if (!(Order is null))
129 {
130 bool First = true;
131
132 foreach (KeyValuePair<VariableReference, bool> P in Order)
133 {
134 if (First)
135 {
136 First = false;
137 sql.Append(" ORDER BY ");
138 }
139 else
140 sql.Append(", ");
141
142 sql.Append(P.Key.VariableName);
143
144 if (!P.Value)
145 sql.Append(" DESC");
146 }
147 }
148
149 if (Offset > 0)
150 {
151 sql.Append(" OFFSET ");
152 sql.Append(Offset.ToString());
153 }
154
155 ConsolidationState State = new ConsolidationState()
156 {
157 ThreadId = Guid.NewGuid().ToString(),
158 N = XmppServerModule.Instance.GetNrOccupants(this.room.Jid, this.room.RoomId, this.room.Domain, true, true)
159 };
160 ScriptConsolidator Consolidator = new ScriptConsolidator(State.ThreadId, State.N < 12 ? 12 : State.N)
161 {
162 Tag = State
163 };
164
165 Consolidator.Added += this.Consolidator_Updated;
166 Consolidator.Updated += this.Consolidator_Updated;
167 Consolidator.Disposed += this.Consolidator_Disposed;
168
169 XmppServerModule.Instance.RegisterConsolidator(State.ThreadId, Consolidator);
170
171 Gateway.XmppClient.SendMessage(Networking.XMPP.MessageType.GroupChat, this.room.Jid, string.Empty, sql.ToString(),
172 string.Empty, string.Empty, State.ThreadId, string.Empty);
173
174 return State.CompletionSource.Task;
175 }
176
177 private class ConsolidationState
178 {
179 public TaskCompletionSource<IResultSetEnumerator> CompletionSource = new TaskCompletionSource<IResultSetEnumerator>();
180 public string ThreadId;
181 public int N;
182 public bool Reported = false;
183 }
184
185 private Task Consolidator_Disposed(object Sender, EventArgs e)
186 {
187 if (Sender is ScriptConsolidator Consolidator &&
188 Consolidator.Tag is ConsolidationState State)
189 {
190 this.Report(Consolidator, State);
191 }
192
193 return Task.CompletedTask;
194 }
195
196 private async Task Consolidator_Updated(object Sender, SourceEventArgs e)
197 {
198 if (Sender is ScriptConsolidator Consolidator &&
199 Consolidator.Tag is ConsolidationState State &&
200 await Consolidator.GetNrReportedSources() == State.N)
201 {
202 this.Report(Consolidator, State);
203 }
204 }
205
206 private void Report(ScriptConsolidator Consolidator, ConsolidationState State)
207 {
208 if (!State.Reported)
209 {
210 State.Reported = true;
211 IVector V = Consolidator.GetResult();
212 State.CompletionSource.TrySetResult(new SynchEnumerator(V.VectorElements.GetEnumerator()));
213 }
214 }
215
221 public Task CreateIndex(string Name, string[] Fields)
222 {
223 throw InvalidOperation();
224 }
225
229 public Task DropCollection()
230 {
231 throw InvalidOperation();
232 }
233
238 public Task<bool> DropIndex(string Name)
239 {
240 throw InvalidOperation();
241 }
242
254 public Task<int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
255 {
256 throw InvalidOperation();
257 }
258
264 public Task Insert(bool Lazy, object Object)
265 {
266 throw InvalidOperation();
267 }
268
274 public Task Update(bool Lazy, IEnumerable<object> Objects)
275 {
276 throw InvalidOperation();
277 }
278
292 public async Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
293 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
294 ScriptNode Node)
295 {
296 IResultSetEnumerator e = await this.Find(Offset, Top, Generic, Where, Variables, Order, Node);
297 bool IsAsyncronous = Processor.IsAsynchronous;
298
299 while (await e.MoveNextAsync())
300 {
301 if (IsAsyncronous)
302 {
303 if (!await Processor.ProcessAsync(e.Current))
304 return false;
305 }
306 else
307 {
308 if (!Processor.Process(e.Current))
309 return false;
310 }
311 }
312
313 return true;
314 }
315
327 public Task<int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
328 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
329 {
330 throw InvalidOperation();
331 }
332 }
333}
334
Consolidates Markdown from multiple sources, sharing the same thread.
Definition: Consolidator.cs:20
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:71
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:4038
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
Task< int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Deletes a set of objects.
Definition: RoomSource.cs:327
string TypeName
Name of corresponding type.
Definition: RoomSource.cs:55
string CollectionName
Name of corresponding collection.
Definition: RoomSource.cs:50
RoomSource(MucRoom Room, string SourceName)
MUC Room Data source.
Definition: RoomSource.cs:30
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Definition: RoomSource.cs:274
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
Definition: RoomSource.cs:221
string Name
Collection name or alias.
Definition: RoomSource.cs:60
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:98
Task< bool > DropIndex(string Name)
Drops an index from the source.
Definition: RoomSource.cs:238
bool IsSource(string Name)
Checks if the name refers to the source.
Definition: RoomSource.cs:72
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:254
Task Insert(bool Lazy, object Object)
Inserts an object.
Definition: RoomSource.cs:264
async Task< bool > Process(IProcessor< object > Processor, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Processes objects matching filter conditions in Where .
Definition: RoomSource.cs:292
Task DropCollection()
Drops the collection from the source.
Definition: RoomSource.cs:229
Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
Definition: RoomSource.cs:82
Consolidates responses from occupants in a MUC room.
Service Module hosting the XMPP broker and its components.
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for processors of objects.
Definition: IProcessor.cs:9
bool Process(T Object)
Processes an object synchronously.
bool IsAsynchronous
If the processor operates asynchronously.
Definition: IProcessor.cs:13
Task< bool > ProcessAsync(T Object)
Processes an object asynchronously.
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:14