Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MqttBrokers.cs
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
46 public static MqttBroker GetCachedBroker(string Key)
47 {
48 lock (brokers)
49 {
50 if (brokers.TryGetValue(Key, out MqttBroker Broker))
51 return Broker;
52 else
53 return null;
54 }
55 }
56
62 public static async Task<MqttBroker> GetBroker(MqttBrokerNode Node, string Key, string Host, int Port, bool Tls, bool TrustServer,
63 string UserName, string Password, string ConnectionSubscription, string WillTopic, string WillData, bool WillRetain,
65 {
66 MqttBroker Broker;
67
68 lock (brokers)
69 {
70 if (!brokers.TryGetValue(Key, out Broker))
71 Broker = null;
72 }
73
74 if (!(Broker is null))
75 {
76 await Broker.SetWill(WillTopic, WillData, WillRetain, WillQoS);
77 return Broker;
78 }
79 else
80 {
81 Broker = new MqttBroker(Node, Host, Port, Tls, TrustServer, UserName, Password, ConnectionSubscription,
82 WillTopic, WillData, WillRetain, WillQoS);
83
84 MqttBroker Result;
85 MqttBroker Obsolete;
86
87 lock (brokers)
88 {
89 if (brokers.TryGetValue(Key, out Result))
90 Obsolete = Broker;
91 else
92 {
93 brokers[Key] = Result = Broker;
94 Obsolete = null;
95 }
96 }
97
98 if (!(Obsolete is null))
99 await Obsolete.DisposeAsync();
100
101 return Result;
102 }
103 }
104
108 public static Task DestroyBroker(string Key)
109 {
110 MqttBroker Broker;
111
112 lock (brokers)
113 {
114 if (!brokers.TryGetValue(Key, out Broker))
115 return Task.CompletedTask;
116
117 brokers.Remove(Key);
118 }
119
120 return Broker.DisposeAsync();
121 }
122
123 }
124}
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static string ComputeSHA1HashString(byte[] Data)
Computes the SHA-1 hash of a block of binary data.
Definition: Hashes.cs:395
MQTT Broker connection object.
Definition: MqttBroker.cs:17
bool Remove(string LocalTopic)
Removes a child topic
Definition: MqttBroker.cs:381
Task DisposeAsync()
Closes the connection and disposes of all resources.
Definition: MqttBroker.cs:107
async Task SetWill(string WillTopic, string WillData, bool WillRetain, MqttQualityOfService WillQoS)
TODO
Definition: MqttBroker.cs:170
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)
Gets an MQTT Broker object, according to connection parameters. If one is not in memory,...
Definition: MqttBrokers.cs:62
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 MqttBroker GetCachedBroker(string Key)
Gets an MQTT Broker object, if available in the cache.
Definition: MqttBrokers.cs:46
static Task DestroyBroker(string Key)
TODO
Definition: MqttBrokers.cs:108
Node representing a connection to an MQTT broker.
MqttQualityOfService
MQTT Quality of Service level.