broker/test/Utils: code formatter used
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
*
|
*
|
||||||
* You may elect to redistribute this code under either of these licenses.
|
* You may elect to redistribute this code under either of these licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.moquette;
|
package io.moquette;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@@ -23,7 +24,6 @@ import io.netty.util.Attribute;
|
|||||||
import io.netty.util.AttributeKey;
|
import io.netty.util.AttributeKey;
|
||||||
import io.netty.util.AttributeMap;
|
import io.netty.util.AttributeMap;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,44 +31,43 @@ import java.io.UnsupportedEncodingException;
|
|||||||
* @author andrea
|
* @author andrea
|
||||||
*/
|
*/
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public static final int MAX_LENGTH_LIMIT = 268435455;
|
public static final int MAX_LENGTH_LIMIT = 268435455;
|
||||||
|
|
||||||
public static final byte VERSION_3_1 = 3;
|
public static final byte VERSION_3_1 = 3;
|
||||||
public static final byte VERSION_3_1_1 = 4;
|
public static final byte VERSION_3_1_1 = 4;
|
||||||
|
|
||||||
static byte readMessageType(ByteBuf in) {
|
static byte readMessageType(ByteBuf in) {
|
||||||
byte h1 = in.readByte();
|
byte h1 = in.readByte();
|
||||||
byte messageType = (byte) ((h1 & 0x00F0) >> 4);
|
byte messageType = (byte) ((h1 & 0x00F0) >> 4);
|
||||||
return messageType;
|
return messageType;
|
||||||
}
|
}
|
||||||
|
|
||||||
static boolean checkHeaderAvailability(ByteBuf in) {
|
static boolean checkHeaderAvailability(ByteBuf in) {
|
||||||
if (in.readableBytes() < 1) {
|
if (in.readableBytes() < 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//byte h1 = in.get();
|
// byte h1 = in.get();
|
||||||
//byte messageType = (byte) ((h1 & 0x00F0) >> 4);
|
// byte messageType = (byte) ((h1 & 0x00F0) >> 4);
|
||||||
in.skipBytes(1); //skip the messageType byte
|
in.skipBytes(1); // skip the messageType byte
|
||||||
|
|
||||||
int remainingLength = Utils.decodeRemainingLenght(in);
|
int remainingLength = Utils.decodeRemainingLenght(in);
|
||||||
if (remainingLength == -1) {
|
if (remainingLength == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check remaining length
|
// check remaining length
|
||||||
if (in.readableBytes() < remainingLength) {
|
if (in.readableBytes() < remainingLength) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//return messageType == type ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK;
|
// return messageType == type ? MessageDecoderResult.OK : MessageDecoderResult.NOT_OK;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode the variable remaining length as defined in MQTT v3.1 specification
|
* Decode the variable remaining length as defined in MQTT v3.1 specification (section 2.1).
|
||||||
* (section 2.1).
|
*
|
||||||
*
|
|
||||||
* @return the decoded length or -1 if needed more data to decode the length field.
|
* @return the decoded length or -1 if needed more data to decode the length field.
|
||||||
*/
|
*/
|
||||||
static int decodeRemainingLenght(ByteBuf in) {
|
static int decodeRemainingLenght(ByteBuf in) {
|
||||||
@@ -85,13 +84,12 @@ public class Utils {
|
|||||||
} while ((digit & 0x80) != 0);
|
} while ((digit & 0x80) != 0);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode the value in the format defined in specification as variable length
|
* Encode the value in the format defined in specification as variable length array.
|
||||||
* array.
|
*
|
||||||
*
|
* @throws IllegalArgumentException
|
||||||
* @throws IllegalArgumentException if the value is not in the specification bounds
|
* if the value is not in the specification bounds [0..268435455].
|
||||||
* [0..268435455].
|
|
||||||
*/
|
*/
|
||||||
static ByteBuf encodeRemainingLength(int value) throws CorruptedFrameException {
|
static ByteBuf encodeRemainingLength(int value) throws CorruptedFrameException {
|
||||||
if (value > MAX_LENGTH_LIMIT || value < 0) {
|
if (value > MAX_LENGTH_LIMIT || value < 0) {
|
||||||
@@ -111,11 +109,11 @@ public class Utils {
|
|||||||
} while (value > 0);
|
} while (value > 0);
|
||||||
return encoded;
|
return encoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a string from the given buffer, reading first the two bytes of len
|
* Load a string from the given buffer, reading first the two bytes of len and then the UTF-8
|
||||||
* and then the UTF-8 bytes of the string.
|
* bytes of the string.
|
||||||
*
|
*
|
||||||
* @return the decoded string or null if NEED_DATA
|
* @return the decoded string or null if NEED_DATA
|
||||||
*/
|
*/
|
||||||
static String decodeString(ByteBuf in) throws UnsupportedEncodingException {
|
static String decodeString(ByteBuf in) throws UnsupportedEncodingException {
|
||||||
@@ -123,8 +121,9 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a byte array from the buffer, use two bytes as length information followed by length bytes.
|
* Read a byte array from the buffer, use two bytes as length information followed by length
|
||||||
* */
|
* bytes.
|
||||||
|
*/
|
||||||
static byte[] readFixedLengthContent(ByteBuf in) throws UnsupportedEncodingException {
|
static byte[] readFixedLengthContent(ByteBuf in) throws UnsupportedEncodingException {
|
||||||
if (in.readableBytes() < 2) {
|
if (in.readableBytes() < 2) {
|
||||||
return null;
|
return null;
|
||||||
@@ -140,15 +139,14 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the IoBuffer with string encoded as MSB, LSB and UTF-8 encoded
|
* Return the IoBuffer with string encoded as MSB, LSB and UTF-8 encoded string content.
|
||||||
* string content.
|
|
||||||
*/
|
*/
|
||||||
public static ByteBuf encodeString(String str) {
|
public static ByteBuf encodeString(String str) {
|
||||||
byte[] raw;
|
byte[] raw;
|
||||||
try {
|
try {
|
||||||
raw = str.getBytes("UTF-8");
|
raw = str.getBytes("UTF-8");
|
||||||
//NB every Java platform has got UTF-8 encoding by default, so this
|
// NB every Java platform has got UTF-8 encoding by default, so this
|
||||||
//exception are never raised.
|
// exception are never raised.
|
||||||
} catch (UnsupportedEncodingException ex) {
|
} catch (UnsupportedEncodingException ex) {
|
||||||
LoggerFactory.getLogger(Utils.class).error(null, ex);
|
LoggerFactory.getLogger(Utils.class).error(null, ex);
|
||||||
return null;
|
return null;
|
||||||
@@ -170,13 +168,17 @@ public class Utils {
|
|||||||
* Return the number of bytes to encode the given remaining length value
|
* Return the number of bytes to encode the given remaining length value
|
||||||
*/
|
*/
|
||||||
static int numBytesToEncode(int len) {
|
static int numBytesToEncode(int len) {
|
||||||
if (0 <= len && len <= 127) return 1;
|
if (0 <= len && len <= 127)
|
||||||
if (128 <= len && len <= 16383) return 2;
|
return 1;
|
||||||
if (16384 <= len && len <= 2097151) return 3;
|
if (128 <= len && len <= 16383)
|
||||||
if (2097152 <= len && len <= 268435455) return 4;
|
return 2;
|
||||||
|
if (16384 <= len && len <= 2097151)
|
||||||
|
return 3;
|
||||||
|
if (2097152 <= len && len <= 268435455)
|
||||||
|
return 4;
|
||||||
throw new IllegalArgumentException("value shoul be in the range [0..268435455]");
|
throw new IllegalArgumentException("value shoul be in the range [0..268435455]");
|
||||||
}
|
}
|
||||||
|
|
||||||
static byte encodeFlags(MqttMessage message) {
|
static byte encodeFlags(MqttMessage message) {
|
||||||
byte flags = 0;
|
byte flags = 0;
|
||||||
if (message.fixedHeader().isDup()) {
|
if (message.fixedHeader().isDup()) {
|
||||||
@@ -185,20 +187,20 @@ public class Utils {
|
|||||||
if (message.fixedHeader().isRetain()) {
|
if (message.fixedHeader().isRetain()) {
|
||||||
flags |= 0x01;
|
flags |= 0x01;
|
||||||
}
|
}
|
||||||
|
|
||||||
flags |= ((message.fixedHeader().qosLevel().value() & 0x03) << 1);
|
flags |= ((message.fixedHeader().qosLevel().value() & 0x03) << 1);
|
||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
//3 = 3.1, 4 = 3.1.1
|
// 3 = 3.1, 4 = 3.1.1
|
||||||
// static final AttributeKey<Integer> PROTOCOL_VERSION = AttributeKey.valueOf("version");
|
// static final AttributeKey<Integer> PROTOCOL_VERSION = AttributeKey.valueOf("version");
|
||||||
//
|
//
|
||||||
// static boolean isMQTT3_1_1(AttributeMap attrsMap) {
|
// static boolean isMQTT3_1_1(AttributeMap attrsMap) {
|
||||||
// Attribute<Integer> versionAttr = attrsMap.attr(PROTOCOL_VERSION);
|
// Attribute<Integer> versionAttr = attrsMap.attr(PROTOCOL_VERSION);
|
||||||
// Integer protocolVersion = versionAttr.get();
|
// Integer protocolVersion = versionAttr.get();
|
||||||
// if (protocolVersion == null) {
|
// if (protocolVersion == null) {
|
||||||
// return true;
|
// return true;
|
||||||
// }
|
// }
|
||||||
// return protocolVersion == VERSION_3_1_1;
|
// return protocolVersion == VERSION_3_1_1;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user