Search in sources :

Example 16 with DefaultPromise

use of io.netty.util.concurrent.DefaultPromise in project thingsboard by thingsboard.

the class MqttClientImpl method connect.

private Future<MqttConnectResult> connect(String host, int port, boolean reconnect) {
    if (this.eventLoop == null) {
        this.eventLoop = new NioEventLoopGroup();
    }
    this.host = host;
    this.port = port;
    Promise<MqttConnectResult> connectFuture = new DefaultPromise<>(this.eventLoop.next());
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(this.eventLoop);
    bootstrap.channel(clientConfig.getChannelClass());
    bootstrap.remoteAddress(host, port);
    bootstrap.handler(new MqttChannelInitializer(connectFuture, host, port, clientConfig.getSslContext()));
    ChannelFuture future = bootstrap.connect();
    future.addListener((ChannelFutureListener) f -> {
        if (f.isSuccess()) {
            MqttClientImpl.this.channel = f.channel();
            MqttClientImpl.this.channel.closeFuture().addListener((ChannelFutureListener) channelFuture -> {
                if (isConnected()) {
                    return;
                }
                ChannelClosedException e = new ChannelClosedException("Channel is closed!");
                if (callback != null) {
                    callback.connectionLost(e);
                }
                pendingSubscriptions.forEach((id, mqttPendingSubscription) -> mqttPendingSubscription.onChannelClosed());
                pendingSubscriptions.clear();
                serverSubscriptions.clear();
                subscriptions.clear();
                pendingServerUnsubscribes.forEach((id, mqttPendingServerUnsubscribes) -> mqttPendingServerUnsubscribes.onChannelClosed());
                pendingServerUnsubscribes.clear();
                qos2PendingIncomingPublishes.clear();
                pendingPublishes.forEach((id, mqttPendingPublish) -> mqttPendingPublish.onChannelClosed());
                pendingPublishes.clear();
                pendingSubscribeTopics.clear();
                handlerToSubscribtion.clear();
                scheduleConnectIfRequired(host, port, true);
            });
        } else {
            scheduleConnectIfRequired(host, port, reconnect);
        }
    });
    return connectFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) MqttDecoder(io.netty.handler.codec.mqtt.MqttDecoder) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) MqttMessageType(io.netty.handler.codec.mqtt.MqttMessageType) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttUnsubscribePayload(io.netty.handler.codec.mqtt.MqttUnsubscribePayload) MqttSubscribePayload(io.netty.handler.codec.mqtt.MqttSubscribePayload) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) HashMultimap(com.google.common.collect.HashMultimap) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MqttEncoder(io.netty.handler.codec.mqtt.MqttEncoder) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) SocketChannel(io.netty.channel.socket.SocketChannel) ImmutableSet(com.google.common.collect.ImmutableSet) EventLoopGroup(io.netty.channel.EventLoopGroup) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) ChannelInitializer(io.netty.channel.ChannelInitializer) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) SslContext(io.netty.handler.ssl.SslContext) Promise(io.netty.util.concurrent.Promise) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) DefaultPromise(io.netty.util.concurrent.DefaultPromise) MqttUnsubscribeMessage(io.netty.handler.codec.mqtt.MqttUnsubscribeMessage) Optional(java.util.Optional) MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFutureListener(io.netty.channel.ChannelFutureListener) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

DefaultPromise (io.netty.util.concurrent.DefaultPromise)16 Channel (io.netty.channel.Channel)7 ChannelFuture (io.netty.channel.ChannelFuture)6 SocketChannel (io.netty.channel.socket.SocketChannel)6 Future (io.netty.util.concurrent.Future)4 Test (org.junit.Test)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 MultiplexedChannelRecordTest (com.github.ambry.network.http2.MultiplexedChannelRecordTest)3 ByteBuf (io.netty.buffer.ByteBuf)3 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 ChannelPool (io.netty.channel.pool.ChannelPool)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 Http2StreamChannel (io.netty.handler.codec.http2.Http2StreamChannel)3 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)3 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)3 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)3 HashMultimap (com.google.common.collect.HashMultimap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ChannelHandler (io.netty.channel.ChannelHandler)2