-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnhancedDatagramSocket.java
147 lines (125 loc) · 4.95 KB
/
EnhancedDatagramSocket.java
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
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Date;
public class EnhancedDatagramSocket extends DatagramSocket {
public EnhancedDatagramSocket(int port) throws SocketException {
super(port);
randomGenerator = new Random(System.currentTimeMillis());
payloadLimitInBytes = DEFAULT_PAYLOAD_LIMIT_IN_BYTES;
lossRate = DEFAULT_LOSS_RATE;
delayInMilliseconds = DEFAULT_DELAY_IN_MILLISECONDS;
timer = new Timer();
plotData = Plot.data();
plotData.xy(0, 0);
currentTime = 0;
currentSecondSentBytes = new AtomicLong(0);
scheduleToCollectPlotData();
}
@Override
public void send(DatagramPacket datagramPacket) throws IOException {
if (datagramPacket.getLength() > payloadLimitInBytes)
throw new PayloadLimitViolationException(
String.format("Payload limit is %d bytes but sending packet contains %d bytes.",
payloadLimitInBytes,
datagramPacket.getLength()
));
currentSecondSentBytes.addAndGet(datagramPacket.getLength());
if (randomGenerator.nextDouble() <= lossRate)
return;
if (delayInMilliseconds != 0)
scheduleToSend(datagramPacket);
else
super.send(datagramPacket);
}
@Override
public void close() {
super.close();
try {
Thread.sleep(SENT_BYTES_SAMPLING_PERIOD_IN_MILLISECONDS);
} catch (InterruptedException ignored) {
}
timer.cancel();
saveSentBytesPerMillisecondPlot();
}
public int getPayloadLimitInBytes() {
return payloadLimitInBytes;
}
public void setPayloadLimitInBytes(int payloadLimitInBytes) {
if (payloadLimitInBytes < 0)
throw new RuntimeException("Payload limit cannot be negative.");
this.payloadLimitInBytes = payloadLimitInBytes;
}
public double getLossRate() {
return lossRate;
}
public void setLossRate(double lossRate) {
if (lossRate < 0)
throw new RuntimeException("Loss rate cannot be negative.");
this.lossRate = lossRate;
}
public void setDelayInMilliseconds(int delayInMilliseconds) {
if (delayInMilliseconds < 0)
throw new RuntimeException("Delay cannot be negative.");
this.delayInMilliseconds = delayInMilliseconds;
}
public long getDelayInMilliseconds() {
return delayInMilliseconds;
}
private void saveSentBytesPerMillisecondPlot() {
plotData.xy(currentTime + SENT_BYTES_SAMPLING_PERIOD_IN_MILLISECONDS, 0);
Plot.PlotOptions plotOptions = Plot.plotOpts();
Plot.DataSeriesOptions dataSeriesOptions = new Plot.DataSeriesOptions();
plotOptions.title("Sent bytes per ms");
try {
Plot.plot(plotOptions)
.series(null, plotData, dataSeriesOptions)
.yAxis("byte", null)
.xAxis("ms", null)
.save(String.format("BytesPerTime [%s]", new Date().toString().replace(":", "-")), "png");
} catch (IOException e) {
e.printStackTrace();
}
}
private void scheduleToSend(DatagramPacket datagramPacket) {
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
if (!isClosed())
EnhancedDatagramSocket.super.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
}
}
}, delayInMilliseconds);
}
private void scheduleToCollectPlotData() {
timer.schedule(new TimerTask() {
@Override
public void run() {
currentTime += SENT_BYTES_SAMPLING_PERIOD_IN_MILLISECONDS;
plotData.xy(currentTime, currentSecondSentBytes.getAndSet(0));
if (!isClosed())
scheduleToCollectPlotData();
}
}, SENT_BYTES_SAMPLING_PERIOD_IN_MILLISECONDS);
}
private static final int DEFAULT_PAYLOAD_LIMIT_IN_BYTES = 1408;
private static final double DEFAULT_LOSS_RATE = 0;
private static final long DEFAULT_DELAY_IN_MILLISECONDS = 0;
private static final int SENT_BYTES_SAMPLING_PERIOD_IN_MILLISECONDS = 50;
private int payloadLimitInBytes;
private double lossRate;
private long delayInMilliseconds;
private Random randomGenerator;
private Timer timer;
private Plot.Data plotData;
private int currentTime;
private AtomicLong currentSecondSentBytes;
}