com.pubnub.api.subscribe.eventengine.effect.ReceiveReconnectEffect.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pubnub-kotlin Show documentation
Show all versions of pubnub-kotlin Show documentation
PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of
broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter
second!
package com.pubnub.api.subscribe.eventengine.effect
import com.pubnub.api.PubNubException
import com.pubnub.api.endpoints.remoteaction.RemoteAction
import com.pubnub.api.eventengine.ManagedEffect
import com.pubnub.api.eventengine.Sink
import com.pubnub.api.subscribe.eventengine.event.SubscribeEvent
import org.slf4j.LoggerFactory
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
internal class ReceiveReconnectEffect(
private val receiveMessagesRemoteAction: RemoteAction,
private val subscribeEventSink: Sink,
private val policy: RetryPolicy,
private val executorService: ScheduledExecutorService,
private val attempts: Int,
private val reason: PubNubException?
) : ManagedEffect {
private val log = LoggerFactory.getLogger(ReceiveReconnectEffect::class.java)
@Transient
private var scheduled: ScheduledFuture<*>? = null
@Transient
private var cancelled = false
@Synchronized
override fun runEffect() {
log.trace("Running ReceiveReconnectEffect")
if (cancelled) {
return
}
val delay = policy.nextDelay(attempts)
if (delay == null) {
subscribeEventSink.add(SubscribeEvent.ReceiveReconnectGiveup(reason ?: PubNubException("Unknown error")))
return
}
scheduled = executorService.schedule({
receiveMessagesRemoteAction.async { result, status ->
if (status.error) {
subscribeEventSink.add(
SubscribeEvent.ReceiveReconnectFailure(
status.exception ?: PubNubException("Unknown error")
)
)
} else {
subscribeEventSink.add(
SubscribeEvent.ReceiveReconnectSuccess(
result!!.messages,
result.subscriptionCursor
)
)
}
}
}, delay.toMillis(), TimeUnit.MILLISECONDS)
}
@Synchronized
override fun cancel() {
if (cancelled) {
return
}
cancelled = true
receiveMessagesRemoteAction.silentCancel()
scheduled?.cancel(true)
}
}