-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicWindow.java
More file actions
186 lines (143 loc) · 4.02 KB
/
DynamicWindow.java
File metadata and controls
186 lines (143 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import java.util.*;
public class DynamicWindow {
private Queue<Transport> BufferedPackage;
private Queue<Long> SendingTime;
private double capacity = 0;
private int writePos = 0;
private int Num = 0;
private int lastSeq = -1;
private double threshold;
boolean ifCongestionControl = false;
public DynamicWindow( ) {
this.capacity = 1;
this.BufferedPackage = new LinkedList<Transport>();
this.SendingTime = new LinkedList<Long>();
this. threshold = 64000;
}
public void setSize(int s) {
this.capacity = s;
}
public void GetCongestionControl() {
this.ifCongestionControl = true;
}
public boolean available() {
return ((int)(this.capacity) - this.Num) > 0;
}
public boolean empty() {
return this.NumOfPackage() == 0;
}
public boolean add(Transport element, long Time){
if(Num < (int)capacity){
// if(writePos >= capacity){
// writePos = 0;
// }
// BufferedPackage[writePos] = element;
// SendingTime[writePos] = Time;
// //////System.out.println("time"+ Time);
// lastSeq = element.getSeqNum();
// writePos++;
// Num++;
// if(writePos >= capacity){
// writePos = 0;
// }
lastSeq = element.getSeqNum();
SendingTime.add(Time);
BufferedPackage.add(element);
Num ++;
return true;
}
return false;
}
public Transport RemoveandAdd(long time) {
if(Num == 0){
return null;
}
// int nextSlot = writePos - Num;
// if(nextSlot < 0){
// nextSlot += capacity;
// }
Transport nextpackage = BufferedPackage.remove();
BufferedPackage.add(nextpackage);
long t = SendingTime.remove();
SendingTime.add(time);
return nextpackage;
}
public Transport remove() {
if(Num == 0){
return null;
}
// int nextSlot = writePos - Num;
// if(nextSlot < 0){
// nextSlot += capacity;
// }
Transport nextpackage = BufferedPackage.remove();
long time = SendingTime.remove();
Num--;
return nextpackage;
}
public int NumOfPackage() {
return Num;
}
public double getsize() {
return capacity;
}
public long GetTime() {
// if(Num == 0) {
// return -1;
// }
// int nextSlot = writePos - Num;
// if(nextSlot < 0){
// nextSlot += capacity;
// }
//
// return SendingTime[nextSlot];
return SendingTime.peek();
}
public int GetNextSeq() {
return this.lastSeq+1;
}
public int GetFirstSeq() {
// int nextSlot = writePos - Num;
// if (nextSlot < 0) {
// nextSlot += capacity;
// }
Transport tp = BufferedPackage.peek();
return tp.getSeqNum();
}
public int Slide(int ack) {
//////System.out.println("unack " + Num);
//////System.out.println("Receive ACK "+ ack);
// //System.err.println("this first seq"+ this.GetFirstSeq());
int i = 0;
while(this.NumOfPackage() > 0 && this.GetFirstSeq() < ack) {
Transport tp = this.remove();
i++;
// System.err.println(this.GetFirstSeq());
// ////System.out.println(tp);
// ////System.out.println(tp.getSeqNum());
}
return i;
}
public void Increase() {
if(!ifCongestionControl) return;
if(capacity < this.threshold) {
capacity = capacity +1;
}
else capacity = capacity +1/capacity;
}
public void shrink() {
if(!ifCongestionControl) return;
//System.out.println("--------------shrink");
//System.out.println(capacity);
if(capacity/2 > 0) {
this.threshold = this.capacity/2;
capacity = this.capacity/2;
} else capacity =1;
}
public void reset() {
if(!ifCongestionControl) return;
// System.out.println("reset..............window");
this.threshold = this.capacity/2;
this.capacity = 1;
}
}