Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MqttClientConnection as an interface for usage in IoTJobsClient #207

Closed
wants to merge 8 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import software.amazon.awssdk.crt.AsyncCallback;
import software.amazon.awssdk.crt.CrtResource;
import software.amazon.awssdk.crt.CrtRuntimeException;
import software.amazon.awssdk.crt.http.HttpHeader;
import software.amazon.awssdk.crt.http.HttpProxyOptions;
import software.amazon.awssdk.crt.http.HttpRequest;
import software.amazon.awssdk.crt.io.SocketOptions;
import software.amazon.awssdk.crt.io.TlsContext;
import software.amazon.awssdk.crt.mqtt.MqttConnectionConfig;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
Expand All @@ -25,7 +23,8 @@
* MqttClientConnection represents a single connection from one MqttClient to an
* MQTT service endpoint
*/
public class MqttClientConnection extends CrtResource {
public class MqttClientConnection extends CrtResource implements MqttPublishInterface, MqttSubscribeHandlerInterface,
MqttSubscribeInterface {

private MqttConnectionConfig config;

Expand All @@ -51,7 +50,7 @@ void deliver(String topic, byte[] payload) {
/**
* Constructs a new MqttClientConnection. Connections are reusable after being
* disconnected.
*
*
* @param config Configuration to use
* @throws MqttException If mqttClient is null
*/
Expand Down Expand Up @@ -209,14 +208,7 @@ public CompletableFuture<Void> disconnect() {
}

/**
* Subscribes to a topic
*
* @param topic The topic to subscribe to
* @param qos {@link QualityOfService} for this subscription
* @param handler A handler which can recieve an MqttMessage when a message is
* published to the topic
* @return Future result is the packet/message id associated with the subscribe
* operation
* {@inheritDoc}
*/
public CompletableFuture<Integer> subscribe(String topic, QualityOfService qos, Consumer<MqttMessage> handler) {
CompletableFuture<Integer> future = new CompletableFuture<>();
Expand All @@ -238,13 +230,7 @@ public CompletableFuture<Integer> subscribe(String topic, QualityOfService qos,
}

/**
* Subscribes to a topic without a handler (messages will only be delivered to
* the OnMessage handler)
*
* @param topic The topic to subscribe to
* @param qos {@link QualityOfService} for this subscription
* @return Future result is the packet/message id associated with the subscribe
* operation
* {@inheritDoc}
*/
public CompletableFuture<Integer> subscribe(String topic, QualityOfService qos) {
return subscribe(topic, qos, null);
Expand Down Expand Up @@ -280,15 +266,7 @@ public CompletableFuture<Integer> unsubscribe(String topic) {
}

/**
* Publishes a message to a topic
*
* @param message The message to publish. The message contains the topic to
* publish to.
* @param qos The {@link QualityOfService} to use for the publish operation
* @param retain Whether or not the message should be retained by the broker to
* be delivered to future subscribers
* @return Future value is the packet/message id associated with the publish
* operation
* {@inheritDoc}
*/
public CompletableFuture<Integer> publish(MqttMessage message, QualityOfService qos, boolean retain) {
CompletableFuture<Integer> future = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com Inc. or its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.awssdk.crt.mqtt;

import java.util.concurrent.CompletableFuture;

public interface MqttPublishInterface {
/**
* Publishes a message to a topic
*
* @param message The message to publish. The message contains the topic to
* publish to.
* @param qos The {@link QualityOfService} to use for the publish operation
* @param retain Whether or not the message should be retained by the broker to
* be delivered to future subscribers
* @return Future value is the packet/message id associated with the publish
* operation
*/
CompletableFuture<Integer> publish(MqttMessage message, QualityOfService qos, boolean retain);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com Inc. or its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.awssdk.crt.mqtt;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public interface MqttSubscribeHandlerInterface {
/**
* Subscribes to a topic
*
* @param topic The topic to subscribe to
* @param qos {@link QualityOfService} for this subscription
* @param handler A handler which can receive an MqttMessage when a message is
* published to the topic
* @return Future result is the packet/message id associated with the subscribe
* operation
*/
CompletableFuture<Integer> subscribe(String topic, QualityOfService qos, Consumer<MqttMessage> handler);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Amazon.com Inc. or its affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.awssdk.crt.mqtt;

import java.util.concurrent.CompletableFuture;

public interface MqttSubscribeInterface {
/**
* Subscribes to a topic without a handler (messages will only be delivered to
* the OnMessage handler)
*
* @param topic The topic to subscribe to
* @param qos {@link QualityOfService} for this subscription
* @return Future result is the packet/message id associated with the subscribe
* operation
*/
CompletableFuture<Integer> subscribe(String topic, QualityOfService qos);
}