com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientView Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hivemq-mqtt-client Show documentation
Show all versions of hivemq-mqtt-client Show documentation
HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
/*
* Copyright 2018 dc-square and the HiveMQ MQTT Client Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.hivemq.client.internal.mqtt.mqtt3;
import com.hivemq.client.internal.mqtt.MqttAsyncClient;
import com.hivemq.client.internal.mqtt.message.connect.connack.mqtt3.Mqtt3ConnAckView;
import com.hivemq.client.internal.mqtt.message.disconnect.mqtt3.Mqtt3DisconnectView;
import com.hivemq.client.internal.mqtt.message.publish.mqtt3.Mqtt3PublishView;
import com.hivemq.client.internal.mqtt.message.subscribe.mqtt3.Mqtt3SubscribeViewBuilder;
import com.hivemq.client.internal.mqtt.message.subscribe.suback.mqtt3.Mqtt3SubAckView;
import com.hivemq.client.internal.mqtt.mqtt3.exceptions.Mqtt3ExceptionFactory;
import com.hivemq.client.internal.mqtt.util.MqttChecks;
import com.hivemq.client.internal.util.Checks;
import com.hivemq.client.mqtt.MqttGlobalPublishFilter;
import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;
import com.hivemq.client.mqtt.mqtt3.Mqtt3BlockingClient;
import com.hivemq.client.mqtt.mqtt3.Mqtt3ClientConfig;
import com.hivemq.client.mqtt.mqtt3.Mqtt3RxClient;
import com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3Connect;
import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;
import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish;
import com.hivemq.client.mqtt.mqtt3.message.subscribe.Mqtt3Subscribe;
import com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck;
import com.hivemq.client.mqtt.mqtt3.message.unsubscribe.Mqtt3Unsubscribe;
import com.hivemq.client.mqtt.mqtt5.message.connect.connack.Mqtt5ConnAck;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5PublishResult;
import com.hivemq.client.mqtt.mqtt5.message.subscribe.suback.Mqtt5SubAck;
import com.hivemq.client.mqtt.mqtt5.message.unsubscribe.unsuback.Mqtt5UnsubAck;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* @author Silvio Giebl
*/
public class Mqtt3AsyncClientView implements Mqtt3AsyncClient {
private static final @NotNull BiFunction CONNACK_MAPPER =
(connAck, throwable) -> {
if (throwable != null) {
throw new CompletionException(Mqtt3ExceptionFactory.map(throwable));
}
return Mqtt3ConnAckView.of(connAck);
};
private static final @NotNull BiFunction SUBACK_MAPPER =
(subAck, throwable) -> {
if (throwable != null) {
throw new CompletionException(Mqtt3ExceptionFactory.map(throwable));
}
return Mqtt3SubAckView.of(subAck);
};
private static final @NotNull BiFunction UNSUBACK_MAPPER =
(unsubAck, throwable) -> {
if (throwable != null) {
throw new CompletionException(Mqtt3ExceptionFactory.map(throwable));
}
return null;
};
private static final @NotNull BiFunction PUBLISH_RESULT_MAPPER =
(publishResult, throwable) -> {
if (throwable != null) {
throw new CompletionException(Mqtt3ExceptionFactory.map(throwable));
}
return Mqtt3PublishView.of(publishResult.getPublish());
};
private static final @NotNull Function DISCONNECT_MAPPER = throwable -> {
throw new CompletionException(Mqtt3ExceptionFactory.map(throwable));
};
private static @NotNull Consumer callbackView(final @NotNull Consumer callback) {
return publish -> callback.accept(Mqtt3PublishView.of(publish));
}
private final @NotNull MqttAsyncClient delegate;
private final @NotNull Mqtt3ClientConfigView clientConfig;
Mqtt3AsyncClientView(final @NotNull MqttAsyncClient delegate) {
this.delegate = delegate;
clientConfig = new Mqtt3ClientConfigView(delegate.getConfig());
}
@Override
public @NotNull CompletableFuture<@NotNull Mqtt3ConnAck> connect(final @Nullable Mqtt3Connect connect) {
return delegate.connect(MqttChecks.connect(connect)).handle(CONNACK_MAPPER);
}
@Override
public @NotNull CompletableFuture<@NotNull Mqtt3SubAck> subscribe(final @Nullable Mqtt3Subscribe subscribe) {
return delegate.subscribe(MqttChecks.subscribe(subscribe)).handle(SUBACK_MAPPER);
}
@Override
public @NotNull CompletableFuture<@NotNull Mqtt3SubAck> subscribe(
final @Nullable Mqtt3Subscribe subscribe, final @Nullable Consumer<@NotNull Mqtt3Publish> callback) {
Checks.notNull(callback, "Callback");
return delegate.subscribe(MqttChecks.subscribe(subscribe), callbackView(callback)).handle(SUBACK_MAPPER);
}
@Override
public @NotNull CompletableFuture<@NotNull Mqtt3SubAck> subscribe(
final @Nullable Mqtt3Subscribe subscribe, final @Nullable Consumer<@NotNull Mqtt3Publish> callback,
final @Nullable Executor executor) {
Checks.notNull(callback, "Callback");
Checks.notNull(executor, "Executor");
return delegate.subscribe(MqttChecks.subscribe(subscribe), callbackView(callback), executor)
.handle(SUBACK_MAPPER);
}
@Override
public void publishes(
final @Nullable MqttGlobalPublishFilter filter, final @Nullable Consumer<@NotNull Mqtt3Publish> callback) {
Checks.notNull(callback, "Callback");
delegate.publishes(filter, callbackView(callback));
}
@Override
public void publishes(
final @Nullable MqttGlobalPublishFilter filter, final @Nullable Consumer<@NotNull Mqtt3Publish> callback,
final @Nullable Executor executor) {
Checks.notNull(callback, "Callback");
Checks.notNull(executor, "Executor");
delegate.publishes(filter, callbackView(callback), executor);
}
@Override
public @NotNull CompletableFuture unsubscribe(final @Nullable Mqtt3Unsubscribe unsubscribe) {
return delegate.unsubscribe(MqttChecks.unsubscribe(unsubscribe)).handle(UNSUBACK_MAPPER);
}
@Override
public @NotNull CompletableFuture<@NotNull Mqtt3Publish> publish(final @Nullable Mqtt3Publish publish) {
return delegate.publish(MqttChecks.publish(publish)).handle(PUBLISH_RESULT_MAPPER);
}
@Override
public @NotNull CompletableFuture disconnect() {
return delegate.disconnect(Mqtt3DisconnectView.DELEGATE).exceptionally(DISCONNECT_MAPPER);
}
@Override
public @NotNull Mqtt3ClientConfig getConfig() {
return clientConfig;
}
@Override
public @NotNull Mqtt3RxClient toRx() {
return new Mqtt3RxClientView(delegate.toRx());
}
@Override
public @NotNull Mqtt3BlockingClient toBlocking() {
return new Mqtt3BlockingClientView(delegate.toBlocking());
}
public static class Mqtt3SubscribeViewAndCallbackBuilder
extends Mqtt3SubscribeViewBuilder
implements Mqtt3SubscribeAndCallbackBuilder.Start.Complete, Mqtt3SubscribeAndCallbackBuilder.Call.Ex {
private final @NotNull Mqtt3AsyncClient client;
private @Nullable Consumer callback;
private @Nullable Executor executor;
public Mqtt3SubscribeViewAndCallbackBuilder(final @NotNull Mqtt3AsyncClient client) {
this.client = client;
}
@Override
protected @NotNull Mqtt3SubscribeViewAndCallbackBuilder self() {
return this;
}
@Override
public @NotNull Mqtt3SubscribeViewAndCallbackBuilder callback(final @Nullable Consumer callback) {
this.callback = Checks.notNull(callback, "Callback");
return this;
}
@Override
public @NotNull Mqtt3SubscribeViewAndCallbackBuilder executor(final @Nullable Executor executor) {
this.executor = Checks.notNull(executor, "Executor");
return this;
}
@Override
public @NotNull CompletableFuture send() {
final Mqtt3Subscribe subscribe = build();
if (callback == null) {
if (executor != null) {
throw new IllegalStateException("Executor must not be given if callback is null.");
}
return client.subscribe(subscribe);
}
if (executor == null) {
return client.subscribe(subscribe, callback);
}
return client.subscribe(subscribe, callback, executor);
}
}
}