Skip to content

Commit

Permalink
Add new constructors for MQTT5 packet builders (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfod authored May 24, 2024
1 parent 4cddd41 commit 3629745
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 86 deletions.
16 changes: 4 additions & 12 deletions samples/mqtt5/src/main/java/com/example/mqtt5/Mqtt5Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ public void accept(Mqtt5WebsocketHandshakeTransformArgs t) {
ConnectPacketBuilder connectBuilder = new ConnectPacketBuilder();
connectBuilder.withClientId(clientID);
// Add a will
PublishPacketBuilder willBuilder = new PublishPacketBuilder();
willBuilder.withTopic("test/topic/will");
willBuilder.withPayload("Goodbye".getBytes());
willBuilder.withQOS(QOS.AT_MOST_ONCE);
PublishPacketBuilder willBuilder = new PublishPacketBuilder("test/topic/will", QOS.AT_MOST_ONCE, "Goodbye".getBytes());
connectBuilder.withWill(willBuilder.build());
// Add the connection options
optionsBuilder.withConnectOptions(connectBuilder.build());
Expand All @@ -192,8 +189,7 @@ public void accept(Mqtt5WebsocketHandshakeTransformArgs t) {
}

// Subscribe
SubscribePacketBuilder subscribePacketBuilder = new SubscribePacketBuilder();
subscribePacketBuilder.withSubscription("test/topic", QOS.AT_LEAST_ONCE);
SubscribePacketBuilder subscribePacketBuilder = new SubscribePacketBuilder("test/topic", QOS.AT_LEAST_ONCE);
// Make sure it is successful
try {
SubAckPacket subAckPacket = client.subscribe(subscribePacketBuilder.build()).get(60, TimeUnit.SECONDS);
Expand All @@ -209,10 +205,7 @@ public void accept(Mqtt5WebsocketHandshakeTransformArgs t) {
}

// Publish
PublishPacketBuilder publishPacketBuilder = new PublishPacketBuilder();
publishPacketBuilder.withPayload("Hello World!".getBytes());
publishPacketBuilder.withQOS(QOS.AT_LEAST_ONCE);
publishPacketBuilder.withTopic("test/topic");
PublishPacketBuilder publishPacketBuilder = new PublishPacketBuilder("test/topic", QOS.AT_LEAST_ONCE, "Hello World!".getBytes());
// Add user properties
List<UserProperty> publishProperties = new ArrayList<UserProperty>();
publishProperties.add(new UserProperty("Red", "Blue"));
Expand All @@ -231,8 +224,7 @@ public void accept(Mqtt5WebsocketHandshakeTransformArgs t) {
}

// Unsubscribe
UnsubscribePacketBuilder unsubscribePacketBuilder = new UnsubscribePacketBuilder();
unsubscribePacketBuilder.withSubscription("test/topic");
UnsubscribePacketBuilder unsubscribePacketBuilder = new UnsubscribePacketBuilder("test/topic");
// Make sure it is successful
try {
client.unsubscribe(unsubscribePacketBuilder.build()).get(60, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,19 @@ public PublishPacketBuilder withUserProperties(List<UserProperty> userProperties
*/
public PublishPacketBuilder() {}

/**
* Creates a new PublishPacketBuilder with common parameters set.
*
* @param topic The topic this message should be published to.
* @param packetQOS The MQTT quality of service level the message should be delivered with.
* @param payload The payload for the publish message.
*/
public PublishPacketBuilder(String topic, QOS packetQOS, byte[] payload) {
this.topic = topic;
this.packetQOS = packetQOS;
this.payload = payload;
}

/**
* Creates a new PublishPacket using the settings set in the builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ public SubscribePacketBuilder withUserProperties(List<UserProperty> userProperti
*/
public SubscribePacketBuilder() {}

/**
* Creates a new SubscribePacketBuilder with one subscription defined.
*
* @param subscription The subscription to add within the SubscribePacket.
*/
public SubscribePacketBuilder(Subscription subscription) {
withSubscription(subscription);
}

/**
* Creates a new SubscribePacketBuilder with one subscription defined.
*
* @param topicFilter The topic filter to subscribe to.
* @param qos The maximum QoS on which the subscriber will accept publish messages.
*/
public SubscribePacketBuilder(String topicFilter, QOS qos) {
withSubscription(new Subscription(topicFilter, qos));
}

/**
* Creates a new SUBSCRIBE packet using the settings set in the builder.
* @return The SubscribePacket created from the builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ static final public class UnsubscribePacketBuilder {

/**
* Sets a single topic filter that the client wishes to unsubscribe from.
* @param subscription A single topic filter that the client wishes to unsubscribe from
*
* @param topicFilter A single topic filter that the client wishes to unsubscribe from.
* @return The UnsubscribePacketBuilder after setting the subscription.
*/
public UnsubscribePacketBuilder withSubscription(String subscription) {
public UnsubscribePacketBuilder withSubscription(String topicFilter) {
if (this.subscriptions == null) {
this.subscriptions = new ArrayList<String>();
}
this.subscriptions.add(subscription);
this.subscriptions.add(topicFilter);
return this;
}

Expand All @@ -80,6 +81,15 @@ public UnsubscribePacketBuilder withUserProperties(List<UserProperty> userProper
*/
public UnsubscribePacketBuilder() {}

/**
* Creates a new UnsubscribePacketBuilder with one subscription defined.
*
* @param topicFilter A single topic filter that the client wishes to unsubscribe from.
*/
public UnsubscribePacketBuilder(String topicFilter) {
withSubscription(topicFilter);
}

/**
* Creates a new UnsubscribePacket using the settings set in the builder.
* @return The UnsubscribePacket created from the builder
Expand Down
Loading

0 comments on commit 3629745

Please sign in to comment.