mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
writting lpPacket...
This commit is contained in:
1 parent
036c4d3dc9
commit
fa3647f721
14 files changed
+553
-147
No files matched your search
@@ -1,9 +1,6 @@
|
||||
package packet;
|
||||
|
||||
import component.ComponentException;
|
||||
import component.IdentifierWrapper;
|
||||
import component.Payload;
|
||||
import component.TTL;
|
||||
import component.*;
|
||||
import encoding.*;
|
||||
|
||||
/*
|
||||
@@ -34,15 +31,74 @@ import encoding.*;
|
||||
// }
|
||||
//
|
||||
public class CPacket implements InteractWithField, IEncodingAble {
|
||||
private MINPacket minPacket;
|
||||
private TTL ttl;
|
||||
private Payload payload;
|
||||
private String flatIdentifier; // 推式包拥有一个扁平的标识
|
||||
public MINPacket minPacket=new MINPacket();
|
||||
public TTL ttl=new TTL();
|
||||
public Payload payload=new Payload();
|
||||
public Identifier srcIdentifier=new Identifier();
|
||||
public Identifier dstIdentifier=new Identifier();
|
||||
|
||||
public CPacket() {
|
||||
minPacket = new MINPacket();
|
||||
ttl = new TTL();
|
||||
payload = new Payload();
|
||||
|
||||
/**
|
||||
* 获取源标识
|
||||
* @return
|
||||
*/
|
||||
public Identifier getSrcIdentifier() {
|
||||
return srcIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置源标识
|
||||
* @param srcIdentifier
|
||||
*/
|
||||
public void setSrcIdentifier(Identifier srcIdentifier) {
|
||||
this.srcIdentifier = srcIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目的标识
|
||||
* @return
|
||||
*/
|
||||
public Identifier getDstIdentifier() {
|
||||
return dstIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置目的标识
|
||||
* @param dstIdentifier
|
||||
*/
|
||||
public void setDstIdentifier(Identifier dstIdentifier) {
|
||||
this.dstIdentifier = dstIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到 TTL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TTL getTtl() {
|
||||
return ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 TTL
|
||||
*
|
||||
* @param ttl
|
||||
*/
|
||||
public void setTtl(TTL ttl) {
|
||||
this.ttl = ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 展示目的标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toUri() throws PacketException {
|
||||
try {
|
||||
return this.dstIdentifier.toUri();
|
||||
} catch (ComponentException e) {
|
||||
throw new PacketException("CPacket.toUri() :"+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,12 +115,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
// 填充可变区 => 非受保护区
|
||||
// 首先清除所有的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());
|
||||
}
|
||||
Block block = new SelfEncodingBase().selfWireEncode(this.ttl);
|
||||
if (block == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -81,13 +132,16 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
|
||||
// 填充标识区
|
||||
// 首先清除所有的标识
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper identifierWrapper =
|
||||
new IdentifierWrapper().createCommonIdentifierByString(this.flatIdentifier);
|
||||
if (identifierWrapper == null) {
|
||||
return false;
|
||||
if((this.srcIdentifier==null)||(this.dstIdentifier==null)){
|
||||
throw new PacketException("CPacket.fillDataToFields: SrcIdentifier or DstIdentifier is null.");
|
||||
}
|
||||
this.minPacket.identifierField.addIdentifier(identifierWrapper);
|
||||
this.minPacket.identifierField.clearIdentifiers();
|
||||
IdentifierWrapper srcIdentifierWrapper =
|
||||
new IdentifierWrapper(new VlInt(TLV.TlvIdentifierCommon),this.srcIdentifier);
|
||||
IdentifierWrapper dstIdentifierWrapper =
|
||||
new IdentifierWrapper(new VlInt(TLV.TlvIdentifierCommon),this.dstIdentifier);
|
||||
this.minPacket.identifierField.addIdentifier(srcIdentifierWrapper);
|
||||
this.minPacket.identifierField.addIdentifier(dstIdentifierWrapper);
|
||||
|
||||
return true;
|
||||
} catch (EncoderException | BlockException | ComponentException e) {
|
||||
@@ -104,9 +158,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
public boolean extraDataFromFields() throws PacketException {
|
||||
try {
|
||||
// 提取 Payload
|
||||
Block payloadBlock = null;
|
||||
payloadBlock = this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
|
||||
Block payloadBlock = this.minPacket.readOnlyField.getBlockByType(new VlInt(TLV.TlvPayload));
|
||||
if (payloadBlock == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -124,14 +176,16 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
}
|
||||
|
||||
// 提取标识
|
||||
IdentifierWrapper commonIdentifier = this.minPacket.identifierField.getIdentifierByType(new VlInt(TLV.TlvIdentifierCommon));
|
||||
if (commonIdentifier == null) {
|
||||
IdentifierWrapper srcIdentifierWrapper = this.minPacket.identifierField.getIdentifier(0);
|
||||
if (srcIdentifierWrapper == null) {
|
||||
return false;
|
||||
}
|
||||
String identifierString = commonIdentifier.toUri();
|
||||
if ((identifierString.length() > 0) && (identifierString.startsWith("/"))) {
|
||||
this.flatIdentifier = identifierString.substring(1);
|
||||
IdentifierWrapper dstIdentifierWrapper = this.minPacket.identifierField.getIdentifier(1);
|
||||
if (dstIdentifierWrapper == null) {
|
||||
return false;
|
||||
}
|
||||
this.srcIdentifier=srcIdentifierWrapper.getIdentifier();
|
||||
this.dstIdentifier=dstIdentifierWrapper.getIdentifier();
|
||||
|
||||
return true;
|
||||
} catch (ComponentException e) {
|
||||
@@ -149,7 +203,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
public int wireEncode(Encoder encoder) throws PacketException {
|
||||
// 首先将属性填充到分区当中
|
||||
if (!this.fillDataToFields()) {
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 指定包类型
|
||||
@@ -172,48 +226,4 @@ public class CPacket implements InteractWithField, IEncodingAble {
|
||||
return this.extraDataFromFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* 展示平面标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String toUri() {
|
||||
return this.flatIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置推式包的平面标识,eg: asdfsvnjweivnjsdcs
|
||||
*
|
||||
* @param identifier
|
||||
*/
|
||||
public void setIdentifier(String identifier) {
|
||||
this.flatIdentifier = identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取推式包的平面标识
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getIdentifier() {
|
||||
return this.flatIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到 TTL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public TTL getTtl() {
|
||||
return ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置 TTL
|
||||
*
|
||||
* @param ttl
|
||||
*/
|
||||
public void setTtl(TTL ttl) {
|
||||
this.ttl = ttl;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user