package com.riaektiv.events

import com.lmax.disruptor.RingBuffer
import java.nio.ByteBuffer

/**
 * Coding With Passion Since 1991
 * Created: 11/24/2016, 10:12 AM Eastern Time
 * @author Sergey Chuykov
 */
class LongEventProducer : EventProducer<ByteBuffer, LongEvent> {

    lateinit var ringBuffer: RingBuffer<LongEvent>

    override fun set(ringBuffer: RingBuffer<LongEvent>) {
        this.ringBuffer = ringBuffer
    }

    override fun onMessage(msg: ByteBuffer) {

        val seq = ringBuffer.next()

        try {
            val event = ringBuffer.get(seq)
            event.value = msg.getLong(0)
        } finally {
            ringBuffer.publish(seq)
        }

    }

}