package cn.minoa.dataRequestInterface; /** * @author mac * @description * 拥塞窗口 * @date 2020-03-24 10:25 */ public class CongestionWindow { //拥塞窗口大小 private Double cwnd; //表示当前正在发送的数据,发送一条数据需要加一,收到成功恢复需要减一 private Double inFlight; //拥塞窗口阈值 private Double thresh; //下一次窗口减小的界限 private Integer nSupress = 0; //收到数据包的数量,收到一个数据包进行加一操作 private Long nIndata = 0l; //发送兴趣包的数量,发出一个兴趣包进行加一操作 private Long nOutInterest = 0l; //更新nsupress时的一个比率 private static Double rate = 0.5d; public CongestionWindow(){ this.cwnd = 4d; this.thresh = 300d; this.inFlight = 0d; } public CongestionWindow(Double cwnd, Double thresh, Double inFlight){ this.inFlight = inFlight; this.cwnd = cwnd; this.thresh = thresh; } /** * 当前窗口是否为空 * @return */ public boolean empty(){ boolean res = false; synchronized (this){ if (inFlight == 0){ res = true; } } return res; } /** * 当前窗口是否已满 * @return */ public boolean full(){ boolean res = false; synchronized (this){ if(cwnd == inFlight){ res = true; } } return res; } /** * 获取当前还能发送多少数据 * 如果不能发送数据,返回值为0 * @return */ public int getAvailableCwnd(){ int res = 0; synchronized (this){ if(inFlight >= cwnd){ res = 0; }else{ res = (int)(cwnd - inFlight); } } return res; } /** * 在onData函数中 * 收到一个数据包,执行加一操作,需要加锁 */ public void nIndataIncrease(){ synchronized (this){ nIndata++; } } /** * 发出一个兴趣包,执行加一操作,不需要加锁 */ public void nOutInterestIncrease(){ nOutInterest++; } /** * inFlight加一 */ public void inFlightIncrease(){ synchronized (this){ this.inFlight++; } } public void inFlightIncrease(int inFlight){ synchronized (this){ this.inFlight += inFlight; } } /** * inFliehgt减一 */ public void inFlightDecrease(){ synchronized (this){ this.inFlight--; } } public void inFlightDecrease(int inFlight){ synchronized (this){ this.inFlight -= inFlight; } } /** * 增加拥塞窗口的值 * */ public void cwndIncrease(){ synchronized (this){ if(this.cwnd < this.thresh){ //慢开始算法 this.cwnd += 1; /*if(this.cwnd > this.thresh){ this.thresh = this.cwnd; }*/ }else{ //拥塞避免算法 this.cwnd += (1.0/this.cwnd); } } System.out.println("-----------------------------------------cwnd:" + cwnd + ", thresh:" + thresh); } /** * 减少拥塞窗口的值 * 每当检测到拥塞时,就需要执行此函数 */ public void cwndDecrease(){ synchronized (this){ if(nIndata >= nSupress){ // this.thresh = Math.floor(this.cwnd/2); this.thresh = this.cwnd/2; this.cwnd = this.thresh; nSupress = (int)(nOutInterest + rate*(nOutInterest - nIndata)); } } System.out.println("-----------------------------------------cwnd:" + cwnd + ", thresh:" + thresh); } /** * 设置拥塞窗口大小 * @param cwnd */ public void setCwnd(Double cwnd){ synchronized (this){ this.cwnd = cwnd; } } /** * 获得当前拥塞窗口大小 * @return */ public Integer getCwnd(){ int res = 0; synchronized (this){ res = this.cwnd.intValue(); } return res; } @Override public String toString() { return "CongestionWindow {" + "cwnd=" + cwnd + ", inFlight=" + inFlight + ", thresh=" + thresh + '}'; } }