mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
fixed some test error
This commit is contained in:
1 parent
fc3671ea00
commit
5785f29ec3
12 files changed
+118
-59
No files matched your search
@@ -23,6 +23,8 @@ public class Block {
|
||||
|
||||
public Block(){
|
||||
this.tlvType=new VlInt(TLV.TlvInvalid);
|
||||
this.length=new VlInt(0);
|
||||
this.elements=new ElementContainer();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,15 +34,15 @@ public class Block {
|
||||
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
|
||||
* @return
|
||||
*/
|
||||
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws EncoderException {
|
||||
public static Block createBlockByBuffer(byte[] buffer,boolean verifyLength) throws BlockException {
|
||||
if(buffer.length==0){
|
||||
return null;
|
||||
throw new BlockException("createBlockByBuffer: buffer.length==0");
|
||||
}
|
||||
|
||||
// 读取type
|
||||
VlInt tlvType=TLV.readType(buffer,new VlInt(0));
|
||||
if(tlvType==null){
|
||||
return null;
|
||||
throw new BlockException("createBlockByBuffer: tlvType read error");
|
||||
}
|
||||
|
||||
// 读取length
|
||||
@@ -48,10 +50,10 @@ public class Block {
|
||||
try {
|
||||
tlvLength = TLV.readVarNumber(buffer,new VlInt(tlvType.size()));
|
||||
} catch (ByteHelperException e) {
|
||||
e.printStackTrace();
|
||||
throw new BlockException("createBlockByBuffer: tlvLength read error");
|
||||
}
|
||||
if(tlvLength==null){
|
||||
return null;
|
||||
throw new BlockException("createBlockByBuffer: tlvLength read error");
|
||||
}
|
||||
|
||||
return createBlockByTypeLengthBuffer(tlvType,tlvLength,
|
||||
@@ -67,7 +69,7 @@ public class Block {
|
||||
* @param verifyLength
|
||||
* @return
|
||||
*/
|
||||
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws EncoderException {
|
||||
public static Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws BlockException {
|
||||
return new Block(tlvType,tlvLength,buffer,verifyLength);
|
||||
}
|
||||
|
||||
@@ -78,33 +80,55 @@ public class Block {
|
||||
* @param buffer
|
||||
* @param verifyLength
|
||||
*/
|
||||
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws EncoderException {
|
||||
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength) throws BlockException{
|
||||
this.tlvType=tlvType;
|
||||
this.length=tlvLength;
|
||||
this.elements=new ElementContainer();
|
||||
|
||||
// 检查长度是否正确
|
||||
if(verifyLength && (!this.length.isEqual(buffer.length))){
|
||||
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): invalid length.");
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): invalid length.");
|
||||
}
|
||||
|
||||
// 设置 Raw
|
||||
Encoder encoder=new Encoder();
|
||||
boolean resetBool=encoder.encoderReset(new SizeT(tlvType.size()+tlvLength.size()+buffer.length),new SizeT(0));
|
||||
boolean resetBool= false;
|
||||
try {
|
||||
resetBool = encoder.encoderReset(new SizeT(tlvType.size()+tlvLength.size()+buffer.length),new SizeT(0));
|
||||
} catch (EncoderException e) {
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error"
|
||||
+e.getMessage());
|
||||
}
|
||||
if(!resetBool){
|
||||
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error");
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoderReset error");
|
||||
}
|
||||
if(encoder.prependByteArray(buffer,new SizeT(buffer.length))==0){
|
||||
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error");
|
||||
try {
|
||||
if(encoder.prependByteArray(buffer,new SizeT(buffer.length))==0){
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error");
|
||||
}
|
||||
} catch (EncoderException e) {
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prepenndByteArray buffer error"
|
||||
+e.getMessage());
|
||||
}
|
||||
if(encoder.prependVarNumber(tlvLength)==0){
|
||||
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error");
|
||||
try {
|
||||
if(encoder.prependVarNumber(tlvLength)==0){
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error");
|
||||
}
|
||||
} catch (EncoderException e) {
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvLength error"
|
||||
+e.getMessage());
|
||||
}
|
||||
if(encoder.prependVarNumber(tlvType)==0){
|
||||
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error");
|
||||
try {
|
||||
if(encoder.prependVarNumber(tlvType)==0){
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error");
|
||||
}
|
||||
} catch (EncoderException e) {
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): prependVarNumber tlvType error"
|
||||
+e.getMessage());
|
||||
}
|
||||
byte[] encodedBuffer=encoder.getBuffer();
|
||||
if(encodedBuffer==null){
|
||||
throw new Error("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoder.getBuffer error");
|
||||
throw new BlockException("Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength): encoder.getBuffer error");
|
||||
}
|
||||
this.raw=encodedBuffer;
|
||||
|
||||
@@ -242,13 +266,18 @@ public class Block {
|
||||
try {
|
||||
subBlockValue = new byte[tlvLength.getVlIntValue2Int()];
|
||||
System.arraycopy(this.value,start,subBlockValue,0,tlvLength.getVlIntValue2Int());
|
||||
Block block=createBlockByTypeLengthBuffer(tlvType,tlvLength,subBlockValue,true);
|
||||
Block block= null;
|
||||
try {
|
||||
block = createBlockByTypeLengthBuffer(tlvType,tlvLength,subBlockValue,true);
|
||||
} catch (BlockException e) {
|
||||
return false;
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.addElement(block);
|
||||
start+=tlvLength.getVlIntValue2Int();
|
||||
} catch (VlIntException | EncoderException e) {
|
||||
} catch (VlIntException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package encoding;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:53 2021/3/22
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class BlockException extends Exception {
|
||||
public BlockException(String msg){
|
||||
super("Block."+msg);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ public interface IEncodingAble {
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
public int wireEncode(Encoder encoder) throws EncoderException;
|
||||
public int wireEncode(Encoder encoder) throws EncoderException, BlockException;
|
||||
|
||||
/**
|
||||
* 线速解码,将一个TLV解码成一个网络包
|
||||
|
||||
@@ -44,7 +44,7 @@ public class SelfEncodingBase {
|
||||
* @param iEncodingAble
|
||||
* @return
|
||||
*/
|
||||
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException {
|
||||
public Block selfWireEncode(IEncodingAble iEncodingAble) throws EncoderException, BlockException {
|
||||
// this.setiEncodingAble(iEncodingAble);
|
||||
if((this.rawBlock!=null)&&this.rawBlock.isValid()){
|
||||
return this.rawBlock;
|
||||
@@ -64,7 +64,7 @@ public class SelfEncodingBase {
|
||||
* @param iEncodingAble
|
||||
* @return
|
||||
*/
|
||||
private Block easyEncoder(IEncodingAble iEncodingAble) throws EncoderException {
|
||||
private Block easyEncoder(IEncodingAble iEncodingAble) throws EncoderException, BlockException {
|
||||
Encoder encoder=new Encoder();
|
||||
|
||||
// 首先测量目标 TLV 的大小
|
||||
|
||||
@@ -59,7 +59,8 @@ public class VlInt {
|
||||
new Double(pow(2, 32)).longValue())) < 0){
|
||||
return 5;
|
||||
}else if(bigInteger.compareTo(BigInteger.valueOf(
|
||||
new Double(pow(2, 64)).longValue())) < 0){
|
||||
new Double(pow(2, 64)).longValue())) <= 0){
|
||||
// 这里其实是与long的最大值做对比,所以可以等
|
||||
return 9;
|
||||
}else{
|
||||
//MIN1.0中,VlInt的size最大为9
|
||||
@@ -103,7 +104,8 @@ public class VlInt {
|
||||
return false;
|
||||
}
|
||||
if(bigInteger.compareTo(BigInteger.valueOf(
|
||||
new Double(pow(2, 64)).longValue())) >= 0){
|
||||
new Double(pow(2, 64)).longValue())) > 0){
|
||||
// 这里其实是与long的最大值做对比,所以不能等于,暂时用大于
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -176,7 +178,7 @@ public class VlInt {
|
||||
public VlInt(long l){
|
||||
this.VlIntValue=BigInteger.valueOf(l);
|
||||
if(!isValidVlIntValue(this.VlIntValue)){
|
||||
throw new Error("VlInt(long l): The l value is invalid, it is out of the bound.");
|
||||
throw new Error("VlInt(long l): out of the bound, the l value is invalid.");
|
||||
}
|
||||
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
|
||||
this.size=SizeOfVarNumber(this.VlIntValue);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean fillDataToFields() throws EncoderException{
|
||||
public boolean fillDataToFields() throws EncoderException, BlockException {
|
||||
// 可变区 => 受保护区(无)
|
||||
|
||||
// 填充可变区 => 非受保护区
|
||||
@@ -128,7 +128,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws EncoderException {
|
||||
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
|
||||
// 首先将属性填充到分区当中
|
||||
if(!this.fillDataToFields()){
|
||||
return 0;
|
||||
|
||||
@@ -68,7 +68,7 @@ public class Data implements IEncodingAble,InteractWithField {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean fillDataToFields() throws EncoderException {
|
||||
public boolean fillDataToFields() throws EncoderException, BlockException {
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充可变区
|
||||
// { => 可变区
|
||||
@@ -216,7 +216,7 @@ public class Data implements IEncodingAble,InteractWithField {
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws EncoderException {
|
||||
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
|
||||
// 首先将各项属性填充到分区当中
|
||||
if(this.fillDataToFields()){
|
||||
return 0;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package packet;
|
||||
|
||||
import encoding.BlockException;
|
||||
import encoding.EncoderException;
|
||||
import encoding.VlIntException;
|
||||
|
||||
@@ -15,7 +16,7 @@ public interface InteractWithField {
|
||||
* 将包的属性填充到网络包的四个分区当中
|
||||
* @return
|
||||
*/
|
||||
boolean fillDataToFields() throws EncoderException;
|
||||
boolean fillDataToFields() throws EncoderException, BlockException;
|
||||
|
||||
/**
|
||||
* 从网络包的四个分区当中提取出特定网络包的相关属性
|
||||
|
||||
@@ -104,7 +104,7 @@ public class Interest implements InteractWithField,IEncodingAble{
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean fillDataToFields() throws EncoderException {
|
||||
public boolean fillDataToFields() throws EncoderException, BlockException {
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充可变区
|
||||
/////////////////////////////////////////////////////////////
|
||||
@@ -292,7 +292,7 @@ public class Interest implements InteractWithField,IEncodingAble{
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws EncoderException {
|
||||
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
|
||||
// 首先将各项属性填充到分区当中
|
||||
if(!this.fillDataToFields()){
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user