PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/aws-iot-device-sdk-java/src/main/java/com/amazonaws/services/iot/client/mqtt/AwsIotMqttConnection.java

https://gitlab.com/github-cloud-corp/aws-iot-device-sdk-java
Java | 170 lines | 123 code | 29 blank | 18 comment | 14 complexity | e315fe52ee33ace9c88667c1acbb2689 MD5 | raw file
  1. /*
  2. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. package com.amazonaws.services.iot.client.mqtt;
  16. import java.util.HashSet;
  17. import java.util.Set;
  18. import javax.net.SocketFactory;
  19. import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
  20. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  21. import org.eclipse.paho.client.mqttv3.MqttException;
  22. import org.eclipse.paho.client.mqttv3.MqttMessage;
  23. import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
  24. import com.amazonaws.services.iot.client.AWSIotException;
  25. import com.amazonaws.services.iot.client.AWSIotMessage;
  26. import com.amazonaws.services.iot.client.core.AbstractAwsIotClient;
  27. import com.amazonaws.services.iot.client.core.AwsIotConnection;
  28. import com.amazonaws.services.iot.client.core.AwsIotMessageCallback;
  29. import com.amazonaws.services.iot.client.core.AwsIotRetryableException;
  30. import lombok.Getter;
  31. import lombok.Setter;
  32. /**
  33. * This class extends {@link AwsIotConnection} to provide the basic MQTT pub/sub
  34. * functionalities using the Paho MQTT library.
  35. */
  36. @Getter
  37. @Setter
  38. public class AwsIotMqttConnection extends AwsIotConnection {
  39. private final SocketFactory socketFactory;
  40. private MqttAsyncClient mqttClient;
  41. private AwsIotMqttMessageListener messageListener;
  42. private AwsIotMqttClientListener clientListener;
  43. private Set<String> serverUris;
  44. public AwsIotMqttConnection(AbstractAwsIotClient client, SocketFactory socketFactory, String serverUri)
  45. throws AWSIotException {
  46. super(client);
  47. this.socketFactory = socketFactory;
  48. messageListener = new AwsIotMqttMessageListener(client);
  49. clientListener = new AwsIotMqttClientListener(client);
  50. try {
  51. mqttClient = new MqttAsyncClient(serverUri, client.getClientId(), new MemoryPersistence());
  52. mqttClient.setCallback(clientListener);
  53. } catch (MqttException e) {
  54. throw new AWSIotException(e);
  55. }
  56. }
  57. AwsIotMqttConnection(AbstractAwsIotClient client, MqttAsyncClient mqttClient) throws AWSIotException {
  58. super(client);
  59. this.mqttClient = mqttClient;
  60. this.socketFactory = null;
  61. }
  62. public void openConnection(AwsIotMessageCallback callback) throws AWSIotException {
  63. try {
  64. AwsIotMqttConnectionListener connectionListener = new AwsIotMqttConnectionListener(client, true, callback);
  65. MqttConnectOptions options = buildMqttConnectOptions(client, socketFactory);
  66. mqttClient.connect(options, null, connectionListener);
  67. } catch (MqttException e) {
  68. throw new AWSIotException(e);
  69. }
  70. }
  71. public void closeConnection(AwsIotMessageCallback callback) throws AWSIotException {
  72. try {
  73. AwsIotMqttConnectionListener connectionListener = new AwsIotMqttConnectionListener(client, false, callback);
  74. mqttClient.disconnect(0, null, connectionListener);
  75. } catch (MqttException e) {
  76. throw new AWSIotException(e);
  77. }
  78. }
  79. @Override
  80. public void publishMessage(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException {
  81. String topic = message.getTopic();
  82. MqttMessage mqttMessage = new MqttMessage(message.getPayload());
  83. mqttMessage.setQos(message.getQos().getValue());
  84. try {
  85. mqttClient.publish(topic, mqttMessage, message, messageListener);
  86. } catch (MqttException e) {
  87. if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_NOT_CONNECTED) {
  88. throw new AwsIotRetryableException(e);
  89. } else {
  90. throw new AWSIotException(e);
  91. }
  92. }
  93. }
  94. @Override
  95. public void subscribeTopic(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException {
  96. try {
  97. mqttClient.subscribe(message.getTopic(), message.getQos().getValue(), message, messageListener);
  98. } catch (MqttException e) {
  99. if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_NOT_CONNECTED) {
  100. throw new AwsIotRetryableException(e);
  101. } else {
  102. throw new AWSIotException(e);
  103. }
  104. }
  105. }
  106. @Override
  107. public void unsubscribeTopic(AWSIotMessage message) throws AWSIotException, AwsIotRetryableException {
  108. try {
  109. mqttClient.unsubscribe(message.getTopic(), message, messageListener);
  110. } catch (MqttException e) {
  111. if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_NOT_CONNECTED) {
  112. throw new AwsIotRetryableException(e);
  113. } else {
  114. throw new AWSIotException(e);
  115. }
  116. }
  117. }
  118. public Set<String> getServerUris() {
  119. return new HashSet<>(serverUris);
  120. }
  121. public void setServerUris(Set<String> serverUris) {
  122. this.serverUris = new HashSet<>(serverUris);
  123. }
  124. private MqttConnectOptions buildMqttConnectOptions(AbstractAwsIotClient client, SocketFactory socketFactory) {
  125. MqttConnectOptions options = new MqttConnectOptions();
  126. options.setSocketFactory(socketFactory);
  127. options.setCleanSession(true);
  128. options.setConnectionTimeout(client.getConnectionTimeout() / 1000);
  129. options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000);
  130. if (serverUris != null && !serverUris.isEmpty()) {
  131. String[] uriArray = new String[serverUris.size()];
  132. serverUris.toArray(uriArray);
  133. options.setServerURIs(uriArray);
  134. }
  135. if (client.getWillMessage() != null) {
  136. AWSIotMessage message = client.getWillMessage();
  137. options.setWill(message.getTopic(), message.getPayload(), message.getQos().getValue(), false);
  138. }
  139. return options;
  140. }
  141. }