mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: ReadOnlyFieldTest
This commit is contained in:
1 parent
0a7ccc95a3
commit
8a2da10583
19 files changed
+1251
-966
No files matched your search
+134
-129
@@ -32,7 +32,7 @@ import encoding.*;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
public class Data implements IEncodingAble,InteractWithField {
|
||||
public class Data implements IEncodingAble, InteractWithField {
|
||||
private MINPacket minPacket;
|
||||
private FreshnessPeriod freshnessPeriod;
|
||||
private Payload payload;
|
||||
@@ -42,10 +42,11 @@ public class Data implements IEncodingAble,InteractWithField {
|
||||
|
||||
/**
|
||||
* 展示数据包的 URI
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
if(this.name==null){
|
||||
public String toUri() {
|
||||
if (this.name == null) {
|
||||
return "";
|
||||
}
|
||||
return this.name.toUri();
|
||||
@@ -53,9 +54,10 @@ public class Data implements IEncodingAble,InteractWithField {
|
||||
|
||||
/**
|
||||
* 获取内容数据包的名字
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Identifier getName(){
|
||||
public Identifier getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@@ -65,6 +67,7 @@ public class Data implements IEncodingAble,InteractWithField {
|
||||
|
||||
/**
|
||||
* 将 Data 的各项属性填充到 MINPacket 中定义的对应分区当中
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@@ -80,187 +83,189 @@ public class Data implements IEncodingAble,InteractWithField {
|
||||
// }
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////\
|
||||
// 填充可变受保护区
|
||||
this.minPacket.mutableField.mutableProtectField.clearBlocks();
|
||||
|
||||
// CongestionMark
|
||||
Block block= null;
|
||||
try {
|
||||
// 填充可变受保护区
|
||||
this.minPacket.mutableField.mutableProtectField.clearBlocks();
|
||||
|
||||
// CongestionMark
|
||||
Block block = null;
|
||||
block = new SelfEncodingBase().selfWireEncode(this.congestionMark);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.mutableField.mutableProtectField.addBlock(block);
|
||||
|
||||
// 填充可变非受保护区(无)
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充只读区
|
||||
// { => 只读区
|
||||
// [FreshnessPeriod]
|
||||
// [NackHeader]
|
||||
// <Payload>
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
this.minPacket.readOnlyField.clearBlocks();
|
||||
|
||||
// FreshnessPeriod
|
||||
if(this.freshnessPeriod.isInitial()){
|
||||
try {
|
||||
block=new SelfEncodingBase().selfWireEncode(this.freshnessPeriod);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
if(block==null){
|
||||
this.minPacket.mutableField.mutableProtectField.addBlock(block);
|
||||
|
||||
// 填充可变非受保护区(无)
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充只读区
|
||||
// { => 只读区
|
||||
// [FreshnessPeriod]
|
||||
// [NackHeader]
|
||||
// <Payload>
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
this.minPacket.readOnlyField.clearBlocks();
|
||||
|
||||
// FreshnessPeriod
|
||||
if (this.freshnessPeriod.isInitial()) {
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.freshnessPeriod);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
}
|
||||
|
||||
// NackHeader
|
||||
if (this.nackHeader.isInitial()) {
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.nackHeader);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
}
|
||||
|
||||
// Payload
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
}
|
||||
|
||||
// NackHeader
|
||||
if(this.nackHeader.isInitial()){
|
||||
try {
|
||||
block=new SelfEncodingBase().selfWireEncode(this.nackHeader);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充标识区
|
||||
/////////////////////////////////////////////////////////////
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper =
|
||||
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
|
||||
if (identifierWrapper == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
|
||||
// Payload
|
||||
try {
|
||||
block=new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
return true;
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
|
||||
throw new PacketException("Data.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 填充标识区
|
||||
/////////////////////////////////////////////////////////////
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper=
|
||||
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
|
||||
if(identifierWrapper==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 MINPacket 的分区中提取出 Data 的各项属性
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean extraDataFromFields() throws PacketException {
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析可变区
|
||||
// { => 可变区
|
||||
// { => 受保护区
|
||||
// [CongestionMark]
|
||||
// }
|
||||
// { => 非受保护区
|
||||
// "Empty"
|
||||
// }
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
Block block=this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
|
||||
if(block!=null){
|
||||
try {
|
||||
if(!this.congestionMark.wireDecode(block)){
|
||||
try {
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析可变区
|
||||
// { => 可变区
|
||||
// { => 受保护区
|
||||
// [CongestionMark]
|
||||
// }
|
||||
// { => 非受保护区
|
||||
// "Empty"
|
||||
// }
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
Block block = this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
|
||||
if (block != null) {
|
||||
if (!this.congestionMark.wireDecode(block)) {
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("Data.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析只读区
|
||||
// { => 只读区
|
||||
// [FreshnessPeriod]
|
||||
// <Payload>
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
ElementContainer elementContainer=this.minPacket.readOnlyField.getBlocks();
|
||||
int len=elementContainer.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Block newblock=elementContainer.getBlock(i);
|
||||
int type;
|
||||
try {
|
||||
type=newblock.getType().getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
throw new Error("Data.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
switch (type){
|
||||
case TLV.TlvFreshnessPeriod:
|
||||
try {
|
||||
if(!this.freshnessPeriod.wireDecode(newblock)){
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析只读区
|
||||
// { => 只读区
|
||||
// [FreshnessPeriod]
|
||||
// <Payload>
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
ElementContainer elementContainer = this.minPacket.readOnlyField.getBlocks();
|
||||
int len = elementContainer.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Block newblock = null;
|
||||
try {
|
||||
newblock = elementContainer.getBlock(i);
|
||||
} catch (BlockException e) {
|
||||
throw new PacketException("Data.extraDataFromFields: " + e.getMessage());
|
||||
}
|
||||
int type;
|
||||
type = newblock.getType().getVlIntValue2Int();
|
||||
switch (type) {
|
||||
case TLV.TlvFreshnessPeriod:
|
||||
if (!this.freshnessPeriod.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("Data.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
case TLV.TlvPayload:
|
||||
if(!this.payload.wireDecode(newblock)){
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
|
||||
case TLV.TlvPayload:
|
||||
if (!this.payload.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析标识区
|
||||
/////////////////////////////////////////////////////////////
|
||||
IdentifierWrapper interestIdentifierWrapper=
|
||||
this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierContentInterest));
|
||||
if(interestIdentifierWrapper==null){
|
||||
return false;
|
||||
}
|
||||
this.setName(interestIdentifierWrapper.getIdentifier());
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析标识区
|
||||
/////////////////////////////////////////////////////////////
|
||||
IdentifierWrapper interestIdentifierWrapper =
|
||||
this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierContentInterest));
|
||||
if (interestIdentifierWrapper == null) {
|
||||
return false;
|
||||
}
|
||||
this.setName(interestIdentifierWrapper.getIdentifier());
|
||||
|
||||
return true;
|
||||
return true;
|
||||
} catch (ComponentException | VlIntException e) {
|
||||
throw new PacketException("Data.extraDataFromFields: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Data 线速编码为一个 TLV
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws PacketException {
|
||||
// 首先将各项属性填充到分区当中
|
||||
if(this.fillDataToFields()){
|
||||
if (this.fillDataToFields()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 指定包类型
|
||||
this.minPacket.packetType=new VlInt(TLV.TlvPacketMINCommon);
|
||||
this.minPacket.packetType = new VlInt(TLV.TlvPacketMINCommon);
|
||||
|
||||
return this.minPacket.wireEncode(encoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 Data
|
||||
*
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) throws PacketException {
|
||||
if(!this.minPacket.wireDecode(block)){
|
||||
if (!this.minPacket.wireDecode(block)) {
|
||||
return false;
|
||||
}
|
||||
return this.extraDataFromFields();
|
||||
|
||||
Reference in new issue
Block a user