Skip to content
Snippets Groups Projects
Select Git revision
  • 5901d33bfebd3956ba7f175df94a53f7d1ddc37c
  • main default protected
  • wip
  • ChenZhangg-Modify_GRADLE_1
  • jetpack-compose-rewrite
  • demo-jump-in-history
  • attachments
  • 1.7.0 protected
  • 1.6.2 protected
  • 1.6.1 protected
  • 1.6.0 protected
  • 1.5.3 protected
  • 1.5.2 protected
  • 1.5.1 protected
  • 1.5.0 protected
  • 1.4.4 protected
  • 1.4.3 protected
  • 1.4.2 protected
  • 1.4.1 protected
  • 1.4.0 protected
  • v1.3.3 protected
  • v1.3.2 protected
  • v1.3.1 protected
  • v1.3.0 protected
  • v1.2.28 protected
  • v1.2.27 protected
  • v1.2.26 protected
27 results

JavaHeartBeatRunner.kt

Blame
  • JavaHeartBeatRunner.kt 1.53 KiB
    package de.kuschku.libquassel.session
    
    import de.kuschku.libquassel.protocol.message.SignalProxyMessage
    import de.kuschku.libquassel.util.compatibility.LoggingHandler.Companion.log
    import de.kuschku.libquassel.util.compatibility.LoggingHandler.LogLevel.INFO
    import org.threeten.bp.Duration
    import org.threeten.bp.Instant
    
    class JavaHeartBeatRunner : Thread(), HeartBeatRunner {
      private var running = true
      private var lastHeartBeatReply: Instant = Instant.now()
    
      private var closeCallback: (() -> Unit)? = null
      private var heartbeatDispatchCallback: ((SignalProxyMessage.HeartBeat) -> Unit)? = null
    
      override fun setCloseCallback(callback: (() -> Unit)?) {
        this.closeCallback = callback
      }
    
      override fun setHeartbeatDispatchCallback(callback: ((SignalProxyMessage.HeartBeat) -> Unit)?) {
        this.heartbeatDispatchCallback = callback
      }
    
      override fun start() {
        while (running) {
          val now = Instant.now()
          val duration = Duration.between(lastHeartBeatReply, now).toMillis()
          if (duration > TIMEOUT) {
            log(INFO, "Heartbeat", "Ping Timeout: Last Response ${duration}ms ago")
            closeCallback?.invoke()
          } else {
            log(INFO, "Heartbeat", "Sending Heartbeat")
            heartbeatDispatchCallback?.invoke(SignalProxyMessage.HeartBeat(now))
          }
          Thread.sleep(DELAY)
        }
      }
    
      override fun end() {
        running = false
      }
    
      override fun setLastHeartBeatReply(time: Instant) {
        this.lastHeartBeatReply = time
      }
    
      companion object {
        const val TIMEOUT = 120_000L
        const val DELAY = 30_000L
      }
    }