Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmppConcentrator.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Windows.Input;
8
10{
15 {
16 private Dictionary<string, bool> capabilities = null;
17 private readonly Dictionary<string, DataSource> dataSources = new Dictionary<string, DataSource>();
18 private readonly bool suportsEvents;
19
20 public XmppConcentrator(TreeNode Parent, XmppClient Client, string BareJid, bool SupportsEventSubscripton, bool SupportsRdp)
21 : base(Parent, Client, BareJid, SupportsRdp)
22 {
23 this.suportsEvents = SupportsEventSubscripton;
24 this.children = new SortedDictionary<string, TreeNode>()
25 {
26 { string.Empty, new Loading(this) }
27 };
28
29 this.CheckCapabilities();
30 }
31
35 public bool SupportsEvents => this.suportsEvents;
36
37 private void CheckCapabilities()
38 {
39 if (this.capabilities is null)
40 {
41 string FullJid = this.FullJid;
42
43 if (!string.IsNullOrEmpty(FullJid))
44 {
45 this.XmppAccountNode.ConcentratorClient.GetCapabilities(FullJid, (Sender, e) =>
46 {
47 if (e.Ok)
48 {
49 Dictionary<string, bool> Capabilities = new Dictionary<string, bool>();
50
51 foreach (string s in e.Capabilities)
52 Capabilities[s] = true;
53
54 this.capabilities = Capabilities;
55 }
56
57 return Task.CompletedTask;
58 }, null);
59 }
60 }
61 }
62
63 public override string TypeName
64 {
65 get { return "Concentrator"; }
66 }
67
68 public string FullJid
69 {
70 get
71 {
72 XmppAccountNode AccountNode = this.XmppAccountNode;
73 if (AccountNode is null || !AccountNode.IsOnline)
74 return null;
75
76 RosterItem Item = AccountNode.Client[this.BareJID];
78
79 if (e is null || e.Availability == Availability.Offline)
80 return null;
81 else
82 return e.From;
83 }
84 }
85
86 private bool loadingChildren = false;
87
88 protected override void LoadChildren()
89 {
90 if (!this.loadingChildren && !this.IsLoaded)
91 {
92 string FullJid = this.FullJid;
93
94 if (!string.IsNullOrEmpty(FullJid))
95 {
96 Mouse.OverrideCursor = Cursors.Wait;
97
99
100 this.loadingChildren = true;
101 ConcentratorClient.GetRootDataSources(FullJid, (Sender, e) =>
102 {
103 this.loadingChildren = false;
104 MainWindow.MouseDefault();
105
106 if (e.Ok)
107 {
108 SortedDictionary<string, TreeNode> Children = new SortedDictionary<string, TreeNode>();
109
110 foreach (DataSourceReference Ref in e.DataSources)
111 {
112 DataSource DataSource = new DataSource(this, Ref.SourceID, Ref.SourceID, Ref.HasChildren);
113 Children[Ref.SourceID] = DataSource;
114
115 DataSource.SubscribeToEvents();
116 }
117
118 this.children = Children;
119
120 this.OnUpdated();
121 this.NodesAdded(Children.Values, this);
122 }
123
124 return Task.CompletedTask;
125 }, null);
126 }
127 }
128
129 base.LoadChildren();
130 }
131
132 public void NodesAdded(IEnumerable<TreeNode> Nodes, TreeNode Parent)
133 {
135
136 foreach (TreeNode Node in Nodes)
137 {
138 View?.NodeAdded(Parent, Node);
139
140 if (Node is DataSource DataSource)
141 {
142 lock (this.dataSources)
143 {
144 this.dataSources[DataSource.Key] = DataSource;
145 }
146 }
147 }
148 }
149
150 public void NodesRemoved(IEnumerable<TreeNode> Nodes, TreeNode Parent)
151 {
152 Controls.ConnectionView View = this.XmppAccountNode?.View;
153 LinkedList<KeyValuePair<TreeNode, TreeNode>> ToRemove = new LinkedList<KeyValuePair<TreeNode, TreeNode>>();
154
155 foreach (TreeNode Node in Nodes)
156 ToRemove.AddLast(new KeyValuePair<TreeNode, TreeNode>(Parent, Node));
157
158 while (!(ToRemove.First is null))
159 {
160 KeyValuePair<TreeNode, TreeNode> P = ToRemove.First.Value;
161 ToRemove.RemoveFirst();
162
163 Parent = P.Key;
164 TreeNode Node = P.Value;
165
166 if (Node.HasChildren.HasValue && Node.HasChildren.Value)
167 {
168 foreach (TreeNode Child in Node.Children)
169 ToRemove.AddLast(new KeyValuePair<TreeNode, TreeNode>(Node, Child));
170 }
171
172 View?.NodeRemoved(Parent, Node);
173
174 if (Node is DataSource DataSource)
175 {
176 if (this.XmppAccountNode.IsOnline)
177 DataSource.UnsubscribeFromEvents();
178
179 lock (this.dataSources)
180 {
181 this.dataSources.Remove(DataSource.Key);
182 }
183 }
184 }
185 }
186
187 protected override void UnloadChildren()
188 {
189 base.UnloadChildren();
190
191 if (!this.IsLoaded)
192 {
193 if (!(this.children is null))
194 this.NodesRemoved(this.children.Values, this);
195
196 this.children = new SortedDictionary<string, TreeNode>()
197 {
198 { string.Empty, new Loading(this) }
199 };
200
201 this.OnUpdated();
202 }
203 }
204
205 internal Task ConcentratorClient_OnEvent(object Sender, SourceEventMessageEventArgs EventMessage)
206 {
207 DataSource Source;
208
209 lock (this.dataSources)
210 {
211 if (!this.dataSources.TryGetValue(EventMessage.Event.SourceId, out Source))
212 return Task.CompletedTask;
213 }
214
215 return Source.ConcentratorClient_OnEvent(Sender, EventMessage);
216 }
217
218 }
219}
Interaction logic for ConnectionView.xaml
Interaction logic for xaml
Represents a data source in a concentrator.
Definition: DataSource.cs:16
bool SupportsEvents
If event subscription is supported for readable nodes.
override void LoadChildren()
Method is called to make sure children are loaded.
override void UnloadChildren()
Method is called to notify children can be unloaded.
Represents a data source in a concentrator.
Definition: Loading.cs:11
Abstract base class for tree nodes in the connection view.
Definition: TreeNode.cs:24
TreeNode[] Children
Children of the node. If null, children are not loaded.
Definition: TreeNode.cs:72
TreeNode Parent
Parent node. May be null if a root node.
Definition: TreeNode.cs:107
virtual void OnUpdated()
Raises the Updated event.
Definition: TreeNode.cs:224
Class representing a normal XMPP account.
Represents an XMPP contact whose capabilities have not been measured.
Definition: XmppContact.cs:24
Implements an XMPP concentrator client interface.
Task GetRootDataSources(string To, EventHandlerAsync< DataSourcesEventArgs > Callback, object State)
Gets all root data sources from the server.
Task GetCapabilities(string To, EventHandlerAsync< CapabilitiesEventArgs > Callback, object State)
Gets the capabilities of a concentrator server.
Event arguments for presence events.
string From
From where the presence was received.
Availability Availability
Resource availability.
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
PresenceEventArgs LastPresence
Last presence received from a resource having this bare JID.
Definition: RosterItem.cs:356
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
Availability
Resource availability.
Definition: Availability.cs:7