-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathMqtt5ClientOptions.java
More file actions
914 lines (818 loc) · 37.1 KB
/
Mqtt5ClientOptions.java
File metadata and controls
914 lines (818 loc) · 37.1 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
package software.amazon.awssdk.crt.mqtt5;
import software.amazon.awssdk.crt.http.HttpProxyOptions;
import software.amazon.awssdk.crt.io.ClientBootstrap;
import software.amazon.awssdk.crt.io.SocketOptions;
import software.amazon.awssdk.crt.io.TlsContext;
import software.amazon.awssdk.crt.io.ExponentialBackoffRetryOptions.JitterMode;
import software.amazon.awssdk.crt.mqtt5.packets.ConnectPacket;
import software.amazon.awssdk.crt.mqtt.MqttConnectionConfig;
import software.amazon.awssdk.crt.internal.IoTDeviceSDKMetrics;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.function.Consumer;
/**
* Configuration for the creation of Mqtt5Clients
*/
public class Mqtt5ClientOptions {
private String hostName;
private Long port;
private ClientBootstrap bootstrap;
private SocketOptions socketOptions;
private TlsContext tlsContext;
private HttpProxyOptions httpProxyOptions;
private ConnectPacket connectOptions;
private ClientSessionBehavior sessionBehavior = ClientSessionBehavior.DEFAULT;
private ExtendedValidationAndFlowControlOptions extendedValidationAndFlowControlOptions = ExtendedValidationAndFlowControlOptions.NONE;
private ClientOfflineQueueBehavior offlineQueueBehavior = ClientOfflineQueueBehavior.DEFAULT;
private JitterMode retryJitterMode = JitterMode.Default;
private Long minReconnectDelayMs;
private Long maxReconnectDelayMs;
private Long minConnectedTimeToResetReconnectDelayMs;
private Long pingTimeoutMs;
private Long connackTimeoutMs;
private Long ackTimeoutSeconds;
private LifecycleEvents lifecycleEvents;
private Consumer<Mqtt5WebsocketHandshakeTransformArgs> websocketHandshakeTransform;
private PublishEvents publishEvents;
private TopicAliasingOptions topicAliasingOptions;
// Indicates whether AWS IoT Metrics are enabled for this client, default to true.
// We don't expose this setting in the builder for now.
private IoTDeviceSDKMetrics iotDeviceSDKMetrics;
private boolean metricsEnabled = true;
/**
* Returns the host name of the MQTT server to connect to.
*
* @return Host name of the MQTT server to connect to.
*/
public String getHostName()
{
return this.hostName;
}
/**
* Returns the network port of the MQTT server to connect to.
*
* @return Network port of the MQTT server to connect to.
*/
public Long getPort()
{
return this.port;
}
/**
* Returns the Client bootstrap used.
*
* @return The Client bootstrap used
*/
public ClientBootstrap getBootstrap()
{
return this.bootstrap;
}
/**
* Returns the socket properties of the underlying MQTT connections made by the client.
*
* @return the socket properties of the underlying MQTT connections made by the client or null if defaults are used.
*/
public SocketOptions getSocketOptions()
{
return this.socketOptions;
}
/**
* Returns the TLS context for secure socket connections.
* If null, then a plaintext connection will be used.
*
* @return TLS context for secure socket connections.
*/
public TlsContext getTlsContext()
{
return this.tlsContext;
}
/**
* Returns the (tunneling) HTTP proxy usage when establishing MQTT connections
*
* @return (tunneling) HTTP proxy usage when establishing MQTT connections
*/
public HttpProxyOptions getHttpProxyOptions()
{
return this.httpProxyOptions;
}
/**
* Returns all configurable options with respect to the CONNECT packet sent by the client, including the will. These
* connect properties will be used for every connection attempt made by the client.
*
* @return all configurable options with respect to the CONNECT packet sent by the client, including the will
*/
public ConnectPacket getConnectOptions()
{
return this.connectOptions;
}
/**
* Returns how the Mqtt5Client should behave with respect to MQTT sessions.
*
* @return How the Mqtt5Client should behave with respect to MQTT sessions.
*/
public ClientSessionBehavior getSessionBehavior()
{
return this.sessionBehavior;
}
/**
* Returns the additional controls for client behavior with respect to operation validation and flow control;
* these checks go beyond the base MQTT5 spec to respect limits of specific MQTT brokers.
*
* @return The additional controls for client behavior with respect to operation validation and flow control
*/
public ExtendedValidationAndFlowControlOptions getExtendedValidationAndFlowControlOptions()
{
return this.extendedValidationAndFlowControlOptions;
}
/**
* Returns how disconnects affect the queued and in-progress operations tracked by the client. Also controls
* how new operations are handled while the client is not connected. In particular, if the client is not connected,
* then any operation that would be failed on disconnect (according to these rules) will also be rejected.
*
* @return How disconnects affect the queued and in-progress operations tracked by the client.
*/
public ClientOfflineQueueBehavior getOfflineQueueBehavior()
{
return this.offlineQueueBehavior;
}
/**
* Returns how the reconnect delay is modified in order to smooth out the distribution of reconnection attempt
* time points for a large set of reconnecting clients.
*
* @return how the reconnect delay is modified in order to smooth out the distribution of reconnection attempt
* time points for a large set of reconnecting clients.
*/
public JitterMode getRetryJitterMode()
{
return this.retryJitterMode;
}
/**
* Returns the minimum amount of time to wait to reconnect after a disconnect.
* Exponential back-off is performed with jitter after each connection failure.
*
* @return The minimum amount of time to wait to reconnect after a disconnect.
*/
public Long getMinReconnectDelayMs() {
return this.minReconnectDelayMs;
}
/**
* Returns the maximum amount of time to wait to reconnect after a disconnect. Exponential back-off is performed with jitter
* after each connection failure.
*
* @return The maximum amount of time to wait to reconnect after a disconnect
*/
public Long getMaxReconnectDelayMs() {
return this.maxReconnectDelayMs;
}
/**
* Returns the amount of time that must elapse with an established connection before the reconnect delay is reset to the minimum.
* This helps alleviate bandwidth-waste in fast reconnect cycles due to permission failures on operations.
*
* @return The amount of time that must elapse with an established connection before the reconnect delay is reset to the minimum
*/
public Long getMinConnectedTimeToResetReconnectDelayMs() {
return this.minConnectedTimeToResetReconnectDelayMs;
}
/**
* Returns the time interval to wait after sending a PINGREQ for a PINGRESP to arrive. If one does not arrive, the client will
* close the current connection.
*
* @return time interval to wait after sending a PINGREQ for a PINGRESP to arrive.
*/
public Long getPingTimeoutMs() {
return this.pingTimeoutMs;
}
/**
* Returns the time interval to wait after sending a CONNECT request for a CONNACK to arrive. If one does not arrive, the
* connection will be shut down.
*
* @return Time interval to wait after sending a CONNECT request for a CONNACK to arrive
*/
public Long getConnackTimeoutMs() {
return this.connackTimeoutMs;
}
/**
* Returns the time interval to wait for an ack after sending a QoS 1+ PUBLISH, SUBSCRIBE, or UNSUBSCRIBE before
* failing the operation.
*
* @return the time interval to wait for an ack after sending a QoS 1+ PUBLISH, SUBSCRIBE, or UNSUBSCRIBE before
* failing the operation.
*/
public Long getAckTimeoutSeconds() {
return this.ackTimeoutSeconds;
}
/**
* Returns the LifecycleEvents interface that will be called when the client gets a LifecycleEvent.
*
* @return The LifecycleEvents interface that will be called when the client gets a LifecycleEvent
*/
public LifecycleEvents getLifecycleEvents() {
return this.lifecycleEvents;
}
/**
* Returns the callback that allows a custom transformation of the HTTP request which acts as the websocket handshake.
* Websockets will be used if this is set to a valid transformation callback. To use websockets but not perform
* a transformation, just set this as a trivial completion callback. If null, the connection will be made
* with direct MQTT.
*
* @return The custom transformation of the HTTP request that acts as the websocket handshake or null.
*/
public Consumer<Mqtt5WebsocketHandshakeTransformArgs> getWebsocketHandshakeTransform() {
return this.websocketHandshakeTransform;
}
/**
* Returns the PublishEvents interface that will be called when the client gets a message.
*
* @return PublishEvents interface that will be called when the client gets a message.
*/
public PublishEvents getPublishEvents() {
return this.publishEvents;
}
/**
* Returns the topic aliasing options to be used by the client
*
* @return the topic aliasing options to be used by the client
*/
public TopicAliasingOptions getTopicAliasingOptions() {
return this.topicAliasingOptions;
}
/**
* Returns whether AWS IoT Device SDK metrics collection is enabled
*
* @return true if metrics are enabled, false otherwise
*/
public boolean getMetricsEnabled() {
return this.metricsEnabled;
}
/**
* Enables or disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
*
* @param enabled true to enable metrics, false to disable
*/
public void setMetricsEnabled(boolean enabled) {
this.metricsEnabled = enabled;
}
/**
* Creates a Mqtt5ClientOptionsBuilder instance
* @param builder The builder to get the Mqtt5ClientOptions values from
*/
public Mqtt5ClientOptions(Mqtt5ClientOptionsBuilder builder) {
this.hostName = builder.hostName;
this.port = builder.port;
this.bootstrap = builder.bootstrap;
this.socketOptions = builder.socketOptions;
this.tlsContext = builder.tlsContext;
this.httpProxyOptions = builder.httpProxyOptions;
this.connectOptions = builder.connectOptions;
this.sessionBehavior = builder.sessionBehavior;
this.extendedValidationAndFlowControlOptions = builder.extendedValidationAndFlowControlOptions;
this.offlineQueueBehavior = builder.offlineQueueBehavior;
this.retryJitterMode = builder.retryJitterMode;
this.minReconnectDelayMs = builder.minReconnectDelayMs;
this.maxReconnectDelayMs = builder.maxReconnectDelayMs;
this.minConnectedTimeToResetReconnectDelayMs = builder.minConnectedTimeToResetReconnectDelayMs;
this.pingTimeoutMs = builder.pingTimeoutMs;
this.connackTimeoutMs = builder.connackTimeoutMs;
this.ackTimeoutSeconds = builder.ackTimeoutSeconds;
this.lifecycleEvents = builder.lifecycleEvents;
this.websocketHandshakeTransform = builder.websocketHandshakeTransform;
this.publishEvents = builder.publishEvents;
this.topicAliasingOptions = builder.topicAliasingOptions;
this.metricsEnabled = builder.metricsEnabled;
this.iotDeviceSDKMetrics = new IoTDeviceSDKMetrics();
}
/*******************************************************************************
* lifecycle methods
******************************************************************************/
/**
* An interface that defines all of the functions the Mqtt5Client will call when it receives a lifecycle event.
*/
public interface LifecycleEvents {
/**
* Called when the client begins a connection attempt
*
* @param client The client associated with the event
* @param onAttemptingConnectReturn The data associated with the onAttemptingConnect event.
*/
public void onAttemptingConnect(Mqtt5Client client, OnAttemptingConnectReturn onAttemptingConnectReturn);
/**
* Called when the client successfully establishes an MQTT connection
*
* @param client The client associated with the event
* @param onConnectionSuccessReturn The data associated with the onConnectionSuccess event.
*/
public void onConnectionSuccess(Mqtt5Client client, OnConnectionSuccessReturn onConnectionSuccessReturn);
/**
* Called when the client fails to establish an MQTT connection
*
* @param client The client associated with the event
* @param onConnectionFailureReturn The data associated with the onConnectionFailure event.
*/
public void onConnectionFailure(Mqtt5Client client, OnConnectionFailureReturn onConnectionFailureReturn);
/**
* Called when the client's current MQTT connection is closed
*
* @param client The client associated with the event
* @param onDisconnectionReturn The data associated with the onDisconnection event.
*/
public void onDisconnection(Mqtt5Client client, OnDisconnectionReturn onDisconnectionReturn);
/**
* Called when the client reaches the 'Stopped' state as a result of the user invoking .stop()
*
* @param client The client associated with the event
* @param onStoppedReturn The data associated with the onStopped event.
*/
public void onStopped(Mqtt5Client client, OnStoppedReturn onStoppedReturn);
}
/**
* An interface that defines all of the publish functions the Mqtt5Client will call when it receives a publish packet.
*/
public interface PublishEvents {
/**
* Called when an MQTT PUBLISH packet is received by the client
*
* @param client The client that has received the message
* @param publishReturn All of the data that was received from the server
*/
public void onMessageReceived(Mqtt5Client client, PublishReturn publishReturn);
}
/*******************************************************************************
* builder
******************************************************************************/
/**
* Controls how the Mqtt5Client should behave with respect to MQTT sessions.
*/
public enum ClientSessionBehavior {
/**
* Default client session behavior. Maps to CLEAN.
*/
DEFAULT(0),
/**
* Always ask for a clean session when connecting
*/
CLEAN(1),
/**
* Always attempt to rejoin an existing session after an initial connection success.
*
* Session rejoin requires an appropriate non-zero session expiry interval in the client's CONNECT options.
*/
REJOIN_POST_SUCCESS(2),
/**
* Always attempt to rejoin an existing session. Since the client does not yet support durable session persistence,
* this option is not guaranteed to be spec compliant because any unacknowledged qos1 publishes (which are
* part of the client session state) will not be present on the initial connection. Until we support
* durable session resumption, this option is technically spec-breaking, but useful.
*/
REJOIN_ALWAYS(3);
private int type;
private ClientSessionBehavior(int code) {
type = code;
}
/**
* @return The native enum integer value associated with this enum value
*/
public int getValue() {
return type;
}
/**
* Creates a ClientSessionBehavior enum value from a native integer value.
*
* @param value native integer value for the Client Session Behavior Type
* @return a new ClientSessionBehavior value
*/
public static ClientSessionBehavior getEnumValueFromInteger(int value) {
ClientSessionBehavior enumValue = enumMapping.get(value);
if (enumValue != null) {
return enumValue;
}
throw new RuntimeException("Illegal ClientSessionBehavior");
}
private static Map<Integer, ClientSessionBehavior> buildEnumMapping() {
return Stream.of(ClientSessionBehavior.values())
.collect(Collectors.toMap(ClientSessionBehavior::getValue, Function.identity()));
}
private static Map<Integer, ClientSessionBehavior> enumMapping = buildEnumMapping();
}
/**
* Additional controls for client behavior with respect to operation validation and flow control; these checks
* go beyond the MQTT5 spec to respect limits of specific MQTT brokers.
*/
public enum ExtendedValidationAndFlowControlOptions {
/**
* Do not do any additional validation or flow control
*/
NONE(0),
/**
* Apply additional client-side validation and operational flow control that respects the
* default AWS IoT Core limits.
*
* Currently applies the following additional validation:
*
* <ol>
* <li> No more than 8 subscriptions per SUBSCRIBE packet </li>
* <li> Topics and topic filters have a maximum of 7 slashes (8 segments), not counting any AWS rules prefix </li>
* <li> Topics must be 256 bytes or less in length </li>
* <li> Client id must be 128 or less bytes in length </li>
* </ol>
*
* Also applies the following flow control:
*
* <ol>
* <li> Outbound throughput throttled to 512KB/s </li>
* <li> Outbound publish TPS throttled to 100 </li>
* </ol>
*/
AWS_IOT_CORE_DEFAULTS(1);
private int type;
private ExtendedValidationAndFlowControlOptions(int code) {
type = code;
}
/**
* @return The native enum integer value associated with this Java enum value
*/
public int getValue() {
return type;
}
/**
* Creates a Java ExtendedValidationAndFlowControlOptions enum value from a native integer value.
*
* @param value native integer value for the extended validation and flow control options
* @return a new ExtendedValidationAndFlowControlOptions value
*/
public static ExtendedValidationAndFlowControlOptions getEnumValueFromInteger(int value) {
ExtendedValidationAndFlowControlOptions enumValue = enumMapping.get(value);
if (enumValue != null) {
return enumValue;
}
throw new RuntimeException("Illegal ExtendedValidationAndFlowControlOptions");
}
private static Map<Integer, ExtendedValidationAndFlowControlOptions> buildEnumMapping() {
return Stream.of(ExtendedValidationAndFlowControlOptions.values())
.collect(Collectors.toMap(ExtendedValidationAndFlowControlOptions::getValue, Function.identity()));
}
private static Map<Integer, ExtendedValidationAndFlowControlOptions> enumMapping = buildEnumMapping();
}
/**
* Controls how disconnects affect the queued and in-progress operations tracked by the client. Also controls
* how operations are handled while the client is not connected. In particular, if the client is not connected,
* then any operation that would be failed on disconnect (according to these rules) will be rejected.
*/
public enum ClientOfflineQueueBehavior {
/**
* Default client operation queue behavior. Maps to FAIL_QOS0_PUBLISH_ON_DISCONNECT.
*/
DEFAULT(0),
/**
* Re-queues QoS 1+ publishes on disconnect; un-acked publishes go to the front while unprocessed publishes stay
* in place. All other operations (QoS 0 publishes, subscribe, unsubscribe) are failed.
*/
FAIL_NON_QOS1_PUBLISH_ON_DISCONNECT(1),
/**
* QoS 0 publishes that are not complete at the time of disconnection are failed. Un-acked QoS 1+ publishes are
* re-queued at the head of the line for immediate retransmission on a session resumption. All other operations
* are requeued in original order behind any retransmissions.
*/
FAIL_QOS0_PUBLISH_ON_DISCONNECT(2),
/**
* All operations that are not complete at the time of disconnection are failed, except operations that
* the MQTT5 spec requires to be retransmitted (un-acked QoS1+ publishes).
*/
FAIL_ALL_ON_DISCONNECT(3);
private int type;
private ClientOfflineQueueBehavior(int code) {
type = code;
}
/**
* @return The native enum integer value associated with this Java enum value
*/
public int getValue() {
return type;
}
/**
* Creates a Java ClientOfflineQueueBehavior enum value from a native integer value.
*
* @param value native integer value for the client operation queue behavior type
* @return a new ClientOfflineQueueBehavior value
*/
public static ClientOfflineQueueBehavior getEnumValueFromInteger(int value) {
ClientOfflineQueueBehavior enumValue = enumMapping.get(value);
if (enumValue != null) {
return enumValue;
}
throw new RuntimeException("Illegal ClientOfflineQueueBehavior");
}
private static Map<Integer, ClientOfflineQueueBehavior> buildEnumMapping() {
return Stream.of(ClientOfflineQueueBehavior.values())
.collect(Collectors.toMap(ClientOfflineQueueBehavior::getValue, Function.identity()));
}
private static Map<Integer, ClientOfflineQueueBehavior> enumMapping = buildEnumMapping();
}
/**
* All of the options for a Mqtt5Client. This includes the settings to make a connection, as well as the
* event callbacks, publish callbacks, and more.
*/
static final public class Mqtt5ClientOptionsBuilder {
private String hostName;
private Long port;
private ClientBootstrap bootstrap;
private SocketOptions socketOptions;
private TlsContext tlsContext;
private HttpProxyOptions httpProxyOptions;
private ConnectPacket connectOptions;
private ClientSessionBehavior sessionBehavior = ClientSessionBehavior.DEFAULT;
private ExtendedValidationAndFlowControlOptions extendedValidationAndFlowControlOptions = ExtendedValidationAndFlowControlOptions.NONE;
private ClientOfflineQueueBehavior offlineQueueBehavior = ClientOfflineQueueBehavior.DEFAULT;
private JitterMode retryJitterMode = JitterMode.Default;
private Long minReconnectDelayMs;
private Long maxReconnectDelayMs;
private Long minConnectedTimeToResetReconnectDelayMs;
private Long pingTimeoutMs;
private Long connackTimeoutMs;
private Long ackTimeoutSeconds;
private LifecycleEvents lifecycleEvents;
private Consumer<Mqtt5WebsocketHandshakeTransformArgs> websocketHandshakeTransform;
private PublishEvents publishEvents;
private TopicAliasingOptions topicAliasingOptions;
private boolean metricsEnabled = true;
/**
* Sets the host name of the MQTT server to connect to.
*
* @param hostName Host name of the MQTT server to connect to.
* @return The Mqtt5ClientOptionsBuilder after setting the host name
*/
public Mqtt5ClientOptionsBuilder withHostName(String hostName)
{
this.hostName = hostName;
return this;
}
/**
* Sets the network port of the MQTT server to connect to.
*
* @param port Network port of the MQTT server to connect to.
* @return The Mqtt5ClientOptionsBuilder after setting the port
*/
public Mqtt5ClientOptionsBuilder withPort(Long port)
{
this.port = port;
return this;
}
/**
* Sets the ClientBootstrap to use. In almost all cases, this should be left null.
*
* @param bootstrap The ClientBootstrap to use
* @return The Mqtt5ClientOptionsBuilder after setting the ClientBootstrap
*/
public Mqtt5ClientOptionsBuilder withBootstrap(ClientBootstrap bootstrap)
{
this.bootstrap = bootstrap;
return this;
}
/**
* Sets the socket properties of the underlying MQTT connections made by the client. Leave null to use
* defaults (no TCP keep alive, 10 second socket timeout).
*
* @param socketOptions The socket properties of the underlying MQTT connections made by the client.
* @return The Mqtt5ClientOptionsBuilder after setting the socket options
*/
public Mqtt5ClientOptionsBuilder withSocketOptions(SocketOptions socketOptions)
{
this.socketOptions = socketOptions;
return this;
}
/**
* Sets the TLS context for secure socket connections.
* If null, then a plaintext connection will be used.
*
* @param tlsContext The TLS context for secure socket connections.
* @return The Mqtt5ClientOptionsBuilder after setting the TlsContext
*/
public Mqtt5ClientOptionsBuilder withTlsContext(TlsContext tlsContext)
{
this.tlsContext = tlsContext;
return this;
}
/**
* Sets the (tunneling) HTTP proxy usage when establishing MQTT connection.
*
* @param httpProxyOptions the (tunneling) HTTP proxy usage when establishing MQTT connection.
* @return The Mqtt5ClientOptionsBuilder after setting the HttpProxyOptions
*/
public Mqtt5ClientOptionsBuilder withHttpProxyOptions(HttpProxyOptions httpProxyOptions)
{
this.httpProxyOptions = httpProxyOptions;
return this;
}
/**
* Sets all configurable options with respect to the CONNECT packet sent by the client, including the Will. These
* connect properties will be used for every connection attempt made by the client.
*
* @param connectOptions Configurable options with respect to the CONNECT packet sent by the client, including the Will.
* @return The Mqtt5ClientOptionsBuilder after setting the connect options
*/
public Mqtt5ClientOptionsBuilder withConnectOptions(ConnectPacket connectOptions)
{
this.connectOptions = connectOptions;
return this;
}
/**
* Sets how the Mqtt5Client should behave with respect to MQTT sessions.
*
* @param sessionBehavior How the Mqtt5Client should behave with respect to MQTT sessions.
* @return The Mqtt5ClientOptionsBuilder after setting the ClientSessionBehavior
*/
public Mqtt5ClientOptionsBuilder withSessionBehavior(ClientSessionBehavior sessionBehavior)
{
this.sessionBehavior = sessionBehavior;
return this;
}
/**
* Sets the additional controls for client behavior with respect to operation validation and flow control; these checks
* go beyond the base MQTT5 spec to respect limits of specific MQTT brokers.
*
* @param extendedValidationAndFlowControlOptions Additional controls for client behavior with respect to operation validation and flow control
* @return The Mqtt5ClientOptionsBuilder after setting the ExtendedValidationAndFlowControlOptions
*/
public Mqtt5ClientOptionsBuilder withExtendedValidationAndFlowControlOptions(ExtendedValidationAndFlowControlOptions extendedValidationAndFlowControlOptions)
{
this.extendedValidationAndFlowControlOptions = extendedValidationAndFlowControlOptions;
return this;
}
/**
* Sets how disconnects affect the queued and in-progress operations tracked by the client. Also controls
* how new operations are handled while the client is not connected. In particular, if the client is not connected,
* then any operation that would be failed on disconnect (according to these rules) will also be rejected.
*
* @param offlineQueueBehavior How disconnects affect the queued and in-progress operations tracked by the client
* @return The Mqtt5ClientOptionsBuilder after setting the ClientOfflineQueueBehavior
*/
public Mqtt5ClientOptionsBuilder withOfflineQueueBehavior(ClientOfflineQueueBehavior offlineQueueBehavior)
{
this.offlineQueueBehavior = offlineQueueBehavior;
return this;
}
/**
* Sets how the reconnect delay is modified in order to smooth out the distribution of reconnection attempt
* time points for a large set of reconnecting clients.
*
* @param retryJitterMode How the reconnect delay is modified in order to smooth out the distribution of reconnection attempt
* time points for a large set of reconnecting clients.
* @return The Mqtt5ClientOptionsBuilder after setting the JitterMode
*/
public Mqtt5ClientOptionsBuilder withRetryJitterMode(JitterMode retryJitterMode)
{
this.retryJitterMode = retryJitterMode;
return this;
}
/**
* Sets the minimum amount of time to wait to reconnect after a disconnect. Exponential back-off is performed with jitter
* after each connection failure.
*
* @param minReconnectDelayMs The minimum amount of time to wait to reconnect after a disconnect.
* @return The Mqtt5ClientOptionsBuilder after setting the minimum reconnect delay
*/
public Mqtt5ClientOptionsBuilder withMinReconnectDelayMs(Long minReconnectDelayMs)
{
this.minReconnectDelayMs = minReconnectDelayMs;
return this;
}
/**
* Sets the maximum amount of time to wait to reconnect after a disconnect. Exponential back-off is performed with jitter
* after each connection failure.
*
* @param maxReconnectDelayMs The maximum amount of time to wait to reconnect after a disconnect
* @return The Mqtt5ClientOptionsBuilder after setting the maximum reconnect delay
*/
public Mqtt5ClientOptionsBuilder withMaxReconnectDelayMs(Long maxReconnectDelayMs)
{
this.maxReconnectDelayMs = maxReconnectDelayMs;
return this;
}
/**
* Sets the minimum time needed to pass to reset the reconnect delay in milliseconds used when the Mqtt5Client connects.
*
* @param minConnectedTimeToResetReconnectDelayMs The minimum time needed to pass to reset the reconnect delay
* @return The Mqtt5ClientOptionsBuilder after setting the minimum time needed to pass to reset the reconnect delay
*/
public Mqtt5ClientOptionsBuilder withMinConnectedTimeToResetReconnectDelayMs(Long minConnectedTimeToResetReconnectDelayMs)
{
this.minConnectedTimeToResetReconnectDelayMs = minConnectedTimeToResetReconnectDelayMs;
return this;
}
/**
* Sets the time interval to wait after sending a PINGREQ for a PINGRESP to arrive. If one does not arrive, the client will
* close the current connection.
*
* @param pingTimeoutMs The time interval to wait after sending a PINGREQ for a PINGRESP to arrive.
* @return The Mqtt5ClientOptionsBuilder after setting the ping timeout time
*/
public Mqtt5ClientOptionsBuilder withPingTimeoutMs(Long pingTimeoutMs)
{
this.pingTimeoutMs = pingTimeoutMs;
return this;
}
/**
* Sets the time interval to wait after sending a CONNECT request for a CONNACK to arrive. If one does not arrive, the
* connection will be shut down.
*
* @param connackTimeoutMs The time interval to wait after sending a CONNECT request for a CONNACK to arrive.
* @return The Mqtt5ClientOptionsBuilder after setting the timeout in milliseconds for getting a ConnAckPacket from the server
*/
public Mqtt5ClientOptionsBuilder withConnackTimeoutMs(Long connackTimeoutMs)
{
this.connackTimeoutMs = connackTimeoutMs;
return this;
}
/**
* Sets the time interval to wait for an ack after sending a QoS 1+ PUBLISH, SUBSCRIBE, or UNSUBSCRIBE before
* failing the operation.
*
* @param ackTimeoutSeconds The time interval to wait for an ack after sending a QoS 1+ PUBLISH, SUBSCRIBE, or UNSUBSCRIBE before
* failing the operation.
* @return The Mqtt5ClientOptionsBuilder after setting the timeout in milliseconds for getting an ACK packet
* from the server when performing an operation
*/
public Mqtt5ClientOptionsBuilder withAckTimeoutSeconds(Long ackTimeoutSeconds)
{
this.ackTimeoutSeconds = ackTimeoutSeconds;
return this;
}
/**
* Sets the Lifecycle Events interface that will be called when the client gets a LifecycleEvent.
*
* @param lifecycleEvents The LifecycleEvents interface that will be called
* @return The Mqtt5ClientOptionsBuilder after setting the Lifecycle Events interface
*/
public Mqtt5ClientOptionsBuilder withLifecycleEvents(LifecycleEvents lifecycleEvents) {
this.lifecycleEvents = lifecycleEvents;
return this;
}
/**
* Sets the callback that allows a custom transformation of the HTTP request that acts as the websocket handshake.
* Websockets will be used if this is set to a valid transformation callback. To use websockets but not perform
* a transformation, just set this as a trivial completion callback. If null, the connection will be made
* with direct MQTT.
*
* @param handshakeTransform Callback that allows a custom transformation of the HTTP request that acts as the websocket handshake.
* @return The Mqtt5ClientOptionsBuilder after setting the websocket handshake transform callback
*/
public Mqtt5ClientOptionsBuilder withWebsocketHandshakeTransform(Consumer<Mqtt5WebsocketHandshakeTransformArgs> handshakeTransform) {
this.websocketHandshakeTransform = handshakeTransform;
return this;
}
/**
* Sets the PublishEvents interface that will be called when the client gets a message.
*
* @param publishEvents The PublishEvents interface that will be called when the client gets a message.
* @return The Mqtt5ClientOptionsBuilder after setting the PublishEvents interface
*/
public Mqtt5ClientOptionsBuilder withPublishEvents(PublishEvents publishEvents) {
this.publishEvents = publishEvents;
return this;
}
/**
* Sets the topic aliasing options for clients constructed from this builder
*
* @param options topic aliasing options that the client should use
* @return The Mqtt5ClientOptionsBuilder object
*/
public Mqtt5ClientOptionsBuilder withTopicAliasingOptions(TopicAliasingOptions options) {
this.topicAliasingOptions = options;
return this;
}
/**
* Enables or disables IoT Device SDK metrics collection. The metrics includes SDK name, version, and platform.
*
* @param enabled true to enable metrics, false to disable.
* @return The Mqtt5ClientOptionsBuilder after setting the metrics option
*/
public Mqtt5ClientOptionsBuilder withMetricsEnabled(boolean enabled) {
this.metricsEnabled = enabled;
return this;
}
/**
* Creates a new Mqtt5ClientOptionsBuilder instance
*
* @param hostName The host name of the MQTT server to connect to.
* @param port The port of the MQTT server to connect to.
*/
public Mqtt5ClientOptionsBuilder(String hostName, Long port) {
this.hostName = hostName;
this.port = port;
}
/**
* Returns a Mqtt5ClientOptions class configured with all of the options set in the Mqtt5ClientOptionsBuilder.
* This can then be used to make a new Mqtt5Client.
*
* @return A configured Mqtt5ClientOptions
*/
public Mqtt5ClientOptions build()
{
return new Mqtt5ClientOptions(this);
}
}
}