Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ComponentSynchronization.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
7
9{
14 public static class ComponentSynchronization
15 {
23 public static async Task SendMessage(CaseInsensitiveString Sender, CaseInsensitiveString Recipient,
24 CaseInsensitiveString RecipientServer, string Xml)
25 {
26 DateTime TP = DateTime.UtcNow;
28 {
29 Sender = Sender,
30 Recipient = Recipient,
31 RecipientServer = RecipientServer,
32 Xml = Xml,
33 Created = TP,
34 NextAttempt = TP.AddHours(1),
35 NrAttempts = 1
36 };
37
38 await Database.Insert();
39
40 await SendMessage(Message);
41 }
42
46 internal static async Task QueueUnprocessedMessages()
47 {
49 {
50 DateTime TP = Message.NextAttempt.ToLocalTime();
51
52 if (TP <= DateTime.Now)
53 await ResendMessage(Message);
54 else
55 Gateway.ScheduleEvent(ResendMessage, TP, Message);
56 }
57 }
58
59 private static async Task ResendMessage(object State)
60 {
61 if (!(State is SynchronizationMessage Message))
62 return;
63
64 Message.NrAttempts++;
65 Message.NextAttempt = DateTime.UtcNow.AddHours(Message.NrAttempts);
66
67 await Database.Update(Message);
68
69 await SendMessage(Message);
70 }
71
72 private static async Task SendMessage(SynchronizationMessage Message)
73 {
74 try
75 {
76 IqResultEventArgs e = await XmppServerModule.Server.IqRequest("set", Message.Sender, Message.Recipient,
77 string.Empty, Message.Xml);
78
79 if (e.Ok)
80 await Database.Delete(Message);
81 else
82 {
83 await XmppServerModule.Server.SendMessage(string.Empty, string.Empty, Message.Sender, Message.Recipient,
84 string.Empty, Message.Xml);
85
86 Gateway.ScheduleEvent(ResendMessage, Message.NextAttempt.ToLocalTime(), Message);
87 }
88 }
89 catch (Exception ex)
90 {
91 Log.Exception(ex);
92 }
93 }
94 }
95}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static DateTime ScheduleEvent(ScheduledEventCallback Callback, DateTime When, object State)
Schedules a one-time event.
Definition: Gateway.cs:3452
Event arguments for responses to IQ queries.
bool Ok
If the response is an OK result response (true), or an error response (false).
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:717
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
Component that synchronizes content in the federated network, by sending synchronization messages and...
static async Task SendMessage(CaseInsensitiveString Sender, CaseInsensitiveString Recipient, CaseInsensitiveString RecipientServer, string Xml)
Sends a synchronization message
Stores information about a synchronization message that must be delivered to a component on another b...