Files
min-dev-java/src/main/java/packet/Interest.java
T

320 lines
10 KiB
Java

package packet;
import component.*;
import encoding.*;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 11:22 2021/3/16
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
//
// 表示一个通用的内容兴趣包
//
// @Description:
// 1. 包格式如下:
// Interest = 5 TLV-LENGTH
// { InterestIdentifier } => 标识区
// { Signature } => 签名区
// { => 只读区
// [CanBePrefix]
// [MustBeRefresh]
// [InterestLifeTime]
// [Nonce]
// [HopLimit]
// <Payload>
// }
// { => 可变区
// { => 受保护区
// [CongestionMark]
// }
// { => 非受保护区
// "Empty"
// }
// }
//
public class Interest implements InteractWithField,IEncodingAble{
private MINPacket minPacket;
private CanBePrefix canBePrefix;
private MustBeRefresh mustBeRefresh;
private InterestLifeTime interestLifeTime;
private Nonce nonce;
private HopLimit hopLimit;
private Payload payload;
private CongestionMark congestionMark;
private Identifier name;
/**
* 获取内容兴趣包的名字
* @return
*/
public Identifier getName(){
return this.name;
}
/**
* 设置内容兴趣包的名字
* @param identifier
*/
public void setName(Identifier identifier){
this.name=identifier;
}
/**
* 判断一个指定的名字和当前的兴趣包的名字是否匹配
* @param identifier
* @return
*/
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){
throw new Error("implement me");
// return data.toUri().equals(this.)
}
/**
* 判断一个内容兴趣包和当前兴趣包是否匹配
* @param interest
* @return
*/
public boolean matchesInterest(Interest interest){
throw new Error("implement me");
}
/**
* 展示兴趣包的 URI
* @return
*/
public String toUri(){
return this.name.toUri();
}
/**
* 将 Interest 的各项属性填充到 MINPacket 中定义的对应分区当中
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
// 填充可变受保护区
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// CongestionMark
Block block=new SelfEncodingBase().selfWireEncode(this.congestionMark);
if(block==null){
return false;
}
this.minPacket.mutableField.mutableProtectField.addBlock(block);
// 填充可变非受保护区(无)
/////////////////////////////////////////////////////////////
//// 填充只读区
/////////////////////////////////////////////////////////////
this.minPacket.readOnlyField.clearBlocks();
// CanBePrefix
if(this.canBePrefix.getCanBePrefix()){
block=new SelfEncodingBase().selfWireEncode(this.canBePrefix);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// MustBeRefresh
if(this.mustBeRefresh.getMustBeRefresh()){
block=new SelfEncodingBase().selfWireEncode(this.mustBeRefresh);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// InterestLifeTime
if(!this.interestLifeTime.isInitial()){
// 默认生存期为4s
this.interestLifeTime.setInterestLifeTime(4000);
}
block=new SelfEncodingBase().selfWireEncode(this.interestLifeTime);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
// Nonce
if(!this.nonce.isInitial()){
// 如果没有指定随机数,随机生成一个随机数
this.nonce.refreshNonce();
}
block=new SelfEncodingBase().selfWireEncode(this.nonce);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
// HopLimit
if(this.hopLimit.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.hopLimit);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// Payload
block=new SelfEncodingBase().selfWireEncode(this.payload);
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 的分区中提取出 Interest 的各项属性
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
// { => 受保护区
// [CongestionMark]
// }
// { => 非受保护区
// "Empty"
// }
// }
/////////////////////////////////////////////////////////////
Block block=this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
if(block!=null){
if(!this.congestionMark.wireDecode(block)){
return false;
}
}
/////////////////////////////////////////////////////////////
//// 解析只读区
// { => 只读区
// [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:
if(!this.interestLifeTime.wireDecode(newblock)){
return false;
}
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());
return true;
}
/**
* 将 Interest 线速编码为一个 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
// 首先将各项属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
}
// 指定包类型
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 VlIntException {
if(!this.minPacket.wireDecode(block)){
return false;
}
return this.extraDataFromFields();
}
}