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 { ...@@ -73,6 +73,10 @@ trait BaseService {
callbacks.unregister(cb) callbacks.unregister(cb)
callbackCount -= 1 callbackCount -= 1
} }
if (callbackCount == 0 && timer != null) {
timer.cancel()
timer = null
}
if (callbackCount == 0 && state != State.CONNECTING && state != State.CONNECTED) { if (callbackCount == 0 && state != State.CONNECTING && state != State.CONNECTED) {
stopBackgroundService() stopBackgroundService()
} }
...@@ -80,6 +84,18 @@ trait BaseService { ...@@ -80,6 +84,18 @@ trait BaseService {
override def registerCallback(cb: IShadowsocksServiceCallback) { override def registerCallback(cb: IShadowsocksServiceCallback) {
if (cb != null) { 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) callbacks.register(cb)
callbackCount += 1 callbackCount += 1
} }
...@@ -102,30 +118,19 @@ trait BaseService { ...@@ -102,30 +118,19 @@ trait BaseService {
this.config = config this.config = config
TrafficMonitor.reset() TrafficMonitor.reset()
trafficMonitorThread = new TrafficMonitorThread(this) trafficMonitorThread = new TrafficMonitorThread()
trafficMonitorThread.start() trafficMonitorThread.start()
val task = new TimerTask {
def run {
clearChildProcessStream()
}
}
timer = new Timer(true)
timer.schedule(task, 60 * 1000, 60 * 1000)
} }
def stopRunner() { def stopRunner() {
// Make sure update total traffic when stopping the runner
updateTrafficTotal(TrafficMonitor.getDeltaTx, TrafficMonitor.getDeltaRx)
TrafficMonitor.reset() TrafficMonitor.reset()
if (trafficMonitorThread != null) { if (trafficMonitorThread != null) {
trafficMonitorThread.stopThread() trafficMonitorThread.stopThread()
trafficMonitorThread = null trafficMonitorThread = null
} }
if (timer != null) {
timer.cancel()
timer = null
}
} }
def updateTrafficTotal(tx: Long, rx: Long) { def updateTrafficTotal(tx: Long, rx: Long) {
...@@ -136,7 +141,6 @@ trait BaseService { ...@@ -136,7 +141,6 @@ trait BaseService {
case Some(profile) => case Some(profile) =>
profile.tx += tx profile.tx += tx
profile.rx += rx profile.rx += rx
Log.d("test", profile.tx + " " + profile.rx)
ShadowsocksApplication.profileManager.updateProfile(profile) ShadowsocksApplication.profileManager.updateProfile(profile)
case None => // Ignore case None => // Ignore
} }
...@@ -144,7 +148,6 @@ trait BaseService { ...@@ -144,7 +148,6 @@ trait BaseService {
}) })
} }
def clearChildProcessStream()
def stopBackgroundService() def stopBackgroundService()
def getServiceMode: Int def getServiceMode: Int
def getTag: String def getTag: String
......
...@@ -55,6 +55,13 @@ trait ServiceBoundContext extends Context { ...@@ -55,6 +55,13 @@ trait ServiceBoundContext extends Context {
def deattachService() { def deattachService() {
if (bgService != null) { if (bgService != null) {
if (callback != null) {
try {
bgService.unregisterCallback(callback)
} catch {
case ignored: RemoteException => // Nothing
}
}
unbindService(connection) unbindService(connection)
bgService = null bgService = null
} }
......
...@@ -429,17 +429,6 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -429,17 +429,6 @@ class ShadowsocksNatService extends Service with BaseService {
Console.runRootCommand(http_sb.toArray) 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) { override def startRunner(config: Config) {
super.startRunner(config) super.startRunner(config)
......
...@@ -214,17 +214,6 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -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) { override def startRunner(config: Config) {
super.startRunner(config) super.startRunner(config)
......
...@@ -39,20 +39,23 @@ object TrafficMonitor { ...@@ -39,20 +39,23 @@ object TrafficMonitor {
else numberFormat.format(n) + ' ' + units(i) else numberFormat.format(n) + ' ' + units(i)
} }
def update(tx: Long, rx: Long) { def updateRate() {
val now = getTraffic(tx, rx) val now = getTraffic(txTotal, rxTotal)
val delta = now.timestamp - last.timestamp val delta = now.timestamp - last.timestamp
txLast = now.tx - last.tx val deltaTx = now.tx - last.tx
rxLast = now.rx - last.rx val deltaRx = now.rx - last.rx
if (delta != 0) { if (delta != 0) {
txRate = txLast * 1000 / delta txRate = deltaTx * 1000 / delta
rxRate = rxLast * 1000 / delta rxRate = deltaRx * 1000 / delta
} }
txTotal += txLast
rxTotal += rxLast
last = now last = now
} }
def update(tx: Long, rx: Long) {
txTotal = tx;
rxTotal = rx;
}
def reset() { def reset() {
txRate = 0 txRate = 0
rxRate = 0 rxRate = 0
...@@ -84,5 +87,17 @@ object TrafficMonitor { ...@@ -84,5 +87,17 @@ object TrafficMonitor {
def getRate(): String = { def getRate(): String = {
formatTraffic(txRate + rxRate) 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} ...@@ -46,7 +46,7 @@ import android.net.{LocalServerSocket, LocalSocket, LocalSocketAddress}
import android.util.Log import android.util.Log
import com.github.shadowsocks.BaseService import com.github.shadowsocks.BaseService
class TrafficMonitorThread(service: BaseService) extends Thread { class TrafficMonitorThread() extends Thread {
val TAG = "TrafficMonitorThread" val TAG = "TrafficMonitorThread"
val PATH = "/data/data/com.github.shadowsocks/stat_path" val PATH = "/data/data/com.github.shadowsocks/stat_path"
...@@ -108,9 +108,6 @@ class TrafficMonitorThread(service: BaseService) extends Thread { ...@@ -108,9 +108,6 @@ class TrafficMonitorThread(service: BaseService) extends Thread {
val stat = new String(array, "UTF-8").split("\\|") val stat = new String(array, "UTF-8").split("\\|")
if (stat.length == 2) { if (stat.length == 2) {
TrafficMonitor.update(stat(0).toLong, stat(1).toLong) 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) 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