code review: LoginFaceICN

This commit is contained in:
ghy
2021-04-21 22:25:49 +08:00
parent d91972ac9f
commit 0abe81a6ad
4 changed files with 113 additions and 59 deletions
+43 -49
View File
@@ -10,6 +10,7 @@ import javafx.concurrent.Task;
import logicface.CallbackInterface.*;
import packet.*;
import util.ConcurrentHelper;
import util.ReschedulableTimer;
import util.TimeHelper;
import java.util.*;
@@ -26,20 +27,27 @@ import java.util.concurrent.locks.ReentrantLock;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class LogicFaceICN extends LogicFace {
public Timer timer; // 定时器,在processEvent函数中就被设置成很大的值
public ReschedulableTimer timer; // 定时器,在processEvent函数中就被设置成很大的值
public long recentExpireTime; // 最近将要超时的时间戳
public Map<String, PITEntry> mPit; // PIT表。不初始化,可以根据它来判断本face是否初始化
public Lock timeoutEventHeapLock=new ReentrantLock(); // 锁, mpit 、timeoutEventHeap、recentExpireTime
public Lock timeoutEventHeapLock; // 锁, mpit 、timeoutEventHeap、recentExpireTime
// 超时事件堆, 以超时时间排序的最小堆,初始化大小为1000
public PriorityBlockingQueue<TimeoutEvent> timeoutEventHeap
=new PriorityBlockingQueue<TimeoutEvent>(1000,new Comparator<TimeoutEvent>(){
@Override
public int compare(TimeoutEvent o1, TimeoutEvent o2) {
return Long.compare(o1.timeoutTime,o2.timeoutTime);
}
});
public Map<String, OnInterestInterface> mFib=new ConcurrentHashMap<>();
public List<PendingPacket> pendingPacketList=new LinkedList<>();
public PriorityBlockingQueue<TimeoutEvent> timeoutEventHeap;
public Map<String, OnInterestInterface> mFib;
public List<PendingPacket> pendingPacketList;
public LogicFaceICN() {
// PIT表。不初始化,可以根据它来判断本face是否初始化
timeoutEventHeapLock = new ReentrantLock();
timeoutEventHeap = new PriorityBlockingQueue<>(1000,new Comparator<TimeoutEvent>(){
@Override
public int compare(TimeoutEvent o1, TimeoutEvent o2) {
return Long.compare(o1.timeoutTime,o2.timeoutTime);
}
});
mFib = new ConcurrentHashMap<>();
pendingPacketList = new ArrayList<>();
}
/**
* 通过最长匹配原则查找FIB表,找到合适的兴趣包处理函数
@@ -78,6 +86,9 @@ public class LogicFaceICN extends LogicFace {
}
IdentifierWrapper identifierWrapper=interest.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry=this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
if(pitEntry == null) {
LoggerHelper.logger.info("no match pit entry");
}
OnInterestInterface oI=this.lookUpFib(identifierWrapper.getIdentifier());
if(oI!=null){
oI.onInterest(interest);
@@ -119,6 +130,7 @@ public class LogicFaceICN extends LogicFace {
IdentifierWrapper identifierWrapper=data.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry = this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
if(pitEntry==null){
LoggerHelper.logger.info("no match pit entry");
return;
}
pitEntry.onDataInterface.onData(pitEntry.interest,data);
@@ -141,6 +153,7 @@ public class LogicFaceICN extends LogicFace {
IdentifierWrapper identifierWrapper=nack.interest.minPacket.identifierField.getIdentifier(0);
PITEntry pitEntry = this.getPitEntryAndDelete(identifierWrapper.getIdentifier());
if(pitEntry==null){
LoggerHelper.logger.info("no match pit entry");
return;
}
pitEntry.onNackInterface.onNack(pitEntry.interest,nack);
@@ -182,20 +195,10 @@ public class LogicFaceICN extends LogicFace {
}
}
if(this.timeoutEventHeap.size()>0){
this.timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
},this.timeoutEventHeap.peek().timeoutTime-currentTime);
this.timer.reset(this.timeoutEventHeap.peek().timeoutTime-currentTime);
this.recentExpireTime=this.timeoutEventHeap.peek().timeoutTime;
}else{
this.timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
},500000);
this.timer.reset(500000);
this.recentExpireTime=currentTime+500000;
}
this.timeoutEventHeapLock.unlock();
@@ -204,27 +207,21 @@ public class LogicFaceICN extends LogicFace {
/**
* 等待 timer超时,触发调用dealWithTimeout函数
*/
// private void detectTimeout(){
// while (true){
// // this.timer.C
// this.dealWithTimeout();
// }
// }
// private void detectTimeout(){
// while (true){
// // this.timer.C
// this.dealWithTimeout();
// }
// }
/**
* 在 processEvent之前先初始化一些东西主要 包括初始化PIT表,和将定时器启动起来
*/
public void initBeforeProcessEvent(){
this.mPit=new ConcurrentHashMap<>();
this.timer=new Timer();
this.timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
},500000);
this.timer.schedule(this::dealWithTimeout,500000);
this.recentExpireTime=TimeHelper.getTimestampMS()+500000;
// ConcurrentHelper.doConcurrent(() -> detectTimeout());
// ConcurrentHelper.doConcurrent(() -> detectTimeout());
}
/**
@@ -236,6 +233,7 @@ public class LogicFaceICN extends LogicFace {
MINPacket minPacket = this.receivePacket();
if(minPacket==null){
LoggerHelper.logger.debug("LogicFaceICN.doReceivePacket: receivePacket null");
return;
}
IdentifierWrapper identifier=minPacket.identifierField.getIdentifier(0);
if(identifier==null){
@@ -259,12 +257,13 @@ public class LogicFaceICN extends LogicFace {
public void processEvent() throws LogicFaceException {
this.initBeforeProcessEvent();
for (int i = 0; i < this.pendingPacketList.size(); i++) {
if(this.pendingPacketList.get(i).data!=null){
this.putData(this.pendingPacketList.get(i).data);
PendingPacket pendingPacket = this.pendingPacketList.get(i);
if(pendingPacket.data!=null){
this.putData(pendingPacket.data);
continue;
}
this.expressInterest(this.pendingPacketList.get(i).interest,this.pendingPacketList.get(i).onDataInterface,
this.pendingPacketList.get(i).onTimeoutInterface,this.pendingPacketList.get(i).onNackInterface);
this.expressInterest(pendingPacket.interest,pendingPacket.onDataInterface,
pendingPacket.onTimeoutInterface, pendingPacket.onNackInterface);
}
this.pendingPacketList=null; // release the memory of pendingPacketLt
this.doReceivePacket();
@@ -287,12 +286,7 @@ public class LogicFaceICN extends LogicFace {
if(this.timeoutEventHeap.peek().timeoutTime<this.recentExpireTime){
long durationMs=this.timeoutEventHeap.peek().timeoutTime-TimeHelper.getTimestampMS();
this.recentExpireTime=this.timeoutEventHeap.peek().timeoutTime;
timer.schedule(new TimerTask() {
@Override
public void run() {
dealWithTimeout();
}
}, durationMs);
timer.reset(durationMs);
}
this.timeoutEventHeapLock.unlock();
}
@@ -347,7 +341,7 @@ public class LogicFaceICN extends LogicFace {
return;
}
IdentifierWrapper interestName=interest.minPacket.identifierField.getIdentifier(0);
Identifier interestName=interest.getName();
if(interestName==null){
LoggerHelper.logger.debug("LogicFaceICN.expressInterest: minPacket getIdentifier null");
return;
@@ -388,7 +382,7 @@ public class LogicFaceICN extends LogicFace {
}
}
// TODO:似乎更新了
public void registerPrefix(Identifier identifier,OnInterestInterface onInterestInterface,
OnRegisterFailInterface onRegisterFailInterface,
OnRegisterSuccessInterface onRegisterSuccessInterface) throws LogicFaceException {
+13 -5
View File
@@ -13,9 +13,17 @@ import packet.Interest;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class PITEntry{
public Interest interest = new Interest(); // 兴趣包
public long expireTime = 0; // 本PIT表项的超时时间
OnDataInterface onDataInterface = null; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface = null; // 兴趣包超时的处理函数
OnNackInterface onNackInterface = null; // 兴趣包无路由的处理函数
public Interest interest; // 兴趣包
public long expireTime; // 本PIT表项的超时时间
OnDataInterface onDataInterface; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface; // 兴趣包超时的处理函数
OnNackInterface onNackInterface; // 兴趣包无路由的处理函数
public PITEntry() {
interest = new Interest();
expireTime = 0;
onDataInterface = null;
onTimeoutInterface = null;
onNackInterface = null;
}
}
+13 -5
View File
@@ -14,9 +14,17 @@ import packet.Interest;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class PendingPacket {
public Interest interest=new Interest();
public Data data=new Data();
OnDataInterface onDataInterface = null; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface = null; // 兴趣包超时的处理函数
OnNackInterface onNackInterface = null; // 兴趣包无路由的处理函数
public Interest interest;
public Data data;
OnDataInterface onDataInterface; // 收到对应的数据包的处理函数
OnTimeoutInterface onTimeoutInterface; // 兴趣包超时的处理函数
OnNackInterface onNackInterface; // 兴趣包无路由的处理函数
public PendingPacket() {
interest = new Interest();
data = new Data();
onDataInterface = null;
onTimeoutInterface = null;
onNackInterface = null;
}
}
@@ -0,0 +1,44 @@
package util;
import java.util.Timer;
import java.util.TimerTask;
/*
* @Author: hongyu guo
* @Description:
* @Version: 1.0.0
* @Date: 21:35 2021/04/21
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ReschedulableTimer extends Timer {
private Runnable task;
private TimerTask timerTask;
public void schedule(Runnable runnable, long delay)
{
task = runnable;
timerTask = new TimerTask()
{
@Override
public void run()
{
task.run();
}
};
this.schedule(timerTask, delay);
}
public void reset(long delay)
{
timerTask.cancel();
timerTask = new TimerTask()
{
@Override
public void run()
{
task.run();
}
};
this.schedule(timerTask, delay);
}
}