Adapted Netty parser to use ByteBuffer instead of byte[]

This commit is contained in:
andrea
2013-11-26 12:25:48 +01:00
parent d596e41716
commit e9bb34898b
5 changed files with 49 additions and 17 deletions
+1 -1
View File
@@ -23,7 +23,7 @@
<dependency> <dependency>
<groupId>org.dna.mqtt</groupId> <groupId>org.dna.mqtt</groupId>
<artifactId>moquette-parser-commons</artifactId> <artifactId>moquette-parser-commons</artifactId>
<version>0.2</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
@@ -1,6 +1,7 @@
package org.dna.mqtt.moquette.parser.netty; package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.util.List; import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage; import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
@@ -51,9 +52,10 @@ class PublishDecoder extends DemuxDecoder {
in.resetReaderIndex(); in.resetReaderIndex();
return; return;
} }
byte[] b = new byte[payloadSize]; // byte[] b = new byte[payloadSize];
in.readBytes(b); ByteBuf bb = Unpooled.buffer(payloadSize);
message.setPayload(b); in.readBytes(bb);
message.setPayload(bb.nioBuffer());
out.add(message); out.add(message);
} }
@@ -2,6 +2,8 @@ package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage; import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
@@ -65,7 +67,8 @@ public class PublishDecoderTest {
public void testHeaderWithMessageID_Payload() throws Exception { public void testHeaderWithMessageID_Payload() throws Exception {
m_buff = Unpooled.buffer(14); m_buff = Unpooled.buffer(14);
int messageID = 123; int messageID = 123;
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C}; // byte[] payload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = ByteBuffer.allocate(3).put(new byte[]{0x0A, 0x0B, 0x0C});
initHeaderWithMessageID_Payload(m_buff, messageID, payload); initHeaderWithMessageID_Payload(m_buff, messageID, payload);
//Exercise //Exercise
@@ -76,7 +79,8 @@ public class PublishDecoderTest {
assertNotNull(message); assertNotNull(message);
assertEquals("Fake Topic", message.getTopicName()); assertEquals("Fake Topic", message.getTopicName());
assertEquals(messageID, (int) message.getMessageID()); assertEquals(messageID, (int) message.getMessageID());
TestUtils.verifyEquals(payload, message.getPayload()); // TestUtils.verifyEquals(payload, message.getPayload());
assertEquals(payload, message.getPayload());
} }
@Test @Test
@@ -141,7 +145,9 @@ public class PublishDecoderTest {
PublishMessage message = (PublishMessage)m_results.get(0); PublishMessage message = (PublishMessage)m_results.get(0);
assertNotNull(message); assertNotNull(message);
assertEquals("/topic", message.getTopicName()); assertEquals("/topic", message.getTopicName());
assertEquals("Test my payload", new String(message.getPayload())); // assertEquals("Test my payload", new String(message.getPayload()));
Buffer expectedPayload = ByteBuffer.allocate(15).put("Test my payload".getBytes()).flip();
assertEquals(expectedPayload, message.getPayload());
} }
private void initHeader(ByteBuf buff) throws IllegalAccessException { private void initHeader(ByteBuf buff) throws IllegalAccessException {
@@ -170,6 +176,16 @@ public class PublishDecoderTest {
buff.writeBytes(tmp); buff.writeBytes(tmp);
} }
private void initHeaderWithMessageID_Payload(ByteBuf buff, int messageID, ByteBuffer payload) throws IllegalAccessException {
ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
tmp.writeShort(messageID);
tmp.writeBytes(payload);
buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | 0x02) //set Qos to 1
.writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
//topic name
buff.writeBytes(tmp);
}
private ByteBuf generatePublishQoS0(ByteBuf payload) throws IllegalAccessException { private ByteBuf generatePublishQoS0(ByteBuf payload) throws IllegalAccessException {
int size = payload.capacity(); int size = payload.capacity();
ByteBuf messageBody = Unpooled.buffer(size); ByteBuf messageBody = Unpooled.buffer(size);
@@ -3,6 +3,7 @@ package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.nio.ByteBuffer;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -35,7 +36,8 @@ public class PublishEncoderTest {
msg.setTopicName(topic); msg.setTopicName(topic);
//variable part //variable part
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C}; byte[] bpayload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(3).put(bpayload).flip();
msg.setPayload(payload); msg.setPayload(payload);
//Exercise //Exercise
@@ -48,7 +50,8 @@ public class PublishEncoderTest {
//Variable part //Variable part
verifyString(topic, m_out); verifyString(topic, m_out);
verifyBuff(payload.length, payload, m_out); payload.rewind();
verifyBuff(bpayload.length, payload, m_out);
} }
@@ -61,7 +64,8 @@ public class PublishEncoderTest {
msg.setTopicName(topic); msg.setTopicName(topic);
//variable part //variable part
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C}; byte[] bpayload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(3).put(bpayload).flip();
msg.setPayload(payload); msg.setPayload(payload);
//Exercise //Exercise
@@ -76,7 +80,8 @@ public class PublishEncoderTest {
verifyString(topic, m_out); verifyString(topic, m_out);
assertEquals(0, m_out.readByte()); //MessageID MSB assertEquals(0, m_out.readByte()); //MessageID MSB
assertEquals(1, m_out.readByte()); //MessageID LSB assertEquals(1, m_out.readByte()); //MessageID LSB
verifyBuff(payload.length, payload, m_out); payload.rewind();
verifyBuff(bpayload.length, payload, m_out);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@@ -120,7 +125,8 @@ public class PublishEncoderTest {
msg.setTopicName(null); msg.setTopicName(null);
//variable part //variable part
byte[] payload = new byte[]{0x0A, 0x0B, 0x0C}; byte[] bpayload = new byte[]{0x0A, 0x0B, 0x0C};
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(3).put(bpayload).flip();
msg.setPayload(payload); msg.setPayload(payload);
ByteBuf out = Unpooled.buffer(); ByteBuf out = Unpooled.buffer();
@@ -136,7 +142,8 @@ public class PublishEncoderTest {
msg.setTopicName(topic); msg.setTopicName(topic);
//variable part //variable part
byte[] payload = "Test my payload".getBytes(); byte[] bpayload = "Test my payload".getBytes();
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(bpayload.length).put(bpayload).flip();
msg.setPayload(payload); msg.setPayload(payload);
//Exercise //Exercise
@@ -145,16 +152,17 @@ public class PublishEncoderTest {
//Verify //Verify
//2 byte header + (2+6) topic name + [2 message ID if QoQ <> 0]+ payload //2 byte header + (2+6) topic name + [2 message ID if QoQ <> 0]+ payload
int size = m_out.readableBytes(); int size = m_out.readableBytes();
assertEquals(10+payload.length, size); assertEquals(10 + bpayload.length, size);
assertEquals(0x30, m_out.readByte()); //1 byte assertEquals(0x30, m_out.readByte()); //1 byte
assertEquals(23, m_out.readByte()); //1 byte the length assertEquals(23, m_out.readByte()); //1 byte the length
} }
@Test @Test
public void testEncodeBigMessage() throws Exception { public void testEncodeBigMessage() throws Exception {
byte[] payload = new byte[256]; byte[] bpayload = new byte[256];
TestUtils.generateRandomPayload(256).readBytes(payload); TestUtils.generateRandomPayload(256).readBytes(bpayload);
ByteBuffer payload = (ByteBuffer) ByteBuffer.allocate(bpayload.length).put(bpayload).flip();
String topic = "/topic"; String topic = "/topic";
PublishMessage msg = new PublishMessage(); PublishMessage msg = new PublishMessage();
@@ -168,7 +176,7 @@ public class PublishEncoderTest {
m_encoder.encode(m_mockedContext, msg, m_out); m_encoder.encode(m_mockedContext, msg, m_out);
int size = m_out.readableBytes(); int size = m_out.readableBytes();
assertEquals(11+payload.length, size); assertEquals(11 + bpayload.length, size);
assertEquals(0x30, m_out.readByte()); //1 byte assertEquals(0x30, m_out.readByte()); //1 byte
} }
@@ -6,6 +6,7 @@ import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator; import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@@ -31,6 +32,11 @@ public class TestUtils {
} }
} }
static void verifyBuff(int numBytes, ByteBuffer bytes, ByteBuf buff) {
assertTrue(numBytes <= buff.readableBytes());
assertEquals(bytes, buff.nioBuffer());
}
/** /**
* Verify the presence of the given string starting from the current position * Verify the presence of the given string starting from the current position
* inside the buffer. * inside the buffer.