use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class ManyToOneRingBuffer method controlledRead.
/**
* {@inheritDoc}
*/
public int controlledRead(final ControlledMessageHandler handler, final int messageCountLimit) {
int messagesRead = 0;
final AtomicBuffer buffer = this.buffer;
final int headPositionIndex = this.headPositionIndex;
long head = buffer.getLong(headPositionIndex);
final int capacity = this.capacity;
int headIndex = (int) head & (capacity - 1);
final int maxBlockLength = capacity - headIndex;
int bytesRead = 0;
try {
while ((bytesRead < maxBlockLength) && (messagesRead < messageCountLimit)) {
final int recordIndex = headIndex + bytesRead;
final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
if (recordLength <= 0) {
break;
}
final int alignedLength = align(recordLength, ALIGNMENT);
bytesRead += alignedLength;
final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
if (PADDING_MSG_TYPE_ID == messageTypeId) {
continue;
}
final ControlledMessageHandler.Action action = handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
if (ABORT == action) {
bytesRead -= alignedLength;
break;
}
++messagesRead;
if (BREAK == action) {
break;
}
if (COMMIT == action) {
buffer.setMemory(headIndex, bytesRead, (byte) 0);
buffer.putLongOrdered(headPositionIndex, head + bytesRead);
headIndex += bytesRead;
head += bytesRead;
bytesRead = 0;
}
}
} finally {
if (bytesRead > 0) {
buffer.setMemory(headIndex, bytesRead, (byte) 0);
buffer.putLongOrdered(headPositionIndex, head + bytesRead);
}
}
return messagesRead;
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class OneToOneRingBuffer method controlledRead.
/**
* {@inheritDoc}
*/
public int controlledRead(final ControlledMessageHandler handler, final int messageCountLimit) {
int messagesRead = 0;
final AtomicBuffer buffer = this.buffer;
final int headPositionIndex = this.headPositionIndex;
long head = buffer.getLong(headPositionIndex);
int bytesRead = 0;
final int capacity = this.capacity;
int headIndex = (int) head & (capacity - 1);
final int contiguousBlockLength = capacity - headIndex;
try {
while ((bytesRead < contiguousBlockLength) && (messagesRead < messageCountLimit)) {
final int recordIndex = headIndex + bytesRead;
final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
if (recordLength <= 0) {
break;
}
final int alignedLength = align(recordLength, ALIGNMENT);
bytesRead += alignedLength;
final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
if (PADDING_MSG_TYPE_ID == messageTypeId) {
continue;
}
final ControlledMessageHandler.Action action = handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
if (ABORT == action) {
bytesRead -= alignedLength;
break;
}
++messagesRead;
if (BREAK == action) {
break;
}
if (COMMIT == action) {
buffer.putLongOrdered(headPositionIndex, head + bytesRead);
headIndex += bytesRead;
head += bytesRead;
bytesRead = 0;
}
}
} finally {
if (bytesRead > 0) {
buffer.putLongOrdered(headPositionIndex, head + bytesRead);
}
}
return messagesRead;
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class ManyToOneRingBufferTest method shouldAbortOnControlledReadOfSecondMessage.
@Test
public void shouldAbortOnControlledReadOfSecondMessage() {
final String msg = "Hello World";
final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
final int srcLength = srcBuffer.putStringAscii(0, msg);
final RingBuffer ringBuffer = new ManyToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
final MutableInteger counter = new MutableInteger();
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
return counter.getAndIncrement() == 0 ? ControlledMessageHandler.Action.CONTINUE : ControlledMessageHandler.Action.ABORT;
};
final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler);
assertEquals(2, counter.get());
assertEquals(1, messagesRead);
assertNotEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class OneToOneRingBufferTest method shouldAbortOnControlledRead.
@Test
public void shouldAbortOnControlledRead() {
final String msg = "Hello World";
final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
final int srcLength = srcBuffer.putStringAscii(0, msg);
final RingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
assertEquals(MSG_TYPE_ID, msgTypeId);
assertEquals(HEADER_LENGTH, index);
assertEquals(srcLength, length);
assertEquals(msg, buffer.getStringAscii(index));
return ControlledMessageHandler.Action.ABORT;
};
final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler, 1);
assertEquals(0, messagesRead);
assertEquals(0, ringBuffer.consumerPosition());
}
use of org.agrona.concurrent.ControlledMessageHandler in project agrona by real-logic.
the class OneToOneRingBufferTest method shouldAbortOnControlledReadOfSecondMessage.
@Test
public void shouldAbortOnControlledReadOfSecondMessage() {
final String msg = "Hello World";
final ExpandableArrayBuffer srcBuffer = new ExpandableArrayBuffer();
final int srcLength = srcBuffer.putStringAscii(0, msg);
final RingBuffer ringBuffer = new OneToOneRingBuffer(new UnsafeBuffer(new byte[1024]));
final MutableInteger counter = new MutableInteger();
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
assertTrue(ringBuffer.write(MSG_TYPE_ID, srcBuffer, 0, srcLength));
final ControlledMessageHandler controlledMessageHandler = (msgTypeId, buffer, index, length) -> {
return counter.getAndIncrement() == 0 ? ControlledMessageHandler.Action.CONTINUE : ControlledMessageHandler.Action.ABORT;
};
final int messagesRead = ringBuffer.controlledRead(controlledMessageHandler);
assertEquals(2, counter.get());
assertEquals(1, messagesRead);
assertNotEquals(ringBuffer.producerPosition(), ringBuffer.consumerPosition());
}
Aggregations