2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2017-01-04 14:37:29 +01:00
|
|
|
* Copyright (c) 2012-2017 The original author or authorsgetRockQuestions()
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server.netty;
|
2013-09-15 17:18:26 +02:00
|
|
|
|
2016-02-03 23:20:15 +01:00
|
|
|
import io.moquette.parser.proto.Utils;
|
|
|
|
|
import io.moquette.parser.proto.messages.*;
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.spi.impl.ProtocolProcessor;
|
2016-02-03 23:20:15 +01:00
|
|
|
import static io.moquette.parser.proto.messages.AbstractMessage.*;
|
2013-09-15 17:18:26 +02:00
|
|
|
import io.netty.channel.ChannelHandler.Sharable;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
2015-08-01 11:59:00 +02:00
|
|
|
|
2015-09-25 10:03:57 +02:00
|
|
|
|
2013-09-15 17:18:26 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2017-01-04 11:13:58 +01:00
|
|
|
|
2013-09-15 17:18:26 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author andrea
|
|
|
|
|
*/
|
|
|
|
|
@Sharable
|
|
|
|
|
public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(NettyMQTTHandler.class);
|
2015-09-25 10:03:57 +02:00
|
|
|
private final ProtocolProcessor m_processor;
|
|
|
|
|
|
|
|
|
|
public NettyMQTTHandler(ProtocolProcessor processor) {
|
|
|
|
|
m_processor = processor;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 17:18:26 +02:00
|
|
|
@Override
|
|
|
|
|
public void channelRead(ChannelHandlerContext ctx, Object message) {
|
|
|
|
|
AbstractMessage msg = (AbstractMessage) message;
|
2017-01-11 15:33:23 +01:00
|
|
|
String messageType = Utils.msgType2String(msg.getMessageType());
|
2017-01-18 23:27:02 +01:00
|
|
|
LOG.debug("Processing MQTT message. MessageType = {}.", messageType);
|
2013-09-15 17:18:26 +02:00
|
|
|
try {
|
|
|
|
|
switch (msg.getMessageType()) {
|
|
|
|
|
case CONNECT:
|
2016-01-17 11:11:36 +01:00
|
|
|
m_processor.processConnect(ctx.channel(), (ConnectMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case SUBSCRIBE:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processSubscribe(ctx.channel(), (SubscribeMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case UNSUBSCRIBE:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processUnsubscribe(ctx.channel(), (UnsubscribeMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBLISH:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processPublish(ctx.channel(), (PublishMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBREC:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processPubRec(ctx.channel(), (PubRecMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBCOMP:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processPubComp(ctx.channel(), (PubCompMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBREL:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processPubRel(ctx.channel(), (PubRelMessage) msg);
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case DISCONNECT:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processDisconnect(ctx.channel());
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
|
|
|
|
case PUBACK:
|
2016-01-24 17:37:52 +01:00
|
|
|
m_processor.processPubAck(ctx.channel(), (PubAckMessage) msg);
|
2013-09-15 17:18:26 +02:00
|
|
|
break;
|
|
|
|
|
case PINGREQ:
|
|
|
|
|
PingRespMessage pingResp = new PingRespMessage();
|
2014-01-10 10:45:56 +01:00
|
|
|
ctx.writeAndFlush(pingResp);
|
2013-09-15 17:18:26 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2017-01-11 15:33:23 +01:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
LOG.error(
|
|
|
|
|
"An unexpected exception was caught while processing MQTT message. MessageType = {}, cause = {}, errorMessage = {}.",
|
|
|
|
|
messageType, ex.getCause(), ex.getMessage());
|
|
|
|
|
ctx.fireExceptionCaught(ex);
|
2013-09-15 17:18:26 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-15 18:16:24 +02:00
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
2016-01-24 19:10:23 +01:00
|
|
|
String clientID = NettyUtils.clientID(ctx.channel());
|
2015-09-22 15:29:02 +02:00
|
|
|
if (clientID != null && !clientID.isEmpty()) {
|
2017-01-11 15:33:23 +01:00
|
|
|
LOG.info("Notifying connection lost event. MqttClientId = {}.", clientID);
|
2016-11-17 22:50:04 +01:00
|
|
|
m_processor.processConnectionLost(clientID, ctx.channel());
|
2015-09-22 15:29:02 +02:00
|
|
|
}
|
2015-12-06 11:43:29 +01:00
|
|
|
ctx.close();
|
2013-09-15 17:18:26 +02:00
|
|
|
}
|
2015-08-01 11:59:00 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
2017-01-11 15:33:23 +01:00
|
|
|
LOG.error(
|
|
|
|
|
"An unexpected exception was caught while processing MQTT message. Closing Netty channel. MqttClientId = {}, cause = {}, errorMessage = {}.",
|
|
|
|
|
NettyUtils.clientID(ctx.channel()), cause.getCause(), cause.getMessage());
|
2015-08-01 11:59:00 +02:00
|
|
|
ctx.close();
|
|
|
|
|
}
|
2016-03-06 19:50:26 +01:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
|
if (ctx.channel().isWritable()) {
|
|
|
|
|
m_processor.notifyChannelWritable(ctx.channel());
|
|
|
|
|
}
|
|
|
|
|
ctx.fireChannelWritabilityChanged();
|
|
|
|
|
}
|
2016-11-05 18:19:44 +01:00
|
|
|
|
2013-09-15 17:18:26 +02:00
|
|
|
}
|