Commit 51ceb3da authored by Mygod's avatar Mygod

Refine callback handling

parent 179aab80
...@@ -50,7 +50,6 @@ import com.github.shadowsocks.utils.{State, TrafficMonitor, TrafficMonitorThread ...@@ -50,7 +50,6 @@ import com.github.shadowsocks.utils.{State, TrafficMonitor, TrafficMonitorThread
trait BaseService extends Service { trait BaseService extends Service {
@volatile private var state = State.STOPPED @volatile private var state = State.STOPPED
@volatile private var callbackCount = 0
@volatile protected var config: Config = null @volatile protected var config: Config = null
var timer: Timer = null var timer: Timer = null
...@@ -70,20 +69,17 @@ trait BaseService extends Service { ...@@ -70,20 +69,17 @@ trait BaseService extends Service {
override def unregisterCallback(cb: IShadowsocksServiceCallback) { override def unregisterCallback(cb: IShadowsocksServiceCallback) {
if (cb != null ) { if (cb != null ) {
callbacks.unregister(cb) callbacks.unregister(cb)
callbackCount -= 1
} }
if (callbackCount == 0 && timer != null) { if (callbacks.getRegisteredCallbackCount == 0 && timer != null) {
timer.cancel() timer.cancel()
timer = null timer = null
} }
if (callbackCount == 0 && state == State.STOPPED) {
stopBackgroundService()
}
} }
override def registerCallback(cb: IShadowsocksServiceCallback) { override def registerCallback(cb: IShadowsocksServiceCallback) {
if (cb != null) { if (cb != null) {
if (callbackCount == 0 && timer == null) { callbacks.register(cb)
if (callbacks.getRegisteredCallbackCount != 0 && timer == null) {
val task = new TimerTask { val task = new TimerTask {
def run { def run {
if (TrafficMonitor.updateRate()) updateTrafficRate() if (TrafficMonitor.updateRate()) updateTrafficRate()
...@@ -92,8 +88,6 @@ trait BaseService extends Service { ...@@ -92,8 +88,6 @@ trait BaseService extends Service {
timer = new Timer(true) timer = new Timer(true)
timer.schedule(task, 1000, 1000) timer.schedule(task, 1000, 1000)
} }
callbacks.register(cb)
callbackCount += 1
TrafficMonitor.updateRate() TrafficMonitor.updateRate()
cb.trafficUpdated(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate, cb.trafficUpdated(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal) TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
...@@ -145,14 +139,10 @@ trait BaseService extends Service { ...@@ -145,14 +139,10 @@ trait BaseService extends Service {
} }
} }
def stopBackgroundService()
def getServiceMode: Int def getServiceMode: Int
def getTag: String def getTag: String
def getContext: Context def getContext: Context
def getCallbackCount: Int = {
callbackCount
}
def getState: Int = { def getState: Int = {
state state
} }
...@@ -163,7 +153,7 @@ trait BaseService extends Service { ...@@ -163,7 +153,7 @@ trait BaseService extends Service {
def updateTrafficRate() { def updateTrafficRate() {
val handler = new Handler(getContext.getMainLooper) val handler = new Handler(getContext.getMainLooper)
handler.post(() => { handler.post(() => {
if (callbackCount > 0) { if (callbacks.getRegisteredCallbackCount > 0) {
val txRate = TrafficMonitor.getTxRate val txRate = TrafficMonitor.getTxRate
val rxRate = TrafficMonitor.getRxRate val rxRate = TrafficMonitor.getRxRate
val txTotal = TrafficMonitor.getTxTotal val txTotal = TrafficMonitor.getTxTotal
...@@ -184,7 +174,7 @@ trait BaseService extends Service { ...@@ -184,7 +174,7 @@ trait BaseService extends Service {
protected def changeState(s: Int, msg: String) { protected def changeState(s: Int, msg: String) {
val handler = new Handler(getContext.getMainLooper) val handler = new Handler(getContext.getMainLooper)
handler.post(() => if (state != s) { handler.post(() => if (state != s) {
if (callbackCount > 0) { if (callbacks.getRegisteredCallbackCount > 0) {
val n = callbacks.beginBroadcast() val n = callbacks.beginBroadcast()
for (i <- 0 until n) { for (i <- 0 until n) {
try { try {
......
...@@ -476,19 +476,13 @@ class ShadowsocksNatService extends BaseService { ...@@ -476,19 +476,13 @@ class ShadowsocksNatService extends BaseService {
// reset NAT // reset NAT
killProcesses() killProcesses()
// stop the service if no callback registered // stop the service if nothing has bound to it
if (getCallbackCount == 0) { stopSelf()
stopSelf()
}
// change the state // change the state
changeState(State.STOPPED) changeState(State.STOPPED)
} }
override def stopBackgroundService() {
stopSelf()
}
override def getTag = TAG override def getTag = TAG
override def getServiceMode = Mode.NAT override def getServiceMode = Mode.NAT
override def getContext = getBaseContext override def getContext = getBaseContext
......
...@@ -136,10 +136,8 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -136,10 +136,8 @@ class ShadowsocksVpnService extends VpnService with BaseService {
conn = null conn = null
} }
// stop the service if no callback registered // stop the service if nothing has bound to it
if (getCallbackCount == 0) { stopSelf()
stopSelf()
}
// clean up recevier // clean up recevier
if (closeReceiver != null) { if (closeReceiver != null) {
...@@ -458,10 +456,6 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -458,10 +456,6 @@ class ShadowsocksVpnService extends VpnService with BaseService {
fd fd
} }
override def stopBackgroundService() {
stopSelf()
}
override def getTag = TAG override def getTag = TAG
override def getServiceMode = Mode.VPN override def getServiceMode = Mode.VPN
......
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