2using System.Collections.Generic;
3using System.Threading.Tasks;
22 private readonly
string userVariable;
23 private readonly
string[] userPrivileges;
35 this.client = NLClient;
36 this.userVariable = UserVariable;
37 this.userPrivileges = UserPrivileges;
65 !(v.ValueObject is
IUser User))
70 foreach (
string Privilege
in this.userPrivileges)
72 if (!User.HasPrivilege(Privilege))
76 string TabID = Request.Header[
"X-TabID"];
77 if (
string.IsNullOrEmpty(TabID))
80 string Command = Request.Header[
"X-Command"];
81 if (
string.IsNullOrEmpty(Command))
85 throw new ConflictException(
"The Neuro-Ledger is not currently connected to the network. Synchronization cannot start.");
90 this.Synchronize(TabID);
94 await this.StopSynchronization(TabID);
102 private async
void Synchronize(
string TabID)
108 new KeyValuePair<string, object>(
"jid", this.client.Provider.ExternalIdentity),
109 new KeyValuePair<string, object>(
"name", this.client.Provider.ExternalIdentity)
110 },
false),
true, this.userVariable, this.userPrivileges);
114 new KeyValuePair<string, object>(
"jid", this.client.Provider.ExternalIdentity),
115 new KeyValuePair<string, object>(
"type",
"Neuro-Ledger"),
116 new KeyValuePair<string, object>(
"availability",
"Local")
117 },
false),
true, this.userVariable, this.userPrivileges);
119 LocalStatus LocalStatus =
new LocalStatus()
124 await this.client.Provider.RepairRegistry(
127 LocalStatus.NrBlocks++;
128 return this.Report(LocalStatus, e);
132 LocalStatus.NrAdded++;
133 LocalStatus.NrBlocks++;
134 return this.Report(LocalStatus, e);
138 LocalStatus.NrUpdated++;
139 LocalStatus.NrBlocks++;
140 return this.Report(LocalStatus, e);
144 LocalStatus.NrDeleted++;
145 return this.Report(LocalStatus, e);
148 LocalStatus.Last = DateTime.MinValue;
149 await this.Report(LocalStatus,
true);
151 LinkedList<Task> Tasks =
new LinkedList<Task>();
157 new KeyValuePair<string, object>(
"jid", Item.
BareJid),
159 },
false),
true, this.userVariable, this.userPrivileges);
163 bool HasPresence =
false;
169 Status = await this.client.GetPeerStatus(e);
188 else if (!(Peer is
null))
190 Type =
"Neuro-Ledger";
201 new KeyValuePair<string, object>(
"jid", Item.
BareJid),
202 new KeyValuePair<string, object>(
"type", Type),
203 new KeyValuePair<string, object>(
"availability",
Availability)
204 },
false),
true, this.userVariable, this.userPrivileges);
209 Tasks.AddLast(Task.Run(() => this.SynchronizePeer(TabID, Status, Item)));
213 await Task.WhenAll(Tasks);
217 await
ClientEvents.
PushEvent(
new string[] { TabID },
"ShowError", ex.Message,
false, this.userVariable, this.userPrivileges);
221 private class LocalStatus
224 public int NrBlocks = 0;
225 public int NrAdded = 0;
226 public int NrUpdated = 0;
227 public int NrDeleted = 0;
228 public DateTime Last = DateTime.Now;
229 public DateTime FirstBlock = DateTime.MaxValue;
230 public DateTime LastBlock = DateTime.MinValue;
231 public ulong TotalBytes = 0;
236 if (e.
Block.Created < LocalStatus.FirstBlock)
237 LocalStatus.FirstBlock = e.
Block.Created;
239 if (e.
Block.Created > LocalStatus.LastBlock)
240 LocalStatus.LastBlock = e.
Block.Created;
242 LocalStatus.TotalBytes += e.
Block.Bytes;
244 return this.Report(LocalStatus,
false);
247 private async Task Report(LocalStatus LocalStatus,
bool Done)
249 DateTime Now = DateTime.Now;
250 if ((Now - LocalStatus.Last).TotalSeconds >= 1)
252 LocalStatus.Last = Now;
258 new KeyValuePair<string, object>(
"jid", this.client.Provider.ExternalIdentity),
259 new KeyValuePair<string, object>(
"first", LocalStatus.FirstBlock == DateTime.MaxValue ?
string.Empty : LocalStatus.FirstBlock.ToShortDateString()),
260 new KeyValuePair<string, object>(
"last", LocalStatus.LastBlock == DateTime.MinValue ?
string.Empty : LocalStatus.LastBlock.ToShortDateString()),
261 new KeyValuePair<string, object>(
"nrBlocks", LocalStatus.NrBlocks),
262 new KeyValuePair<string, object>(
"nrNew", LocalStatus.NrAdded),
263 new KeyValuePair<string, object>(
"nrUpdated", LocalStatus.NrUpdated),
264 new KeyValuePair<string, object>(
"blockErrors", LocalStatus.NrDeleted),
265 new KeyValuePair<string, object>(
"totalBytes", LocalStatus.TotalBytes),
266 new KeyValuePair<string, object>(
"done", Done),
267 new KeyValuePair<string, object>(
"nrLoaded",
string.Empty),
268 new KeyValuePair<string, object>(
"nrDenied",
string.Empty),
269 new KeyValuePair<string, object>(
"loadedBytes",
string.Empty),
270 new KeyValuePair<string, object>(
"events",
string.Empty),
271 new KeyValuePair<string, object>(
"objectsAdded",
string.Empty),
272 new KeyValuePair<string, object>(
"objectsUpdated",
string.Empty),
273 new KeyValuePair<string, object>(
"objectsDeleted",
string.Empty),
274 new KeyValuePair<string, object>(
"objectErrors",
string.Empty)
276 },
false),
true, this.userVariable, this.userPrivileges);
285 private async Task StopSynchronization(
string TabID)
296 Status = await this.client.GetPeerStatus(e);
310 this.client.StopSynchronization(Status);
315 await
ClientEvents.
PushEvent(
new string[] { TabID },
"ShowError", ex.Message,
false, this.userVariable, this.userPrivileges);
323 string Msg = await this.client.SynchronizePeer(Status,
true, async (Progress) =>
327 new KeyValuePair<string, object>(
"jid", Item.
BareJid),
328 new KeyValuePair<string, object>(
"first", Progress.First == DateTime.MaxValue ?
string.Empty : Progress.First.ToShortDateString()),
329 new KeyValuePair<string, object>(
"last", Progress.Last == DateTime.MinValue ?
string.Empty : Progress.Last.ToShortDateString()),
330 new KeyValuePair<string, object>(
"nrBlocks", Progress.NrBlocks),
331 new KeyValuePair<string, object>(
"nrNew", Progress.NrNew),
332 new KeyValuePair<string, object>(
"nrUpdated", Progress.NrUpdated),
333 new KeyValuePair<string, object>(
"nrLoaded", Progress.NrLoaded),
334 new KeyValuePair<string, object>(
"nrDenied", Progress.NrDenied),
335 new KeyValuePair<string, object>(
"blockErrors", Progress.BlockErrors),
336 new KeyValuePair<string, object>(
"totalBytes", Progress.TotalBytes),
337 new KeyValuePair<string, object>(
"loadedBytes", Progress.LoadedBytes),
338 new KeyValuePair<string, object>(
"events", Progress.Events),
339 new KeyValuePair<string, object>(
"objectsAdded", Progress.ObjectsAdded),
340 new KeyValuePair<string, object>(
"objectsUpdated", Progress.ObjectsUpdated),
341 new KeyValuePair<string, object>(
"objectsDeleted", Progress.ObjectsDeleted),
342 new KeyValuePair<string, object>(
"objectErrors", Progress.ObjectErrors),
343 new KeyValuePair<string, object>(
"done", Progress.Done)
344 },
false),
true, this.userVariable, this.userPrivileges);
346 KeyValuePair<string, string>[] Errors = Progress.PopBlockErrorMessages();
347 if (!(Errors is
null))
349 foreach (KeyValuePair<string, string> P
in Errors)
353 new KeyValuePair<string, object>(
"time", DateTime.Now.ToLongTimeString()),
354 new KeyValuePair<string, object>(
"peer", P.Key),
355 new KeyValuePair<string, object>(
"message", P.Value)
356 },
false),
true, this.userVariable, this.userPrivileges);
361 if (!
string.IsNullOrEmpty(Msg))
365 new KeyValuePair<string, object>(
"jid", Item.
BareJid),
366 new KeyValuePair<string, object>(
"message", Msg)
367 },
false),
true, this.userVariable, this.userPrivileges);
372 await
ClientEvents.
PushEvent(
new string[] { TabID },
"ShowError", ex.Message,
false, this.userVariable, this.userPrivileges);
Helps with common JSON-related tasks.
static string Encode(string s)
Encodes a string for inclusion in JSON.
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Static class managing the runtime environment of the IoT Gateway.
static XmppClient XmppClient
XMPP Client connection of gateway.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
The request could not be completed due to a conflict with the current state of the resource....
The server understood the request, but is refusing to fulfill it. Authorization will not help and the...
Represents an HTTP request.
Variables Session
Contains session states, if the resource requires sessions, or null otherwise.
string ResourceName
Name of resource.
Represets a response of an HTTP client request.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
Event arguments for presence events.
bool IsOnline
If contact is online.
Availability Availability
Resource availability.
Contains information about current synchronization status for a peer in the network.
bool IsNeuroLedger
If the peer is a Neuro-Ledger node.
Maintains information about an item in the roster.
string NameOrBareJid
Returns the name of the contact, or the Bare JID, if there's no name provided.
string BareJid
Bare JID of the roster item.
PresenceEventArgs LastPresence
Last presence received from a resource having this bare JID.
PresenceEventArgs[] Resources
Active resources utilized by contact.
XmppState State
Current state of connection.
RosterItem[] Roster
Items in the roster.
Event arguments for block reference events.
BlockReference Block
Block reference.
Contains information about a variable.
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Allows operators to manually start network synchronization.
override bool HandlesSubPaths
If the resource handles sub-paths.
override bool UserSessions
If the resource uses user sessions.
SynchronizationResource(string ResourceName, NeuroLedgerClient NLClient, string UserVariable, params string[] UserPrivileges)
Allows operators to manually start network synchronization.
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
bool AllowsPOST
If the POST method is allowed.
POST Interface for HTTP resources.
Basic interface for a user.
Availability
Resource availability.
XmppState
State of XMPP connection.