add test: ReadOnlyFieldTest

This commit is contained in:
free will committed 2021-03-25 17:41:13 +08:00
1 parent 0a7ccc95a3
commit 8a2da10583
19 files changed
+1251 -966

No files matched your search

+94 -67
View File
@@ -17,51 +17,55 @@ import java.util.List;
* @Date: 17:34 2021/3/10
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class IdentifierComponentContainer{
class IdentifierComponentContainer {
private List<IdentifierComponent> identifierComponents;
public IdentifierComponentContainer(){
identifierComponents=new LinkedList<>();
public IdentifierComponentContainer() {
identifierComponents = new LinkedList<>();
}
/**
* 获取所有名称组件
*
* @return
*/
public List<IdentifierComponent> getElements(){
public List<IdentifierComponent> getElements() {
return identifierComponents;
}
/**
* 组件个数
*
* @return
*/
public int length(){
public int length() {
return identifierComponents.size();
}
/**
* 清空组件
*/
public void clear(){
public void clear() {
identifierComponents.clear();
}
/**
* 添加一个组件
*
* @param identifierComponent
*/
public void addElement(IdentifierComponent identifierComponent){
public void addElement(IdentifierComponent identifierComponent) {
identifierComponents.add(identifierComponent);
}
/**
* 获取指定索引位置的组件
*
* @param index
* @return
*/
public IdentifierComponent getElement(int index){
if(index>=this.length()){
public IdentifierComponent getElement(int index) {
if (index >= this.length()) {
return null;
}
return this.identifierComponents.get(index);
@@ -69,27 +73,28 @@ class IdentifierComponentContainer{
/**
* 获取前 num 个名称组件构成的标识
*
* @param num
* @return
*/
public IdentifierComponentContainer getPrefix(int num){
if(num>this.length()){
public IdentifierComponentContainer getPrefix(int num) {
if (num > this.length()) {
return null;
}
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
IdentifierComponentContainer identifierComponentContainer = new IdentifierComponentContainer();
for (int i = 0; i < num; i++) {
identifierComponentContainer.addElement(this.getElement(i));
}
return identifierComponentContainer;
}
public IdentifierComponentContainer getSubIdentifierContainer(int startIndex,int num){
if((startIndex+num)>this.length()){
public IdentifierComponentContainer getSubIdentifierContainer(int startIndex, int num) {
if ((startIndex + num) > this.length()) {
return null;
}
IdentifierComponentContainer identifierComponentContainer=new IdentifierComponentContainer();
for (int i = startIndex; i < startIndex+num; i++) {
IdentifierComponentContainer identifierComponentContainer = new IdentifierComponentContainer();
for (int i = startIndex; i < startIndex + num; i++) {
identifierComponentContainer.addElement(this.getElement(i));
}
return identifierComponentContainer;
@@ -99,8 +104,13 @@ class IdentifierComponentContainer{
public class Identifier implements TlvComponentBase, IEncodingAble {
private IdentifierComponentContainer components;
public Identifier() {
components = new IdentifierComponentContainer();
}
/**
* 根据一个 TLV Block 创建一个Identifier
*
* @param block
*/
public Identifier(Block block) throws ComponentException {
@@ -109,35 +119,38 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
/**
* 根据标识的字符串标识创建一个标识
*
* @param identifierString
*/
public Identifier(String identifierString){
public Identifier(String identifierString) {
this.buildIdentifierByString(identifierString);
}
/**
* 根据components构造一个Identifier
*
* @param container
*/
public Identifier(IdentifierComponentContainer container){
public Identifier(IdentifierComponentContainer container) {
this.buildIdentifierByComponents(container);
}
/**
* 根据一个 TLV Block 初始化当前Identifier
*
* @param block
*/
public boolean buildIdentifierByBlock(Block block) throws ComponentException {
// 如果不存在子 TLV, 则不可能是一个 Identifier
try {
if(!block.parseSubElements()){
if (!block.parseSubElements()) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("Identifier.buildIdentifierByBlock: "+e.getMessage());
throw new ComponentException("Identifier.buildIdentifierByBlock: " + e.getMessage());
}
if(block.getSubElements().length()<1){
if (block.getSubElements().length() < 1) {
return false;
}
return this.wireDecode(block);
@@ -145,15 +158,16 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
/**
* 根据标识字符串初始化当前Identifier
*
* @param identifierString
* @return
*/
public boolean buildIdentifierByString(String identifierString){
if((identifierString.length()<=0)||(!identifierString.startsWith("/"))){
public boolean buildIdentifierByString(String identifierString) {
if ((identifierString.length() <= 0) || (!identifierString.startsWith("/"))) {
return false;
}
String[] componentStrings=identifierString.split("/");
String[] componentStrings = identifierString.split("/");
this.components.clear();
for (int i = 1; i < componentStrings.length; i++) {
this.components.addElement(new IdentifierComponent(componentStrings[i]));
@@ -163,11 +177,12 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
/**
* 根据 components 初始化当前 Identifier
*
* @param container
* @return
*/
public boolean buildIdentifierByComponents(IdentifierComponentContainer container){
if(container.length()<=0){
public boolean buildIdentifierByComponents(IdentifierComponentContainer container) {
if (container.length() <= 0) {
return false;
}
@@ -184,146 +199,158 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
* // 1. 需要注意,本函数只对 Identifier 进行编码,不包含外层区分不同标识的Type,编码的格式如下:
* // Identifier = 101 TLV-LENGTH
* // *IdentifierComponent
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
int totalLength = 0;
// 编码 TLV-VALUE
for (int i = 0; i < components.length(); i++) {
int tmpLen= 0;
int tmpLen = 0;
tmpLen = components.getElement(i).wireEncode(encoder);
if(tmpLen<=0){
if (tmpLen <= 0) {
return -1;
}
totalLength+=tmpLen;
totalLength += tmpLen;
}
// 编码 TLV-LENGTH
int tmpLen= 0;
int tmpLen = 0;
try {
tmpLen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("Identifier.wireEncode: "+e.getMessage());
throw new ComponentException("Identifier.wireEncode: " + e.getMessage());
}
if(tmpLen<=0){
if (tmpLen <= 0) {
return -1;
}
totalLength+=tmpLen;
totalLength += tmpLen;
// 编码 TLV-TYPE
try {
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
tmpLen = encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
} catch (EncoderException e) {
throw new ComponentException("Identifier.wireEncode: "+e.getMessage());
throw new ComponentException("Identifier.wireEncode: " + e.getMessage());
}
if(tmpLen<=0){
if (tmpLen <= 0) {
return -1;
}
totalLength+=tmpLen;
totalLength += tmpLen;
return totalLength;
}
/**
* 将 TLV Block 进行线速解码为一个 Identifier
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifier))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvIdentifier))) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("Identifier.wireDecode: "+e.getMessage());
}
this.components.clear();
ElementContainer elementContainer=block.getSubElements();
for (int i = 0; i < elementContainer.length(); i++) {
this.components.addElement(new IdentifierComponent(elementContainer.getBlock(i)));
}
return true;
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
this.components.clear();
ElementContainer elementContainer = block.getSubElements();
for (int i = 0; i < elementContainer.length(); i++) {
this.components.addElement(new IdentifierComponent(elementContainer.getBlock(i)));
}
return true;
} catch (BlockException e) {
throw new ComponentException("Identifier.wireDecode: " + e.getMessage());
}
}
/**
* 将Identifier表示成Uri字符串
* // @Description:
* // 1. https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme
*
* @return
*/
public String toUri(){
String res="";
public String toUri() {
String res = "";
for (int i = 0; i < components.length(); i++) {
res+=("/"+components.getElement(i).toUri());
res += ("/" + components.getElement(i).toUri());
}
return res;
}
/**
* 判断是否名称组件数量为空
*
* @return
*/
public boolean empty(){
return components.length()==0;
public boolean empty() {
return components.length() == 0;
}
/**
* 获取本标识包含的名称组件数量
*
* @return
*/
public int size(){
public int size() {
return components.length();
}
/**
* 获取本标识包含的第 index 个名称组件
*
* @param index
* @return
*/
public IdentifierComponent get(int index){
public IdentifierComponent get(int index) {
return this.components.getElement(index);
}
/**
* 获取标识中所有的名称组件
*
* @return
*/
public IdentifierComponentContainer getComponents(){
public IdentifierComponentContainer getComponents() {
return this.components;
}
/**
* 获取前 num 个名称组件构成的标识
*
* @param num
* @return
*/
public IdentifierComponentContainer getPrefix(int num){
public IdentifierComponentContainer getPrefix(int num) {
return this.components.getPrefix(num);
}
/**
* 返回从第 startIndex 个组件开始往后的 num 个组件组成的新标识
*
* @return
*/
public Identifier getSubIdentifier(int startIndex,int num){
return new Identifier(this.components.getSubIdentifierContainer(startIndex,num));
public Identifier getSubIdentifier(int startIndex, int num) {
return new Identifier(this.components.getSubIdentifierContainer(startIndex, num));
}
/**
* 判断当前标识是否有效
*
* @return
*/
public boolean isValid(){
return this.components.length()>0;
public boolean isValid() {
return this.components.length() > 0;
}
}
@@ -34,145 +34,157 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
private long intValue; // 如果 Marker == 0x01,则本组件存储一个非负整型
private byte[] byteArrayValue; // 如果 Marker == 0x02,则本组件存储一个字节数组
private static final byte MarkerString = 0x00;
private static final byte MarkerString = 0x00;
private static final byte MarkerNonNegativeInteger = 0x01;
private static final byte MarkerByteArray = 0x02;
private static final byte MarkerByteArray = 0x02;
/**
* 用于判断某个字节是否在已分配的Marker列表当中
*
* @param marker
* @return
*/
private boolean isInMarker(byte marker){
if((marker==MarkerString)||(marker==MarkerNonNegativeInteger)||(marker==MarkerByteArray)){
private boolean isInMarker(byte marker) {
if ((marker == MarkerString) || (marker == MarkerNonNegativeInteger) || (marker == MarkerByteArray)) {
return true;
}else{
} else {
return false;
}
}
/**
* 根据一个字符串构造一个 IdentifierComponent
*
* @param nameComponent
*/
public IdentifierComponent(String nameComponent){
this.identifierBlock=new Block();
public IdentifierComponent(String nameComponent) {
this.identifierBlock = new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(nameComponent.length()+1));
byte[] value= new byte[0];
this.identifierBlock.setLength(new VlInt(nameComponent.length() + 1));
byte[] value = new byte[0];
try {
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
} catch (VlIntException e) {
e.printStackTrace();
}
value[0]=MarkerString;
System.arraycopy(nameComponent.getBytes(),0,value,1,nameComponent.getBytes().length);
value[0] = MarkerString;
System.arraycopy(nameComponent.getBytes(), 0, value, 1, nameComponent.getBytes().length);
this.identifierBlock.setValue(value);
this.marker=MarkerString;
this.stringValue=nameComponent;
this.marker = MarkerString;
this.stringValue = nameComponent;
}
/**
* 根据一个非负整数构造一个 IdentifierComponent
*
* @param intValue
*/
public IdentifierComponent(long intValue){
this.identifierBlock=new Block();
public IdentifierComponent(long intValue) {
this.identifierBlock = new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(new VlInt(intValue).size()+1));
byte[] value= new byte[0];
this.identifierBlock.setLength(new VlInt(new VlInt(intValue).size() + 1));
byte[] value = new byte[0];
try {
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
} catch (VlIntException e) {
e.printStackTrace();
}
value[0]=MarkerNonNegativeInteger;
System.arraycopy(new VlInt(intValue).getVlIntBytes(),0,value,1,new VlInt(intValue).size());
value[0] = MarkerNonNegativeInteger;
System.arraycopy(new VlInt(intValue).getVlIntBytes(), 0, value, 1, new VlInt(intValue).size());
this.identifierBlock.setValue(value);
this.marker=MarkerNonNegativeInteger;
this.intValue=intValue;
this.marker = MarkerNonNegativeInteger;
this.intValue = intValue;
}
/**
* 根据一个字节数组构造一个 IdentifierComponent
*
* @param arr
*/
public IdentifierComponent(byte[] arr){
this.identifierBlock=new Block();
public IdentifierComponent(byte[] arr) {
this.identifierBlock = new Block();
this.identifierBlock.setType(new VlInt(TLV.TlvIdentifierComponent));
this.identifierBlock.setLength(new VlInt(arr.length+1));
byte[] value= new byte[0];
this.identifierBlock.setLength(new VlInt(arr.length + 1));
byte[] value = new byte[0];
try {
value = new byte[this.identifierBlock.getLength().getVlIntValue2Int()];
} catch (VlIntException e) {
e.printStackTrace();
}
value[0]=MarkerByteArray;
System.arraycopy(arr,0,value,1,arr.length);
value[0] = MarkerByteArray;
System.arraycopy(arr, 0, value, 1, arr.length);
this.identifierBlock.setValue(value);
this.marker=MarkerByteArray;
this.byteArrayValue=arr;
this.marker = MarkerByteArray;
this.byteArrayValue = arr;
}
/**
* 根据一个 TLV Block 创建一个 IdentifierComponent
*
* @param block
* @return
*/
public IdentifierComponent(Block block) throws ComponentException {
public IdentifierComponent(Block block) throws ComponentException {
this.wireDecode(block);
}
/**
* 以字符串形式获取本 IdentifierComponent 的值
*
* @return
*/
public String getString(){
public String getString() {
return this.stringValue;
}
/**
* 以非负整数的形式获取本 IdentifierComponent 的值
*
* @return
*/
public long getNonNegativeInteger(){
public long getNonNegativeInteger() {
return this.intValue;
}
/**
* 以字节数组的形式获取本 IdentifierComponent 的值
*
* @return
*/
public byte[] getByteArray(){
public byte[] getByteArray() {
return this.byteArrayValue;
}
/**
* 判断当前 IdentifierComponent 是否是存储字符串的类型
*
* @return
*/
public boolean isString(){
return this.marker==MarkerString;
public boolean isString() {
return this.marker == MarkerString;
}
/**
* 判断当前 IdentifierComponent 是否是存储非负整数的类型
*
* @return
*/
public boolean isNonNegativeInteger(){
return this.marker==MarkerNonNegativeInteger;
public boolean isNonNegativeInteger() {
return this.marker == MarkerNonNegativeInteger;
}
/**
* 判断当前 IdentifierComponent 是否是存储字节数组的类型
*
* @return
*/
public boolean isByteArray(){
return this.marker==MarkerByteArray;
public boolean isByteArray() {
return this.marker == MarkerByteArray;
}
/**
* 线速编码,将 IdentifierComponent 编码成网络中传输的字节数组格式
*
* @param encoder
* @return
*/
@@ -183,71 +195,66 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
/**
* 线速解码,将TLV Block解码成 IdentifierComponent
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
this.identifierBlock=new Block(block);
//todo:越界问题
int length= 0;
try {
this.identifierBlock = new Block(block);
//todo:越界问题
int length = 0;
length = this.identifierBlock.getLength().getVlIntValue2Int();
} catch (VlIntException e) {
throw new ComponentException("IdentifierComponent.wireDecode: "+e.getMessage());
}
byte[] value=this.identifierBlock.getValue();
byte[] value = this.identifierBlock.getValue();
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
if(length<1){
return false;
}
// 判断Marker是否有效
if(!isInMarker(value[0])){
return false;
}
this.marker=value[0];
// 根据不同类型的Marker,给不同的成员赋值
byte[] v= new byte[0];
try {
v = ByteHelper.getTailBytes(value,1);
} catch (ByteHelperException e) {
e.printStackTrace();
}
if(isString()){
this.stringValue=new String(v);
}else if(isNonNegativeInteger()){
// 如果Marker指定是非负整型,则TLV-LENGTH的有效值为2、3、5、9
if((length!=2)&&(length!=3)&&(length!=5)&&(length!=9)){
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
if (length < 1) {
return false;
}
try {
this.intValue=TLV.readNonNegativeInteger(v,0,v.length);
} catch (TLVException e) {
throw new ComponentException("IdentifierComponent.wireDecode: "+e.getMessage());
}
}else{ // byte[]
this.byteArrayValue=v;
}
return true;
// 判断Marker是否有效
if (!isInMarker(value[0])) {
return false;
}
this.marker = value[0];
// 根据不同类型的Marker,给不同的成员赋值
byte[] v = new byte[0];
v = ByteHelper.getTailBytes(value, 1);
if (isString()) {
this.stringValue = new String(v);
} else if (isNonNegativeInteger()) {
// 如果Marker指定是非负整型,则TLV-LENGTH的有效值为2、3、5、9
if ((length != 2) && (length != 3) && (length != 5) && (length != 9)) {
return false;
}
this.intValue = TLV.readNonNegativeInteger(v, 0, v.length);
} else { // byte[]
this.byteArrayValue = v;
}
return true;
} catch (ByteHelperException | VlIntException | TLVException e) {
throw new ComponentException("IdentifierComponnet.wireDecode: " + e.getMessage());
}
}
/**
* 将 IdentifierComponent 使用百分号编码规则编码
*
* @return
*/
public String toUri(){
if(isString()){
public String toUri() {
if (isString()) {
return StringHelper.escape(this.stringValue);
}else if(isNonNegativeInteger()){
} else if (isNonNegativeInteger()) {
// return StringHelper.FormatUint(this.intValue,10);
// todo: 需要对long类型按照指定编码规则编码成字符串
return "";
}else{
} else {
return StringHelper.escape(new String(this.byteArrayValue));
}
}
+69 -58
View File
@@ -12,43 +12,46 @@ import java.util.List;
* @Date: 11:02 2021/3/12
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class IdentifierContainer{
class IdentifierContainer {
private List<IdentifierWrapper> identifierWrappers;
public IdentifierContainer(){
identifierWrappers=new LinkedList<>();
public IdentifierContainer() {
identifierWrappers = new LinkedList<>();
}
/**
* 组件个数
*
* @return
*/
public int length(){
return identifierWrappers.size();
public int length() {
return identifierWrappers.size();
}
/**
* 清空所有组件
*/
public void clear(){
public void clear() {
identifierWrappers.clear();
}
/**
* 增加一个组件
*
* @param identifierWrapper
*/
public void addElement(IdentifierWrapper identifierWrapper){
public void addElement(IdentifierWrapper identifierWrapper) {
identifierWrappers.add(identifierWrapper);
}
/**
* 获取指定索引位置的组件
*
* @param index
* @return
*/
public IdentifierWrapper getElement(int index){
if(index>=this.length()){
public IdentifierWrapper getElement(int index) {
if (index >= this.length()) {
return null;
}
return this.identifierWrappers.get(index);
@@ -56,163 +59,171 @@ class IdentifierContainer{
/**
* 获取标识区中第一个指定类型标识
*
* @param vlInt
* @return
*/
public IdentifierWrapper getElementByType(VlInt vlInt){
public IdentifierWrapper getElementByType(VlInt vlInt) {
for (int i = 0; i < identifierWrappers.size(); i++) {
if(identifierWrappers.get(i).getTlvType().isEqual(vlInt)){
if (identifierWrappers.get(i).getTlvType().isEqual(vlInt)) {
return identifierWrappers.get(i);
}
}
return null;
}
public void setElement(int index,IdentifierWrapper wrapper){
if(index>=this.length()){
public void setElement(int index, IdentifierWrapper wrapper) {
if (index >= this.length()) {
return;
}
this.identifierWrappers.set(index,wrapper);
this.identifierWrappers.set(index, wrapper);
}
}
public class IdentifierField implements TlvComponentBase, IEncodingAble {
private IdentifierContainer identifiers;
public IdentifierField(){
identifiers=new IdentifierContainer();
public IdentifierField() {
identifiers = new IdentifierContainer();
}
/**
* 添加一个标识
*
* @param wrapper
*/
public void addIdentifier(IdentifierWrapper wrapper){
public void addIdentifier(IdentifierWrapper wrapper) {
this.identifiers.addElement(wrapper);
}
/**
* 获取标识区中第一个指定类型标识
*
* @param tlvType
* @return
*/
public IdentifierWrapper getIdentifierByType(VlInt tlvType){
public IdentifierWrapper getIdentifierByType(VlInt tlvType) {
return this.identifiers.getElementByType(tlvType);
}
/**
* 获取指定位置的标识
*
* @param index
* @return
*/
public IdentifierWrapper getIdentifier(int index){
public IdentifierWrapper getIdentifier(int index) {
return this.identifiers.getElement(index);
}
/**
* 设置指定位置的标识
*
* @param index
* @param wrapper
*/
public void setIdentifier(int index,IdentifierWrapper wrapper){
this.identifiers.setElement(index,wrapper);
public void setIdentifier(int index, IdentifierWrapper wrapper) {
this.identifiers.setElement(index, wrapper);
}
/**
* 清空标识区所有的标识
*/
public void clearIdentifiers(){
public void clearIdentifiers() {
this.identifiers.clear();
}
/**
* 获取所有的标识
*
* @return
*/
public IdentifierContainer getIdentifiers(){
public IdentifierContainer getIdentifiers() {
return this.identifiers;
}
/**
* 将标识区线速编码为 TLV
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果标识区不存在标识,则是一个无效的MIN包,直接抛出错误
if(this.identifiers.length()==0){
if (this.identifiers.length() == 0) {
return 0;
}
int totalLength=0;
int totalLength = 0;
// 编码 TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.identifiers.length()-1; i >= 0; i--) {
int tmplen= 0;
for (int i = this.identifiers.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = this.identifiers.getElement(i).wireEncode(encoder);
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen= 0;
int tmplen = 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("IdentifierField.wireEncode: "+e.getMessage());
throw new ComponentException("IdentifierField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvIdentifierField));
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvIdentifierField));
} catch (EncoderException e) {
throw new ComponentException("IdentifierField.wireEncode: "+e.getMessage());
throw new ComponentException("IdentifierField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 IdentifierField
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 TLV-TYPE 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvIdentifierField))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 TLV-TYPE 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvIdentifierField))) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("IdentifierField.wireDecode: "+e.getMessage());
}
IdentifierContainer identifierContainer=new IdentifierContainer();
int len=block.getSubElements().length();
for (int i = 0; i < len; i++) {
IdentifierWrapper newWrapper=new IdentifierWrapper(block.getSubElements().getBlock(i));
if(newWrapper==null){
return false;
}
identifierContainer.addElement(newWrapper);
}
this.identifiers=identifierContainer;
return true;
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
IdentifierContainer identifierContainer = new IdentifierContainer();
int len = block.getSubElements().length();
for (int i = 0; i < len; i++) {
IdentifierWrapper newWrapper = new IdentifierWrapper(block.getSubElements().getBlock(i));
if (newWrapper == null) {
return false;
}
identifierContainer.addElement(newWrapper);
}
this.identifiers = identifierContainer;
return true;
} catch (BlockException e) {
throw new ComponentException("IdentifierField.wireDecode: " + e.getMessage());
}
}
}
+106 -90
View File
@@ -13,10 +13,11 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
private Identifier identifier;
private VlInt tlvType;
public IdentifierWrapper(){
public IdentifierWrapper() {
identifier = new Identifier();
}
public VlInt getTlvType(){
public VlInt getTlvType() {
return this.tlvType;
}
@@ -25,70 +26,71 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
*/
public IdentifierWrapper(Block block) throws ComponentException {
try {
if(!block.parseSubElements()){
if (!block.parseSubElements()) {
throw new ComponentException("IdentifierWrapper(Block): block.parseSubElements error");
}
// 检查是否包含有且仅有一个 Identifier TLV
if ((block.getSubElements().length() != 1) && block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)) {
throw new ComponentException("IdentifierWrapper(Block): (block.getSubElements().length()!=1)" +
"&&block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)");
}
this.tlvType = block.getType();
this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0));
} catch (BlockException e) {
throw new ComponentException("IdentifierWrapper(Block): "+e.getMessage());
throw new ComponentException("IdentifierWrapper(Block): " + e.getMessage());
}
// 检查是否包含有且仅有一个 Identifier TLV
if((block.getSubElements().length()!=1)&&block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)){
throw new ComponentException("IdentifierWrapper(Block): (block.getSubElements().length()!=1)" +
"&&block.getSubElements().getBlock(0).getType().isEqual(TLV.TlvIdentifier)");
}
this.tlvType=block.getType();
this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0));
}
/**
* 自定义标识类型,根据标识的字符串标识创建一个自定义的标识
* @Description:
* // 1. 格式如下:
* // CustomIdentifier = <自定义标识类型> TLV-LENGTH
* // <Identifier>
*
* @param tlvType
* @param identifierString
* @return
* @Description: // 1. 格式如下:
* // CustomIdentifier = <自定义标识类型> TLV-LENGTH
* // <Identifier>
*/
public IdentifierWrapper(VlInt tlvType,String identifierString) throws ComponentException {
if(TLV.isValidIdentifierType(tlvType)){
public IdentifierWrapper(VlInt tlvType, String identifierString) throws ComponentException {
if (TLV.isValidIdentifierType(tlvType)) {
throw new ComponentException("IdentifierWrapper(VlInt tlvType,String identifierString): "
+"tlvType invalid");
+ "tlvType invalid");
}
this.tlvType=tlvType;
this.tlvType = tlvType;
this.identifier.buildIdentifierByString(identifierString);
}
/**
* 自定义标识类型,根据 components 创建一个自定义的标识
* @Description:
* // 1. 格式如下:
* // CustomIdentifier = <自定义标识类型> TLV-LENGTH
* // <Identifier>
*
* @param tlvType
* @param container
* @return
* @Description: // 1. 格式如下:
* // CustomIdentifier = <自定义标识类型> TLV-LENGTH
* // <Identifier>
*/
public IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container) throws ComponentException {
if(TLV.isValidIdentifierType(tlvType)){
public IdentifierWrapper(VlInt tlvType, IdentifierComponentContainer container) throws ComponentException {
if (TLV.isValidIdentifierType(tlvType)) {
throw new ComponentException("IdentifierWrapper(VlInt tlvType,IdentifierComponentContainer container): "
+"tlvType invalid");
+ "tlvType invalid");
}
this.tlvType=tlvType;
this.tlvType = tlvType;
this.identifier.buildIdentifierByComponents(container);
}
/**
* 自定义标识类型,根据 TLV Block 创建一个自定义标识
*
* @param tlvType
* @param block
* @return
*/
public IdentifierWrapper(VlInt tlvType,Block block) throws ComponentException {
if(TLV.isValidIdentifierType(tlvType)){
public IdentifierWrapper(VlInt tlvType, Block block) throws ComponentException {
if (TLV.isValidIdentifierType(tlvType)) {
// todo: 报错
}
this.tlvType=tlvType;
this.tlvType = tlvType;
this.identifier.buildIdentifierByBlock(block);
}
@@ -99,28 +101,30 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 102 TLV-LENGTH
* // <Identifier>
*
* @param identifierString
* @return
*/
public IdentifierWrapper createCommonIdentifierByString(String identifierString){
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
public IdentifierWrapper createCommonIdentifierByString(String identifierString) {
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierCommon);
identifierWrapper.identifier.buildIdentifierByString(identifierString);
return identifierWrapper;
}
/**
* 根据components构造一个通用的身份标识
* 根据components构造一个通用的身份标识
* //
* // @Description:
* // 1. 格式如下:
* // CommonIdentifier = 102 TLV-LENGTH
* // <Identifier>
*
* @param container
*/
public IdentifierWrapper createCommonIdentifierByComponents(IdentifierComponentContainer container){
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
public IdentifierWrapper createCommonIdentifierByComponents(IdentifierComponentContainer container) {
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierCommon);
identifierWrapper.identifier.buildIdentifierByComponents(container);
return identifierWrapper;
}
@@ -133,11 +137,12 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // CommonIdentifier = 102 TLV-LENGTH
* // <Identifier>
* // @param block 传递 Identifier Block,而非 IdentifierWrapper Block
*
* @return
*/
public IdentifierWrapper createCommonIdentifierByBlock(Block block) throws ComponentException {
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierCommon);
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierCommon);
identifierWrapper.identifier.buildIdentifierByBlock(block);
return identifierWrapper;
}
@@ -149,12 +154,13 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 103 TLV-LENGTH
* // <Identifier>
*
* @param identifierString
* @return
*/
public IdentifierWrapper createContentInterestIdentifierByString(String identifierString){
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
public IdentifierWrapper createContentInterestIdentifierByString(String identifierString) {
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentInterest);
identifierWrapper.identifier.buildIdentifierByString(identifierString);
return identifierWrapper;
}
@@ -166,12 +172,13 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 103 TLV-LENGTH
* // <Identifier>
*
* @param container
* @return
*/
public IdentifierWrapper createContentInterestIdentifierByComponents(IdentifierComponentContainer container){
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
public IdentifierWrapper createContentInterestIdentifierByComponents(IdentifierComponentContainer container) {
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentInterest);
identifierWrapper.identifier.buildIdentifierByComponents(container);
return identifierWrapper;
}
@@ -183,12 +190,13 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 103 TLV-LENGTH
* // <Identifier>
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
*
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
* @return
*/
public IdentifierWrapper createContentInterestIdentifierByBlock(Block block) throws ComponentException {
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentInterest);
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentInterest);
identifierWrapper.identifier.buildIdentifierByBlock(block);
return identifierWrapper;
}
@@ -200,12 +208,13 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 104 TLV-LENGTH
* // <Identifier>
*
* @param identifierString
* @return
*/
public IdentifierWrapper createContentDataIdentifierByString(String identifierString){
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
public IdentifierWrapper createContentDataIdentifierByString(String identifierString) {
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentData);
identifierWrapper.identifier.buildIdentifierByString(identifierString);
return identifierWrapper;
}
@@ -217,12 +226,13 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 104 TLV-LENGTH
* // <Identifier>
*
* @param container
* @return
*/
public IdentifierWrapper createContentDataIdentifierByComponents(IdentifierComponentContainer container){
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
public IdentifierWrapper createContentDataIdentifierByComponents(IdentifierComponentContainer container) {
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentData);
identifierWrapper.identifier.buildIdentifierByComponents(container);
return identifierWrapper;
}
@@ -234,29 +244,32 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // CommonIdentifier = 104 TLV-LENGTH
* // <Identifier>
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
*
* @param block 传递 Identifier Block,而非 IdentifierWrapper Block
* @return
*/
public static IdentifierWrapper createContentDataIdentifierByBlock(Block block) throws ComponentException {
IdentifierWrapper identifierWrapper=new IdentifierWrapper();
identifierWrapper.tlvType=new VlInt(TLV.TlvIdentifierContentData);
IdentifierWrapper identifierWrapper = new IdentifierWrapper();
identifierWrapper.tlvType = new VlInt(TLV.TlvIdentifierContentData);
identifierWrapper.identifier.buildIdentifierByBlock(block);
return identifierWrapper;
}
/**
* 判断是否有效
*
* @return
*/
public boolean isValid(){
public boolean isValid() {
return TLV.isValidIdentifierType(this.tlvType);
}
/**
* 获取 identifier 对象
*
* @return
*/
public Identifier getIdentifier(){
public Identifier getIdentifier() {
return this.identifier;
}
@@ -267,86 +280,89 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
* // 1. 格式如下:
* // IdentifierWrapper = TLV-TYPE TLV-LENGTH
* // <Identifier>
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen= 0;
int tmplen = 0;
try {
tmplen = this.identifier.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: "+e.getMessage());
throw new ComponentException("IdentifierWrapper.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: "+e.getMessage());
throw new ComponentException("IdentifierWrapper.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(this.tlvType);
tmplen = encoder.prependVarNumber(this.tlvType);
} catch (EncoderException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: "+e.getMessage());
throw new ComponentException("IdentifierWrapper.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
return totalLength;
}
/**
* 线速解码,从 TLV Block 中解码一个 IdentifierWrapper
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查TLV值是否有效
if(!TLV.isValidIdentifierType(block.getType())){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查TLV值是否有效
if (!TLV.isValidIdentifierType(block.getType())) {
return false;
}
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
// 判断是否有且仅有一个子TLV
if (block.getSubElements().length() != 1) {
return false;
}
// 尝试将唯一的子TLV Block解析成一个 Identifier
if (!this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0))) {
return false;
}
this.tlvType = block.getType();
return true;
} catch (BlockException e) {
throw new ComponentException("IdentifierWrapper.wireDecode: "+e.getMessage());
throw new ComponentException("IdentifierWrapper.wireDecode: " + e.getMessage());
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
return false;
}
// 尝试将唯一的子TLV Block解析成一个 Identifier
if(!this.identifier.buildIdentifierByBlock(block.getSubElements().getBlock(0))){
return false;
}
this.tlvType=block.getType();
return true;
}
/**
* 将 IdentifierWrapper 表示成Uri 字符串
*
* @return
*/
public String toUri(){
public String toUri() {
return this.identifier.toUri();
}
}
+16 -12
View File
@@ -15,6 +15,10 @@ import encoding.*;
public class KeyLocator implements TlvComponentBase, IEncodingAble {
private Identifier identifier;
public KeyLocator(){
identifier=new Identifier();
}
/**
* 根据一个 TLV Block 创建一个 KeyLocator
* @param block
@@ -151,23 +155,23 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查Type是否正确
if(!TLV.isValidIdentifierType(block.getType())){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查Type是否正确
if(!TLV.isValidIdentifierType(block.getType())){
return false;
}
// 解析子 TLV
if(!block.parseSubElements()){
return false;
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
return false;
}
// 解析子TLV为一个Identifier
return this.identifier.wireDecode(block.getSubElements().getBlock(0));
} catch (BlockException e) {
throw new ComponentException("KeyLocator.wireDecode: "+e.getMessage());
}
// 判断是否有且仅有一个子TLV
if(block.getSubElements().length()!=1){
return false;
}
// 解析子TLV为一个Identifier
return this.identifier.wireDecode(block.getSubElements().getBlock(0));
}
}
@@ -12,6 +12,10 @@ import encoding.*;
public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
private ElementContainer blocks;
public MutableDangerousField(){
blocks=new ElementContainer();
}
/**
* 往可变非受保护区添加一个Block
* @param block
@@ -24,8 +28,12 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
* 获取可变非受保护区指定位置的 Block
* @return
*/
public Block getBlock(int index){
return this.blocks.getBlock(index);
public Block getBlock(int index) throws ComponentException {
try {
return this.blocks.getBlock(index);
} catch (BlockException e) {
throw new ComponentException("MutableDangerousField.getBlock: "+e.getMessage());
}
}
/**
@@ -47,8 +55,12 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
* 获取可变非受保护区中,第一个指定TLV-TYPE的子 TLV Block
* @return
*/
public Block getBlockByType(VlInt tlvType){
return this.blocks.getElement(tlvType);
public Block getBlockByType(VlInt tlvType) throws ComponentException {
try {
return this.blocks.getElement(tlvType);
} catch (BlockException e) {
throw new ComponentException("MutableDangerousField.getBlockByType: "+e.getMessage());
}
}
/**
@@ -58,6 +70,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try{
// 如果可变非受保护区没有任何子 TLV,则可以去掉可变非受保护区 TLV
if(this.blocks.length()==0){
return -1;
@@ -68,11 +81,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -81,11 +90,7 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
// 编码 TLV-LENGTH
int tmplen= 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -103,6 +108,9 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
totalLength+=tmplen;
return totalLength;
} catch (EncoderException | BlockException e) {
throw new ComponentException("MutableDangerousField.wireEncode: "+e.getMessage());
}
}
/**
+48 -46
View File
@@ -9,107 +9,109 @@ import encoding.*;
* @Date: 20:48 2021/3/9
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class MutableField implements TlvComponentBase,IEncodingAble {
public class MutableField implements TlvComponentBase, IEncodingAble {
public MutableProtectField mutableProtectField;
public MutableDangerousField mutableDangerousField;
public MutableField(){
mutableProtectField=new MutableProtectField();
mutableDangerousField=new MutableDangerousField();
public MutableField() {
mutableProtectField = new MutableProtectField();
mutableDangerousField = new MutableDangerousField();
}
/**
* 将可变区线速编码为一个 TLV
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
int totalLength = 0;
// 编码 TLV—VALUE
int tmplen= 0;
int tmplen = 0;
try {
tmplen = this.mutableDangerousField.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
throw new ComponentException("MutableField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
tmplen=this.mutableProtectField.wireEncode(encoder);
if(tmplen<=0){
totalLength += tmplen;
tmplen = this.mutableProtectField.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 如果可变保护区和可变非受保护区均为空,则可以去掉可变区
if(totalLength<=0){
if (totalLength <= 0) {
return -1;
}
// 编码 TLV-LENGTH
try {
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
throw new ComponentException("MutableField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: "+e.getMessage());
throw new ComponentException("MutableField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 MutableField
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvMutableField))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvMutableField))) {
return false;
}
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
for (int i = 0; i < block.getSubElements().length(); i++) {
Block subBlock = block.getSubElements().getBlock(i);
if (subBlock.getType().isEqual(TLV.TlvMutableProtectField)) {
if (!this.mutableProtectField.wireDecode(subBlock)) {
return false;
}
} else if (subBlock.getType().isEqual(TLV.TlvMutableDangerousField)) {
if (!this.mutableDangerousField.wireDecode(subBlock)) {
return false;
}
} else {
return false;
}
}
return false;
} catch (BlockException e) {
throw new ComponentException("MutableField.wireDecode: "+e.getMessage());
throw new ComponentException("MutableField.wireDecode: " + e.getMessage());
}
for (int i = 0; i < block.getSubElements().length(); i++) {
Block subBlock=block.getSubElements().getBlock(i);
if(subBlock.getType().isEqual(TLV.TlvMutableProtectField)){
if(!this.mutableProtectField.wireDecode(subBlock)){
return false;
}
}else if(subBlock.getType().isEqual(TLV.TlvMutableDangerousField)){
if(!this.mutableDangerousField.wireDecode(subBlock)){
return false;
}
}else{
return false;
}
}
return false;
}
}
@@ -12,6 +12,10 @@ import encoding.*;
public class MutableProtectField implements TlvComponentBase, IEncodingAble {
private ElementContainer blocks;
public MutableProtectField(){
blocks=new ElementContainer();
}
/**
* 添加一个Block
* @param block
@@ -24,8 +28,12 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
* 获取指定位置的 Block
* @return
*/
public Block getBlock(int index){
return this.blocks.getBlock(index);
public Block getBlock(int index) throws ComponentException {
try {
return this.blocks.getBlock(index);
} catch (BlockException e) {
throw new ComponentException("MutableProtectField.getBlock: "+e.getMessage());
}
}
/**
@@ -47,8 +55,12 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
* 获取第一个指定TLV-TYPE的子 TLV Block
* @return
*/
public Block getBlockByType(VlInt tlvType){
return this.blocks.getElement(tlvType);
public Block getBlockByType(VlInt tlvType) throws ComponentException {
try {
return this.blocks.getElement(tlvType);
} catch (BlockException e) {
throw new ComponentException("MutableProtectField.getBlockByType: "+e.getMessage());
}
}
/**
@@ -58,6 +70,8 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try {
// 如果没有任何子 TLV,则可以去掉它
if(this.blocks.length()==0){
return -1;
@@ -68,11 +82,8 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("MutableProtectField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -103,6 +114,9 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
totalLength+=tmplen;
return totalLength;
} catch (EncoderException | BlockException e) {
throw new ComponentException("MutableProtectField.wireEncode: "+e.getMessage());
}
}
/**
+70 -58
View File
@@ -12,121 +12,133 @@ import encoding.*;
public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
private ElementContainer blocks;
public ReadOnlyField() {
blocks = new ElementContainer();
}
/**
* 往只读区添加一个Block
*
* @param block
*/
public void addBlock(Block block){
public void addBlock(Block block) {
blocks.addElement(block);
}
/**
* 获取只读区指定位置的 Block
*
* @return
*/
public Block getBlock(int index){
return this.blocks.getBlock(index);
public Block getBlock(int index) throws ComponentException {
try {
return this.blocks.getBlock(index);
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.getBlock: " + e.getMessage());
}
}
/**
* 清空只读区中,所有的子 Block
*/
public void clearBlocks(){
public void clearBlocks() {
this.blocks.clear();
}
/**
* 获取只读区所有的 Block
*
* @return
*/
public ElementContainer getBlocks(){
public ElementContainer getBlocks() {
return this.blocks;
}
/**
* 获取只读区中,第一个指定TLV-TYPE的子 TLV Block
*
* @return
*/
public Block getBlockByType(VlInt tlvType){
return this.blocks.getElement(tlvType);
public Block getBlockByType(VlInt tlvType) throws ComponentException {
try {
return this.blocks.getElement(tlvType);
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.getBlockType: " + e.getMessage());
}
}
/**
* 将只读区线速编码为一个 TLV
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if(this.blocks.length()==0){
return 0;
}
int totalLength=0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=this.blocks.length()-1;i>=0;i++){
int tmplen= 0;
try {
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
try {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if (this.blocks.length() == 0) {
return -1;
}
totalLength+=tmplen;
}
// 编码 TLV-LENGTH
int tmplen= 0;
try {
int totalLength = 0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.blocks.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen = 0;
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
} catch (EncoderException e) {
throw new ComponentException("ReadOnlyField.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
return totalLength;
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException | BlockException e) {
throw new ComponentException("ReadOnlyField.wireEncode: " + e.getMessage());
}
}
/**
* 从 TLV Block 中解码出一个 ReadOnlyField
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvReadOnlyField))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvReadOnlyField))) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.wireDecode: "+e.getMessage());
}
this.blocks=block.getSubElements();
return true;
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
this.blocks = block.getSubElements();
return true;
} catch (BlockException e) {
throw new ComponentException("ReadOnlyField.wireDecode: " + e.getMessage());
}
}
}
+83 -64
View File
@@ -12,43 +12,46 @@ import java.util.List;
* @Date: 21:24 2021/3/15
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class SignatureContainer{
class SignatureContainer {
private List<Signature> signatures;
public SignatureContainer(){
signatures=new LinkedList<>();
public SignatureContainer() {
signatures = new LinkedList<>();
}
/**
* 组件个数
*
* @return
*/
public int length(){
public int length() {
return signatures.size();
}
/**
* 清空组件
*/
public void clear(){
public void clear() {
signatures.clear();
}
/**
* 增加一个组件
*
* @param signature
*/
public void addElement(Signature signature){
public void addElement(Signature signature) {
signatures.add(signature);
}
/**
* 获取指定索引位置的组件
*
* @param index
* @return
*/
public Signature getElement(int index){
if(index>=length()){
public Signature getElement(int index) {
if (index >= length()) {
return null;
}
return signatures.get(index);
@@ -56,144 +59,160 @@ class SignatureContainer{
/**
* 设置指定索引位置的组件
*
* @param index
* @param signature
* @return
*/
public boolean setElement(int index,Signature signature){
if(index>=length()){
public boolean setElement(int index, Signature signature) {
if (index >= length()) {
return false;
}
signatures.set(index,signature);
signatures.set(index, signature);
return true;
}
}
public class SignatureField implements TlvComponentBase, IEncodingAble {
private SignatureContainer signatures;
public SignatureField() {
signatures = new SignatureContainer();
}
public SignatureField(SignatureContainer container) {
this.signatures = container;
}
/**
* 添加一个签名
*
* @param signature
*/
public void addSignature(Signature signature){
public void addSignature(Signature signature) {
signatures.addElement(signature);
}
/**
* 获取指定位置的签名
*
* @param index
* @return
*/
public Signature getSignature(int index){
public Signature getSignature(int index) {
return signatures.getElement(index);
}
/**
* 获取所有签名
*
* @return
*/
public SignatureContainer getSignatures(){
public SignatureContainer getSignatures() {
return signatures;
}
/**
* 将签名区线速编码为一个 TLV
*
* @param encoder
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
// 如果签名区没有签名,则可以直接去掉签名区 TLV,因为它不是必须的
if(signatures.length()==0){
if (signatures.length() == 0) {
return -1;
}
int totalLength=0;
int totalLength = 0;
// 编码 TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for(int i=signatures.length()-1;i>=0;i--){
Signature newSignature=signatures.getElement(i);
int tmplen= 0;
for (int i = signatures.length() - 1; i >= 0; i--) {
Signature newSignature = signatures.getElement(i);
int tmplen = 0;
tmplen = newSignature.getSigValue().wireEncode(encoder);
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
tmplen=newSignature.getSigInfo().wireEncode(encoder);
if(tmplen<=0){
totalLength += tmplen;
tmplen = newSignature.getSigInfo().wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
}
// 编码签名区 TLV-LENGTH
int tmplen= 0;
int tmplen = 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: "+e.getMessage());
throw new ComponentException("SignatureField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
// 编码签名区 TLV-TYPE
try {
tmplen=encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
} catch (EncoderException e) {
throw new ComponentException("SignatureField.wireEncode: "+e.getMessage());
throw new ComponentException("SignatureField.wireEncode: " + e.getMessage());
}
if(tmplen<=0){
if (tmplen <= 0) {
return -1;
}
totalLength+=tmplen;
totalLength += tmplen;
return totalLength;
}
/**
* 从 TLV Block 中解码出一个 SignatureField
*
* @param block
* @return
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 TYPE 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvSignatureField))){
return false;
}
// 解析子 TLV
try {
if(!block.parseSubElements()){
// 检查 TYPE 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvSignatureField))) {
return false;
}
} catch (BlockException e) {
throw new ComponentException("SignatureField.wireDecode: "+e.getMessage());
}
ElementContainer subElements=block.getSubElements();
int subElementNum=subElements.length();
if(subElementNum%2!=0){
// throw new Error("SignatureField.wireDecode: Invalid SignatureField TLV, require even num sub element");
return false;
}
// 存储所有解析到的签名
SignatureContainer signatureContainer=new SignatureContainer();
// 解析每一个签名
for (int i = 0; i < subElementNum; i+=2) {
// 第奇数个子 TLV 解析为 SignatureInfo
SignatureInfo signatureInfo= null;
signatureInfo = new SignatureInfo(subElements.getBlock(i));
// 第偶数个子 TLV 解析为 SignatureValue
SignatureValue signatureValue=new SignatureValue(subElements.getBlock(i+1));
// 构造Signature
Signature signature=new Signature(signatureInfo,signatureValue);
signatureContainer.addElement(signature);
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
ElementContainer subElements = block.getSubElements();
int subElementNum = subElements.length();
if (subElementNum % 2 != 0) {
// throw new Error("SignatureField.wireDecode: Invalid SignatureField TLV, require even num sub element");
return false;
}
// 存储所有解析到的签名
SignatureContainer signatureContainer = new SignatureContainer();
// 解析每一个签名
for (int i = 0; i < subElementNum; i += 2) {
// 第奇数个子 TLV 解析为 SignatureInfo
SignatureInfo signatureInfo = null;
signatureInfo = new SignatureInfo(subElements.getBlock(i));
// 第偶数个子 TLV 解析为 SignatureValue
SignatureValue signatureValue = new SignatureValue(subElements.getBlock(i + 1));
// 构造Signature
Signature signature = new Signature(signatureInfo, signatureValue);
signatureContainer.addElement(signature);
}
// 赋值
signatures.clear();
this.signatures = signatureContainer;
return true;
} catch (BlockException e) {
throw new ComponentException("SignatureField.wireDecode: " + e.getMessage());
}
// 赋值
signatures.clear();
this.signatures=signatureContainer;
return true;
}
}
+13 -1
View File
@@ -17,11 +17,18 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
private SignatureType signatureType;
private KeyLocator keyLocator;
public SignatureInfo(){
signatureType=new SignatureType();
keyLocator=new KeyLocator();
}
/**
* 根据签名值类型创建一个 SignatureInfo
* @param value
*/
public SignatureInfo(SignatureAlgorithm value){
signatureType=new SignatureType();
keyLocator=new KeyLocator();
this.buildSignatureInfoBySignatureTypeValue(value);
}
@@ -172,7 +179,12 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
boolean findSignatureType=false;
int elementSize=block.getSubElements().length();
for (int i=0;i<elementSize;i++) {
Block cacheBlock=block.getSubElements().getBlock(i);
Block cacheBlock= null;
try {
cacheBlock = block.getSubElements().getBlock(i);
} catch (BlockException e) {
throw new ComponentException("SignatureInfo.wireDecode:"+e.getMessage());
}
int type= 0;
try {
type = cacheBlock.getType().getVlIntValue2Int();
+20 -14
View File
@@ -14,20 +14,20 @@ import minsecurity.Common;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
class SignatureTypeValue{
private int signatureTypeValue;
public SignatureTypeValue(int signatureTypeValue){
this.signatureTypeValue=signatureTypeValue;
}
public int getSignatureTypeValue() {
return signatureTypeValue;
}
public void setSignatureTypeValue(int signatureTypeValue) {
this.signatureTypeValue = signatureTypeValue;
}
}
//class SignatureTypeValue{
// private int signatureTypeValue;
// public SignatureTypeValue(int signatureTypeValue){
// this.signatureTypeValue=signatureTypeValue;
// }
//
// public int getSignatureTypeValue() {
// return signatureTypeValue;
// }
//
// public void setSignatureTypeValue(int signatureTypeValue) {
// this.signatureTypeValue = signatureTypeValue;
// }
//}
class SignatureAlgorithm{
public int getSignatureAlgorithm() {
return signatureAlgorithm;
@@ -38,6 +38,8 @@ class SignatureAlgorithm{
}
private int signatureAlgorithm;
public SignatureAlgorithm(){
}
public SignatureAlgorithm(int value){
this.signatureAlgorithm=value;
}
@@ -45,6 +47,10 @@ class SignatureAlgorithm{
public class SignatureType implements TlvComponentBase, IEncodingAble {
private SignatureAlgorithm value; // SignatureAlgorithm
public SignatureType(){
value=new SignatureAlgorithm();
}
/**
* 根据签名类型值创建一个 SignatureType
* @param signatureTypeValue
+1 -1
View File
@@ -301,7 +301,7 @@ public class Block {
* @param tlvType
* @return
*/
public Block getElement(VlInt tlvType){
public Block getElement(VlInt tlvType) throws BlockException {
return this.elements.getElement(tlvType);
}
+6 -4
View File
@@ -1,5 +1,7 @@
package encoding;
import component.ComponentException;
import java.util.LinkedList;
import java.util.List;
@@ -67,18 +69,18 @@ public class ElementContainer{
* @param tlvType
* @return
*/
public Block getElement(VlInt tlvType){
public Block getElement(VlInt tlvType) throws BlockException {
for(Block block: this.elementContainer){
if(block.getType().isEqual(tlvType)){
return block;
}
}
return null;
throw new BlockException("ReadOnlyField.getBlock: no found the tlvType");
}
public Block getBlock(int index){
public Block getBlock(int index) throws BlockException {
if(index>=this.length()){
return null;
throw new BlockException("ReadOnlyField.getBlock: index is out of bound.");
}
return this.elementContainer.get(index);
+88 -78
View File
@@ -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
View File
@@ -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();
+93 -96
View File
@@ -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
View File
@@ -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());
}
}
}
@@ -0,0 +1,167 @@
package component;
import encoding.*;
import org.junit.Test;
import java.util.Arrays;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 15:41 2021/3/25
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ReadOnlyFieldTest {
@Test
public void TestReadOnlyField_AddBlock() throws EncoderException, ComponentException, BlockException {
ReadOnlyField readOnlyField=new ReadOnlyField();
byte[] buf={(byte) 1, (byte)2,(byte) 3,(byte) 4,(byte) 5,(byte) 6,(byte) 7,(byte) 8};
SignatureValue got=new SignatureValue(buf);
SignatureValue got1=new SignatureValue(buf);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field.wireEncode(encoder);
byte[] buffer=encoder.getBuffer();
System.out.println("encoder buf: "+ Arrays.toString(buffer));
Block block=Block.createBlockByBuffer(buffer,false);
System.out.println("readOnlyField's block: "+block.getType()+", "
+block.getLength()+", "+Arrays.toString(block.getValue()));
readOnlyField.addBlock(block);
Block block1=readOnlyField.getBlock(0);
System.out.println("readOnlyField's block: "+block1.getType()+", "
+block1.getLength()+", "+Arrays.toString(block1.getValue()));
}
@Test
public void TestReadOnlyField_GetBlock() throws BlockException, EncoderException, ComponentException {
ReadOnlyField readOnlyField=new ReadOnlyField();
byte[] buf={(byte) 1, (byte)2,(byte) 3,(byte) 4,(byte) 5,(byte) 6,(byte) 7,(byte) 8};
SignatureValue got=new SignatureValue(buf);
SignatureValue got1=new SignatureValue(buf);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field.wireEncode(encoder);
byte[] buffer=encoder.getBuffer();
System.out.println("encoder buf: "+ Arrays.toString(buffer));
Block block=Block.createBlockByBuffer(buffer,false);
System.out.println("readOnlyField's block: "+block.getType()+", "
+block.getLength()+", "+Arrays.toString(block.getValue()));
readOnlyField.addBlock(block);
readOnlyField.addBlock(block);
Block blockError=readOnlyField.getBlock(2);
System.out.println("readOnlyField's block: "+blockError.getType()+", "
+blockError.getLength()+", "+Arrays.toString(blockError.getValue()));
}
@Test
public void TestReadOnlyField_ClearBlocks() throws BlockException, EncoderException, ComponentException{
ReadOnlyField readOnlyField=new ReadOnlyField();
byte[] buf={(byte) 1, (byte)2,(byte) 3,(byte) 4,(byte) 5,(byte) 6,(byte) 7,(byte) 8};
SignatureValue got=new SignatureValue(buf);
SignatureValue got1=new SignatureValue(buf);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field.wireEncode(encoder);
byte[] buffer=encoder.getBuffer();
System.out.println("encoder buf: "+ Arrays.toString(buffer));
Block block=Block.createBlockByBuffer(buffer,false);
System.out.println("readOnlyField's block: "+block.getType()+", "
+block.getLength()+", "+Arrays.toString(block.getValue()));
readOnlyField.addBlock(block);
readOnlyField.addBlock(block);
readOnlyField.clearBlocks();
System.out.println(readOnlyField.getBlocks().length());
System.out.println(Arrays.toString(readOnlyField.getBlockByType(new VlInt(202)).getValue()));
System.out.println(Arrays.toString(readOnlyField.getBlockByType(new VlInt(51)).getValue()));
}
@Test
public void TestReadOnlyField_WireEncode() throws BlockException, EncoderException, ComponentException{
ReadOnlyField readOnlyField=new ReadOnlyField();
byte[] buf={(byte) 1, (byte)2,(byte) 3,(byte) 4,(byte) 5,(byte) 6,(byte) 7,(byte) 8};
SignatureValue got=new SignatureValue(buf);
SignatureValue got1=new SignatureValue(buf);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field.wireEncode(encoder);
byte[] buffer=encoder.getBuffer();
System.out.println("encoder buf: "+ Arrays.toString(buffer));
Block block=Block.createBlockByBuffer(buffer,false);
System.out.println("readOnlyField's block: "+block.getType()+", "
+block.getLength()+", "+Arrays.toString(block.getValue()));
readOnlyField.addBlock(block);
readOnlyField.addBlock(block);
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
readOnlyField.wireEncode(encoder);
byte[] buffer1=encoder.getBuffer();
System.out.println("readOnlyField buf: "+Arrays.toString(buffer1));
}
@Test
public void TestReadOnlyField_WireDecode()throws BlockException, EncoderException, ComponentException{
ReadOnlyField readOnlyField=new ReadOnlyField();
byte[] buf={(byte) 1, (byte)2,(byte) 3,(byte) 4,(byte) 5,(byte) 6,(byte) 7,(byte) 8};
SignatureValue got=new SignatureValue(buf);
SignatureValue got1=new SignatureValue(buf);
SignatureAlgorithm value=new SignatureAlgorithm(0);
SignatureInfo info=new SignatureInfo(value);
Signature signature=new Signature(info,got);
Signature signature1=new Signature(info,got1);
SignatureContainer container=new SignatureContainer();
container.addElement(signature);
SignatureField field=new SignatureField(container);
field.addSignature(signature1);
Encoder encoder=new Encoder();
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
field.wireEncode(encoder);
byte[] buffer=encoder.getBuffer();
System.out.println("encoder buf: "+ Arrays.toString(buffer));
Block block=Block.createBlockByBuffer(buffer,false);
System.out.println("readOnlyField's block: "+block.getType()+", "
+block.getLength()+", "+Arrays.toString(block.getValue()));
readOnlyField.addBlock(block);
readOnlyField.addBlock(block);
encoder.encoderReset(new SizeT(Encoder.MaxPacketSize),new SizeT(0));
readOnlyField.wireEncode(encoder);
byte[] buffer1=encoder.getBuffer();
System.out.println("readOnlyField buf: "+Arrays.toString(buffer1));
Block realBlock=Block.createBlockByBuffer(buffer1,false);
ReadOnlyField readOnlyField1=new ReadOnlyField();
readOnlyField1.wireDecode(realBlock);
System.out.println(readOnlyField.getBlocks().length());
}
}