Add realtime traffic in profiles selector

This commit is contained in:
Mygod
2015-11-21 16:04:55 +08:00
parent c08296890a
commit 407ba8beaa
10 changed files with 146 additions and 156 deletions
@@ -10,15 +10,15 @@ case class Traffic(tx: Long, rx: Long, timestamp: Long)
object TrafficMonitor {
var last: Traffic = getTraffic(0, 0)
// Kilo bytes per second
// Bytes per second
var txRate: Long = 0
var rxRate: Long = 0
// Kilo bytes for the current session
// Bytes for the current session
var txTotal: Long = 0
var rxTotal: Long = 0
// Kilo bytes for the last query
// Bytes for the last query
var txLast: Long = 0
var rxLast: Long = 0
@@ -42,14 +42,14 @@ object TrafficMonitor {
def update(tx: Long, rx: Long) {
val now = getTraffic(tx, rx)
val delta = now.timestamp - last.timestamp
if (delta != 0) {
txRate = (now.tx - last.tx) * 1000 / delta
rxRate = (now.rx - last.rx) * 1000 / delta
}
txLast = now.tx - last.tx
rxLast = now.rx - last.rx
txTotal += now.tx - last.tx
rxTotal += now.rx - last.rx
if (delta != 0) {
txRate = txLast * 1000 / delta
rxRate = rxLast * 1000 / delta
}
txTotal += txLast
rxTotal += rxLast
last = now
}
@@ -70,7 +70,7 @@ class TrafficMonitorThread(service: BaseService) extends Thread {
closeServerSocket()
}
override def run(): Unit = {
override def run() {
try {
new File(PATH).delete()
@@ -108,9 +108,9 @@ class TrafficMonitorThread(service: BaseService) extends Thread {
val stat = new String(array, "UTF-8").split("\\|")
if (stat.length == 2) {
TrafficMonitor.update(stat(0).toLong, stat(1).toLong)
service.updateTrafficTotal(TrafficMonitor.txLast, TrafficMonitor.rxLast)
service.updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
service.updateTrafficTotal(TrafficMonitor.txLast, TrafficMonitor.rxLast)
}
output.write(0)