Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PubSubService.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Windows.Input;
5using System.Windows.Media;
13
15{
17 {
18 private readonly PubSubClient pubSubClient;
19 internal bool SupportsAccessAuthorize;
20 internal bool SupportsAccessOpen;
21 internal bool SupportsAccessPresence;
22 internal bool SupportsAccessRoster;
23 internal bool SupportsAccessWhitelist;
24 internal bool SupportsCollections;
25 internal bool SupportsNodeConfiguration;
26 internal bool SupportsCreateAndConfigure;
27 internal bool SupportsCreateNodes;
28 internal bool SupportsDeleteItems;
29 internal bool SupportsDeleteNodes;
30 internal bool SupportsItemIds;
31 internal bool SupportsLastPublished;
32 internal bool SupportsLeasedSubscription;
33 internal bool SupportsManageSubscriptions;
34 internal bool SupportsemberAffiliation;
35 internal bool SupportsMetaData;
36 internal bool SupportsModifyAffiliations;
37 internal bool SupportsMultiCollection;
38 internal bool SupportsMultiItems;
39 internal bool SupportsOutcastAffiliation;
40 internal bool SupportsPersistentItems;
41 internal bool SupportsPresenceSubscribe;
42 internal bool SupportsPublish;
43 internal bool SupportsPublishOnlyAffiliation;
44 internal bool SupportsPublisheAffiliation;
45 internal bool SupportsPurgeNodes;
46 internal bool SupportsRetractItems;
47 internal bool SupportsRetrieveAffiliations;
48 internal bool SupportsRetrieveDefault;
49 internal bool SupportsRetrieveDefaultSub;
50 internal bool SupportsRetrieveItems;
51 internal bool SupportsRetrieveSubscriptions;
52 internal bool SupportsSubscribe;
53 internal bool SupportsSubscriptionOptions;
54 internal bool SupportsSubscriptionNotifications;
55
56 public PubSubService(TreeNode Parent, string JID, string Name, string Node, Dictionary<string, bool> Features, PubSubClient PubSubClient)
57 : base(Parent, JID, Name, Node, Features)
58 {
59 this.SupportsAccessAuthorize = Features.ContainsKey(PubSubClient.NamespacePubSubAccessAuthorize);
60 this.SupportsAccessOpen = Features.ContainsKey(PubSubClient.NamespacePubSubAccessOpen);
61 this.SupportsAccessPresence = Features.ContainsKey(PubSubClient.NamespacePubSubAccessPresence);
62 this.SupportsAccessRoster = Features.ContainsKey(PubSubClient.NamespacePubSubAccessRoster);
63 this.SupportsAccessWhitelist = Features.ContainsKey(PubSubClient.NamespacePubSubAccessWhitelist);
64 this.SupportsCollections = Features.ContainsKey(PubSubClient.NamespacePubSubCollections);
65 this.SupportsNodeConfiguration = Features.ContainsKey(PubSubClient.NamespacePubSubNodeConfiguration);
66 this.SupportsCreateAndConfigure = Features.ContainsKey(PubSubClient.NamespacePubSubCreateAndConfigure);
67 this.SupportsCreateNodes = Features.ContainsKey(PubSubClient.NamespacePubSubCreateNodes);
68 this.SupportsDeleteItems = Features.ContainsKey(PubSubClient.NamespacePubSubDeleteItems);
69 this.SupportsDeleteNodes = Features.ContainsKey(PubSubClient.NamespacePubSubDeleteNodes);
70 this.SupportsItemIds = Features.ContainsKey(PubSubClient.NamespacePubSubItemIds);
71 this.SupportsLastPublished = Features.ContainsKey(PubSubClient.NamespacePubSubLastPublished);
72 this.SupportsLeasedSubscription = Features.ContainsKey(PubSubClient.NamespacePubSubLeasedSubscription);
73 this.SupportsManageSubscriptions = Features.ContainsKey(PubSubClient.NamespacePubSubManageSubscriptions);
74 this.SupportsemberAffiliation = Features.ContainsKey(PubSubClient.NamespacePubSubemberAffiliation);
75 this.SupportsMetaData = Features.ContainsKey(PubSubClient.NamespacePubSubMetaData);
76 this.SupportsModifyAffiliations = Features.ContainsKey(PubSubClient.NamespacePubSubModifyAffiliations);
77 this.SupportsMultiCollection = Features.ContainsKey(PubSubClient.NamespacePubSubMultiCollection);
78 this.SupportsMultiItems = Features.ContainsKey(PubSubClient.NamespacePubSubMultiItems);
79 this.SupportsOutcastAffiliation = Features.ContainsKey(PubSubClient.NamespacePubSubOutcastAffiliation);
80 this.SupportsPersistentItems = Features.ContainsKey(PubSubClient.NamespacePubSubPersistentItems);
81 this.SupportsPresenceSubscribe = Features.ContainsKey(PubSubClient.NamespacePubSubPresenceSubscribe);
82 this.SupportsPublish = Features.ContainsKey(PubSubClient.NamespacePubSubPublish);
83 this.SupportsPublishOnlyAffiliation = Features.ContainsKey(PubSubClient.NamespacePubSubPublishOnlyAffiliation);
84 this.SupportsPublisheAffiliation = Features.ContainsKey(PubSubClient.NamespacePubSubPublisheAffiliation);
85 this.SupportsPurgeNodes = Features.ContainsKey(PubSubClient.NamespacePubSubPurgeNodes);
86 this.SupportsRetractItems = Features.ContainsKey(PubSubClient.NamespacePubSubRetractItems);
87 this.SupportsRetrieveAffiliations = Features.ContainsKey(PubSubClient.NamespacePubSubRetrieveAffiliations);
88 this.SupportsRetrieveDefault = Features.ContainsKey(PubSubClient.NamespacePubSubRetrieveDefault);
89 this.SupportsRetrieveDefaultSub = Features.ContainsKey(PubSubClient.NamespacePubSubRetrieveDefaultSub);
90 this.SupportsRetrieveItems = Features.ContainsKey(PubSubClient.NamespacePubSubRetrieveItems);
91 this.SupportsRetrieveSubscriptions = Features.ContainsKey(PubSubClient.NamespacePubSubRetrieveSubscriptions);
92 this.SupportsSubscribe = Features.ContainsKey(PubSubClient.NamespacePubSubSubscribe);
93 this.SupportsSubscriptionOptions = Features.ContainsKey(PubSubClient.NamespacePubSubSubscriptionOptions);
94 this.SupportsSubscriptionNotifications = Features.ContainsKey(PubSubClient.NamespacePubSubSubscriptionNotifications);
95
96 this.pubSubClient = PubSubClient;
97
98 this.children = new SortedDictionary<string, TreeNode>()
99 {
100 { string.Empty, new Loading(this) }
101 };
102
103 this.pubSubClient.ItemNotification += this.PubSubClient_ItemNotification;
104 this.pubSubClient.ItemRetracted += this.PubSubClient_ItemRetracted;
105 this.pubSubClient.NodePurged += this.PubSubClient_NodePurged;
106 this.pubSubClient.SubscriptionRequest += this.PubSubClient_SubscriptionRequest;
107 }
108
109 private Task PubSubClient_SubscriptionRequest(object Sender, SubscriptionRequestEventArgs e)
110 {
111 // TODO
112 return Task.CompletedTask;
113 }
114
115 private Task PubSubClient_NodePurged(object Sender, NodeNotificationEventArgs e)
116 {
117 if (this.TryGetChild(e.NodeName, out TreeNode N) && N is PubSubNode Node)
118 Node.Purged(e);
119
120 return Task.CompletedTask;
121 }
122
123 private Task PubSubClient_ItemRetracted(object Sender, ItemNotificationEventArgs e)
124 {
125 if (this.TryGetChild(e.NodeName, out TreeNode N) && N is PubSubNode Node)
126 Node.ItemRetracted(e);
127
128 return Task.CompletedTask;
129 }
130
131 private Task PubSubClient_ItemNotification(object Sender, ItemNotificationEventArgs e)
132 {
133 if (this.TryGetChild(e.NodeName, out TreeNode N) && N is PubSubNode Node)
134 Node.ItemNotification(e);
135
136 return Task.CompletedTask;
137 }
138
139 public PubSubClient PubSubClient => this.pubSubClient;
140
141 public override ImageSource ImageResource => XmppAccountNode.database;
142
143 public override string ToolTip
144 {
145 get
146 {
147 return "Publish/Subscribe Service";
148 }
149 }
150
151 private bool loadingChildren = false;
152
153 protected override void LoadChildren()
154 {
155 if (!this.loadingChildren && !this.IsLoaded)
156 {
157 Mouse.OverrideCursor = Cursors.Wait;
158
159 this.loadingChildren = true;
160 this.Account.Client.SendServiceItemsDiscoveryRequest(this.pubSubClient.ComponentAddress, (Sender, e) =>
161 {
162 this.loadingChildren = false;
163 MainWindow.MouseDefault();
164
165 if (e.Ok)
166 {
167 SortedDictionary<string, TreeNode> Children = new SortedDictionary<string, TreeNode>();
168
169 this.NodesRemoved(this.children.Values, this);
170
171 foreach (Item Item in e.Items)
172 {
173 this.Account.Client.SendServiceDiscoveryRequest(this.PubSubClient.ComponentAddress, Item.Node, (sender2, e2) =>
174 {
175 if (e2.Ok)
176 {
177 Item Item2 = (Item)e2.State;
178 string Jid = Item2.JID;
179 string Node = Item2.Node;
180 string Name = Item2.Name;
181 NodeType NodeType = NodeType.leaf;
182 PubSubNode NewNode;
183
184 foreach (Identity Identity in e2.Identities)
185 {
186 if (Identity.Category == "pubsub")
187 {
188 if (!Enum.TryParse(Identity.Type, out NodeType))
189 NodeType = NodeType.leaf;
190
191 if (!string.IsNullOrEmpty(Identity.Name))
192 Name = Identity.Name;
193 }
194 }
195
196 lock (Children)
197 {
198 Children[Item2.Node] = NewNode = new PubSubNode(this, Jid, Node, Name, NodeType);
199 this.children = new SortedDictionary<string, TreeNode>(Children);
200 }
201
202 this.OnUpdated();
203 this.NodesAdded(new TreeNode[] { NewNode }, this);
204 }
205
206 return Task.CompletedTask;
207
208 }, Item);
209 }
210 }
211 else
212 MainWindow.ErrorBox(string.IsNullOrEmpty(e.ErrorText) ? "Unable to get root nodes." : e.ErrorText);
213
214 return Task.CompletedTask;
215
216 }, null);
217 }
218
219 base.LoadChildren();
220 }
221
222 public override bool CanAddChildren => true;
223
224 public override void Add()
225 {
226 Mouse.OverrideCursor = Cursors.Wait;
227
228 this.pubSubClient.GetDefaultNodeConfiguration((Sender, e) =>
229 {
230 MainWindow.MouseDefault();
231
232 if (e.Ok)
233 {
234 int c = e.Form.Fields.Length;
235 Field[] Fields = new Field[c + 1];
236 DataForm Form = null;
237
238 Array.Copy(e.Form.Fields, 0, Fields, 1, c);
239 Fields[0] = new TextSingleField(null, "Node", "Node Name:", true, new string[] { string.Empty }, null, "Name of the node to create.",
240 StringDataType.Instance, null, string.Empty, false, false, false);
241
242 Form = new DataForm(this.pubSubClient.Client,
243 (sender2, e2) =>
244 {
245 string NodeName = Form["Node"].ValueString;
246 string Title = Form["pubsub#title"]?.ValueString ?? string.Empty;
247
248 if (!Enum.TryParse(Form["pubsub#node_type"]?.ValueString ?? string.Empty, out NodeType Type))
249 Type = NodeType.leaf;
250
251 Mouse.OverrideCursor = Cursors.Wait;
252
253 this.pubSubClient.CreateNode(NodeName, e.Form, (sender3, e3) =>
254 {
255 MainWindow.MouseDefault();
256
257 if (e3.Ok)
258 {
259 if (this.IsLoaded)
260 {
261 PubSubNode Node = new PubSubNode(this, this.pubSubClient.ComponentAddress, NodeName, Title, Type);
262
263 if (this.children is null)
264 this.children = new SortedDictionary<string, TreeNode>() { { Node.Key, Node } };
265 else
266 {
267 lock (this.children)
268 {
269 this.children[Node.Key] = Node;
270 }
271 }
272
273 MainWindow.UpdateGui(() =>
274 {
275 this.Account?.View?.NodeAdded(this, Node);
276 this.OnUpdated();
277 return Task.CompletedTask;
278 });
279 }
280 }
281 else
282 MainWindow.ErrorBox("Unable to create node: " + e3.ErrorText);
283
284 return Task.CompletedTask;
285 }, null);
286
287 return Task.CompletedTask;
288 },
289 (sender2, e2) =>
290 {
291 // Do nothing.
292 return Task.CompletedTask;
293 }, string.Empty, string.Empty, Fields);
294
295 MainWindow.UpdateGui(async () =>
296 {
297 ParameterDialog Dialog = await ParameterDialog.CreateAsync(Form);
298 Dialog.ShowDialog();
299 });
300 }
301 else
302 MainWindow.ErrorBox("Unable to get default node properties: " + e.ErrorText);
303
304 return Task.CompletedTask;
305
306 }, null);
307 }
308
309
310 }
311}
Interaction logic for ParameterDialog.xaml
static async Task< ParameterDialog > CreateAsync(DataForm Form)
Interaction logic for ParameterDialog.xaml
Interaction logic for xaml
Represents a data source in a concentrator.
Definition: Loading.cs:11
Represents a node in a Publish/Subscribe service.
Definition: PubSubNode.cs:27
override void Add()
Is called when the user wants to add a node to the current node.
override void LoadChildren()
Method is called to make sure children are loaded.
Abstract base class for tree nodes in the connection view.
Definition: TreeNode.cs:24
TreeNode Parent
Parent node. May be null if a root node.
Definition: TreeNode.cs:107
bool TryGetChild(string ChildKey, out TreeNode Child)
Tries to get the child node corresponding to a given key.
Definition: TreeNode.cs:96
Class representing a normal XMPP account.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
static readonly StringDataType Instance
Public instance of data type.
string ErrorText
Any error specific text.
bool Ok
If the response is an OK result response (true), or an error response (false).
Client managing communication with a Publish/Subscribe component. https://xmpp.org/extensions/xep-006...
Definition: PubSubClient.cs:20
const string NamespacePubSubMultiItems
http://jabber.org/protocol/pubsub#multi-items
const string NamespacePubSubOutcastAffiliation
http://jabber.org/protocol/pubsub#outcast-affiliation
const string NamespacePubSubLastPublished
http://jabber.org/protocol/pubsub#last-published
const string NamespacePubSubDeleteItems
http://jabber.org/protocol/pubsub#delete-items
const string NamespacePubSubRetrieveDefaultSub
http://jabber.org/protocol/pubsub#retrieve-default-sub
const string NamespacePubSubModifyAffiliations
http://jabber.org/protocol/pubsub#modify-affiliations
const string NamespacePubSubCollections
http://jabber.org/protocol/pubsub#collections
Definition: PubSubClient.cs:94
const string NamespacePubSubPublish
http://jabber.org/protocol/pubsub#publish
const string NamespacePubSubCreateNodes
http://jabber.org/protocol/pubsub#create-nodes
const string NamespacePubSubRetrieveSubscriptions
http://jabber.org/protocol/pubsub#retrieve-subscriptions
const string NamespacePubSubRetractItems
http://jabber.org/protocol/pubsub#retract-items
const string NamespacePubSubSubscriptionOptions
http://jabber.org/protocol/pubsub#subscription-options
const string NamespacePubSubLeasedSubscription
http://jabber.org/protocol/pubsub#leased-subscription
const string NamespacePubSubAccessRoster
http://jabber.org/protocol/pubsub#access-roster
Definition: PubSubClient.cs:84
const string NamespacePubSubManageSubscriptions
http://jabber.org/protocol/pubsub#manage-subscriptions
const string NamespacePubSubAccessPresence
http://jabber.org/protocol/pubsub#access-presence
Definition: PubSubClient.cs:79
const string NamespacePubSubPresenceSubscribe
http://jabber.org/protocol/pubsub#presence-subscribe
const string NamespacePubSubPersistentItems
http://jabber.org/protocol/pubsub#persistent-items
const string NamespacePubSubCreateAndConfigure
http://jabber.org/protocol/pubsub#create-and-configure
const string NamespacePubSubMultiCollection
http://jabber.org/protocol/pubsub#multi-collection
const string NamespacePubSubDeleteNodes
http://jabber.org/protocol/pubsub#delete-nodes
const string NamespacePubSubMetaData
http://jabber.org/protocol/pubsub#meta-data
const string NamespacePubSubemberAffiliation
http://jabber.org/protocol/pubsub#member-affiliation
const string NamespacePubSubSubscribe
http://jabber.org/protocol/pubsub#subscribe
const string NamespacePubSubRetrieveDefault
http://jabber.org/protocol/pubsub#retrieve-default
const string NamespacePubSubSubscriptionNotifications
http://jabber.org/protocol/pubsub#subscription-notifications
const string NamespacePubSubAccessAuthorize
http://jabber.org/protocol/pubsub#access-authorize
Definition: PubSubClient.cs:69
const string NamespacePubSubItemIds
http://jabber.org/protocol/pubsub#item-ids
const string NamespacePubSubRetrieveAffiliations
http://jabber.org/protocol/pubsub#retrieve-affiliations
const string NamespacePubSubRetrieveItems
http://jabber.org/protocol/pubsub#retrieve-items
const string NamespacePubSubNodeConfiguration
http://jabber.org/protocol/pubsub#config-node
Definition: PubSubClient.cs:99
const string NamespacePubSubPurgeNodes
http://jabber.org/protocol/pubsub#purge-nodes
const string NamespacePubSubAccessWhitelist
http://jabber.org/protocol/pubsub#access-whitelist
Definition: PubSubClient.cs:89
const string NamespacePubSubPublishOnlyAffiliation
http://jabber.org/protocol/pubsub#publish-only-affiliation
const string NamespacePubSubAccessOpen
http://jabber.org/protocol/pubsub#access-open
Definition: PubSubClient.cs:74
const string NamespacePubSubPublisheAffiliation
http://jabber.org/protocol/pubsub#publisher-affiliation