update LogicFace

This commit is contained in:
free will
2021-04-26 18:01:11 +08:00
parent 79bb16267e
commit 2f010e30a3
+70 -40
View File
@@ -9,6 +9,7 @@ import encoding.VlIntException;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;
import packet.*;
import security.KeyChain;
import java.io.File;
import java.io.IOException;
@@ -37,6 +38,7 @@ public class LogicFace {
public LinkService linkService; // 与logicFace绑定的linkService
public LogicFaceCounters logicFaceCounters=new LogicFaceCounters(); // logicFace 流量统计对象
public long expireTime; // 超时时间 ms
public KeyChain keyChain;// 用于注册前缀时进行签名
public boolean state; // true 为 up , false 为down
/**
@@ -58,6 +60,7 @@ public class LogicFace {
tcpTransport.linkService=this.linkService;
this.transport=tcpTransport;
this.logicFaceType=LogicFaceTypeTCP;
this.keyChain=null;
this.state=true;
return true;
} catch (IOException e) {
@@ -83,6 +86,7 @@ public class LogicFace {
this.linkService.transport=udpTransport;
udpTransport.linkService=this.linkService;
this.transport=udpTransport;
this.keyChain=null;
this.logicFaceType=LogicFaceTypeUDP;
this.state=true;
return true;
@@ -110,6 +114,7 @@ public class LogicFace {
this.linkService.transport=unixStreamTransport;
unixStreamTransport.linkService=this.linkService;
this.transport=unixStreamTransport;
this.keyChain=null;
this.logicFaceType=LogicFaceTypeUnix;
this.state=true;
return true;
@@ -118,6 +123,22 @@ public class LogicFace {
}
}
/**
* GetKeyChain 获取用于注册前缀时签名的秘钥链
* @return
*/
public KeyChain getKeyChain(){
return this.keyChain;
}
/**
* SetKeyChain 设置用于注册前缀时签名的秘钥链
* @param keyChain
*/
public void setKeyChain(KeyChain keyChain){
this.keyChain=keyChain;
}
/**
* 在MIR中注册一个标识,路由指向本客户端
* // (1) 构造一个命令兴趣包, 通过l.SendInterest(interest) 把兴趣包发出去;
@@ -127,58 +148,65 @@ public class LogicFace {
* @param timeout 超时时间,以 毫秒 为单位
* @return
*/
public boolean registerIdentifier(Identifier identifier,long timeout) throws LogicFaceException {
try {
Interest interest=MgmtCommand.createRegisterIdentifierInterest(identifier);
if(interest==null){
return false;
}
if(!this.sendInterest(interest)){
return false;
}
if(!this.transport.setReadTimeout(timeout)){
return false;
}
MINPacket minPacket=this.receivePacket();
if(minPacket==null){
if(!this.transport.setReadTimeout(-1)){
return false;
}
}
IdentifierWrapper identifierWrapper=minPacket.identifierField.getIdentifier(0);
if(identifierWrapper==null){
return false;
}
if(identifierWrapper.getTlvType().getVlIntValue2Int()!=TLV.TlvIdentifierContentData){
LoggerHelper.logger.error("receive packet is not data");
return false;
}
// TODO 解析数据包
return true;
} catch (LogicFaceException | VlIntException | ComponentException e) {
throw new LogicFaceException("LogicFace.registerIdentifier: "+e.getMessage());
}
public boolean registerIdentifier(Identifier identifier,long timeout,IRegisterPrefixHelper helper) throws LogicFaceException {
return helper.registerPrefix(identifier,this,this.keyChain);
// try {
// Interest interest=MgmtCommand.createRegisterIdentifierInterest(identifier);
// if(interest==null){
// return false;
// }
// if(!this.sendInterest(interest)){
// return false;
// }
// if(!this.transport.setReadTimeout(timeout)){
// return false;
// }
// MINPacket minPacket=this.receivePacket();
// if(minPacket==null){
// if(!this.transport.setReadTimeout(-1)){
// return false;
// }
// }
//
// IdentifierWrapper identifierWrapper=minPacket.identifierField.getIdentifier(0);
// if(identifierWrapper==null){
// return false;
// }
// if(identifierWrapper.getTlvType().getVlIntValue2Int()!=TLV.TlvIdentifierContentData){
// LoggerHelper.logger.error("receive packet is not data");
// return false;
// }
// // TODO 解析数据包
//
// return true;
// } catch (LogicFaceException | VlIntException | ComponentException e) {
// throw new LogicFaceException("LogicFace.registerIdentifier: "+e.getMessage());
// }
}
/**
* 从 LogicFace 中接收一个网络包
* @param timeout 读超时时间, 毫秒为单位, 小于 0 表示不超时
* @return 多标识网络包
* @throws LogicFaceException
*/
public MINPacket receivePacket() throws LogicFaceException {
public MINPacket receivePacket(long timeout) throws LogicFaceException {
if(!this.transport.setReadTimeout(timeout)){
return null;
}
return this.linkService.receivePacket();
}
/**
* 从 LogicFace 中接收一个普通推送式网络包, 如果收到的包不是普通推送式网络包,则这个包会被忽略
* @param timeout 读超时时间, 毫秒为单位, 小于 0 表示不超时
* @return
* @throws LogicFaceException
*/
public CPacket receiveCPacket() throws LogicFaceException {
public CPacket receiveCPacket(long timeout) throws LogicFaceException {
try {
while (true){
MINPacket minPacket=this.receivePacket();
MINPacket minPacket=this.receivePacket(timeout);
int packetType=minPacket.packetType.getVlIntValue2Int();
if(packetType == TLV.TlvIdentifierCommon){
CPacket cPacket=new CPacket().createCPacketByMINPacket(minPacket);
@@ -192,13 +220,14 @@ public class LogicFace {
/**
* 从 LogicFace 中接收一个普通推送式网络包, 如果收到的包不是普通推送式网络包,则这个包会被忽略
* @param timeout 读超时时间, 毫秒为单位, 小于 0 表示不超时
* @return
* @throws LogicFaceException
*/
public Interest receiveInterest() throws LogicFaceException {
public Interest receiveInterest(long timeout) throws LogicFaceException {
try {
while (true){
MINPacket minPacket=this.receivePacket();
MINPacket minPacket=this.receivePacket(timeout);
int packetType=minPacket.packetType.getVlIntValue2Int();
if(packetType == TLV.TlvIdentifierContentInterest){
Interest interest=new Interest().createInterestByMINPacket(minPacket);
@@ -212,12 +241,13 @@ public class LogicFace {
/**
* 从 LogicFace 中接收一个数据包, 如果收到的包不是数据包,则这个包会被忽略
* @param timeout 读超时时间, 毫秒为单位, 小于 0 表示不超时
* @return
*/
public Data receiveData() throws LogicFaceException {
public Data receiveData(long timeout) throws LogicFaceException {
try {
while (true){
MINPacket minPacket=this.receivePacket();
MINPacket minPacket=this.receivePacket(timeout);
int packetType=minPacket.packetType.getVlIntValue2Int();
if(packetType == TLV.TlvIdentifierContentData){
Data data=new Data().createDataByMINPacket(minPacket);