All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.kpavlov.jreactive8583.netty.pipeline.IdleEventHandler.kt Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package com.github.kpavlov.jreactive8583.netty.pipeline

import com.github.kpavlov.jreactive8583.iso.MessageClass
import com.github.kpavlov.jreactive8583.iso.MessageFactory
import com.github.kpavlov.jreactive8583.iso.MessageFunction
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.handler.timeout.IdleState
import io.netty.handler.timeout.IdleStateEvent

/**
 * IdleEventHandler sends heartbeats (administrative messages) when channel becomes idle,
 * i.e. `IdleStateEvent` is received.
 */
public open class IdleEventHandler(
    private val isoMessageFactory: MessageFactory,
) : ChannelInboundHandlerAdapter() {
    public override fun userEventTriggered(
        ctx: ChannelHandlerContext,
        evt: Any,
    ) {
        if (evt is IdleStateEvent &&
            (evt.state() == IdleState.READER_IDLE || evt.state() == IdleState.ALL_IDLE)
        ) {
            val heartbeatMessage = createHeartbeatMessage()
            ctx.write(heartbeatMessage)
            ctx.flush()
        }
    }

    protected fun createHeartbeatMessage(): T =
        isoMessageFactory.newMessage(
            MessageClass.NETWORK_MANAGEMENT,
            MessageFunction.REQUEST,
        )
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy