-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufferbloat.pde
275 lines (227 loc) · 5.86 KB
/
bufferbloat.pde
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
float x,y;
float hs = 100.0;
float ps = 20;
float rate = 15;
int maxPackets = 20;
Host host1;
Host host2;
Transaction transaction1;
// Processing.js functions
void setup() {
// size of canvas
size(800,200);
noStroke();
frameRate(30);
host1 = new Host(width/6,height/2,hs);
host2 = new Host(width/6*5,height/2,hs);
transaction1 = new Transaction(maxPackets);
transaction1.setPath(width/6+hs/2,height/2,width/6*5-hs/2,height/2);
transaction1.setPause(true);
}
void draw() {
background(#222222);
// draw hosts
host1.display();
host2.display();
// draw connection between hosts
line(width/6,height/2,width/6*5,height/2);
transaction1.display();
}
// class definitions
class Host {
float xpos;
float ypos;
float size;
Host(float x, float y, float s) {
xpos = x;
ypos = y;
size = s;
}
void display() {
stroke();
fill(255);
rectMode(CENTER);
rect(xpos,ypos,size,size);
}
}
class Packet {
private float size;
private float rate;
private int color;
private int timer;
boolean done;
private float xpos;
private float ypos;
private float xStart;
private float yStart;
private float xEnd;
private float yEnd;
Packet(float _size, float _rate, int _color, int _timer) {
size = _size;
rate = _rate;
color = _color;
timer = _timer;
done = false;
}
Packet(float _size, float _rate, int _color) {
size = _size;
rate = _rate;
color = _color;
timer = -1;
done = false;
}
void setPath(float _xStart, float _yStart, float _xEnd, float _yEnd) {
xStart = _xStart;
yStart = _yStart;
xEnd = _xEnd;
yEnd = _yEnd;
xpos = xStart;
ypos = yStart;
}
void display() {
if (timer != 0) {
//console.log(xpos + "," + ypos);
fill(color);
ellipse(xpos,ypos,size,size);
// defines rate at which the packet animates
xpos = getDelta(xpos,xStart,xEnd);
ypos = getDelta(ypos,yStart,yEnd);
}
}
private float getDelta(float delta, float start, float end) {
if (start == end) {
delta = start;
} else {
if (start < end) {
delta = delta + rate;
if (delta > end) {
delta = start;
timer--;
}
} else {
delta = delta - rate;
if (delta < end) {
delta = start;
timer--;
}
}
}
if (timer == 0) {
done = true;
}
return delta;
}
}
class Stream {
int maxPackets;
int packetCount;
int timer;
int color;
boolean useTimer;
boolean pause;
int packetsDone;
boolean[] packetState;
Packet[] packets;
float xStart;
float yStart;
float xEnd;
float yEnd;
Stream (int _maxPackets) {
maxPackets = _maxPackets;
packetCount = 0;
timer = 0;
color = #0000ff;
useTimer = true;
pause = false;
packetsDone = 0;
packetState = new boolean[maxPackets];
packets = new Packet[maxPackets];
}
void setColor (int _color) {
color = _color;
}
void setPath(float _xStart, float _yStart, float _xEnd, float _yEnd) {
xStart = _xStart;
yStart = _yStart;
xEnd = _xEnd;
yEnd = _yEnd;
}
void setTimer(boolean status) {
useTimer = status;
}
void setPause(boolean status) {
pause = status;
}
void addPacket() {
if (packetCount < maxPackets) {
// rate is a global variable
packets[packetCount] = new Packet(ps,rate,color,1);
packets[packetCount].setPath(xStart,yStart,xEnd,yEnd);
packetState[packetCount] = false;
packetCount++;
}
}
void display() {
for (i = 0; i < packetCount; i++) {
packets[i].display();
if (packetState[i] == false) {
if (packets[i].done == true) {
packetState[i] = true;
packetsDone++;
}
}
}
if (useTimer == true) {
if (pause == false) {
timer++;
if (timer % 5 == 0) {
addPacket();
}
}
}
}
}
class Transaction {
int maxPackets;
int packetsTransmitted;
boolean pause;
Stream transmit;
Stream receive;
float xStart;
float yStart;
float xEnd;
float yEnd;
Transaction(int _maxPackets) {
maxPackets = _maxPackets;
packetsTransmitted = 0;
transmit = new Stream(maxPackets);
transmit.setColor(#00ff00);
receive = new Stream(maxPackets);
receive.setColor(#0000ff);
receive.setTimer(false);
}
void setPath(float _xStart, float _yStart, float _xEnd, float _yEnd) {
xStart = _xStart;
yStart = _yStart;
xEnd = _xEnd;
yEnd = _yEnd;
transmit.setPath(xStart,yStart,xEnd,yEnd);
receive.setPath(xEnd,yEnd,xStart,yStart);
}
void setPause(boolean status) {
transmit.setPause(status);
receive.setPause(status);
}
void display() {
transmit.display();
receive.display();
if (packetsTransmitted != transmit.packetsDone) {
//console.log(packetsTransmitted + "," + transmit.packetsDone);
int packetsToAdd = transmit.packetsDone - packetsTransmitted;
for (int i=0; i < packetsToAdd; i++) {
receive.addPacket();
}
packetsTransmitted = transmit.packetsDone;
}
}
}