Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SniffableDatabase.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
10
12{
17 {
18 private bool prevHasSniffers = false;
19
24 : base(false)
25 {
26 }
27
31 public override void Add(ISniffer Sniffer)
32 {
33 base.Add(Sniffer);
34 this.CheckEventHandlers();
35 }
36
40 public override void AddRange(IEnumerable<ISniffer> Sniffers)
41 {
42 base.AddRange(Sniffers);
43 this.CheckEventHandlers();
44 }
45
49 public override bool Remove(ISniffer Sniffer)
50 {
51 bool Result = base.Remove(Sniffer);
52 this.CheckEventHandlers();
53 return Result;
54 }
55
56 private void CheckEventHandlers()
57 {
58 bool b = this.HasSniffers;
59
60 if (this.prevHasSniffers != b)
61 {
62 if (b)
63 {
64 Database.CollectionCleared += this.Database_CollectionCleared;
65 Database.CollectionRepaired += this.Database_CollectionRepaired;
66 Database.ObjectDeleted += this.Database_ObjectDeleted;
67 Database.ObjectInserted += this.Database_ObjectInserted;
68 Database.ObjectUpdated += this.Database_ObjectUpdated;
69
70 Search.ObjectAddedToIndex += this.Search_ObjectAddedToIndex;
71 Search.ObjectUpdatedInIndex += this.Search_ObjectUpdatedInIndex;
72 Search.ObjectRemovedFromIndex += this.Search_ObjectRemovedFromIndex;
73 }
74 else
75 {
76 Database.CollectionCleared -= this.Database_CollectionCleared;
77 Database.CollectionRepaired -= this.Database_CollectionRepaired;
78 Database.ObjectDeleted -= this.Database_ObjectDeleted;
79 Database.ObjectInserted -= this.Database_ObjectInserted;
80 Database.ObjectUpdated -= this.Database_ObjectUpdated;
81
82 Search.ObjectAddedToIndex -= this.Search_ObjectAddedToIndex;
83 Search.ObjectUpdatedInIndex -= this.Search_ObjectUpdatedInIndex;
84 Search.ObjectRemovedFromIndex -= this.Search_ObjectRemovedFromIndex;
85 }
86
87 this.prevHasSniffers = b;
88 }
89 }
90
91 internal static async Task<string> GetJSON(object Object)
92 {
93 GenericObject Obj = await Database.Generalize(Object);
94 Dictionary<string, object> Obj2 = new Dictionary<string, object>()
95 {
96 { "ObjectId", Obj.ObjectId },
97 { "TypeName", Obj.TypeName },
98 { "CollectionName", Obj.CollectionName }
99 };
100
101 foreach (KeyValuePair<string, object> P in Obj)
102 Obj2[P.Key] = P.Value;
103
104 return JSON.Encode(Obj2, true);
105 }
106
107 private async void Database_ObjectInserted(object Sender, ObjectEventArgs e)
108 {
109 if (this.HasSniffers)
110 {
111 try
112 {
113 this.TransmitText(await GetJSON(e.Object));
114 }
115 catch (Exception)
116 {
117 this.TransmitText("Object of type " + e.Object.GetType().FullName + " inserted.");
118 }
119 }
120 }
121
122 private async void Database_ObjectUpdated(object Sender, ObjectEventArgs e)
123 {
124 if (this.HasSniffers)
125 {
126 try
127 {
128 this.ReceiveText(await GetJSON(e.Object));
129 }
130 catch (Exception)
131 {
132 this.ReceiveText("Object of type " + e.Object.GetType().FullName + " updated.");
133 }
134 }
135 }
136
137 private async void Database_ObjectDeleted(object Sender, ObjectEventArgs e)
138 {
139 if (this.HasSniffers)
140 {
141 try
142 {
143 this.Error(await GetJSON(e.Object));
144 }
145 catch (Exception)
146 {
147 this.Error("Object of type " + e.Object.GetType().FullName + " deleted.");
148 }
149 }
150 }
151
152 private Task Database_CollectionRepaired(object Sender, CollectionRepairedEventArgs e)
153 {
154 if (this.HasSniffers)
155 {
156 try
157 {
158 this.Warning("Collection has been repaired: " + e.Collection);
159
160 if (!(e.Flagged is null))
161 {
162 foreach (FlagSource Source in e.Flagged)
163 this.Exception("Flagged " + Source.Count.ToString() + " time(s) for:" + Source.Reason + "\r\n\r\n" + Source.StackTrace);
164 }
165 }
166 catch (Exception)
167 {
168 // Ignore.
169 }
170 }
171
172 return Task.CompletedTask;
173 }
174
175 private Task Database_CollectionCleared(object Sender, CollectionEventArgs e)
176 {
177 if (this.HasSniffers)
178 this.Information("Collection has been cleared: " + e.Collection);
179
180 return Task.CompletedTask;
181 }
182
183 private Task Search_ObjectAddedToIndex(object Sender, ObjectReferenceEventArgs e)
184 {
185 if (this.HasSniffers)
186 this.Information("Object added to full-text-search index: " + e.Reference.IndexCollection);
187
188 return Task.CompletedTask;
189 }
190
191 private Task Search_ObjectUpdatedInIndex(object Sender, ObjectReferenceEventArgs e)
192 {
193 if (this.HasSniffers)
194 this.Information("Object updated in full-text-search index: " + e.Reference.IndexCollection);
195
196 return Task.CompletedTask;
197 }
198
199 private Task Search_ObjectRemovedFromIndex(object Sender, ObjectReferenceEventArgs e)
200 {
201 if (this.HasSniffers)
202 this.Information("Object removed from full-text-search index: " + e.Reference.IndexCollection);
203
204 return Task.CompletedTask;
205 }
206 }
207}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
Class that can be used to sniff on database updates.
SniffableDatabase()
Class that can be used to sniff on database updates.
override void AddRange(IEnumerable< ISniffer > Sniffers)
ICommunicationLayer.AddRange
override bool Remove(ISniffer Sniffer)
ICommunicationLayer.Remove
override void Add(ISniffer Sniffer)
ICommunicationLayer.Add
Simple base class for classes implementing communication protocols.
void TransmitText(string Text)
Called when text has been transmitted.
void Exception(Exception Exception)
Called to inform the viewer of an exception state.
void ReceiveText(string Text)
Called when text has been received.
ISniffer[] Sniffers
Registered sniffers.
bool HasSniffers
If there are sniffers registered on the object.
void Error(string Error)
Called to inform the viewer of an error state.
void Warning(string Warning)
Called to inform the viewer of a warning state.
void Information(string Comment)
Called to inform the viewer of something.
Event arguments for collection events.
Event arguments for collection repaired events.
FlagSource[] Flagged
If the collection have been flagged as corrupt, and from what stack traces. Is null,...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< GenericObject > Generalize(object Object)
Creates a generalized representation of an object.
Definition: Database.cs:2473
Source of code flagging a collection for repair.
Definition: FlagSource.cs:9
int Count
Number of times the collection has been flagged from this source.
Definition: FlagSource.cs:41
string StackTrace
Stack trace of source flagging the collection.
Definition: FlagSource.cs:35
string Reason
Reason for flagging collection.
Definition: FlagSource.cs:30
string IndexCollection
Collection of full-text-search index.
Event arguments for database object events.
Generic object. Contains a sequence of properties.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:10