Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqttBrokers.cs
1using System.Collections.Generic;
2using System.Text;
3using System.Threading.Tasks;
6
8{
12 public static class MqttBrokers
13 {
14 private static readonly Dictionary<string, MqttBroker> brokers = new Dictionary<string, MqttBroker>();
15
27 public static string GetKey(string Host, int Port, bool Tls, bool TrustServer, string UserName, string Password, string ConnectionSubscription)
28 {
29 StringBuilder sb = new StringBuilder();
30
31 sb.AppendLine(Host);
32 sb.AppendLine(Port.ToString());
33 sb.AppendLine(Tls.ToString());
34 sb.AppendLine(TrustServer.ToString());
35 sb.AppendLine(UserName);
36 sb.AppendLine(Password);
37 sb.AppendLine(ConnectionSubscription);
38
39 return Hashes.ComputeSHA1HashString(Encoding.UTF8.GetBytes(sb.ToString()));
40 }
41
45 public static async Task<MqttBroker> GetBroker(MqttBrokerNode Node, string Key, string Host, int Port, bool Tls, bool TrustServer,
46 string UserName, string Password, string ConnectionSubscription, string WillTopic, string WillData, bool WillRetain,
48 {
49 MqttBroker Broker;
50
51 lock (brokers)
52 {
53 if (!brokers.TryGetValue(Key, out Broker))
54 Broker = null;
55 }
56
57 if (!(Broker is null))
58 {
59 await Broker.SetWill(WillTopic, WillData, WillRetain, WillQoS);
60 return Broker;
61 }
62 else
63 {
64 Broker = new MqttBroker(Node, Host, Port, Tls, TrustServer, UserName, Password, ConnectionSubscription,
65 WillTopic, WillData, WillRetain, WillQoS);
66
67 MqttBroker Result;
68 MqttBroker Obsolete;
69
70 lock (brokers)
71 {
72 if (brokers.TryGetValue(Key, out Result))
73 Obsolete = Broker;
74 else
75 {
76 brokers[Key] = Result = Broker;
77 Obsolete = null;
78 }
79 }
80
81 if (!(Obsolete is null))
82 await Obsolete.DisposeAsync();
83
84 return Result;
85 }
86 }
87
91 public static Task DestroyBroker(string Key)
92 {
93 MqttBroker Broker;
94
95 lock (brokers)
96 {
97 if (!brokers.TryGetValue(Key, out Broker))
98 return Task.CompletedTask;
99
100 brokers.Remove(Key);
101 }
102
103 return Broker.DisposeAsync();
104 }
105
106 }
107}
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string ComputeSHA1HashString(byte[] Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:274
MQTT Broker connection object.
Definition: MqttBroker.cs:17
bool Remove(string LocalTopic)
Removes a child topic
Definition: MqttBroker.cs:388
Task DisposeAsync()
Closes the connection and disposes of all resources.
Definition: MqttBroker.cs:114
async Task SetWill(string WillTopic, string WillData, bool WillRetain, MqttQualityOfService WillQoS)
TODO
Definition: MqttBroker.cs:177
Static class managing connections to MQTT brokers.
Definition: MqttBrokers.cs:13
static async Task< MqttBroker > GetBroker(MqttBrokerNode Node, string Key, string Host, int Port, bool Tls, bool TrustServer, string UserName, string Password, string ConnectionSubscription, string WillTopic, string WillData, bool WillRetain, MqttQualityOfService WillQoS)
TODO
Definition: MqttBrokers.cs:45
static string GetKey(string Host, int Port, bool Tls, bool TrustServer, string UserName, string Password, string ConnectionSubscription)
Gets sort key for MQTT broker
Definition: MqttBrokers.cs:27
static Task DestroyBroker(string Key)
TODO
Definition: MqttBrokers.cs:91
Node representing a connection to an MQTT broker.
MqttQualityOfService
MQTT Quality of Service level.