Commit 656bb28a authored by Max Lv's avatar Max Lv

Stop the timer when all callbacks removed

parent 6e974da8
Subproject commit f226d47ea43fe4705e80d2b922f5ecdaf9d8cc24
Subproject commit d462e4cd8d44d7275957afab9255b98e54bb2630
......@@ -73,6 +73,10 @@ trait BaseService {
callbacks.unregister(cb)
callbackCount -= 1
}
if (callbackCount == 0 && timer != null) {
timer.cancel()
timer = null
}
if (callbackCount == 0 && state != State.CONNECTING && state != State.CONNECTED) {
stopBackgroundService()
}
......@@ -80,6 +84,18 @@ trait BaseService {
override def registerCallback(cb: IShadowsocksServiceCallback) {
if (cb != null) {
if (callbackCount == 0 && timer == null) {
val task = new TimerTask {
def run {
TrafficMonitor.updateRate()
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
}
}
timer = new Timer(true)
timer.schedule(task, 1000, 1000)
}
callbacks.register(cb)
callbackCount += 1
}
......@@ -102,30 +118,19 @@ trait BaseService {
this.config = config
TrafficMonitor.reset()
trafficMonitorThread = new TrafficMonitorThread(this)
trafficMonitorThread = new TrafficMonitorThread()
trafficMonitorThread.start()
val task = new TimerTask {
def run {
clearChildProcessStream()
}
}
timer = new Timer(true)
timer.schedule(task, 60 * 1000, 60 * 1000)
}
def stopRunner() {
// Make sure update total traffic when stopping the runner
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
TrafficMonitor.reset()
if (trafficMonitorThread != null) {
trafficMonitorThread.stopThread()
trafficMonitorThread = null
}
if (timer != null) {
timer.cancel()
timer = null
}
}
def updateTrafficTotal(tx: Long, rx: Long) {
......@@ -136,7 +141,6 @@ trait BaseService {
case Some(profile) =>
profile.tx += tx
profile.rx += rx
Log.d("test", profile.tx + " " + profile.rx)
ShadowsocksApplication.profileManager.updateProfile(profile)
case None => // Ignore
}
......@@ -144,7 +148,6 @@ trait BaseService {
})
}
def clearChildProcessStream()
def stopBackgroundService()
def getServiceMode: Int
def getTag: String
......
......@@ -55,6 +55,13 @@ trait ServiceBoundContext extends Context {
def deattachService() {
if (bgService != null) {
if (callback != null) {
try {
bgService.unregisterCallback(callback)
} catch {
case ignored: RemoteException => // Nothing
}
}
unbindService(connection)
bgService = null
}
......
......@@ -429,17 +429,6 @@ class ShadowsocksNatService extends Service with BaseService {
Console.runRootCommand(http_sb.toArray)
}
override def clearChildProcessStream() {
try {
sslocalProcess.getInputStream.skip(65535)
sstunnelProcess.getInputStream.skip(65535)
pdnsdProcess.getInputStream.skip(65535)
redsocksProcess.getInputStream.skip(65535)
} catch {
case ex: Exception => // Ignore
}
}
override def startRunner(config: Config) {
super.startRunner(config)
......
......@@ -214,17 +214,6 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}
}
override def clearChildProcessStream() {
try {
sslocalProcess.getInputStream.skip(65535)
sstunnelProcess.getInputStream.skip(65535)
pdnsdProcess.getInputStream.skip(65535)
tun2socksProcess.getInputStream.skip(65535)
} catch {
case ex: Exception => // Ignore
}
}
override def startRunner(config: Config) {
super.startRunner(config)
......
......@@ -39,20 +39,23 @@ object TrafficMonitor {
else numberFormat.format(n) + ' ' + units(i)
}
def update(tx: Long, rx: Long) {
val now = getTraffic(tx, rx)
def updateRate() {
val now = getTraffic(txTotal, rxTotal)
val delta = now.timestamp - last.timestamp
txLast = now.tx - last.tx
rxLast = now.rx - last.rx
val deltaTx = now.tx - last.tx
val deltaRx = now.rx - last.rx
if (delta != 0) {
txRate = txLast * 1000 / delta
rxRate = rxLast * 1000 / delta
txRate = deltaTx * 1000 / delta
rxRate = deltaRx * 1000 / delta
}
txTotal += txLast
rxTotal += rxLast
last = now
}
def update(tx: Long, rx: Long) {
txTotal = tx;
rxTotal = rx;
}
def reset() {
txRate = 0
rxRate = 0
......@@ -84,5 +87,17 @@ object TrafficMonitor {
def getRate(): String = {
formatTraffic(txRate + rxRate)
}
def getDeltaTx(): Long = {
val last = txLast
txLast = txTotal
txTotal - last
}
def getDeltaRx(): Long = {
val last = rxLast
rxLast = rxTotal
rxTotal - last
}
}
......@@ -46,7 +46,7 @@ import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress}
import android.util.Log
import com.github.shadowsocks.BaseService
class TrafficMonitorThread(service: BaseService) extends Thread {
class TrafficMonitorThread() extends Thread {
val TAG = "TrafficMonitorThread"
val PATH = "/data/data/com.github.shadowsocks/stat_path"
......@@ -108,9 +108,6 @@ 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)
}
output.write(0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment