-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPINGStatTracker.java
More file actions
73 lines (66 loc) · 2.85 KB
/
PINGStatTracker.java
File metadata and controls
73 lines (66 loc) · 2.85 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
//Classe che tiene traccia delle statistiche dei pacchetti di cui se ne vuole tener traccia
public class PINGStatTracker {
protected long packetsSendTime[];
protected long packetsRTT[];
protected int packetsSent;
protected int packetsLost;
protected int packetsReceived;
protected float percentPacketLoss;
protected long minRTT;
protected long maxRTT;
protected long avgRTT;
public PINGStatTracker(int packetsNumber){
packetsSendTime = new long[packetsNumber];
packetsRTT = new long[packetsNumber];
packetsSent = packetsNumber;
percentPacketLoss = 0;
avgRTT = 0;
packetsReceived = 0;
}
//Metodo da invocare ogni volta che si invia un pacchetto, che registra il momento in cui viene effettuato l' invio
public String updateSend(int packetID){
long sentTime = System.currentTimeMillis();
packetsSendTime[packetID] = sentTime;
return " " + String.valueOf(sentTime);
}
//Metodo da invocare ogni volta che riceviamo un pacchetto, in modo da registrare il tempo che esso ha impiegato per
//essere spedito dal server al client, con annessa stampa
public void updateOnReceive(int packetID){
packetsRTT[packetID] = System.currentTimeMillis() - packetsSendTime[packetID];
packetsReceived++;
System.out.println("PING " + packetID + " " + packetsSendTime[packetID] + " RTT: " + packetsRTT[packetID] + " ms");
}
//Metodo da invocare ogni volta che un pacchetto è stato perso, con annessa stampa di avviso
public void updateOnFailedReceive(int packetID){
packetsRTT[packetID] = -1;
packetsLost++;
System.out.println("PING " + packetID + " " + packetsSendTime[packetID] + " RTT: *");
}
//Metodo per aggiornare ogni statistica finale dei pacchetti
private void updateStatistics(){
percentPacketLoss = ((float) packetsLost / (float) packetsSent) * 100;
int i;
minRTT = packetsRTT[0];
maxRTT = packetsRTT[0];
for(i = 0; i < packetsSent; i++){
if(packetsRTT[i] != -1){
if(packetsRTT[i] < minRTT || minRTT == -1)
minRTT = packetsRTT[i];
else if(packetsRTT[i] > maxRTT)
maxRTT = packetsRTT[i];
avgRTT += packetsRTT[i];
}
}
if(packetsReceived > 0)
avgRTT = avgRTT / packetsReceived;
else
avgRTT = -1;
}
//Stampa dei risultati ottenuti
public void printStatistics(){
updateStatistics();
System.out.println("---- PING Statistics ----");
System.out.println(packetsSent + " packets transmitted, " + packetsReceived + " packets received, " + percentPacketLoss + "% packet loss");
System.out.println("round trip (ms) min/avg/max = " + minRTT + "/" + avgRTT + "/" + maxRTT);
}
}