89 lines
2.8 KiB
Java
89 lines
2.8 KiB
Java
package SBC;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.util.Objects;
|
|
|
|
import static code.common.ValidString;
|
|
|
|
public class NetworkTransmitReceiveInfo {
|
|
public String name;
|
|
public long bytesReceived;
|
|
public long packetsReceived;
|
|
public long errorsReceived;
|
|
public long droppedReceived;
|
|
public long fifoReceived;
|
|
public long frameReceived;
|
|
public long compressedReceived;
|
|
public long multicastReceived;
|
|
public long bytesTransmitted;
|
|
public long packetsTransmitted;
|
|
public long errorsTransmitted;
|
|
public long droppedTransmitted;
|
|
public long fifoTransmitted;
|
|
public long collsTransmitted;
|
|
public long carrierTransmitted;
|
|
public long compressedTransmitted;
|
|
public long timetick;
|
|
|
|
/**
|
|
* Calculate the download speed
|
|
* @param prev Previous NetworkTransmitReceiveInfo
|
|
* @param unit Speed unit (KB, MB, GB)
|
|
* @return Download speed in {unit}/second
|
|
*/
|
|
public double RxSpeed(NetworkTransmitReceiveInfo prev, String unit){
|
|
if (prev!=null){
|
|
if (Objects.equals(prev.name, name)){
|
|
long timeDiff = timetick - prev.timetick;
|
|
if (timeDiff>0){
|
|
long bytesDiff = bytesReceived - prev.bytesReceived;
|
|
if (ValidString(unit)) unit = unit.toUpperCase();
|
|
Double speed = ConvertToUnit(unit, timeDiff, bytesDiff);
|
|
if (speed != null) return speed;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Nullable
|
|
private Double ConvertToUnit(String unit, long timeDiff, long bytesDiff) {
|
|
if (bytesDiff>0){
|
|
double speed = ((double) bytesDiff / timeDiff) * 1000;
|
|
switch (unit) {
|
|
case "KB" :
|
|
return (speed / 1024);
|
|
case "MB" :
|
|
return (speed / 1024 / 1024);
|
|
case "GB" :
|
|
return (speed / 1024 / 1024 / 1024);
|
|
default :
|
|
return speed;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Calculate the upload speed
|
|
* @param prev Previous NetworkTransmitReceiveInfo
|
|
* @param unit Speed unit (KB, MB, GB)
|
|
* @return Upload speed in {unit}/second
|
|
*/
|
|
public double TxSpeed(NetworkTransmitReceiveInfo prev, String unit){
|
|
if (prev!=null){
|
|
if (Objects.equals(prev.name, name)){
|
|
long timeDiff = timetick - prev.timetick;
|
|
if (timeDiff>0){
|
|
long bytesDiff = bytesTransmitted - prev.bytesTransmitted;
|
|
if (ValidString(unit)) unit = unit.toUpperCase();
|
|
Double speed = ConvertToUnit(unit, timeDiff, bytesDiff);
|
|
if (speed != null) return speed;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|