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.mqtt; 6 7 /** 8 * Represents a message to publish, or a message that was received. 9 */ 10 public final class MqttMessage { 11 private String topic; 12 private final byte[] payload; 13 private QualityOfService qos; 14 private boolean retain; 15 private boolean dup; 16 17 /** 18 * Constructs a new message. 19 * 20 * @param topic Message topic. 21 * @param payload Message payload. 22 * @param qos {@link QualityOfService}. When sending, the 23 * {@link QualityOfService} to use for delivery. When receiving, 24 * the {@link QualityOfService} used for delivery. 25 * @param retain Retain flag. When sending, whether the message should be 26 * retained by the broker and delivered to future subscribers. 27 * When receiving, whether the message was sent as a result of a 28 * new subscription being made. 29 * @param dup DUP flag. Ignored when sending. When receiving, indicates 30 * whether this might be re-delivery of an earlier attempt to 31 * send the message. 32 */ MqttMessage(String topic, byte[] payload, QualityOfService qos, boolean retain, boolean dup)33 public MqttMessage(String topic, byte[] payload, QualityOfService qos, boolean retain, boolean dup) { 34 this.topic = topic; 35 this.payload = payload; 36 this.dup = dup; 37 this.qos = qos; 38 this.retain = retain; 39 } 40 41 /** 42 * Constructs a new message. 43 * 44 * @param topic Message topic. 45 * @param payload Message payload. 46 * @param qos {@link QualityOfService}. When sending, the 47 * {@link QualityOfService} to use for delivery. When receiving, 48 * the {@link QualityOfService} used for delivery. 49 * @param retain Retain flag. When sending, whether the message should be 50 * retained by the broker and delivered to future subscribers. 51 * When receiving, whether the message was sent as a result of a 52 * new subscription being made. 53 */ MqttMessage(String topic, byte[] payload, QualityOfService qos, boolean retain)54 public MqttMessage(String topic, byte[] payload, QualityOfService qos, boolean retain) { 55 this(topic, payload, qos, retain, false); 56 } 57 58 /** 59 * Constructs a new message. 60 * 61 * @param topic Message topic. 62 * @param payload Message payload. 63 * @param qos {@link QualityOfService}. When sending, the 64 * {@link QualityOfService} to use for delivery. When receiving, 65 * the {@link QualityOfService} used for delivery. 66 */ MqttMessage(String topic, byte[] payload, QualityOfService qos)67 public MqttMessage(String topic, byte[] payload, QualityOfService qos) { 68 this(topic, payload, qos, false, false); 69 } 70 71 /** 72 * @deprecated Use alternate constructor. 73 * @param topic Message topic. 74 * @param payload Message payload. 75 */ 76 @Deprecated MqttMessage(String topic, byte[] payload)77 public MqttMessage(String topic, byte[] payload) { 78 this(topic, payload, QualityOfService.AT_LEAST_ONCE, false, false); 79 } 80 81 /** 82 * Gets the topic associated with this message 83 * @return The topic 84 */ getTopic()85 public String getTopic() { 86 return topic; 87 } 88 89 /** 90 * Gets the message payload 91 * @return Message payload 92 */ getPayload()93 public byte[] getPayload() { 94 return payload; 95 } 96 97 /** 98 * Gets the {@link QualityOfService}. When sending, the {@link QualityOfService} 99 * to use for delivery. When receiving, the {@link QualityOfService} used for 100 * delivery. 101 * 102 * @return The {@link QualityOfService} 103 */ getQos()104 public QualityOfService getQos() { 105 return qos; 106 } 107 108 /** 109 * Gets the retain flag. When sending, whether the message should be retained by 110 * the broker and delivered to future subscribers. When receiving, whether the 111 * message was sent as a result of a new subscription being made. 112 * 113 * @return Retain flag 114 */ getRetain()115 public boolean getRetain() { 116 return retain; 117 } 118 119 /** 120 * Gets the DUP flag. Ignored when sending. When receiving, indicates whether 121 * this might be re-delivery of an earlier attempt to send the message. 122 * 123 * @return DUP flag 124 */ getDup()125 public boolean getDup() { 126 return dup; 127 } 128 129 } 130