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;
2using System.Collections.Generic;
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 try
110 {
111 await this.TransmitText(await GetJSON(e.Object));
112 }
113 catch (Exception)
114 {
115 await this.TransmitText("Object of type " + e.Object.GetType().FullName + " inserted.");
116 }
117 }
118
119 private async void Database_ObjectUpdated(object Sender, ObjectEventArgs e)
120 {
121 try
122 {
123 await this.ReceiveText(await GetJSON(e.Object));
124 }
125 catch (Exception)
126 {
127 await this.ReceiveText("Object of type " + e.Object.GetType().FullName + " updated.");
128 }
129 }
130
131 private async void Database_ObjectDeleted(object Sender, ObjectEventArgs e)
132 {
133 try
134 {
135 await this.Error(await GetJSON(e.Object));
136 }
137 catch (Exception)
138 {
139 await this.Error("Object of type " + e.Object.GetType().FullName + " deleted.");
140 }
141 }
142
143 private async Task Database_CollectionRepaired(object Sender, CollectionRepairedEventArgs e)
144 {
145 try
146 {
147 await this.Warning("Collection has been repaired: " + e.Collection);
148
149 if (!(e.Flagged is null))
150 {
151 foreach (FlagSource Source in e.Flagged)
152 await this.Exception("Flagged " + Source.Count.ToString() + " time(s) for:" + Source.Reason + "\r\n\r\n" + Source.StackTrace);
153 }
154 }
155 catch (Exception)
156 {
157 // Ignore.
158 }
159 }
160
161 private Task Database_CollectionCleared(object Sender, CollectionEventArgs e)
162 {
163 return this.Information("Collection has been cleared: " + e.Collection);
164 }
165
166 private Task Search_ObjectAddedToIndex(object Sender, ObjectReferenceEventArgs e)
167 {
168 return this.Information("Object added to full-text-search index: " + e.Reference.IndexCollection);
169 }
170
171 private Task Search_ObjectUpdatedInIndex(object Sender, ObjectReferenceEventArgs e)
172 {
173 return this.Information("Object updated in full-text-search index: " + e.Reference.IndexCollection);
174 }
175
176 private Task Search_ObjectRemovedFromIndex(object Sender, ObjectReferenceEventArgs e)
177 {
178 return this.Information("Object removed from full-text-search index: " + e.Reference.IndexCollection);
179 }
180 }
181}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
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.
Task Error(string Error)
Called to inform the viewer of an error state.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
ISniffer[] Sniffers
Registered sniffers.
Task Information(string Comment)
Called to inform the viewer of something.
Task TransmitText(string Text)
Called when text has been transmitted.
bool HasSniffers
If there are sniffers registered on the object.
Task ReceiveText(string Text)
Called when text has been received.
Task Warning(string Warning)
Called to inform the viewer of a warning state.
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:19
static Task< GenericObject > Generalize(object Object)
Creates a generalized representation of an object.
Definition: Database.cs:1678
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:11