|
| 1 | +/** |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.streamnative.pulsar.handlers.mqtt.broker.channel; |
| 15 | + |
| 16 | +import static com.google.common.base.Preconditions.checkArgument; |
| 17 | +import io.netty.channel.ChannelHandler.Sharable; |
| 18 | +import io.netty.channel.ChannelHandlerContext; |
| 19 | +import io.streamnative.pulsar.handlers.mqtt.broker.MQTTService; |
| 20 | +import io.streamnative.pulsar.handlers.mqtt.broker.processor.MQTTBrokerProtocolMethodProcessor; |
| 21 | +import io.streamnative.pulsar.handlers.mqtt.common.MQTTCommonInboundHandler; |
| 22 | +import io.streamnative.pulsar.handlers.mqtt.common.adapter.MqttAdapterMessage; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import lombok.extern.slf4j.Slf4j; |
| 25 | + |
| 26 | +/** |
| 27 | + * MQTT in bound handler. |
| 28 | + */ |
| 29 | +@Sharable |
| 30 | +@Slf4j |
| 31 | +public class MQTTBrokerInboundHandler extends MQTTCommonInboundHandler { |
| 32 | + |
| 33 | + public static final String NAME = "handler"; |
| 34 | + |
| 35 | + private final MQTTService mqttService; |
| 36 | + |
| 37 | + public MQTTBrokerInboundHandler(MQTTService mqttService) { |
| 38 | + this.mqttService = mqttService; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| 43 | + super.channelActive(ctx); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void channelRead(ChannelHandlerContext ctx, Object message) { |
| 48 | + checkArgument(message instanceof MqttAdapterMessage); |
| 49 | + MqttAdapterMessage adapterMsg = (MqttAdapterMessage) message; |
| 50 | + processors.computeIfAbsent(adapterMsg.getClientId(), key -> { |
| 51 | + MQTTBrokerProtocolMethodProcessor p = new MQTTBrokerProtocolMethodProcessor(mqttService, ctx); |
| 52 | + CompletableFuture<Void> inactiveFuture = p.getInactiveFuture(); |
| 53 | + inactiveFuture.whenComplete((id, ex) -> { |
| 54 | + processors.remove(adapterMsg.getClientId()); |
| 55 | + }); |
| 56 | + return p; |
| 57 | + }); |
| 58 | + super.channelRead(ctx, message); |
| 59 | + } |
| 60 | +} |
0 commit comments