com.github.kpavlov.jreactive8583.netty.pipeline.IdleEventHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netty-iso8583 Show documentation
Show all versions of netty-iso8583 Show documentation
ISO8583 protocol client and server Netty connectors.
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,
)
}