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
@@ -39,129 +39,134 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
private Payload payload;
|
||||
private String flatIdentifier; // 推式包拥有一个扁平的标识
|
||||
|
||||
public CPacket(){
|
||||
minPacket=new MINPacket();
|
||||
ttl=new TTL();
|
||||
payload=new Payload();
|
||||
public CPacket() {
|
||||
minPacket = new MINPacket();
|
||||
ttl = new TTL();
|
||||
payload = new Payload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CPacket 的各项属性填充到 MINPacket 中定义的对应分区当中
|
||||
* @Description:
|
||||
* // 1. 主要填充标识区、只读区和可变区
|
||||
*
|
||||
* @return
|
||||
* @Description: // 1. 主要填充标识区、只读区和可变区
|
||||
*/
|
||||
@Override
|
||||
public boolean fillDataToFields() throws PacketException {
|
||||
// 可变区 => 受保护区(无)
|
||||
|
||||
// 填充可变区 => 非受保护区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
|
||||
Block block= null;
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.ttl);
|
||||
// 可变区 => 受保护区(无)
|
||||
|
||||
// 填充可变区 => 非受保护区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
|
||||
Block block = null;
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.ttl);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("CPacket.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.mutableField.mutableDangerousField.addBlock(block);
|
||||
|
||||
// 填充只读区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.readOnlyField.clearBlocks();
|
||||
block = new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
|
||||
// 填充标识区
|
||||
// 首先清除所有的标识
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper =
|
||||
new IdentifierWrapper().createCommonIdentifierByString(this.flatIdentifier);
|
||||
if (identifierWrapper == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
|
||||
return true;
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
|
||||
throw new PacketException("CPacket.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.mutableField.mutableDangerousField.addBlock(block);
|
||||
|
||||
// 填充只读区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.readOnlyField.clearBlocks();
|
||||
try {
|
||||
block=new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
|
||||
// 填充标识区
|
||||
// 首先清除所有的标识
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper=
|
||||
new IdentifierWrapper().createCommonIdentifierByString(this.flatIdentifier);
|
||||
if(identifierWrapper==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 MINPacket 的分区当中提取出 CPacket 的各项属性
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean extraDataFromFields() throws PacketException {
|
||||
// 提取 Payload
|
||||
Block payloadBlock=this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
if(payloadBlock==null){
|
||||
return false;
|
||||
}
|
||||
if(this.payload.wireDecode(payloadBlock)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取 TTL
|
||||
Block ttlBlock=this.minPacket.mutableField.mutableDangerousField.getBlockByType(new VlInt(TLV.TlvTTL));
|
||||
if(ttlBlock==null){
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if(this.ttl.wireDecode(ttlBlock)){
|
||||
// 提取 Payload
|
||||
Block payloadBlock = null;
|
||||
payloadBlock = this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
|
||||
if (payloadBlock == null) {
|
||||
return false;
|
||||
}
|
||||
if (this.payload.wireDecode(payloadBlock)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取 TTL
|
||||
Block ttlBlock = this.minPacket.mutableField.mutableDangerousField.getBlockByType(new VlInt(TLV.TlvTTL));
|
||||
if (ttlBlock == null) {
|
||||
return false;
|
||||
}
|
||||
if (this.ttl.wireDecode(ttlBlock)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取标识
|
||||
IdentifierWrapper commonIdentifier = this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierCommon));
|
||||
if (commonIdentifier == null) {
|
||||
return false;
|
||||
}
|
||||
String identifierString = commonIdentifier.toUri();
|
||||
if ((identifierString.length() > 0) && (identifierString.startsWith("/"))) {
|
||||
this.flatIdentifier = identifierString.substring(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("CPacket.extraDataFromFields: "+e.getMessage());
|
||||
throw new PacketException("CPacket.extraDataFromFields: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 提取标识
|
||||
IdentifierWrapper commonIdentifier=this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierCommon));
|
||||
if(commonIdentifier==null){
|
||||
return false;
|
||||
}
|
||||
String identifierString=commonIdentifier.toUri();
|
||||
if((identifierString.length()>0)&&(identifierString.startsWith("/"))){
|
||||
this.flatIdentifier=identifierString.substring(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CPacket 编码成 TLV
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws PacketException {
|
||||
// 首先将属性填充到分区当中
|
||||
if(!this.fillDataToFields()){
|
||||
if (!this.fillDataToFields()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 指定包类型
|
||||
this.minPacket.packetType=new VlInt(TLV.TlvPacketMINCommon);
|
||||
this.minPacket.packetType = new VlInt(TLV.TlvPacketMINCommon);
|
||||
|
||||
return this.minPacket.wireEncode(encoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 CPacket
|
||||
*
|
||||
* @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();
|
||||
@@ -169,30 +174,34 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
|
||||
/**
|
||||
* 展示平面标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
public String toUri() {
|
||||
return this.flatIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推式包的平面标识,eg: asdfsvnjweivnjsdcs
|
||||
*
|
||||
* @param identifier
|
||||
*/
|
||||
public void setIdentifier(String identifier){
|
||||
this.flatIdentifier=identifier;
|
||||
public void setIdentifier(String identifier) {
|
||||
this.flatIdentifier = identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推式包的平面标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getIdentifier(){
|
||||
public String getIdentifier() {
|
||||
return this.flatIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到 TTL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TTL getTtl() {
|
||||
@@ -201,6 +210,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
|
||||
/**
|
||||
* 设置 TTL
|
||||
*
|
||||
* @param ttl
|
||||
*/
|
||||
public void setTtl(TTL ttl) {
|
||||
|
||||
+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();
|
||||
|
||||
@@ -35,7 +35,7 @@ import encoding.*;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
public class Interest implements InteractWithField,IEncodingAble{
|
||||
public class Interest implements InteractWithField, IEncodingAble {
|
||||
private MINPacket minPacket;
|
||||
private CanBePrefix canBePrefix;
|
||||
private MustBeRefresh mustBeRefresh;
|
||||
@@ -48,59 +48,66 @@ public class Interest implements InteractWithField,IEncodingAble{
|
||||
|
||||
/**
|
||||
* 获取内容兴趣包的名字
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Identifier getName(){
|
||||
public Identifier getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置内容兴趣包的名字
|
||||
*
|
||||
* @param identifier
|
||||
*/
|
||||
public void setName(Identifier identifier){
|
||||
this.name=identifier;
|
||||
public void setName(Identifier identifier) {
|
||||
this.name = identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个指定的名字和当前的兴趣包的名字是否匹配
|
||||
*
|
||||
* @param identifier
|
||||
* @return
|
||||
*/
|
||||
public boolean matchesName(Identifier identifier){
|
||||
public boolean matchesName(Identifier identifier) {
|
||||
throw new Error("implement me");
|
||||
// return identifier.toUri().equals(this.name.toUri());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个内容兴趣包和一个内容数据包是否匹配
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
public boolean matchesData(Data data){
|
||||
public boolean matchesData(Data data) {
|
||||
throw new Error("implement me");
|
||||
// return data.toUri().equals(this.)
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断一个内容兴趣包和当前兴趣包是否匹配
|
||||
*
|
||||
* @param interest
|
||||
* @return
|
||||
*/
|
||||
public boolean matchesInterest(Interest interest){
|
||||
public boolean matchesInterest(Interest interest) {
|
||||
throw new Error("implement me");
|
||||
}
|
||||
|
||||
/**
|
||||
* 展示兴趣包的 URI
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
public String toUri() {
|
||||
return this.name.toUri();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Interest 的各项属性填充到 MINPacket 中定义的对应分区当中
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@@ -196,143 +203,133 @@ public class Interest implements InteractWithField,IEncodingAble{
|
||||
|
||||
|
||||
return true;
|
||||
}catch (EncoderException | BlockException | ComponentException e){
|
||||
throw new PacketException("Interest.fillDataToFields: "+e.getMessage());
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("Interest.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 MINPacket 的分区中提取出 Interest 的各项属性
|
||||
*
|
||||
* @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("Interest.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析只读区
|
||||
// { => 只读区
|
||||
// [CanBePrefix]
|
||||
// [MustBeRefresh]
|
||||
// [InterestLifeTime]
|
||||
// [Nonce]
|
||||
// [HopLimit]
|
||||
// <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.TlvCanBePrefix:
|
||||
if(!this.canBePrefix.wireDecode(newblock)){
|
||||
return false;
|
||||
}
|
||||
case TLV.TlvMustBeRefresh:
|
||||
if(!this.mustBeRefresh.wireDecode(newblock)){
|
||||
return false;
|
||||
}
|
||||
case TLV.TlvInterestLifeTime:
|
||||
try {
|
||||
if(!this.interestLifeTime.wireDecode(newblock)){
|
||||
/////////////////////////////////////////////////////////////
|
||||
//// 解析只读区
|
||||
// { => 只读区
|
||||
// [CanBePrefix]
|
||||
// [MustBeRefresh]
|
||||
// [InterestLifeTime]
|
||||
// [Nonce]
|
||||
// [HopLimit]
|
||||
// <Payload>
|
||||
// }
|
||||
/////////////////////////////////////////////////////////////
|
||||
ElementContainer elementContainer = this.minPacket.readOnlyField.getBlocks();
|
||||
int len = elementContainer.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Block newblock = null;
|
||||
newblock = elementContainer.getBlock(i);
|
||||
int type;
|
||||
type = newblock.getType().getVlIntValue2Int();
|
||||
switch (type) {
|
||||
case TLV.TlvCanBePrefix:
|
||||
if (!this.canBePrefix.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
case TLV.TlvNonce:
|
||||
try {
|
||||
if(!this.nonce.wireDecode(newblock)){
|
||||
case TLV.TlvMustBeRefresh:
|
||||
if (!this.mustBeRefresh.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
case TLV.TlvHopLimit:
|
||||
try {
|
||||
if(!this.hopLimit.wireDecode(newblock)){
|
||||
case TLV.TlvInterestLifeTime:
|
||||
if (!this.interestLifeTime.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
|
||||
}
|
||||
case TLV.TlvPayload:
|
||||
if(!this.payload.wireDecode(newblock)){
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
// TODO: 这边可以规定哪些范围是自定义的,哪些是预留的,然后根据需要这边抛出错误
|
||||
case TLV.TlvNonce:
|
||||
if (!this.nonce.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
case TLV.TlvHopLimit:
|
||||
if (!this.hopLimit.wireDecode(newblock)) {
|
||||
return false;
|
||||
}
|
||||
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 | BlockException | VlIntException e) {
|
||||
throw new PacketException("Interest.extraDataFromFields: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将 Interest 线速编码为一个 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 中解码出一个 Interest
|
||||
*
|
||||
* @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();
|
||||
|
||||
+111
-145
@@ -19,95 +19,78 @@ public class MINPacket implements IEncodingAble {
|
||||
|
||||
/**
|
||||
* 判断 MIN 包是否是一个有效的包
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isPacketValid(){
|
||||
public boolean isPacketValid() {
|
||||
return TLV.isValidPacketType(packetType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将 Packet 线速编码为 TLV
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws PacketException {
|
||||
if(!this.isPacketValid()){
|
||||
return -1;
|
||||
}
|
||||
int totalLength=0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
|
||||
// 可变区
|
||||
int tmplen= 0;
|
||||
try {
|
||||
if (!this.isPacketValid()) {
|
||||
return -1;
|
||||
}
|
||||
int totalLength = 0;
|
||||
|
||||
// 编码 TLV-VALUE
|
||||
|
||||
// 可变区
|
||||
int tmplen = 0;
|
||||
tmplen = this.mutableField.wireEncode(encoder);
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 只读区
|
||||
try {
|
||||
tmplen=this.readOnlyField.wireEncode(encoder);
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
// 只读区
|
||||
tmplen = this.readOnlyField.wireEncode(encoder);
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 签名区
|
||||
try {
|
||||
tmplen=this.signatureField.wireEncode(encoder);
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
// 签名区
|
||||
tmplen = this.signatureField.wireEncode(encoder);
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 标识区
|
||||
try {
|
||||
tmplen=this.identifierField.wireEncode(encoder);
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
// 标识区
|
||||
tmplen = this.identifierField.wireEncode(encoder);
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码包 TLV-LENGTH
|
||||
try {
|
||||
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
|
||||
} catch (EncoderException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
// 编码包 TLV-LENGTH
|
||||
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
// 编码包 TLV-TYPE
|
||||
try {
|
||||
tmplen=encoder.prependVarNumber(this.packetType);
|
||||
} catch (EncoderException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
|
||||
}
|
||||
if(tmplen<=0){
|
||||
return -1;
|
||||
}
|
||||
totalLength+=tmplen;
|
||||
// 编码包 TLV-TYPE
|
||||
tmplen = encoder.prependVarNumber(this.packetType);
|
||||
|
||||
return totalLength;
|
||||
if (tmplen <= 0) {
|
||||
return -1;
|
||||
}
|
||||
totalLength += tmplen;
|
||||
|
||||
return totalLength;
|
||||
} catch (EncoderException | ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireEncode: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,100 +99,83 @@ public class MINPacket implements IEncodingAble {
|
||||
* // 1. 解析出四个区的TLV;
|
||||
* // 2. 检查必要的TLV是否存在,不存在则抛出异常,以下为必须条件:
|
||||
* // a. 必须包含标识区,且标识区至少包含一个标识
|
||||
*
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) throws PacketException {
|
||||
// 首先判断包是否有效
|
||||
if(!block.isValid()){
|
||||
return false;
|
||||
}
|
||||
// 首先解析子 Block
|
||||
try {
|
||||
if(!block.parseSubElements()){
|
||||
// 首先判断包是否有效
|
||||
if (!block.isValid()) {
|
||||
return false;
|
||||
}
|
||||
// 首先解析子 Block
|
||||
if (!block.parseSubElements()) {
|
||||
return false;
|
||||
}
|
||||
} catch (BlockException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
|
||||
// 标记,用来记录是否满足必要条件
|
||||
boolean existIdentifier=false;
|
||||
|
||||
// 解析四个标识区
|
||||
int elementSize=block.getSubElements().length();
|
||||
for (int i=0;i<elementSize;i++) {
|
||||
Block cacheBlock=block.getSubElements().getBlock(i);
|
||||
int judgeType= 0;
|
||||
try {
|
||||
// 标记,用来记录是否满足必要条件
|
||||
boolean existIdentifier = false;
|
||||
|
||||
// 解析四个标识区
|
||||
int elementSize = block.getSubElements().length();
|
||||
for (int i = 0; i < elementSize; i++) {
|
||||
Block cacheBlock = null;
|
||||
try {
|
||||
cacheBlock = block.getSubElements().getBlock(i);
|
||||
} catch (BlockException e) {
|
||||
throw new Error("MINPacket.wireDecode: " + e.getMessage());
|
||||
}
|
||||
int judgeType = 0;
|
||||
judgeType = cacheBlock.getType().getVlIntValue2Int();
|
||||
} catch (VlIntException e) {
|
||||
throw new Error("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
switch (judgeType){
|
||||
case TLV.TlvIdentifierField: // 目的标识区
|
||||
// 解析标识区,保证其中至少存在一个标识
|
||||
try {
|
||||
if(!cacheBlock.parseSubElements()){
|
||||
switch (judgeType) {
|
||||
case TLV.TlvIdentifierField: // 目的标识区
|
||||
// 解析标识区,保证其中至少存在一个标识
|
||||
if (!cacheBlock.parseSubElements()) {
|
||||
return false;
|
||||
}
|
||||
} catch (BlockException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
if(!cacheBlock.hasSubElement()){
|
||||
if (!cacheBlock.hasSubElement()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析标识区
|
||||
if (!this.identifierField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 标示解析到了至少一个以上的合法标识
|
||||
existIdentifier = true;
|
||||
case TLV.TlvSignatureField: // 签名区
|
||||
// 解析签名区,保证如果存在签名,则签名均符合格式
|
||||
if (!this.signatureField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
case TLV.TlvReadOnlyField: // 只读区
|
||||
// 解析只读区
|
||||
if (!this.readOnlyField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
case TLV.TlvMutableField: // 可变区
|
||||
// 解析可变区
|
||||
if (!this.mutableField.wireDecode(cacheBlock)) {
|
||||
return false;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
// 解析标识区
|
||||
try {
|
||||
if(!this.identifierField.wireDecode(cacheBlock)){
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
|
||||
// 标示解析到了至少一个以上的合法标识
|
||||
existIdentifier=true;
|
||||
case TLV.TlvSignatureField: // 签名区
|
||||
// 解析签名区,保证如果存在签名,则签名均符合格式
|
||||
try {
|
||||
if(!this.signatureField.wireDecode(cacheBlock)){
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
case TLV.TlvReadOnlyField: // 只读区
|
||||
// 解析只读区
|
||||
try {
|
||||
if(!this.readOnlyField.wireDecode(cacheBlock)){
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
case TLV.TlvMutableField: // 可变区
|
||||
// 解析可变区
|
||||
try {
|
||||
if(!this.mutableField.wireDecode(cacheBlock)){
|
||||
return false;
|
||||
}
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 判断是否有必须的组件缺失
|
||||
if(!existIdentifier){
|
||||
return false;
|
||||
}
|
||||
// 判断是否有必须的组件缺失
|
||||
if (!existIdentifier) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
} catch (BlockException | VlIntException | ComponentException e) {
|
||||
throw new PacketException("MINPacket.wireDecode: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user