mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: ReadOnlyFieldTest
This commit is contained in:
1 parent
0a7ccc95a3
commit
8a2da10583
19 files changed
+1251
-966
No files matched your search
@@ -39,129 +39,134 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
private Payload payload;
|
||||
private String flatIdentifier; // 推式包拥有一个扁平的标识
|
||||
|
||||
public CPacket(){
|
||||
minPacket=new MINPacket();
|
||||
ttl=new TTL();
|
||||
payload=new Payload();
|
||||
public CPacket() {
|
||||
minPacket = new MINPacket();
|
||||
ttl = new TTL();
|
||||
payload = new Payload();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CPacket 的各项属性填充到 MINPacket 中定义的对应分区当中
|
||||
* @Description:
|
||||
* // 1. 主要填充标识区、只读区和可变区
|
||||
*
|
||||
* @return
|
||||
* @Description: // 1. 主要填充标识区、只读区和可变区
|
||||
*/
|
||||
@Override
|
||||
public boolean fillDataToFields() throws PacketException {
|
||||
// 可变区 => 受保护区(无)
|
||||
|
||||
// 填充可变区 => 非受保护区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
|
||||
Block block= null;
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.ttl);
|
||||
// 可变区 => 受保护区(无)
|
||||
|
||||
// 填充可变区 => 非受保护区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
|
||||
Block block = null;
|
||||
try {
|
||||
block = new SelfEncodingBase().selfWireEncode(this.ttl);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("CPacket.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.mutableField.mutableDangerousField.addBlock(block);
|
||||
|
||||
// 填充只读区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.readOnlyField.clearBlocks();
|
||||
block = new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
|
||||
// 填充标识区
|
||||
// 首先清除所有的标识
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper =
|
||||
new IdentifierWrapper().createCommonIdentifierByString(this.flatIdentifier);
|
||||
if (identifierWrapper == null) {
|
||||
return false;
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
|
||||
return true;
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
|
||||
throw new PacketException("CPacket.fillDataToFields: " + e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.mutableField.mutableDangerousField.addBlock(block);
|
||||
|
||||
// 填充只读区
|
||||
// 首先清除所有的Block
|
||||
this.minPacket.readOnlyField.clearBlocks();
|
||||
try {
|
||||
block=new SelfEncodingBase().selfWireEncode(this.payload);
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
throw new PacketException("CPacket.fillDataToFields: "+e.getMessage());
|
||||
}
|
||||
if(block==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.readOnlyField.addBlock(block);
|
||||
|
||||
// 填充标识区
|
||||
// 首先清除所有的标识
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper=
|
||||
new IdentifierWrapper().createCommonIdentifierByString(this.flatIdentifier);
|
||||
if(identifierWrapper==null){
|
||||
return false;
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 MINPacket 的分区当中提取出 CPacket 的各项属性
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean extraDataFromFields() throws PacketException {
|
||||
// 提取 Payload
|
||||
Block payloadBlock=this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
if(payloadBlock==null){
|
||||
return false;
|
||||
}
|
||||
if(this.payload.wireDecode(payloadBlock)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取 TTL
|
||||
Block ttlBlock=this.minPacket.mutableField.mutableDangerousField.getBlockByType(new VlInt(TLV.TlvTTL));
|
||||
if(ttlBlock==null){
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if(this.ttl.wireDecode(ttlBlock)){
|
||||
// 提取 Payload
|
||||
Block payloadBlock = null;
|
||||
payloadBlock = this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
|
||||
if (payloadBlock == null) {
|
||||
return false;
|
||||
}
|
||||
if (this.payload.wireDecode(payloadBlock)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取 TTL
|
||||
Block ttlBlock = this.minPacket.mutableField.mutableDangerousField.getBlockByType(new VlInt(TLV.TlvTTL));
|
||||
if (ttlBlock == null) {
|
||||
return false;
|
||||
}
|
||||
if (this.ttl.wireDecode(ttlBlock)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取标识
|
||||
IdentifierWrapper commonIdentifier = this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierCommon));
|
||||
if (commonIdentifier == null) {
|
||||
return false;
|
||||
}
|
||||
String identifierString = commonIdentifier.toUri();
|
||||
if ((identifierString.length() > 0) && (identifierString.startsWith("/"))) {
|
||||
this.flatIdentifier = identifierString.substring(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("CPacket.extraDataFromFields: "+e.getMessage());
|
||||
throw new PacketException("CPacket.extraDataFromFields: " + e.getMessage());
|
||||
}
|
||||
|
||||
// 提取标识
|
||||
IdentifierWrapper commonIdentifier=this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierCommon));
|
||||
if(commonIdentifier==null){
|
||||
return false;
|
||||
}
|
||||
String identifierString=commonIdentifier.toUri();
|
||||
if((identifierString.length()>0)&&(identifierString.startsWith("/"))){
|
||||
this.flatIdentifier=identifierString.substring(1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 CPacket 编码成 TLV
|
||||
*
|
||||
* @param encoder
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int wireEncode(Encoder encoder) throws PacketException {
|
||||
// 首先将属性填充到分区当中
|
||||
if(!this.fillDataToFields()){
|
||||
if (!this.fillDataToFields()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 指定包类型
|
||||
this.minPacket.packetType=new VlInt(TLV.TlvPacketMINCommon);
|
||||
this.minPacket.packetType = new VlInt(TLV.TlvPacketMINCommon);
|
||||
|
||||
return this.minPacket.wireEncode(encoder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 TLV Block 中解码出一个 CPacket
|
||||
*
|
||||
* @param block
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean wireDecode(Block block) throws PacketException {
|
||||
if(!this.minPacket.wireDecode(block)){
|
||||
if (!this.minPacket.wireDecode(block)) {
|
||||
return false;
|
||||
}
|
||||
return this.extraDataFromFields();
|
||||
@@ -169,30 +174,34 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
|
||||
/**
|
||||
* 展示平面标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toUri(){
|
||||
public String toUri() {
|
||||
return this.flatIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推式包的平面标识,eg: asdfsvnjweivnjsdcs
|
||||
*
|
||||
* @param identifier
|
||||
*/
|
||||
public void setIdentifier(String identifier){
|
||||
this.flatIdentifier=identifier;
|
||||
public void setIdentifier(String identifier) {
|
||||
this.flatIdentifier = identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推式包的平面标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getIdentifier(){
|
||||
public String getIdentifier() {
|
||||
return this.flatIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到 TTL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TTL getTtl() {
|
||||
@@ -201,6 +210,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
|
||||
/**
|
||||
* 设置 TTL
|
||||
*
|
||||
* @param ttl
|
||||
*/
|
||||
public void setTtl(TTL ttl) {
|
||||
|
||||
Reference in new issue
Block a user