1 2 /** 3 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 * SPDX-License-Identifier: Apache-2.0. 5 */ 6 package software.amazon.awssdk.crt.mqtt; 7 8 import software.amazon.awssdk.crt.CrtResource; 9 import software.amazon.awssdk.crt.CrtRuntimeException; 10 import software.amazon.awssdk.crt.io.ClientBootstrap; 11 import software.amazon.awssdk.crt.io.TlsContext; 12 13 /** 14 * This class wraps aws-c-mqtt to provide the basic MQTT pub/sub functionalities 15 * via the AWS Common Runtime 16 * 17 * One MqttClient class is needed per application. It can create any number of connections to 18 * any number of MQTT endpoints 19 */ 20 public class MqttClient extends CrtResource { 21 22 private TlsContext tlsContext; 23 24 /** 25 * Creates an MqttClient with no TLS from the provided {@link ClientBootstrap} 26 * @param clientBootstrap The ClientBootstrap to use 27 * @throws CrtRuntimeException If the system is unable to allocate space for a native MQTT client structure 28 */ MqttClient(ClientBootstrap clientBootstrap)29 public MqttClient(ClientBootstrap clientBootstrap) throws CrtRuntimeException { 30 acquireNativeHandle(mqttClientNew(clientBootstrap.getNativeHandle())); 31 addReferenceTo(clientBootstrap); 32 } 33 34 /** 35 * Creates an MqttClient with no TLS from the default static {@link ClientBootstrap} 36 * 37 * @throws CrtRuntimeException If the system is unable to allocate space for a native MQTT client structure 38 */ MqttClient()39 public MqttClient() throws CrtRuntimeException { 40 ClientBootstrap defaultBootstrap = ClientBootstrap.getOrCreateStaticDefault(); 41 acquireNativeHandle(mqttClientNew(defaultBootstrap.getNativeHandle())); 42 addReferenceTo(defaultBootstrap); 43 } 44 45 /** 46 * Creates an MqttClient from the provided {@link ClientBootstrap} and {@link TlsContext} 47 * @param clientBootstrap The ClientBootstrap to use 48 * @param context the tls context to use 49 * @throws CrtRuntimeException If the system is unable to allocate space for a native MQTT client structure 50 */ MqttClient(ClientBootstrap clientBootstrap, TlsContext context)51 public MqttClient(ClientBootstrap clientBootstrap, TlsContext context) throws CrtRuntimeException { 52 acquireNativeHandle(mqttClientNew(clientBootstrap.getNativeHandle())); 53 addReferenceTo(clientBootstrap); 54 addReferenceTo(context); 55 this.tlsContext = context; 56 } 57 58 /** 59 * Creates an MqttClient with a default static {@link ClientBootstrap} and provided {@link TlsContext} 60 * 61 * @param context the tls context to use 62 * @throws CrtRuntimeException If the system is unable to allocate space for a native MQTT client structure 63 */ MqttClient(TlsContext context)64 public MqttClient(TlsContext context) throws CrtRuntimeException { 65 ClientBootstrap defaultBootstrap = ClientBootstrap.getOrCreateStaticDefault(); 66 acquireNativeHandle(mqttClientNew(defaultBootstrap.getNativeHandle())); 67 addReferenceTo(defaultBootstrap); 68 addReferenceTo(context); 69 this.tlsContext = context; 70 } 71 72 /** 73 * @return the tls context used by all connections associated with this client. 74 */ getTlsContext()75 public TlsContext getTlsContext() { return tlsContext; } 76 77 /** 78 * Cleans up the native resources associated with this client. The client is unusable after this call 79 */ 80 @Override releaseNativeHandle()81 protected void releaseNativeHandle() { 82 if (!isNull()) { 83 mqttClientDestroy(getNativeHandle()); 84 } 85 } 86 87 /** 88 * Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits. 89 * Resources that wait are responsible for calling releaseReferences() manually. 90 */ 91 @Override canReleaseReferencesImmediately()92 protected boolean canReleaseReferencesImmediately() { return true; } 93 94 /******************************************************************************* 95 * native methods 96 ******************************************************************************/ mqttClientNew(long bootstrap)97 private static native long mqttClientNew(long bootstrap) throws CrtRuntimeException; 98 mqttClientDestroy(long client)99 private static native void mqttClientDestroy(long client); 100 } 101