Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConnectionSensitiveModel.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
5
7{
12 {
13 private readonly Property<XmppState> connectionState;
14 private readonly Property<bool> connected;
15 private NetworkModel networkModel;
16
21 {
22 this.connected = new Property<bool>(nameof(this.Connected), false, this);
23 this.connectionState = new Property<XmppState>(nameof(this.ConnectionState), XmppState.Offline, this);
24 }
25
29 public bool Connected => this.connected.Value;
30
34 public NetworkModel Network => this.networkModel;
35
40 {
41 get => this.connectionState.Value;
42 set
43 {
44 this.connectionState.Value = value;
45 this.connected.Value = value == XmppState.Connected;
46 }
47 }
48
50 public override async Task Start()
51 {
52 await base.Start();
53
54 this.networkModel = await MainWindow.InstantiateModel<NetworkModel>();
55 this.networkModel.OnStateChanged += this.NetworkModel_OnStateChanged;
56
58 {
59 this.ConnectionState = this.networkModel.State;
60 return Task.CompletedTask;
61 });
62 }
63
65 public override async Task Stop()
66 {
67 this.networkModel.OnStateChanged -= this.NetworkModel_OnStateChanged;
68 await base.Stop();
69 }
70
71 private Task NetworkModel_OnStateChanged(object Sender, XmppState NewState)
72 {
73 MainWindow.UpdateGui(async () =>
74 {
75 try
76 {
77 this.ConnectionState = this.networkModel.State;
78 await this.StateChanged(NewState);
79 }
80 catch (Exception ex)
81 {
82 Log.Exception(ex);
83 }
84 });
85
86 return Task.CompletedTask;
87 }
88
93 protected virtual async Task StateChanged(XmppState NewState)
94 {
95 EventHandlerAsync<XmppState> h = this.OnStateChanged;
96 if (h is not null)
97 await h(this, NewState);
98 }
99
103 public event EventHandlerAsync<XmppState> OnStateChanged;
104 }
105}
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
XmppState
State of XMPP connection.
Definition: XmppState.cs:7