-
Notifications
You must be signed in to change notification settings - Fork 7
/
LastWillAndTestamentBuilder.cs
309 lines (280 loc) · 10.7 KB
/
LastWillAndTestamentBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright 2024-present HiveMQ and the HiveMQ Community
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace HiveMQtt.Client;
using HiveMQtt.MQTT5.Types;
using HiveMQtt.Client.Internal;
using HiveMQtt.Client.Exceptions;
using System.Text;
public class LastWillAndTestamentBuilder
{
private readonly Dictionary<string, string> userProperties = new();
private string? topic;
private byte[]? payload;
private QualityOfService qos = QualityOfService.AtMostOnceDelivery;
private bool retain;
private long? willDelayInterval;
private byte? payloadFormatIndicator;
private long? messageExpiryInterval;
private string? contentType;
private string? responseTopic;
private byte[]? correlationData;
/// <summary>
/// Sets the Topic for the Last Will and Testament message.
/// </summary>
/// <param name="topic">The topic name.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithTopic(string topic)
{
this.topic = topic;
return this;
}
/// <summary>
/// Sets the Payload for the Last Will and Testament message.
/// </summary>
/// <param name="payload">The payload to send in bytes.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithPayload(byte[] payload)
{
this.payload = payload;
return this;
}
/// <summary>
/// Sets the Payload for the Last Will and Testament message.
/// </summary>
/// <param name="payload">The string payload to send.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithPayload(string payload)
{
this.payload = Encoding.UTF8.GetBytes(payload);
return this;
}
/// <summary>
/// Sets the Quality of Service Level for the Last Will and Testament message.
/// </summary>
/// <param name="qos">The quality of service level.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithQualityOfServiceLevel(QualityOfService qos)
{
this.qos = qos;
return this;
}
/// <summary>
/// Sets the Retain flag for the Last Will and Testament message.
/// </summary>
/// <param name="retain">The boolean value of the retain flag.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithRetain(bool retain)
{
this.retain = retain;
return this;
}
/// <summary>
/// Sets the Will Delay Interval. This value is the delay the broker
/// should wait before publishing the Last Will and Testament message.
/// <para>
/// If the Will Delay Interval is absent, the default value is 0 and
/// there is no delay before the Will Message is published.
/// </para>
/// <para>
/// Odd Fact: the maximum value of this option equates to
/// a 136 year delay. (2^32 - 1) seconds = 136 years.
/// </para>
/// </summary>
/// <param name="willDelayInterval">The delay value in seconds.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithWillDelayInterval(long willDelayInterval)
{
this.willDelayInterval = willDelayInterval;
return this;
}
/// <summary>
/// Sets the Payload Format Indicator. This value indicates the format
/// of the payload. The value is a single byte with the following
/// possible values:
/// <list type="bullet">
/// <item>
/// <term>0</term>
/// <description>UTF-8 Encoded Character Data</description>
/// </item>
/// <item>
/// <term>1</term>
/// <description>Binary Data</description>
/// </item>
/// </list>
/// <para>
/// If the Payload Format Indicator is absent, the default value is 0.
/// </para>
/// </summary>
/// <param name="payloadFormatIndicator">The PayloadFormatIndicator value.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithPayloadFormatIndicator(int payloadFormatIndicator)
{
if (payloadFormatIndicator is < 0 or > 1)
{
throw new HiveMQttClientException("Payload Format Indicator must be 0 or 1");
}
this.payloadFormatIndicator = (byte)payloadFormatIndicator;
return this;
}
/// <summary>
/// Sets the Payload Format Indicator. This value indicates the format
/// of the payload. The value is a MQTT5PayloadFormatIndicator enum.
/// </summary>
/// <param name="payloadFormatIndicator">A value from the MQTT5PayloadFormatIndicator enum.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithPayloadFormatIndicator(MQTT5PayloadFormatIndicator payloadFormatIndicator)
{
this.payloadFormatIndicator = (byte)payloadFormatIndicator;
return this;
}
/// <summary>
/// Sets the Message Expiry Interval. This value is the time in seconds
/// that the broker should retain the Last Will and Testament message.
/// <para>
/// If the Message Expiry Interval is absent, the default value is 0 and
/// the message is retained until the Session ends.
/// </para>
/// <para>
/// Odd Fact: the maximum value of this option equates to
/// a 136 year delay. (2^32 - 1) seconds = 136 years.
/// </para>
/// </summary>
/// <param name="messageExpiryInterval">The delay value in seconds.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithMessageExpiryInterval(long messageExpiryInterval)
{
this.messageExpiryInterval = messageExpiryInterval;
return this;
}
/// <summary>
/// Sets the Content Type. This value is a UTF-8 encoded string that
/// indicates the content type of the payload.
/// <para>
/// The value of the Content Type is defined by the sending and
/// receiving application.
/// </para>
/// </summary>
/// <param name="contentType">The payload content type.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithContentType(string contentType)
{
this.contentType = contentType;
return this;
}
/// <summary>
/// Sets the Response Topic. This value is a UTF-8 encoded string that
/// indicates the topic the receiver should use to respond to the
/// Last Will and Testament message.
/// </summary>
/// <param name="responseTopic">The topic name for a response message.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithResponseTopic(string responseTopic)
{
this.responseTopic = responseTopic;
return this;
}
/// <summary>
/// Sets the Correlation Data. This value is a byte array that
/// indicates the correlation data for the Last Will and Testament message.
/// <para>
/// The Correlation Data is used by the sender of the Request Message
/// to identify which request the Response Message is for when it is
/// received.
/// </para>
/// </summary>
/// <param name="correlationData">The correlation data in bytes.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithCorrelationData(byte[] correlationData)
{
this.correlationData = correlationData;
return this;
}
/// <summary>
/// Set a user property to be sent with the Last Will and Testament message.
/// </summary>
/// <param name="key">The key of the user property.</param>
/// <param name="value">The value of the user property to send.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithUserProperty(string key, string value)
{
this.userProperties.Add(key, value);
return this;
}
/// <summary>
/// Set a collection of user properties to be sent with the Last Will and Testament message.
/// </summary>
/// <param name="properties">A dictionary of user properties to send.</param>
/// <returns>The builder instance.</returns>
public LastWillAndTestamentBuilder WithUserProperties(Dictionary<string, string> properties)
{
foreach (var property in properties)
{
this.userProperties.Add(property.Key, property.Value);
}
return this;
}
/// <summary>
/// Builds the Last Will and Testament message.
/// </summary>
/// <returns>The Last Will and Testament message.</returns>
/// <exception cref="HiveMQttClientException">Thrown if the Last Will and Testament message is invalid in any way.</exception>
public LastWillAndTestament Build()
{
if (this.topic is null)
{
throw new HiveMQttClientException("LastWillAndTestament requires a Topic: Topic must not be null");
}
else
{
Validator.ValidateTopicName(this.topic);
}
if (this.payload is null)
{
throw new HiveMQttClientException("LastWillAndTestament requires a Payload: Payload must not be null");
}
var lastWillAndTestament = new LastWillAndTestament(this.topic, this.payload, this.qos, this.retain);
if (this.willDelayInterval.HasValue)
{
lastWillAndTestament.WillDelayInterval = this.willDelayInterval.Value;
}
if (this.payloadFormatIndicator.HasValue)
{
lastWillAndTestament.PayloadFormatIndicator = this.payloadFormatIndicator.Value;
}
if (this.messageExpiryInterval.HasValue)
{
lastWillAndTestament.MessageExpiryInterval = this.messageExpiryInterval.Value;
}
if (this.contentType is not null)
{
lastWillAndTestament.ContentType = this.contentType;
}
if (this.responseTopic is not null)
{
lastWillAndTestament.ResponseTopic = this.responseTopic;
}
if (this.correlationData is not null)
{
lastWillAndTestament.CorrelationData = this.correlationData;
}
if (this.userProperties.Count > 0)
{
lastWillAndTestament.UserProperties = this.userProperties;
}
lastWillAndTestament.Validate();
return lastWillAndTestament;
}
}