2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2015-08-08 19:36:14 +02:00
|
|
|
* Copyright (c) 2012-2015 The original author or authors
|
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
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
import io.moquette.proto.Utils;
|
|
|
|
|
import io.moquette.proto.messages.*;
|
|
|
|
|
import io.moquette.spi.impl.ProtocolProcessor;
|
|
|
|
|
import static io.moquette.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
|
|
|
|
|
|
|
|
import io.netty.handler.codec.CorruptedFrameException;
|
2015-09-25 10:03:57 +02:00
|
|
|
|
2013-09-15 17:18:26 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @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;
|
2013-11-27 10:43:08 +01:00
|
|
|
LOG.info("Received a message of type {}", Utils.msgType2String(msg.getMessageType()));
|
2013-09-15 17:18:26 +02:00
|
|
|
try {
|
|
|
|
|
switch (msg.getMessageType()) {
|
|
|
|
|
case CONNECT:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processConnect(new NettyChannel(ctx), (ConnectMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case SUBSCRIBE:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processSubscribe(new NettyChannel(ctx), (SubscribeMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case UNSUBSCRIBE:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processUnsubscribe(new NettyChannel(ctx), (UnsubscribeMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBLISH:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processPublish(new NettyChannel(ctx), (PublishMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBREC:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processPubRec(new NettyChannel(ctx), (PubRecMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBCOMP:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processPubComp(new NettyChannel(ctx), (PubCompMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case PUBREL:
|
2015-09-25 10:03:57 +02:00
|
|
|
m_processor.processPubRel(new NettyChannel(ctx), (PubRelMessage) msg);
|
|
|
|
|
break;
|
2013-09-15 17:18:26 +02:00
|
|
|
case DISCONNECT:
|
2015-11-15 11:16:18 +01:00
|
|
|
m_processor.processDisconnect(new NettyChannel(ctx));
|
2015-09-25 10:03:57 +02:00
|
|
|
break;
|
|
|
|
|
case PUBACK:
|
|
|
|
|
m_processor.processPubAck(new NettyChannel(ctx), (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;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
LOG.error("Bad error in processing the message", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-15 18:16:24 +02:00
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
2015-06-07 17:47:50 +02:00
|
|
|
String clientID = (String) NettyUtils.getAttribute(ctx, NettyChannel.ATTR_KEY_CLIENTID);
|
2015-09-22 15:29:02 +02:00
|
|
|
if (clientID != null && !clientID.isEmpty()) {
|
2015-10-17 18:52:04 +02:00
|
|
|
//if the channel was of a correctly connected client, inform messaging
|
|
|
|
|
//else it was of a not completed CONNECT message or sessionStolen
|
|
|
|
|
boolean stolen = false;
|
|
|
|
|
Boolean stolenAttr = (Boolean) NettyUtils.getAttribute(ctx, NettyChannel.ATTR_KEY_SESSION_STOLEN);
|
|
|
|
|
if (stolenAttr != null && stolenAttr == Boolean.TRUE) {
|
|
|
|
|
stolen = stolenAttr;
|
|
|
|
|
}
|
2015-11-17 21:41:29 +01:00
|
|
|
m_processor.processConnectionLost(clientID, stolen);
|
2015-09-22 15:29:02 +02:00
|
|
|
}
|
2013-09-15 17:18:26 +02:00
|
|
|
ctx.close(/*false*/);
|
|
|
|
|
}
|
2015-08-01 11:59:00 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
|
|
if (cause instanceof CorruptedFrameException) {
|
|
|
|
|
//something goes bad with decoding
|
|
|
|
|
LOG.warn("Error decoding a packet, probably a bad formatted packet, message: " + cause.getMessage());
|
|
|
|
|
} else {
|
|
|
|
|
LOG.error("Ugly error on networking", cause);
|
|
|
|
|
}
|
|
|
|
|
ctx.close();
|
|
|
|
|
}
|
2013-09-15 17:18:26 +02:00
|
|
|
}
|