From b698ec3ee00656c4c21b8dfc464160b846dbd84b Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 22 Mar 2018 18:46:48 -0700 Subject: [PATCH] Switch to polling neighbors on unsupported kernels Fixes #12. --- .../vpnhotspot/net/IpNeighbourMonitor.kt | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbourMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbourMonitor.kt index 9888a2c3..6f5f7427 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbourMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/IpNeighbourMonitor.kt @@ -7,8 +7,11 @@ import be.mygod.vpnhotspot.R import be.mygod.vpnhotspot.debugLog import be.mygod.vpnhotspot.thread import java.io.InterruptedIOException +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService +import java.util.concurrent.TimeUnit -class IpNeighbourMonitor private constructor() { +class IpNeighbourMonitor private constructor() : Runnable { companion object { private const val TAG = "IpNeighbourMonitor" private val callbacks = HashSet() @@ -28,10 +31,8 @@ class IpNeighbourMonitor private constructor() { } fun unregisterCallback(callback: Callback) { if (!callbacks.remove(callback) || callbacks.isNotEmpty()) return - val monitor = instance ?: return + instance?.destroy() instance = null - val process = monitor.monitor - if (process != null) thread("$TAG-killer") { process.destroy() } } } @@ -44,6 +45,7 @@ class IpNeighbourMonitor private constructor() { private var updatePosted = false val neighbours = HashMap() private var monitor: Process? = null + private var pool: ScheduledExecutorService? = null init { thread("$TAG-input") { @@ -69,12 +71,18 @@ class IpNeighbourMonitor private constructor() { } } monitor.waitFor() - if (monitor.exitValue() != 0) app.toast(R.string.noisy_su_failure) } catch (ignore: InterruptedIOException) { } + if (monitor.exitValue() == 0) return@thread + Log.w(TAG, "Failed to set up monitor, switching to polling") + val pool = Executors.newScheduledThreadPool(1) + pool.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS) + this.pool = pool } } - fun flush() = thread("$TAG-flush") { + fun flush() = thread("$TAG-flush") { run() } + + override fun run() { val process = ProcessBuilder("ip", "neigh") .redirectErrorStream(true) .start() @@ -110,4 +118,10 @@ class IpNeighbourMonitor private constructor() { } updatePosted = true } + + fun destroy() { + val monitor = monitor + if (monitor != null) thread("$TAG-killer") { monitor.destroy() } + pool?.shutdown() + } }