Files
IM-server-new/broker/src/main/java/io/moquette/server/netty/NettyMQTTHandler.java
T

122 lines
4.5 KiB
Java
Raw Normal View History

/*
2017-01-04 14:37:29 +01:00
* Copyright (c) 2012-2017 The original author or authorsgetRockQuestions()
* ------------------------------------------------------
* 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.
*/
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.*;
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
import io.netty.handler.codec.CorruptedFrameException;
2013-09-15 17:18:26 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
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);
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;
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:
m_processor.processConnect(ctx.channel(), (ConnectMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case SUBSCRIBE:
m_processor.processSubscribe(ctx.channel(), (SubscribeMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case UNSUBSCRIBE:
m_processor.processUnsubscribe(ctx.channel(), (UnsubscribeMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case PUBLISH:
m_processor.processPublish(ctx.channel(), (PublishMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case PUBREC:
m_processor.processPubRec(ctx.channel(), (PubRecMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case PUBCOMP:
m_processor.processPubComp(ctx.channel(), (PubCompMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case PUBREL:
m_processor.processPubRel(ctx.channel(), (PubRelMessage) msg);
break;
2013-09-15 17:18:26 +02:00
case DISCONNECT:
m_processor.processDisconnect(ctx.channel());
break;
case PUBACK:
m_processor.processPubAck(ctx.channel(), (PubAckMessage) msg);
2013-09-15 17:18:26 +02:00
break;
case PINGREQ:
PingRespMessage pingResp = new PingRespMessage();
ctx.writeAndFlush(pingResp);
2013-09-15 17:18:26 +02:00
break;
}
} catch (Exception ex) {
LOG.error("Bad error in processing the message", ex);
ctx.fireExceptionCaught(ex);
2013-09-15 17:18:26 +02:00
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientID = NettyUtils.clientID(ctx.channel());
if (clientID != null && !clientID.isEmpty()) {
m_processor.processConnectionLost(clientID, ctx.channel());
}
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 {
if (cause instanceof CorruptedFrameException) {
//something goes bad with decoding
LOG.warn("Error decoding a packet, probably a bad formatted packet, message: " + cause.getMessage());
} else if (cause instanceof IOException && "Connection reset by peer".equals(cause.getMessage())) {
LOG.warn("Network connection closed abruptly");
2015-08-01 11:59:00 +02:00
} else {
LOG.error("Ugly error on networking", cause);
}
ctx.close();
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
if (ctx.channel().isWritable()) {
m_processor.notifyChannelWritable(ctx.channel());
}
ctx.fireChannelWritabilityChanged();
}
2013-09-15 17:18:26 +02:00
}