@@ -38,6 +38,7 @@ public abstract class MqttClientTransportConfigImplBuilder<B extends MqttClientT
38
38
private @ Nullable InetSocketAddress serverAddress ;
39
39
private @ NotNull Object serverHost = DEFAULT_SERVER_HOST ; // String or InetAddress
40
40
private int serverPort = -1 ;
41
+ private @ Nullable InetSocketAddress localAddress ;
41
42
private @ Nullable MqttClientSslConfigImpl sslConfig ;
42
43
private @ Nullable MqttWebSocketConfigImpl webSocketConfig ;
43
44
@@ -51,6 +52,7 @@ public abstract class MqttClientTransportConfigImplBuilder<B extends MqttClientT
51
52
serverAddress = builder .serverAddress ;
52
53
serverHost = builder .serverHost ;
53
54
serverPort = builder .serverPort ;
55
+ localAddress = builder .localAddress ;
54
56
sslConfig = builder .sslConfig ;
55
57
webSocketConfig = builder .webSocketConfig ;
56
58
}
@@ -78,8 +80,8 @@ void set(final @NotNull MqttClientTransportConfigImpl transportConfig) {
78
80
return self ();
79
81
}
80
82
81
- private void setServerHost (final @ NotNull Object serverHost ) {
82
- this . serverHost = serverHost ;
83
+ private void setServerHost (final @ NotNull Object host ) {
84
+ serverHost = host ;
83
85
if (serverAddress != null ) {
84
86
serverPort = serverAddress .getPort ();
85
87
serverAddress = null ;
@@ -100,6 +102,69 @@ private void setServerHost(final @NotNull Object serverHost) {
100
102
return self ();
101
103
}
102
104
105
+ public @ NotNull B localAddress (final @ Nullable InetSocketAddress address ) {
106
+ if (address == null ) {
107
+ localAddress = null ;
108
+ } else {
109
+ localAddress = checkLocalAddress (address );
110
+ }
111
+ return self ();
112
+ }
113
+
114
+ public @ NotNull B localAddress (final @ Nullable String address ) {
115
+ if (address == null ) {
116
+ removeLocalAddress ();
117
+ } else {
118
+ localAddress = checkLocalAddress (new InetSocketAddress (address , getLocalPort ()));
119
+ }
120
+ return self ();
121
+ }
122
+
123
+ public @ NotNull B localAddress (final @ Nullable InetAddress address ) {
124
+ if (address == null ) {
125
+ removeLocalAddress ();
126
+ } else {
127
+ localAddress = new InetSocketAddress (address , getLocalPort ());
128
+ }
129
+ return self ();
130
+ }
131
+
132
+ private @ NotNull InetSocketAddress checkLocalAddress (final @ NotNull InetSocketAddress address ) {
133
+ if (address .isUnresolved ()) {
134
+ throw new IllegalArgumentException ("Local bind address must not be unresolved." );
135
+ }
136
+ return address ;
137
+ }
138
+
139
+ private void removeLocalAddress () {
140
+ if ((localAddress != null ) && (localAddress .getAddress () != null )) {
141
+ if (localAddress .getPort () == 0 ) {
142
+ localAddress = null ;
143
+ } else {
144
+ localAddress = new InetSocketAddress (localAddress .getPort ());
145
+ }
146
+ }
147
+ }
148
+
149
+ private int getLocalPort () {
150
+ return (localAddress == null ) ? 0 : localAddress .getPort ();
151
+ }
152
+
153
+ public @ NotNull B localPort (final int port ) {
154
+ if (port == 0 ) {
155
+ if ((localAddress != null ) && (localAddress .getPort () != 0 )) {
156
+ if (localAddress .getAddress () == null ) {
157
+ localAddress = null ;
158
+ } else {
159
+ localAddress = new InetSocketAddress (localAddress .getAddress (), 0 );
160
+ }
161
+ }
162
+ } else {
163
+ localAddress = new InetSocketAddress ((localAddress == null ) ? null : localAddress .getAddress (), port );
164
+ }
165
+ return self ();
166
+ }
167
+
103
168
public @ NotNull B sslWithDefaultConfig () {
104
169
this .sslConfig = MqttClientSslConfigImpl .DEFAULT ;
105
170
return self ();
@@ -156,7 +221,7 @@ private int getServerPort() {
156
221
}
157
222
158
223
@ NotNull MqttClientTransportConfigImpl buildTransportConfig () {
159
- return new MqttClientTransportConfigImpl (getServerAddress (), sslConfig , webSocketConfig );
224
+ return new MqttClientTransportConfigImpl (getServerAddress (), localAddress , sslConfig , webSocketConfig );
160
225
}
161
226
162
227
public static class Default extends MqttClientTransportConfigImplBuilder <Default >
0 commit comments