forked from j62/ctbrec
Implement bandwidth meter for remote recording
This commit is contained in:
parent
a4ceb2f513
commit
583c4ec722
|
@ -10,7 +10,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class BandwidthMeter {
|
public class BandwidthMeter {
|
||||||
|
|
||||||
private static final Duration MEASURE_TIMEFRAME = Duration.ofSeconds(10);
|
public static final Duration MEASURE_TIMEFRAME = Duration.ofSeconds(10);
|
||||||
private static List<Record> records = new ArrayList<>(100);
|
private static List<Record> records = new ArrayList<>(100);
|
||||||
private static Lock lock = new ReentrantLock(true);
|
private static Lock lock = new ReentrantLock(true);
|
||||||
private static List<Listener> listeners = new ArrayList<>();
|
private static List<Listener> listeners = new ArrayList<>();
|
||||||
|
@ -30,14 +30,28 @@ public class BandwidthMeter {
|
||||||
|
|
||||||
Instant oneSecondAgo = Instant.now().minus(Duration.ofSeconds(1));
|
Instant oneSecondAgo = Instant.now().minus(Duration.ofSeconds(1));
|
||||||
if (lastUpdate.isBefore(oneSecondAgo)) {
|
if (lastUpdate.isBefore(oneSecondAgo)) {
|
||||||
long throughput = getThroughput();
|
fireEvent(getThroughput(), MEASURE_TIMEFRAME);
|
||||||
for (Listener listener : listeners) {
|
|
||||||
listener.bandwidthCalculated(throughput, MEASURE_TIMEFRAME);
|
|
||||||
}
|
|
||||||
lastUpdate = Instant.now();
|
lastUpdate = Instant.now();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setThroughput(long bytes, Duration d) {
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
records.clear();
|
||||||
|
records.add(new Record(bytes));
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
fireEvent(bytes, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void fireEvent(long throughput, Duration timeframe) {
|
||||||
|
for (Listener listener : listeners) {
|
||||||
|
listener.bandwidthCalculated(throughput, timeframe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the throughput over the last 10 seconds
|
* Get the throughput over the last 10 seconds
|
||||||
* @return throughput in bytes
|
* @return throughput in bytes
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -24,6 +25,7 @@ import ctbrec.Model;
|
||||||
import ctbrec.Recording;
|
import ctbrec.Recording;
|
||||||
import ctbrec.event.EventBusHolder;
|
import ctbrec.event.EventBusHolder;
|
||||||
import ctbrec.event.RecordingStateChangedEvent;
|
import ctbrec.event.RecordingStateChangedEvent;
|
||||||
|
import ctbrec.io.BandwidthMeter;
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpClient;
|
||||||
import ctbrec.io.HttpException;
|
import ctbrec.io.HttpException;
|
||||||
import ctbrec.io.InstantJsonAdapter;
|
import ctbrec.io.InstantJsonAdapter;
|
||||||
|
@ -208,6 +210,9 @@ public class RemoteRecorder implements Recorder {
|
||||||
JSONObject resp = new JSONObject(json);
|
JSONObject resp = new JSONObject(json);
|
||||||
spaceTotal = resp.getLong("spaceTotal");
|
spaceTotal = resp.getLong("spaceTotal");
|
||||||
spaceFree = resp.getLong("spaceFree");
|
spaceFree = resp.getLong("spaceFree");
|
||||||
|
long throughput = resp.getLong("throughput");
|
||||||
|
Duration timeframe = Duration.ofSeconds(resp.getInt("throughputTimeframe"));
|
||||||
|
BandwidthMeter.setThroughput(throughput, timeframe);
|
||||||
} else {
|
} else {
|
||||||
LOG.error(COULDNT_SYNCHRONIZE_WITH_SERVER_HTTP_STATUS, response.code(), json);
|
LOG.error(COULDNT_SYNCHRONIZE_WITH_SERVER_HTTP_STATUS, response.code(), json);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue