Avoid false positive warning about server configuration

Use AtomicReference for the startingTimer and check
if the source is the current one
This commit is contained in:
sim 2023-08-31 21:31:33 +02:00
parent 93d42bfd19
commit 3e7900da61
1 changed files with 13 additions and 8 deletions

View File

@ -15,28 +15,33 @@ import org.unifiedpush.distributor.nextpush.services.FailureHandler
import org.unifiedpush.distributor.nextpush.services.RestartWorker import org.unifiedpush.distributor.nextpush.services.RestartWorker
import org.unifiedpush.distributor.nextpush.services.StartService import org.unifiedpush.distributor.nextpush.services.StartService
import org.unifiedpush.distributor.nextpush.utils.LowKeepAliveNotification import org.unifiedpush.distributor.nextpush.utils.LowKeepAliveNotification
import org.unifiedpush.distributor.nextpush.utils.NoPingNotification
import org.unifiedpush.distributor.nextpush.utils.NoStartNotification import org.unifiedpush.distributor.nextpush.utils.NoStartNotification
import org.unifiedpush.distributor.nextpush.utils.TAG import org.unifiedpush.distributor.nextpush.utils.TAG
import java.lang.Exception import java.lang.Exception
import java.util.Calendar import java.util.Calendar
import java.util.Timer import java.util.Timer
import java.util.TimerTask import java.util.TimerTask
import java.util.concurrent.atomic.AtomicReference
import kotlin.concurrent.schedule import kotlin.concurrent.schedule
class SSEListener(val context: Context) : EventSourceListener() { class SSEListener(val context: Context) : EventSourceListener() {
override fun onOpen(eventSource: EventSource, response: Response) { override fun onOpen(eventSource: EventSource, response: Response) {
FailureHandler.newEventSource(context, eventSource) FailureHandler.newEventSource(context, eventSource)
startingTimer?.cancel() val timer: TimerTask? = if (
if (!AppCompanion.bufferedResponseChecked.get()) { !AppCompanion.bufferedResponseChecked.get() &&
if (!AppCompanion.booting.getAndSet(false)) { !AppCompanion.booting.getAndSet(false)
startingTimer = Timer().schedule(45_000L /* 45secs */) { ) {
Timer().schedule(45_000L /* 45secs */) {
if (FailureHandler.newFail(context, eventSource)) {
StartService.stopService() StartService.stopService()
NoStartNotification(context).show() NoStartNotification(context).show()
} }
} }
} else {
null
} }
startingTimer.getAndSet(timer)?.cancel()
StartService.wakeLock?.let { StartService.wakeLock?.let {
while (it.isHeld) { while (it.isHeld) {
it.release() it.release()
@ -57,7 +62,7 @@ class SSEListener(val context: Context) : EventSourceListener() {
when (type) { when (type) {
"start" -> { "start" -> {
AppCompanion.started.set(true) AppCompanion.started.set(true)
startingTimer?.cancel() startingTimer.getAndSet(null)?.cancel()
AppCompanion.bufferedResponseChecked.set(true) AppCompanion.bufferedResponseChecked.set(true)
NoStartNotification(context).delete() NoStartNotification(context).delete()
} }
@ -150,12 +155,12 @@ class SSEListener(val context: Context) : EventSourceListener() {
} }
private fun clearVars() { private fun clearVars() {
startingTimer?.cancel() startingTimer.getAndSet(null)?.cancel()
AppCompanion.started.set(false) AppCompanion.started.set(false)
AppCompanion.pinged.set(false) AppCompanion.pinged.set(false)
} }
companion object { companion object {
private var startingTimer: TimerTask? = null private var startingTimer: AtomicReference<TimerTask?> = AtomicReference(null)
} }
} }