PacketConsumerCaller


@ExperimentalApi
class PacketConsumerCaller<T : Any?>


A utility that connects a producer of Packet of type T to a PacketConsumer via a Channel.

This class is experimental and will be renamed or removed in a future release.

The owner of this class is responsible for invoking the run method, which will start sending the queued Packet to the wrapped PacketConsumer. The owner is also responsible for calling release to close the underlying Channel once this consumer is no longer needed.

Summary

Public companion functions

PacketConsumerCaller<T>
<T : Any?> create(
    packetConsumer: PacketConsumer<T>,
    executorService: ExecutorService,
    errorConsumer: Consumer<Exception>
)

Creates a PacketConsumerCaller instance.

Public functions

ListenableFuture<Void?>

Queues a Packet for processing.

Unit

Releases the internal Channel and cancels coroutine to queue Packet to the wrapped PacketConsumer.

Unit
run()

Start sending queued Packet the wrapped PacketConsumer

Boolean

Attempts to queue a Packet for processing without suspending.

Public companion functions

create

fun <T : Any?> create(
    packetConsumer: PacketConsumer<T>,
    executorService: ExecutorService,
    errorConsumer: Consumer<Exception>
): PacketConsumerCaller<T>

Creates a PacketConsumerCaller instance.

Parameters
packetConsumer: PacketConsumer<T>

The PacketConsumer to wrap.

executorService: ExecutorService

The ExecutorService on which the PacketConsumer.queuePacket is invoked.

errorConsumer: Consumer<Exception>

A Consumer for thrown Exception.

Public functions

queuePacket

fun queuePacket(packet: PacketConsumer.Packet<T>): ListenableFuture<Void?>

Queues a Packet for processing.

The ownership of the packet is transferred to the wrapped PacketConsumer and the caller must not modify the packet.

release

fun release(): Unit

Releases the internal Channel and cancels coroutine to queue Packet to the wrapped PacketConsumer.

run

fun run(): Unit

Start sending queued Packet the wrapped PacketConsumer

tryQueuePacket

fun tryQueuePacket(packet: PacketConsumer.Packet<T>): Boolean

Attempts to queue a Packet for processing without suspending.

If this method returns true, the ownership of the packet is transferred to the wrapped PacketConsumer and the caller must not modify the packet. If returns false, it means the consumer is at capacity and cannot accept the packet at the moment.

Throws
ClosedSendChannelException

If the channel to send packet to the PacketConsumer is closed.