Added simple handler to test the codec speed
This commit is contained in:
+127
-2
@@ -1,7 +1,132 @@
|
|||||||
package org.eclipse.moquette.parser.netty.performance;
|
package org.eclipse.moquette.parser.netty.performance;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandler;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import org.eclipse.moquette.proto.Utils;
|
||||||
|
import org.eclipse.moquette.proto.messages.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import static org.eclipse.moquette.proto.messages.AbstractMessage.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by andrea on 6/2/15.
|
* Created by andrea on 6/2/15.
|
||||||
*/
|
*/
|
||||||
public class LoopHandler {
|
@ChannelHandler.Sharable
|
||||||
}
|
class LoopMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(LoopMQTTHandler.class);
|
||||||
|
private ProtocolDecodingServer.SharedState m_state;
|
||||||
|
|
||||||
|
public LoopMQTTHandler(ProtocolDecodingServer.SharedState state) {
|
||||||
|
this.m_state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object message) {
|
||||||
|
AbstractMessage msg = (AbstractMessage) message;
|
||||||
|
LOG.info("Received a message of type {}", Utils.msgType2String(msg.getMessageType()));
|
||||||
|
try {
|
||||||
|
switch (msg.getMessageType()) {
|
||||||
|
case CONNECT:
|
||||||
|
handleConnect(ctx, (ConnectMessage) message);
|
||||||
|
return;
|
||||||
|
case SUBSCRIBE:
|
||||||
|
handleSubscribe(ctx, (SubscribeMessage) msg);
|
||||||
|
return;
|
||||||
|
case PUBLISH:
|
||||||
|
handlePublish(ctx, (PublishMessage) msg);
|
||||||
|
return;
|
||||||
|
case DISCONNECT:
|
||||||
|
ctx.close();
|
||||||
|
// case PUBACK:
|
||||||
|
// NettyChannel channel;
|
||||||
|
// synchronized (m_channelMapper) {
|
||||||
|
// if (!m_channelMapper.containsKey(ctx)) {
|
||||||
|
// m_channelMapper.put(ctx, new NettyChannel(ctx));
|
||||||
|
// }
|
||||||
|
// channel = m_channelMapper.get(ctx);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// m_messaging.handleProtocolMessage(channel, msg);
|
||||||
|
break;
|
||||||
|
case PINGREQ:
|
||||||
|
PingRespMessage pingResp = new PingRespMessage();
|
||||||
|
ctx.writeAndFlush(pingResp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
LOG.error("Bad error in processing the message", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handlePublish(ChannelHandlerContext ctx, PublishMessage msg) {
|
||||||
|
if (!m_state.isForwardable()) {
|
||||||
|
LOG.info("Subscriber not yet connected, LoopHandler instance is {}", this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.debug("push forward message the topic {}", msg.getTopicName());
|
||||||
|
if (LOG.isDebugEnabled()) {
|
||||||
|
LOG.debug("content <{}>", payload2Str(msg.getPayload()));
|
||||||
|
}
|
||||||
|
//publish always at Qos0, to don't handle PUBACK or the complete Qos2 workflow
|
||||||
|
PublishMessage pubMessage = new PublishMessage();
|
||||||
|
pubMessage.setRetainFlag(false);
|
||||||
|
pubMessage.setTopicName(msg.getTopicName());
|
||||||
|
pubMessage.setQos(QOSType.MOST_ONE);
|
||||||
|
pubMessage.setPayload(msg.getPayload());
|
||||||
|
|
||||||
|
m_state.getSubscriberCh().writeAndFlush(pubMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleSubscribe(ChannelHandlerContext ctx, SubscribeMessage msg) {
|
||||||
|
m_state.setForwardable(true);
|
||||||
|
LOG.debug(" new value of flag {}, LoopHandler instance is {}", m_state.isForwardable(), this);
|
||||||
|
SubAckMessage ackMessage = new SubAckMessage();
|
||||||
|
ackMessage.setMessageID(msg.getMessageID());
|
||||||
|
ctx.writeAndFlush(ackMessage);
|
||||||
|
LOG.debug("subscribed client to {}", msg.subscriptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
static String payload2Str(ByteBuffer content) {
|
||||||
|
byte[] b = new byte[content.remaining()];
|
||||||
|
content.mark();
|
||||||
|
content.get(b);
|
||||||
|
content.reset();
|
||||||
|
return new String(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleConnect(ChannelHandlerContext ctx, ConnectMessage msg) {
|
||||||
|
String clientID = msg.getClientID();
|
||||||
|
if (clientID.toLowerCase().startsWith("sub")) {
|
||||||
|
m_state.setSubscriberCh(ctx);
|
||||||
|
} else if (clientID.toLowerCase().startsWith("pub")) {
|
||||||
|
m_state.setPublisherCh(ctx);
|
||||||
|
} else {
|
||||||
|
//we don't admit other names
|
||||||
|
ConnAckMessage koResp = new ConnAckMessage();
|
||||||
|
koResp.setReturnCode(ConnAckMessage.IDENTIFIER_REJECTED);
|
||||||
|
ctx.writeAndFlush(koResp);
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnAckMessage okResp = new ConnAckMessage();
|
||||||
|
okResp.setReturnCode(ConnAckMessage.CONNECTION_ACCEPTED);
|
||||||
|
ctx.writeAndFlush(okResp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
// NettyChannel channel = m_channelMapper.get(ctx);
|
||||||
|
// String clientID = (String) channel.getAttribute(NettyChannel.ATTR_KEY_CLIENTID);
|
||||||
|
// m_messaging.lostConnection(channel, clientID);
|
||||||
|
// ctx.close(/*false*/);
|
||||||
|
// synchronized (m_channelMapper) {
|
||||||
|
// m_channelMapper.remove(ctx);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
+134
-1
@@ -1,7 +1,140 @@
|
|||||||
package org.eclipse.moquette.parser.netty.performance;
|
package org.eclipse.moquette.parser.netty.performance;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.*;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.SocketChannel;
|
||||||
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
import io.netty.handler.timeout.IdleState;
|
||||||
|
import io.netty.handler.timeout.IdleStateEvent;
|
||||||
|
import io.netty.handler.timeout.IdleStateHandler;
|
||||||
|
import org.eclipse.moquette.commons.Constants;
|
||||||
|
import org.eclipse.moquette.parser.netty.MQTTDecoder;
|
||||||
|
import org.eclipse.moquette.parser.netty.MQTTEncoder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by andrea on 6/2/15.
|
* Emulates a broker, but doesn't apply any protocol logic, just forward the qos0 publishes
|
||||||
|
* to the lone subscriber, it's used just to measure the protocol decoding/encoding overhead.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public class ProtocolDecodingServer {
|
public class ProtocolDecodingServer {
|
||||||
|
|
||||||
|
class MoquetteIdleTimoutHandler extends ChannelDuplexHandler {
|
||||||
|
@Override
|
||||||
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||||
|
if (evt instanceof IdleStateEvent) {
|
||||||
|
IdleState e = ((IdleStateEvent) evt).state();
|
||||||
|
if (e == IdleState.ALL_IDLE) {
|
||||||
|
//fire a channelInactive to trigger publish of Will
|
||||||
|
ctx.fireChannelInactive();
|
||||||
|
ctx.close();
|
||||||
|
} /*else if (e.getState() == IdleState.WRITER_IDLE) {
|
||||||
|
ctx.writeAndFlush(new PingMessage());
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(ProtocolDecodingServer.class);
|
||||||
|
|
||||||
|
EventLoopGroup m_bossGroup;
|
||||||
|
EventLoopGroup m_workerGroup;
|
||||||
|
|
||||||
|
static final class SharedState {
|
||||||
|
private volatile ChannelHandlerContext subscriberCh;
|
||||||
|
private volatile ChannelHandlerContext publisherCh;
|
||||||
|
private volatile boolean forwardPublish = false;
|
||||||
|
|
||||||
|
public boolean isForwardable() {
|
||||||
|
return forwardPublish;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setForwardable(boolean forwardPublish) {
|
||||||
|
this.forwardPublish = forwardPublish;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriberCh(ChannelHandlerContext subscriberCh) {
|
||||||
|
this.subscriberCh = subscriberCh;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChannelHandlerContext getSubscriberCh() {
|
||||||
|
return subscriberCh;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublisherCh(ChannelHandlerContext publisherCh) {
|
||||||
|
this.publisherCh = publisherCh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final SharedState state = new SharedState();
|
||||||
|
|
||||||
|
void init() {
|
||||||
|
String host = "0.0.0.0";
|
||||||
|
int port = 1883;
|
||||||
|
m_bossGroup = new NioEventLoopGroup();
|
||||||
|
m_workerGroup = new NioEventLoopGroup();
|
||||||
|
|
||||||
|
ServerBootstrap b = new ServerBootstrap();
|
||||||
|
b.group(m_bossGroup, m_workerGroup)
|
||||||
|
.channel(NioServerSocketChannel.class)
|
||||||
|
.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||||
|
@Override
|
||||||
|
public void initChannel(SocketChannel ch) throws Exception {
|
||||||
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
|
try {
|
||||||
|
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
|
||||||
|
pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
|
||||||
|
pipeline.addLast("decoder", new MQTTDecoder());
|
||||||
|
pipeline.addLast("encoder", new MQTTEncoder());
|
||||||
|
pipeline.addLast("handler", new LoopMQTTHandler(state));
|
||||||
|
} catch (Throwable th) {
|
||||||
|
LOG.error("Severe error during pipeline creation", th);
|
||||||
|
throw th;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.option(ChannelOption.SO_BACKLOG, 128)
|
||||||
|
.option(ChannelOption.SO_REUSEADDR, true)
|
||||||
|
.option(ChannelOption.TCP_NODELAY, true)
|
||||||
|
.childOption(ChannelOption.SO_KEEPALIVE, true);
|
||||||
|
try {
|
||||||
|
// Bind and start to accept incoming connections.
|
||||||
|
ChannelFuture f = b.bind(host, port);
|
||||||
|
LOG.info("Server binded host: {}, port: {}", host, port);
|
||||||
|
f.sync();
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
LOG.error(null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
if (m_workerGroup == null) {
|
||||||
|
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
|
||||||
|
}
|
||||||
|
if (m_bossGroup == null) {
|
||||||
|
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
|
||||||
|
}
|
||||||
|
m_workerGroup.shutdownGracefully();
|
||||||
|
m_bossGroup.shutdownGracefully();
|
||||||
|
|
||||||
|
LOG.info("Closed boss and worker Event loops");
|
||||||
|
System.out.println("Server stopped");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final void main(String[] args) {
|
||||||
|
final ProtocolDecodingServer server = new ProtocolDecodingServer();
|
||||||
|
server.init();
|
||||||
|
|
||||||
|
System.out.println("Loop server started");
|
||||||
|
//Bind a shutdown hook
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user