finished component: Payload

This commit is contained in:
free will
2021-03-09 19:45:26 +08:00
parent 472f3585c6
commit 4c4e466673
+107
View File
@@ -0,0 +1,107 @@
package component;
import encoding.*;
/*
* @Author: Wang Feng
* @Description: 表示网络包负载
// 1. 格式如下:
// Payload = PAYLOAD-TYPE TLV-LENGTH *OCTET
// 2. 详情参见:http://gitea.qjm253.cn/PKUSZ-future-network-lab/minlib/src/branch/master/docs/PacketFormat.md#4-signature
* @Version: 1.0.0
* @Date: 19:26 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
// 成员变量
private byte[] value;
// 函数
/**
* 根据 TLV Block 初始化一个 Payload
* @param block
* @return
*/
public boolean buildPayloadByBlock(Block block){
return this.wireDecode(block);
}
/**
* 得到Payload TLV中实际包含的数据
* @return
*/
public byte[] getValue() {
return value;
}
/**
* 设置 Payload TLV 中的Value
* @param value
*/
public void setValue(byte[] value) {
this.value = value;
this.doInitial();
}
// 接口实现-变量区
private boolean initial=false;
// 接口实现-方法区
@Override
public void doInitial() {
initial=true;
}
@Override
public boolean isInitial() {
return initial;
}
/**
* 将 Payload 线速编码为一个 TLV
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen=encoder.prependByteArray(this.value,new SizeT(this.value.length));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
if(tmplen==0){
return 0;
}
totalLength+=tmplen;
return totalLength;
}
@Override
public boolean wireDecode(Block block) {
// 检查 Type 是否正确
if(!TLV.expectType(block.getTlvType(),new VlInt(TLV.TlvPayload))){
return false;
}
// 存储Value
// int len=block.getLength().getVlIntValue2Int();
// byte[] bytes=new byte[len];
// System.arraycopy(block.getValue(),0,bytes,0,len);
this.setValue(block.getValue());
return true;
}
}