add error handle: ComponentException & PacketException

This commit is contained in:
free will committed 2021-03-24 11:14:54 +08:00
1 parent d7f063cbb9
commit 7bf0906291
35 files changed
+955 -362

No files matched your search

+33 -11
View File
@@ -1,5 +1,6 @@
package packet;
import component.ComponentException;
import component.IdentifierWrapper;
import component.Payload;
import component.TTL;
@@ -51,13 +52,18 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException, BlockException {
public boolean fillDataToFields() throws PacketException {
// 可变区 => 受保护区(无)
// 填充可变区 => 非受保护区
// 首先清除所有的Block
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
Block block=new SelfEncodingBase().selfWireEncode(this.ttl);
Block block= null;
try {
block = new SelfEncodingBase().selfWireEncode(this.ttl);
} catch (EncoderException | BlockException e) {
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -66,7 +72,11 @@ public class CPacket implements InteractWithField, IEncodingAble {
// 填充只读区
// 首先清除所有的Block
this.minPacket.readOnlyField.clearBlocks();
block=new SelfEncodingBase().selfWireEncode(this.payload);
try {
block=new SelfEncodingBase().selfWireEncode(this.payload);
} catch (EncoderException | BlockException e) {
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -90,7 +100,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
public boolean extraDataFromFields() throws PacketException {
// 提取 Payload
Block payloadBlock=this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
if(payloadBlock==null){
@@ -105,8 +115,12 @@ public class CPacket implements InteractWithField, IEncodingAble {
if(ttlBlock==null){
return false;
}
if(this.ttl.wireDecode(ttlBlock)){
return false;
try {
if(this.ttl.wireDecode(ttlBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("CPacket.extraDataFromFields: "+e.getMessage());
}
// 提取标识
@@ -128,7 +142,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
public int wireEncode(Encoder encoder) throws PacketException {
// 首先将属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
@@ -137,7 +151,11 @@ public class CPacket implements InteractWithField, IEncodingAble {
// 指定包类型
this.minPacket.packetType=new VlInt(TLV.TlvPacketMINCommon);
return this.minPacket.wireEncode(encoder);
try {
return this.minPacket.wireEncode(encoder);
} catch (EncoderException e) {
throw new PacketException("CPacket.wireEncode: "+e.getMessage());
}
}
/**
@@ -146,9 +164,13 @@ public class CPacket implements InteractWithField, IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
if(!this.minPacket.wireDecode(block)){
return false;
public boolean wireDecode(Block block) throws PacketException {
try {
if(!this.minPacket.wireDecode(block)){
return false;
}
} catch (VlIntException e) {
throw new PacketException("CPacket.wireDecode: "+e.getMessage());
}
return this.extraDataFromFields();
}
+38 -13
View File
@@ -68,7 +68,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException, BlockException {
public boolean fillDataToFields() throws PacketException {
/////////////////////////////////////////////////////////////
//// 填充可变区
// { => 可变区
@@ -84,7 +84,12 @@ public class Data implements IEncodingAble,InteractWithField {
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// CongestionMark
Block block=new SelfEncodingBase().selfWireEncode(this.congestionMark);
Block block= null;
try {
block = new SelfEncodingBase().selfWireEncode(this.congestionMark);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -104,7 +109,11 @@ public class Data implements IEncodingAble,InteractWithField {
// FreshnessPeriod
if(this.freshnessPeriod.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.freshnessPeriod);
try {
block=new SelfEncodingBase().selfWireEncode(this.freshnessPeriod);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -113,7 +122,11 @@ public class Data implements IEncodingAble,InteractWithField {
// NackHeader
if(this.nackHeader.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.nackHeader);
try {
block=new SelfEncodingBase().selfWireEncode(this.nackHeader);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -121,7 +134,11 @@ public class Data implements IEncodingAble,InteractWithField {
}
// Payload
block=new SelfEncodingBase().selfWireEncode(this.payload);
try {
block=new SelfEncodingBase().selfWireEncode(this.payload);
} catch (EncoderException | BlockException e) {
throw new PacketException("Data.fillDataToFields: "+e.getMessage());
}
if(block==null){
return false;
}
@@ -146,7 +163,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
public boolean extraDataFromFields() throws PacketException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
@@ -160,8 +177,12 @@ public class Data implements IEncodingAble,InteractWithField {
/////////////////////////////////////////////////////////////
Block block=this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
if(block!=null){
if(!this.congestionMark.wireDecode(block)){
return false;
try {
if(!this.congestionMark.wireDecode(block)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Data.extraDataFromFields: "+e.getMessage());
}
}
@@ -184,8 +205,12 @@ public class Data implements IEncodingAble,InteractWithField {
}
switch (type){
case TLV.TlvFreshnessPeriod:
if(!this.freshnessPeriod.wireDecode(newblock)){
return false;
try {
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)){
@@ -216,10 +241,10 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
public int wireEncode(Encoder encoder) throws PacketException {
// 首先将各项属性填充到分区当中
if(this.fillDataToFields()){
return 0;
return -1;
}
// 指定包类型
@@ -234,7 +259,7 @@ public class Data implements IEncodingAble,InteractWithField {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws PacketException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+2 -2
View File
@@ -16,11 +16,11 @@ public interface InteractWithField {
* 将包的属性填充到网络包的四个分区当中
* @return
*/
boolean fillDataToFields() throws EncoderException, BlockException;
boolean fillDataToFields() throws EncoderException, BlockException, PacketException;
/**
* 从网络包的四个分区当中提取出特定网络包的相关属性
* @return
*/
boolean extraDataFromFields() throws VlIntException;
boolean extraDataFromFields() throws VlIntException, PacketException;
}
+103 -82
View File
@@ -104,96 +104,101 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean fillDataToFields() throws EncoderException, BlockException {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
public boolean fillDataToFields() throws PacketException {
try {
/////////////////////////////////////////////////////////////
//// 填充可变区
/////////////////////////////////////////////////////////////
// 填充可变受保护区
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// 填充可变受保护区
this.minPacket.mutableField.mutableProtectField.clearBlocks();
// CongestionMark
Block block=new SelfEncodingBase().selfWireEncode(this.congestionMark);
if(block==null){
return false;
}
this.minPacket.mutableField.mutableProtectField.addBlock(block);
// CongestionMark
Block block = new SelfEncodingBase().selfWireEncode(this.congestionMark);
if (block == null) {
return false;
}
this.minPacket.mutableField.mutableProtectField.addBlock(block);
// 填充可变非受保护区(无)
// 填充可变非受保护区(无)
/////////////////////////////////////////////////////////////
//// 填充只读区
/////////////////////////////////////////////////////////////
this.minPacket.readOnlyField.clearBlocks();
/////////////////////////////////////////////////////////////
//// 填充只读区
/////////////////////////////////////////////////////////////
this.minPacket.readOnlyField.clearBlocks();
// CanBePrefix
if(this.canBePrefix.getCanBePrefix()){
block=new SelfEncodingBase().selfWireEncode(this.canBePrefix);
if(block==null){
// CanBePrefix
if (this.canBePrefix.getCanBePrefix()) {
block = new SelfEncodingBase().selfWireEncode(this.canBePrefix);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// MustBeRefresh
if (this.mustBeRefresh.getMustBeRefresh()) {
block = new SelfEncodingBase().selfWireEncode(this.mustBeRefresh);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// InterestLifeTime
if (!this.interestLifeTime.isInitial()) {
// 默认生存期为4s
this.interestLifeTime.setInterestLifeTime(4000);
}
block = new SelfEncodingBase().selfWireEncode(this.interestLifeTime);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// MustBeRefresh
if(this.mustBeRefresh.getMustBeRefresh()){
block=new SelfEncodingBase().selfWireEncode(this.mustBeRefresh);
if(block==null){
// Nonce
if (!this.nonce.isInitial()) {
// 如果没有指定随机数,随机生成一个随机数
this.nonce.refreshNonce();
}
block = new SelfEncodingBase().selfWireEncode(this.nonce);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// InterestLifeTime
if(!this.interestLifeTime.isInitial()){
// 默认生存期为4s
this.interestLifeTime.setInterestLifeTime(4000);
}
block=new SelfEncodingBase().selfWireEncode(this.interestLifeTime);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
// HopLimit
if (this.hopLimit.isInitial()) {
block = new SelfEncodingBase().selfWireEncode(this.hopLimit);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// Nonce
if(!this.nonce.isInitial()){
// 如果没有指定随机数,随机生成一个随机数
this.nonce.refreshNonce();
}
block=new SelfEncodingBase().selfWireEncode(this.nonce);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
// HopLimit
if(this.hopLimit.isInitial()){
block=new SelfEncodingBase().selfWireEncode(this.hopLimit);
if(block==null){
// Payload
block = new SelfEncodingBase().selfWireEncode(this.payload);
if (block == null) {
return false;
}
this.minPacket.readOnlyField.addBlock(block);
}
// Payload
block=new SelfEncodingBase().selfWireEncode(this.payload);
if(block==null){
return false;
}
this.minPacket.readOnlyField.addBlock(block);
/////////////////////////////////////////////////////////////
//// 填充标识区
/////////////////////////////////////////////////////////////
this.minPacket.identifierField.clearIdentifiers();
IdentifierWrapper identifierWrapper =
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
if (identifierWrapper == null) {
return false;
}
this.minPacket.identifierField.addIdentifier(identifierWrapper);
/////////////////////////////////////////////////////////////
//// 填充标识区
/////////////////////////////////////////////////////////////
this.minPacket.identifierField.clearIdentifiers();
IdentifierWrapper identifierWrapper=
new IdentifierWrapper().createContentInterestIdentifierByComponents(this.name.getComponents());
if(identifierWrapper==null){
return false;
}
this.minPacket.identifierField.addIdentifier(identifierWrapper);
return true;
return true;
}catch (EncoderException | BlockException e){
throw new PacketException("Interest.fillDataToFields: "+e.getMessage());
}
}
/**
@@ -201,7 +206,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean extraDataFromFields() throws VlIntException {
public boolean extraDataFromFields() throws PacketException {
/////////////////////////////////////////////////////////////
//// 解析可变区
// { => 可变区
@@ -215,8 +220,12 @@ public class Interest implements InteractWithField,IEncodingAble{
/////////////////////////////////////////////////////////////
Block block=this.minPacket.mutableField.mutableProtectField.getBlockByType(new VlInt(TLV.TlvCongestionMark));
if(block!=null){
if(!this.congestionMark.wireDecode(block)){
return false;
try {
if(!this.congestionMark.wireDecode(block)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
}
@@ -251,16 +260,28 @@ public class Interest implements InteractWithField,IEncodingAble{
return false;
}
case TLV.TlvInterestLifeTime:
if(!this.interestLifeTime.wireDecode(newblock)){
return false;
try {
if(!this.interestLifeTime.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvNonce:
if(!this.nonce.wireDecode(newblock)){
return false;
try {
if(!this.nonce.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvHopLimit:
if(!this.hopLimit.wireDecode(newblock)){
return false;
try {
if(!this.hopLimit.wireDecode(newblock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("Interest.extraDataFromFields: "+e.getMessage());
}
case TLV.TlvPayload:
if(!this.payload.wireDecode(newblock)){
@@ -292,10 +313,10 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException, BlockException {
public int wireEncode(Encoder encoder) throws PacketException {
// 首先将各项属性填充到分区当中
if(!this.fillDataToFields()){
return 0;
return -1;
}
// 指定包类型
@@ -310,7 +331,7 @@ public class Interest implements InteractWithField,IEncodingAble{
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws PacketException {
if(!this.minPacket.wireDecode(block)){
return false;
}
+71 -25
View File
@@ -1,9 +1,6 @@
package packet;
import component.IdentifierField;
import component.MutableField;
import component.ReadOnlyField;
import component.SignatureField;
import component.*;
import encoding.*;
/*
@@ -35,7 +32,7 @@ public class MINPacket implements IEncodingAble {
* @return
*/
@Override
public int wireEncode(Encoder encoder) throws EncoderException {
public int wireEncode(Encoder encoder) throws PacketException {
if(!this.isPacketValid()){
return -1;
}
@@ -44,42 +41,67 @@ public class MINPacket implements IEncodingAble {
// 编码 TLV-VALUE
// 可变区
int tmplen=this.mutableField.wireEncode(encoder);
int tmplen= 0;
try {
tmplen = this.mutableField.wireEncode(encoder);
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 只读区
tmplen=this.readOnlyField.wireEncode(encoder);
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.signatureField.wireEncode(encoder);
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.identifierField.wireEncode(encoder);
try {
tmplen=this.identifierField.wireEncode(encoder);
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码包 TLV-LENGTH
tmplen=encoder.prependVarNumber(new VlInt(totalLength));
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-TYPE
tmplen=encoder.prependVarNumber(this.packetType);
try {
tmplen=encoder.prependVarNumber(this.packetType);
} catch (EncoderException e) {
throw new PacketException("MINPacket.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
@@ -98,14 +120,18 @@ public class MINPacket implements IEncodingAble {
* @return
*/
@Override
public boolean wireDecode(Block block) throws VlIntException {
public boolean wireDecode(Block block) throws PacketException {
// 首先判断包是否有效
if(!block.isValid()){
return false;
}
// 首先解析子 Block
if(!block.parseSubElements()){
return false;
try {
if(!block.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
// 标记,用来记录是否满足必要条件
@@ -124,36 +150,56 @@ public class MINPacket implements IEncodingAble {
switch (judgeType){
case TLV.TlvIdentifierField: // 目的标识区
// 解析标识区,保证其中至少存在一个标识
if(!cacheBlock.parseSubElements()){
return false;
try {
if(!cacheBlock.parseSubElements()){
return false;
}
} catch (BlockException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
if(!cacheBlock.hasSubElement()){
return false;
}
// 解析标识区
if(!this.identifierField.wireDecode(cacheBlock)){
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: // 签名区
// 解析签名区,保证如果存在签名,则签名均符合格式
if(!this.signatureField.wireDecode(cacheBlock)){
return false;
try {
if(!this.signatureField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
case TLV.TlvReadOnlyField: // 只读区
// 解析只读区
if(!this.readOnlyField.wireDecode(cacheBlock)){
return false;
try {
if(!this.readOnlyField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
case TLV.TlvMutableField: // 可变区
// 解析可变区
if(!this.mutableField.wireDecode(cacheBlock)){
return false;
try {
if(!this.mutableField.wireDecode(cacheBlock)){
return false;
}
} catch (ComponentException e) {
throw new PacketException("MINPacket.wireDecode: "+e.getMessage());
}
default:
default:
return false;
}
+14
View File
@@ -0,0 +1,14 @@
package packet;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 10:59 2021/3/24
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class PacketException extends Exception {
public PacketException(String msg){
super("Packet-"+msg);
}
}