Skip to content

Commit

Permalink
Fix getting stuck offline when offline for a long time
Browse files Browse the repository at this point in the history
This can be worked around by restarting aTox or just toggling UDP/TCP.

The 60-second timeout was chosen arbitrarily and is the only value I
tested.
  • Loading branch information
robinlinden committed Dec 17, 2020
1 parent 04eecf2 commit 0f2a522
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions atox/src/main/kotlin/ToxService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import androidx.core.app.NotificationCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.asLiveData
import java.util.Timer
import javax.inject.Inject
import kotlin.concurrent.schedule
import kotlinx.coroutines.flow.filter
import ltd.evilcorp.atox.tox.ToxStarter
import ltd.evilcorp.core.repository.UserRepository
Expand All @@ -28,6 +30,7 @@ class ToxService : LifecycleService() {
private var connectionStatus = ConnectionStatus.None

private val notifier by lazy { getSystemService<NotificationManager>()!! }
private var bootstrapTimer = Timer()

@Inject
lateinit var tox: Tox
Expand Down Expand Up @@ -98,6 +101,17 @@ class ToxService : LifecycleService() {
if (user.connectionStatus == connectionStatus) return@observe
connectionStatus = user.connectionStatus
notifier.notify(notificationId, notificationFor(connectionStatus))
if (connectionStatus == ConnectionStatus.None) {
Log.i(TAG, "Gone offline, scheduling bootstrap")
bootstrapTimer.schedule(60_000) {
Log.i(TAG, "Been offline for too long, bootstrapping")
tox.isBootstrapNeeded = true
}
} else {
Log.i(TAG, "Online, cancelling bootstrap")
bootstrapTimer.cancel()
bootstrapTimer = Timer()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion domain/src/main/kotlin/tox/Tox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Tox @Inject constructor(
val publicKey: PublicKey by lazy { tox.getPublicKey() }

var started = false
var isBootstrapNeeded = true

private var running = false
private var isBootstrapNeeded = true

private lateinit var tox: ToxWrapper

Expand Down

0 comments on commit 0f2a522

Please sign in to comment.