Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SnifferView.xaml.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Xml;
5using System.Xml.Schema;
6using System.Xml.Xsl;
7using System.Windows;
8using System.Windows.Controls;
9using System.Windows.Media;
10using Microsoft.Win32;
13using Waher.Events;
16using System.Threading.Tasks;
17
19{
23 public partial class SnifferView : UserControl, ITabView
24 {
25 private readonly TreeNode node;
26 private readonly string identifier;
27 private readonly bool custom;
28 private TabSniffer sniffer;
29
30 public SnifferView(TreeNode Node, string Identifier, bool Custom)
31 {
32 this.node = Node;
33 this.identifier = Identifier;
34 this.sniffer = null;
35 this.custom = Custom;
36
38 }
39
40 public void Dispose()
41 {
42 this.node?.RemoveSniffer(this.sniffer);
43 this.node?.ViewClosed();
44 }
45
46 public TreeNode Node => this.node;
47 public string Identifier => this.identifier;
48 public bool Custom => this.custom;
49
50 public TabSniffer Sniffer
51 {
52 get => this.sniffer;
53 internal set => this.sniffer = value;
54 }
55
56 public void Add(SniffItem Item)
57 {
58 MainWindow.UpdateGui(this.AddItem, Item);
59 }
60
61 private Task AddItem(object P)
62 {
63 this.SnifferListView.Items.Add((SniffItem)P);
64 this.SnifferListView.ScrollIntoView(P);
65 return Task.CompletedTask;
66 }
67
68 public void NewButton_Click(object Sender, RoutedEventArgs e)
69 {
70 this.SnifferListView.Items.Clear();
71 }
72
73 public void SaveButton_Click(object Sender, RoutedEventArgs e)
74 {
75 this.SaveAsButton_Click(Sender, e);
76 }
77
78 public void SaveAsButton_Click(object Sender, RoutedEventArgs e)
79 {
80 SaveFileDialog Dialog = new SaveFileDialog()
81 {
82 AddExtension = true,
83 CheckPathExists = true,
84 CreatePrompt = false,
85 DefaultExt = "xml",
86 Filter = "XML Files (*.xml)|*.xml|HTML Files (*.html,*.htm)|*.html,*.htm|All Files (*.*)|*.*",
87 Title = "Save sniff file"
88 };
89
90 bool? Result = Dialog.ShowDialog(MainWindow.FindWindow(this));
91
92 if (Result.HasValue && Result.Value)
93 {
94 try
95 {
96 if (Dialog.FilterIndex == 2)
97 {
98 StringBuilder Xml = new StringBuilder();
99 using (XmlWriter w = XmlWriter.Create(Xml, XML.WriterSettings(true, true)))
100 {
101 this.SaveAsXml(w);
102 }
103
104 string Html = XSL.Transform(Xml.ToString(), sniffToHtml);
105
106 File.WriteAllText(Dialog.FileName, Html, Encoding.UTF8);
107 }
108 else
109 {
110 using (FileStream f = File.Create(Dialog.FileName))
111 {
112 using (XmlWriter w = XmlWriter.Create(f, XML.WriterSettings(true, false)))
113 {
114 this.SaveAsXml(w);
115 }
116 }
117 }
118 }
119 catch (Exception ex)
120 {
121 MessageBox.Show(MainWindow.FindWindow(this), ex.Message, "Unable to save file.", MessageBoxButton.OK, MessageBoxImage.Error);
122 }
123 }
124 }
125
126 private static readonly XslCompiledTransform sniffToHtml = XSL.LoadTransform("Waher.Client.WPF.Transforms.SniffToHTML.xslt");
127 private static readonly XmlSchema schema = XSL.LoadSchema("Waher.Client.WPF.Schema.Sniff.xsd");
128 private const string sniffNamespace = "http://waher.se/Schema/Sniff.xsd";
129 private const string sniffRoot = "Sniff";
130
131 private void SaveAsXml(XmlWriter w)
132 {
133 w.WriteStartElement(sniffRoot, sniffNamespace);
134
135 foreach (SniffItem Item in this.SnifferListView.Items)
136 {
137 w.WriteStartElement(Item.Type.ToString());
138 w.WriteAttributeString("timestamp", XML.Encode(Item.Timestamp));
139
140 if (!(Item.Data is null))
141 w.WriteValue(Convert.ToBase64String(Item.Data));
142 else
143 w.WriteValue(Item.Message);
144
145 w.WriteEndElement();
146 }
147
148 w.WriteEndElement();
149 w.Flush();
150 }
151
152 public void OpenButton_Click(object Sender, RoutedEventArgs e)
153 {
154 try
155 {
156 OpenFileDialog Dialog = new OpenFileDialog()
157 {
158 AddExtension = true,
159 CheckFileExists = true,
160 CheckPathExists = true,
161 DefaultExt = "xml",
162 Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
163 Multiselect = false,
164 ShowReadOnly = true,
165 Title = "Open sniff file"
166 };
167
168 bool? Result = Dialog.ShowDialog(MainWindow.FindWindow(this));
169
170 if (Result.HasValue && Result.Value)
171 {
172 XmlDocument Xml = new XmlDocument()
173 {
174 PreserveWhitespace = true
175 };
176 Xml.Load(Dialog.FileName);
177
178 this.Load(Xml, Dialog.FileName);
179 }
180 }
181 catch (Exception ex)
182 {
183 ex = Log.UnnestException(ex);
184 MessageBox.Show(ex.Message, "Unable to load file.", MessageBoxButton.OK, MessageBoxImage.Error);
185 }
186 }
187
188 public void Load(XmlDocument Xml, string FileName)
189 {
190 XmlElement E;
191 DateTime Timestamp;
192 Color ForegroundColor;
193 Color BackgroundColor;
194 string Message;
195 byte[] Data;
196 bool IsData;
197
198 XSL.Validate(FileName, Xml, sniffRoot, sniffNamespace, schema);
199
200 this.SnifferListView.Items.Clear();
201
202 foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
203 {
204 E = N as XmlElement;
205 if (E is null)
206 continue;
207
208 if (!Enum.TryParse(E.LocalName, out SniffItemType Type))
209 continue;
210
211 Timestamp = XML.Attribute(E, "timestamp", DateTime.MinValue);
212
213 switch (Type)
214 {
215 case SniffItemType.DataReceived:
216 ForegroundColor = Colors.White;
217 BackgroundColor = Colors.Navy;
218 IsData = true;
219 break;
220
221 case SniffItemType.DataTransmitted:
222 ForegroundColor = Colors.Black;
223 BackgroundColor = Colors.White;
224 IsData = true;
225 break;
226
227 case SniffItemType.TextReceived:
228 ForegroundColor = Colors.White;
229 BackgroundColor = Colors.Navy;
230 IsData = false;
231 break;
232
233 case SniffItemType.TextTransmitted:
234 ForegroundColor = Colors.Black;
235 BackgroundColor = Colors.White;
236 IsData = false;
237 break;
238
239 case SniffItemType.Information:
240 ForegroundColor = Colors.Yellow;
241 BackgroundColor = Colors.DarkGreen;
242 IsData = false;
243 break;
244
245 case SniffItemType.Warning:
246 ForegroundColor = Colors.Black;
247 BackgroundColor = Colors.Yellow;
248 IsData = false;
249 break;
250
251 case SniffItemType.Error:
252 ForegroundColor = Colors.Yellow;
253 BackgroundColor = Colors.Red;
254 IsData = false;
255 break;
256
257 case SniffItemType.Exception:
258 ForegroundColor = Colors.Yellow;
259 BackgroundColor = Colors.DarkRed;
260 IsData = false;
261 break;
262
263 default:
264 continue;
265 }
266
267 if (IsData)
268 {
269 Data = Convert.FromBase64String(E.InnerText);
270 Message = TabSniffer.HexToString(Data);
271 }
272 else
273 {
274 Data = null;
275 Message = E.InnerText;
276 }
277
278 this.Add(new SniffItem(Timestamp, Type, Message, Data, ForegroundColor, BackgroundColor));
279 }
280 }
281
282 private void UserControl_SizeChanged(object Sender, SizeChangedEventArgs e)
283 {
284 if (this.SnifferListView.View is GridView GridView)
285 GridView.Columns[1].Width = Math.Max(this.ActualWidth - GridView.Columns[0].ActualWidth - SystemParameters.VerticalScrollBarWidth - 8, 10);
286 }
287
288 }
289}
Interaction logic for SnifferView.xaml
void InitializeComponent()
InitializeComponent
Represents one item in a sniffer output.
Definition: SniffItem.cs:23
byte[] Data
Optional binary data.
Definition: SniffItem.cs:70
SniffItemType Type
Sniff item type.
Definition: SniffItem.cs:55
DateTime Timestamp
Timestamp of event.
Definition: SniffItem.cs:50
Interaction logic for xaml
Abstract base class for tree nodes in the connection view.
Definition: TreeNode.cs:24
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
static XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
Definition: XML.cs:1177
Static class managing loading of XSL resources stored as embedded resources or in content files.
Definition: XSL.cs:15
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
Definition: XSL.cs:23
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
Definition: XSL.cs:70
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
Definition: XSL.cs:162
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Definition: XSL.cs:118
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
Interface for tab view user controls in the client.
Definition: ITabView.cs:10