From 51ceb3dac55992a59d08aae7df193d515df2ded2 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 26 Dec 2015 23:10:54 +0800 Subject: [PATCH] Refine callback handling --- .../com/github/shadowsocks/BaseService.scala | 20 +++++-------------- .../shadowsocks/ShadowsocksNatService.scala | 10 ++-------- .../shadowsocks/ShadowsocksVpnService.scala | 10 ++-------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/main/scala/com/github/shadowsocks/BaseService.scala b/src/main/scala/com/github/shadowsocks/BaseService.scala index 777290a5..a2b7528b 100644 --- a/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -50,7 +50,6 @@ import com.github.shadowsocks.utils.{State, TrafficMonitor, TrafficMonitorThread trait BaseService extends Service { @volatile private var state = State.STOPPED - @volatile private var callbackCount = 0 @volatile protected var config: Config = null var timer: Timer = null @@ -70,20 +69,17 @@ trait BaseService extends Service { override def unregisterCallback(cb: IShadowsocksServiceCallback) { if (cb != null ) { callbacks.unregister(cb) - callbackCount -= 1 } - if (callbackCount == 0 && timer != null) { + if (callbacks.getRegisteredCallbackCount == 0 && timer != null) { timer.cancel() timer = null } - if (callbackCount == 0 && state == State.STOPPED) { - stopBackgroundService() - } } override def registerCallback(cb: IShadowsocksServiceCallback) { if (cb != null) { - if (callbackCount == 0 && timer == null) { + callbacks.register(cb) + if (callbacks.getRegisteredCallbackCount != 0 && timer == null) { val task = new TimerTask { def run { if (TrafficMonitor.updateRate()) updateTrafficRate() @@ -92,8 +88,6 @@ trait BaseService extends Service { timer = new Timer(true) timer.schedule(task, 1000, 1000) } - callbacks.register(cb) - callbackCount += 1 TrafficMonitor.updateRate() cb.trafficUpdated(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate, TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal) @@ -145,14 +139,10 @@ trait BaseService extends Service { } } - def stopBackgroundService() def getServiceMode: Int def getTag: String def getContext: Context - def getCallbackCount: Int = { - callbackCount - } def getState: Int = { state } @@ -163,7 +153,7 @@ trait BaseService extends Service { def updateTrafficRate() { val handler = new Handler(getContext.getMainLooper) handler.post(() => { - if (callbackCount > 0) { + if (callbacks.getRegisteredCallbackCount > 0) { val txRate = TrafficMonitor.getTxRate val rxRate = TrafficMonitor.getRxRate val txTotal = TrafficMonitor.getTxTotal @@ -184,7 +174,7 @@ trait BaseService extends Service { protected def changeState(s: Int, msg: String) { val handler = new Handler(getContext.getMainLooper) handler.post(() => if (state != s) { - if (callbackCount > 0) { + if (callbacks.getRegisteredCallbackCount > 0) { val n = callbacks.beginBroadcast() for (i <- 0 until n) { try { diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index f25becb5..93cf2327 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -476,19 +476,13 @@ class ShadowsocksNatService extends BaseService { // reset NAT killProcesses() - // stop the service if no callback registered - if (getCallbackCount == 0) { - stopSelf() - } + // stop the service if nothing has bound to it + stopSelf() // change the state changeState(State.STOPPED) } - override def stopBackgroundService() { - stopSelf() - } - override def getTag = TAG override def getServiceMode = Mode.NAT override def getContext = getBaseContext diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index 849af214..9e3c5f31 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -136,10 +136,8 @@ class ShadowsocksVpnService extends VpnService with BaseService { conn = null } - // stop the service if no callback registered - if (getCallbackCount == 0) { - stopSelf() - } + // stop the service if nothing has bound to it + stopSelf() // clean up recevier if (closeReceiver != null) { @@ -458,10 +456,6 @@ class ShadowsocksVpnService extends VpnService with BaseService { fd } - override def stopBackgroundService() { - stopSelf() - } - override def getTag = TAG override def getServiceMode = Mode.VPN