Skip to content

Commit 2bd10f1

Browse files
committed
Bug: 481097 - Exposing the currently connected server URI to client
Adds a new method getCurrentServerURI() to MqttClient and MqttAsyncClient that allows you to check which server you are connected to. This is especially useful when you are providing a list of servers to the MqttConnectOptions as getServerURI only tells you the URI that you initialised the Client with. Signed-off-by: James Sutton <james.sutton@uk.ibm.com>
1 parent af6344c commit 2bd10f1

8 files changed

Lines changed: 56 additions & 1 deletion

File tree

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttAsyncClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,21 @@ public String getClientId() {
637637
public String getServerURI() {
638638
return serverURI;
639639
}
640+
641+
/**
642+
* Returns the currently connected Server URI
643+
* Implemented due to: https://bugs.eclipse.org/bugs/show_bug.cgi?id=481097
644+
*
645+
* Where getServerURI only returns the URI that was provided in
646+
* MqttAsyncClient's constructor, getCurrentServerURI returns the URI of the
647+
* Server that the client is currently connected to. This would be different in scenarios
648+
* where multiple server URIs have been provided to the MqttConnectOptions.
649+
*
650+
* @return the currently connected server URI
651+
*/
652+
public String getCurrentServerURI(){
653+
return comms.getNetworkModules()[comms.getNetworkModuleIndex()].getServerURI();
654+
}
640655

641656
/**
642657
* Get a topic object which can be used to publish messages.

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/MqttClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,21 @@ public IMqttDeliveryToken[] getPendingDeliveryTokens() {
459459
public String getServerURI() {
460460
return aClient.getServerURI();
461461
}
462+
463+
/**
464+
* Returns the currently connected Server URI
465+
* Implemented due to: https://bugs.eclipse.org/bugs/show_bug.cgi?id=481097
466+
*
467+
* Where getServerURI only returns the URI that was provided in
468+
* MqttAsyncClient's constructor, getCurrentServerURI returns the URI of the
469+
* Server that the client is currently connected to. This would be different in scenarios
470+
* where multiple server URIs have been provided to the MqttConnectOptions.
471+
*
472+
* @return the currently connected server URI
473+
*/
474+
public String getCurrentServerURI(){
475+
return aClient.getCurrentServerURI();
476+
}
462477

463478
/* (non-Javadoc)
464479
* @see org.eclipse.paho.client.mqttv3.IMqttClient#getTopic(java.lang.String)

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/LocalNetworkModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,8 @@ public void stop() throws IOException {
8888
}
8989
}
9090
}
91+
92+
public String getServerURI() {
93+
return "local://" + brokerName;
94+
}
9195
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/NetworkModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ public interface NetworkModule {
3030
public OutputStream getOutputStream() throws IOException;
3131

3232
public void stop() throws IOException;
33+
34+
public String getServerURI();
3335
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/SSLNetworkModule.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ public class SSLNetworkModule extends TCPNetworkModule {
3333

3434
private String[] enabledCiphers;
3535
private int handshakeTimeoutSecs;
36-
36+
37+
private String host;
38+
private int port;
3739
/**
3840
* Constructs a new SSLNetworkModule using the specified host and
3941
* port. The supplied SSLSocketFactory is used to supply the network
4042
* socket.
4143
*/
4244
public SSLNetworkModule(SSLSocketFactory factory, String host, int port, String resourceContext) {
4345
super(factory, host, port, resourceContext);
46+
this.host = host;
47+
this.port = port;
4448
log.setResourceName(resourceContext);
4549
}
4650

@@ -90,4 +94,8 @@ public void start() throws IOException, MqttException {
9094
// reset timeout to default value
9195
socket.setSoTimeout(soTimeout);
9296
}
97+
98+
public String getServerURI() {
99+
return "ssl://" + host + ":" + port;
100+
}
93101
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/TCPNetworkModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ public void stop() throws IOException {
104104
public void setConnectTimeout(int timeout) {
105105
this.conTimeout = timeout;
106106
}
107+
108+
public String getServerURI() {
109+
return "tcp://" + host + ":" + port;
110+
}
107111
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketNetworkModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,8 @@ public void stop() throws IOException {
104104
super.stop();
105105
}
106106

107+
public String getServerURI() {
108+
return "ws://" + host + ":" + port;
109+
}
110+
107111
}

org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/internal/websocket/WebSocketSecureNetworkModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public void stop() throws IOException {
101101
super.stop();
102102
}
103103

104+
public String getServerURI() {
105+
return "wss://" + host + ":" + port;
106+
}
104107

105108

106109
}

0 commit comments

Comments
 (0)