Moved project package from org.eclipse.moquette to io.moquette
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2015 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* 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.testclient;
|
||||
|
||||
import io.moquette.commons.Constants;
|
||||
import io.moquette.parser.netty.MQTTDecoder;
|
||||
import io.moquette.parser.netty.MQTTEncoder;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.moquette.proto.messages.AbstractMessage;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Class used just to send and receive MQTT messages without any protocol login
|
||||
* in action, just use the encoder/decoder part.
|
||||
*
|
||||
* @author andrea
|
||||
*/
|
||||
public class Client {
|
||||
|
||||
public static interface ICallback {
|
||||
void call(AbstractMessage msg);
|
||||
}
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Client.class);
|
||||
final ClientNettyMQTTHandler handler = new ClientNettyMQTTHandler();
|
||||
EventLoopGroup workerGroup;
|
||||
Channel m_channel;
|
||||
private boolean m_connectionLost = false;
|
||||
|
||||
public Client(String host) {
|
||||
this(host, Constants.PORT);
|
||||
}
|
||||
|
||||
public Client(String host, int port) {
|
||||
|
||||
handler.setClient(this);
|
||||
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
|
||||
try {
|
||||
Bootstrap b = new Bootstrap();
|
||||
b.group(workerGroup);
|
||||
b.channel(NioSocketChannel.class);
|
||||
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||
b.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
pipeline.addLast("decoder", new MQTTDecoder());
|
||||
pipeline.addLast("encoder", new MQTTEncoder());
|
||||
pipeline.addLast("handler", handler);
|
||||
}
|
||||
});
|
||||
|
||||
// Start the client.
|
||||
m_channel = b.connect(host, port).sync().channel();
|
||||
} catch (Exception ex) {
|
||||
LOG.error("Error received in client setup", ex);
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
ICallback callback;
|
||||
public void setCallback(ICallback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public void sendMessage(AbstractMessage msg) {
|
||||
m_channel.writeAndFlush(msg);
|
||||
}
|
||||
|
||||
void messageReceived(AbstractMessage msg) {
|
||||
LOG.info("Received message " + msg);
|
||||
if (this.callback != null) {
|
||||
this.callback.call(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void setConnectionLost(boolean status) {
|
||||
m_connectionLost = status;
|
||||
}
|
||||
|
||||
public boolean isConnectionLost() {
|
||||
return m_connectionLost;
|
||||
}
|
||||
|
||||
public void close() throws InterruptedException {
|
||||
// Wait until the connection is closed.
|
||||
m_channel.closeFuture().sync();
|
||||
if (workerGroup == null) {
|
||||
throw new IllegalStateException("Invoked close on an Acceptor that wasn't initialized");
|
||||
}
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2012-2015 The original author or authors
|
||||
* ------------------------------------------------------
|
||||
* 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.testclient;
|
||||
|
||||
import io.moquette.proto.Utils;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.moquette.proto.messages.AbstractMessage;
|
||||
import io.moquette.server.netty.NettyMQTTHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author andrea
|
||||
*/
|
||||
@ChannelHandler.Sharable
|
||||
class ClientNettyMQTTHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(NettyMQTTHandler.class);
|
||||
private Client m_client;
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object message) {
|
||||
AbstractMessage msg = (AbstractMessage) message;
|
||||
LOG.info("Received a message of type {}", Utils.msgType2String(msg.getMessageType()));
|
||||
m_client.messageReceived(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx)throws Exception {
|
||||
m_client.setConnectionLost(true);
|
||||
ctx.close(/*false*/);
|
||||
}
|
||||
|
||||
void setClient(Client client) {
|
||||
m_client = client;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package io.moquette.testclient;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.moquette.parser.netty.Utils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* This class is used to have a fluent interface to interact with a server.
|
||||
* Inspired by org.kaazing.robot
|
||||
*/
|
||||
public class RawClient {
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
class RawMessageHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object message) {
|
||||
LOG.info("Received a message {}", message);
|
||||
try {
|
||||
ByteBuf in = (ByteBuf) message;
|
||||
int readBytes = in.readableBytes();
|
||||
heapBuffer.writeBytes(in);
|
||||
readableBytesSem.release(readBytes);
|
||||
} finally {
|
||||
ReferenceCountUtil.release(message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
//m_client.setConnectionLost(true);
|
||||
disconnectLatch.countDown();
|
||||
ctx.close(/*false*/);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RawClient.class);
|
||||
final RawMessageHandler handler;
|
||||
EventLoopGroup workerGroup;
|
||||
Channel m_channel;
|
||||
|
||||
private boolean connected = false;
|
||||
private ByteBuf heapBuffer;
|
||||
private CountDownLatch disconnectLatch;
|
||||
private final Semaphore readableBytesSem;
|
||||
|
||||
private RawClient(String host, int port) {
|
||||
handler = new RawMessageHandler();
|
||||
heapBuffer = Unpooled.buffer(128);
|
||||
disconnectLatch = new CountDownLatch(1);
|
||||
readableBytesSem = new Semaphore(0, true);
|
||||
|
||||
workerGroup = new NioEventLoopGroup();
|
||||
|
||||
try {
|
||||
Bootstrap b = new Bootstrap();
|
||||
b.group(workerGroup);
|
||||
b.channel(NioSocketChannel.class);
|
||||
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||
b.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
pipeline.addLast("handler", handler);
|
||||
}
|
||||
});
|
||||
|
||||
// Start the client.
|
||||
m_channel = b.connect(host, port).sync().channel();
|
||||
this.connected = true;
|
||||
} catch (Exception ex) {
|
||||
LOG.error("Error received in client setup", ex);
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
public static RawClient connect(String host, int port) {
|
||||
return new RawClient(host, port);
|
||||
}
|
||||
|
||||
public RawClient isConnected() {
|
||||
if (!this.connected) {
|
||||
throw new IllegalStateException("Can't connect the client");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public RawClient write(int... bytes) {
|
||||
ByteBuf buff = Unpooled.buffer(bytes.length);
|
||||
buff.clear();
|
||||
for (int b : bytes) {
|
||||
buff.writeByte((byte) b);
|
||||
}
|
||||
m_channel.write(buff);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RawClient writeWithSize(String str) {
|
||||
ByteBuf buff = Unpooled.buffer(str.length() + 2);
|
||||
buff.writeBytes(Utils.encodeString(str));
|
||||
m_channel.write(buff);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write just String bytes not length
|
||||
* */
|
||||
public RawClient write(String str) {
|
||||
ByteBuf out = Unpooled.buffer(str.length());
|
||||
byte[] raw;
|
||||
try {
|
||||
raw = str.getBytes("UTF-8");
|
||||
//NB every Java platform has got UTF-8 encoding by default, so this
|
||||
//exception are never raised.
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
out.writeBytes(raw);
|
||||
m_channel.write(out);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RawClient flush() {
|
||||
m_channel.flush();
|
||||
return this;
|
||||
}
|
||||
|
||||
public RawClient read(int expectedByte) {
|
||||
try {
|
||||
readableBytesSem.acquire(1);
|
||||
} catch (InterruptedException e) {
|
||||
throw new IllegalStateException("Interrupted while waiting data");
|
||||
}
|
||||
byte b = heapBuffer.readByte();
|
||||
if (b != expectedByte) {
|
||||
throw new IllegalStateException(String.format("Expected byte 0x%02X but found 0x%02X", b, expectedByte));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect the closing of the underling channel, with timeout
|
||||
* */
|
||||
public void closed(long timeout) throws InterruptedException {
|
||||
disconnectLatch.await(timeout, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect closing the connect with unbound time
|
||||
* */
|
||||
public void closed() throws InterruptedException {
|
||||
disconnectLatch.await();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user