-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial client listener * set listener options parameter ids * clang-format * update class ids * binding listenerPublishEvents; add mqtt5 utils * setup client in mqtt5 listener * set the correct booleanmethod * update comments and rename listener variable name * clangformat * add mqtt5listeneroptionsBuilder, update cr * update comments for PublishEvents * clang-format * fix validation order * adding the first listener test * Revert "adding the first listener test" This reverts commit d049df6.
- Loading branch information
Showing
10 changed files
with
1,695 additions
and
724 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/main/java/software/amazon/awssdk/crt/mqtt5/Mqtt5Listener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
package software.amazon.awssdk.crt.mqtt5; | ||
|
||
import software.amazon.awssdk.crt.CrtResource; | ||
import software.amazon.awssdk.crt.CrtRuntimeException; | ||
|
||
|
||
/** | ||
* This class wraps the aws-c-mqtt MQTT5 client to provide the basic MQTT5 pub/sub functionalities | ||
* via the AWS Common Runtime | ||
* | ||
* One Mqtt5Listener class creates one connection. | ||
* | ||
* MQTT5 support is currently in <b>developer preview</b>. We encourage feedback at all times, but feedback during the | ||
* preview window is especially valuable in shaping the final product. During the preview period we may make | ||
* backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid. | ||
*/ | ||
public class Mqtt5Listener extends CrtResource { | ||
private Mqtt5Client clientReference = null; | ||
|
||
/** | ||
* Creates a Mqtt5Listener instance using the provided Mqtt5ListenerOptions. Once the Mqtt5Listener is created, | ||
* changing the settings will not cause a change in already created Mqtt5Listener's. | ||
* | ||
* @param options The Mqtt5ListenerOptions class to use to configure the new Mqtt5Listener. | ||
* @param client The Mqtt5Client class the mqtt5 listener listen to | ||
* @throws CrtRuntimeException If the system is unable to allocate space for a native MQTT5 client structure | ||
*/ | ||
public Mqtt5Listener(Mqtt5ListenerOptions options, Mqtt5Client client) throws CrtRuntimeException { | ||
|
||
clientReference = client; | ||
acquireNativeHandle(mqtt5ListenerNew( | ||
options, | ||
client, | ||
this | ||
)); | ||
// addRefrenceTo client after the native handler get acquired | ||
addReferenceTo(client); | ||
} | ||
|
||
/** | ||
* Cleans up the native resources associated with this client. The client is unusable after this call | ||
*/ | ||
@Override | ||
protected void releaseNativeHandle() { | ||
if (!isNull()) { | ||
mqtt5ListenerDestroy(getNativeHandle()); | ||
removeReferenceTo(clientReference); | ||
} | ||
} | ||
|
||
/** | ||
* Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits. | ||
* Resources that wait are responsible for calling releaseReferences() manually. | ||
*/ | ||
@Override | ||
protected boolean canReleaseReferencesImmediately() { return false; } | ||
|
||
|
||
/******************************************************************************* | ||
* native methods | ||
******************************************************************************/ | ||
private static native long mqtt5ListenerNew( | ||
Mqtt5ListenerOptions options, | ||
Mqtt5Client client, | ||
Mqtt5Listener listener | ||
) throws CrtRuntimeException; | ||
private static native void mqtt5ListenerDestroy(long listener); | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
src/main/java/software/amazon/awssdk/crt/mqtt5/Mqtt5ListenerOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
package software.amazon.awssdk.crt.mqtt5; | ||
|
||
import software.amazon.awssdk.crt.mqtt5.Mqtt5ClientOptions.LifecycleEvents; | ||
|
||
/** | ||
* Configuration for the creation of Mqtt5Listener | ||
* | ||
* MQTT5 support is currently in <b>developer preview</b>. We encourage feedback at all times, but feedback during the | ||
* preview window is especially valuable in shaping the final product. During the preview period we may make | ||
* backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid. | ||
*/ | ||
public class Mqtt5ListenerOptions { | ||
|
||
private LifecycleEvents lifecycleEvents; | ||
private ListenerPublishEvents listenerPublishEvents; | ||
|
||
/** | ||
* Returns the LifecycleEvents interface that will be called when the client gets a LifecycleEvent. | ||
* | ||
* @return The LifecycleEvents interface that will be called when the client gets a LifecycleEvent | ||
*/ | ||
public LifecycleEvents getLifecycleEvents() { | ||
return this.lifecycleEvents; | ||
} | ||
|
||
/** | ||
* Returns the PublishEvents interface that will be called when the client gets a message. | ||
* | ||
* @return PublishEvents interface that will be called when the client gets a message. | ||
*/ | ||
public ListenerPublishEvents getListenerPublishEvents() { | ||
return this.listenerPublishEvents; | ||
} | ||
|
||
|
||
public Mqtt5ListenerOptions(Mqtt5ListenerOptionsBuilder builder) | ||
{ | ||
this.lifecycleEvents = builder.lifecycleEvents; | ||
this.listenerPublishEvents = builder.listenerPublishEvents; | ||
} | ||
|
||
/******************************************************************************* | ||
* callback methods | ||
******************************************************************************/ | ||
|
||
/** | ||
* An interface that defines all of the publish functions the Mqtt5Client will call when it receives a publish packet. | ||
*/ | ||
public interface ListenerPublishEvents { | ||
/** | ||
* Called when an MQTT PUBLISH packet is received by the client | ||
* | ||
* @param client The client that has received the message | ||
* @param publishReturn All of the data that was received from the server | ||
* | ||
* @return return true if the message get processed, otherwise false | ||
* If the message get processed, it will not passed down anymore. The client | ||
* and other listener would not get the publishEvent anymore. | ||
*/ | ||
public boolean onMessageReceived(Mqtt5Client client, PublishReturn publishReturn); | ||
} | ||
|
||
/******************************************************************************* | ||
* Builder | ||
******************************************************************************/ | ||
|
||
/** | ||
* All of the options for a Mqtt5Listener. This includes the settings to make a connection, as well as the | ||
* event callbacks, publish callbacks, and more. | ||
*/ | ||
static final public class Mqtt5ListenerOptionsBuilder { | ||
private LifecycleEvents lifecycleEvents; | ||
private ListenerPublishEvents listenerPublishEvents; | ||
|
||
/** | ||
* Sets the Lifecycle Events interface that will be called when the client gets a LifecycleEvent. | ||
* | ||
* @param lifecycleEvents The LifecycleEvents interface that will be called | ||
* @return The Mqtt5ListenerOptionsBuilder after setting the Lifecycle Events interface | ||
*/ | ||
public Mqtt5ListenerOptionsBuilder withLifecycleEvents(LifecycleEvents lifecycleEvents) { | ||
this.lifecycleEvents = lifecycleEvents; | ||
return this; | ||
} | ||
|
||
|
||
/** | ||
* Sets the ListenerPublishEvents interface that will be called when the client gets a message. | ||
* | ||
* @param publishEvents The ListenerPublishEvents interface that will be called when the client gets a message. | ||
* @return The Mqtt5ListenerOptionsBuilder after setting the PublishEvents interface | ||
*/ | ||
public Mqtt5ListenerOptionsBuilder withListenerPublishEvents(ListenerPublishEvents publishEvents) { | ||
this.listenerPublishEvents = publishEvents; | ||
return this; | ||
} | ||
|
||
|
||
/** | ||
* Returns a Mqtt5ListenerOptions class configured with all of the options set in the Mqtt5ListenerOptions. | ||
* This can then be used to make a new Mqtt5Client. | ||
* | ||
* @return A configured Mqtt5ListenerOptions | ||
*/ | ||
public Mqtt5ListenerOptions build() | ||
{ | ||
return new Mqtt5ListenerOptions(this); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.