1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 package software.amazon.awssdk.crt.mqtt5; 6 7 import java.util.Map; 8 import java.util.function.Function; 9 import java.util.stream.Collectors; 10 import java.util.stream.Stream; 11 12 import software.amazon.awssdk.crt.Log; 13 import software.amazon.awssdk.crt.Log.LogLevel; 14 import software.amazon.awssdk.crt.Log.LogSubject; 15 import software.amazon.awssdk.crt.mqtt5.packets.PubAckPacket; 16 17 /** 18 * The type of data returned after calling Publish on an Mqtt5Client. The data contained varies depending 19 * on the publish and its configuration. Use <code>getType()</code> to figure out what type of data is contained and either 20 * <code>getData()</code> to get the data and cast it, or call <code>getResult[Type name here]()</code> to get the data already cast. 21 */ 22 public class PublishResult { 23 private PublishResultType type; 24 25 // The values for the various types of packet (currently just PubAck data) 26 private PubAckPacket pubackData; 27 28 /** 29 * Returns the type of data that was returned after calling Publish on the Mqtt5Client. 30 * You can use this information to determine what type of data is contained and either 31 * <code>getData()</code> to get the data and cast it, or call <code>getResult[Type name here]()</code> to 32 * get the data already cast. 33 * @return The type of data contained in the PublishResult 34 */ getType()35 public PublishResultType getType() { 36 return type; 37 } 38 39 /** 40 * Returns the data contained in the PubAck result. This is based on the PublishResultType, 41 * which is determined by the QoS setting in the published message. 42 * 43 * Note: To get the data type from this function, you will need to cast. For example, to get 44 * the PubAck from result type of PUBACK, you will need to use the following: 45 * <code>PubAckPacket packet = (PubAckPacket)getValue()</code> 46 * 47 * @return The data contained in the PublishResult result 48 */ getValue()49 public Object getValue() { 50 if (type == PublishResultType.NONE) { 51 return null; 52 } else if (type == PublishResultType.PUBACK) { 53 return getResultPubAck(); 54 } else { 55 Log.log(LogLevel.Error, LogSubject.MqttClient, "PublishResult: Cannot get value - unknown type of: " + type); 56 return null; 57 } 58 } 59 60 /** 61 * Returns the data contained in the PublishResult for a PublishResultType of PUBACK. 62 * This occurs for QoS 1 and will return a PubAckPacket. 63 * @return the data contained in the PublishResult for a PublishResultType of PUBACK. 64 */ getResultPubAck()65 public PubAckPacket getResultPubAck() { 66 return pubackData; 67 } 68 69 /** 70 * This is only called in JNI to make a new PublishResult with QoS 0. 71 */ PublishResult()72 private PublishResult() { 73 this.type = PublishResultType.NONE; 74 } 75 76 /** 77 * This is only called in JNI to make a new PublishResult with a PubAck packet (QoS 1). 78 * @param newPubackPacket The PubAckPacket data for QoS 1 packets. Can be null if result is non QoS 1. 79 * @return A newly created PublishResult 80 */ PublishResult(PubAckPacket newPubackPacket)81 private PublishResult(PubAckPacket newPubackPacket) { 82 this.type = PublishResultType.PUBACK; 83 this.pubackData = newPubackPacket; 84 } 85 86 87 /** 88 * The type of data returned after calling Publish on an MQTT5 client. The type returned from a publish 89 * varies based on the QoS settings of the message sent. 90 */ 91 public enum PublishResultType { 92 93 /** 94 * No PublishResult result data (QoS 0) 95 * This means the PublishResult has no data and getValue will return null. 96 */ 97 NONE(0), 98 99 /** 100 * PublishResult result was a publish acknowledgment (PubAck - QoS 1) 101 * This means the PublishResult has a PubAck and getValue will return 102 * the PubAckPacket associated with the publish. 103 */ 104 PUBACK(1); 105 106 private int result; 107 PublishResultType(int code)108 private PublishResultType(int code) { 109 result = code; 110 } 111 112 /** 113 * @return The native enum integer value associated with this Java enum value 114 */ getValue()115 public int getValue() { 116 return result; 117 } 118 119 /** 120 * Creates a Java PublishResultType enum value from a native integer value. 121 * 122 * @param value native integer value for PublishResultType 123 * @return a new PublishResultType value 124 */ getEnumValueFromInteger(int value)125 public static PublishResultType getEnumValueFromInteger(int value) { 126 PublishResultType enumValue = enumMapping.get(value); 127 if (enumValue != null) { 128 return enumValue; 129 } 130 throw new RuntimeException("Illegal PublishResultType"); 131 } 132 buildEnumMapping()133 private static Map<Integer, PublishResultType> buildEnumMapping() { 134 return Stream.of(PublishResultType.values()) 135 .collect(Collectors.toMap(PublishResultType::getValue, Function.identity())); 136 } 137 private static Map<Integer, PublishResultType> enumMapping = buildEnumMapping(); 138 } 139 } 140