use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class CommonContext method isDriverActive.
/**
* Is a media driver active in the current Aeron directory?
*
* @param driverTimeoutMs for the driver liveness check
* @param logHandler for feedback as liveness checked
* @return true if a driver is active or false if not
*/
public boolean isDriverActive(final long driverTimeoutMs, final Consumer<String> logHandler) {
final File dirFile = new File(aeronDirectoryName);
if (dirFile.exists() && dirFile.isDirectory()) {
final File cncFile = new File(aeronDirectoryName, CncFileDescriptor.CNC_FILE);
logHandler.accept(String.format("INFO: Aeron directory %s exists", dirFile));
if (cncFile.exists()) {
MappedByteBuffer cncByteBuffer = null;
logHandler.accept(String.format("INFO: Aeron CnC file %s exists", cncFile));
try {
cncByteBuffer = IoUtil.mapExistingFile(cncFile, CncFileDescriptor.CNC_FILE);
final UnsafeBuffer cncMetaDataBuffer = CncFileDescriptor.createMetaDataBuffer(cncByteBuffer);
final int cncVersion = cncMetaDataBuffer.getInt(CncFileDescriptor.cncVersionOffset(0));
if (CncFileDescriptor.CNC_VERSION != cncVersion) {
throw new IllegalStateException("aeron cnc file version not understood: version=" + cncVersion);
}
final ManyToOneRingBuffer toDriverBuffer = new ManyToOneRingBuffer(CncFileDescriptor.createToDriverBuffer(cncByteBuffer, cncMetaDataBuffer));
final long timestamp = toDriverBuffer.consumerHeartbeatTime();
final long now = System.currentTimeMillis();
final long diff = now - timestamp;
logHandler.accept(String.format("INFO: Aeron toDriver consumer heartbeat is %d ms old", diff));
if (diff <= driverTimeoutMs) {
return true;
}
} catch (final Exception ex) {
LangUtil.rethrowUnchecked(ex);
} finally {
IoUtil.unmap(cncByteBuffer);
}
}
}
return false;
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class FragmentAssemblerTest method shouldFreeSessionBuffer.
@Test
public void shouldFreeSessionBuffer() {
when(header.flags()).thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG).thenReturn(FrameDescriptor.END_FRAG_FLAG);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int offset = 0;
final int length = srcBuffer.capacity() / 2;
srcBuffer.setMemory(0, length, (byte) 65);
srcBuffer.setMemory(length, length, (byte) 66);
assertFalse(adapter.freeSessionBuffer(SESSION_ID));
adapter.onFragment(srcBuffer, offset, length, header);
adapter.onFragment(srcBuffer, length, length, header);
assertTrue(adapter.freeSessionBuffer(SESSION_ID));
assertFalse(adapter.freeSessionBuffer(SESSION_ID));
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class FragmentAssemblerTest method shouldDoNotingIfMidArrivesWithoutBegin.
@Test
public void shouldDoNotingIfMidArrivesWithoutBegin() {
when(header.flags()).thenReturn((byte) 0).thenReturn(FrameDescriptor.END_FRAG_FLAG);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int offset = 0;
final int length = srcBuffer.capacity() / 2;
adapter.onFragment(srcBuffer, offset, length, header);
adapter.onFragment(srcBuffer, offset, length, header);
verify(delegateFragmentHandler, never()).onFragment(any(), anyInt(), anyInt(), any());
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class DataHeaderFlyweight method createDefaultHeader.
/**
* Return an initialised default Data Frame Header.
*
* @param sessionId for the header
* @param streamId for the header
* @param termId for the header
* @return byte array containing the header
*/
public static UnsafeBuffer createDefaultHeader(final int sessionId, final int streamId, final int termId) {
final UnsafeBuffer buffer = new UnsafeBuffer(new byte[HEADER_LENGTH]);
buffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
buffer.putByte(FLAGS_FIELD_OFFSET, (byte) BEGIN_AND_END_FLAGS);
buffer.putShort(TYPE_FIELD_OFFSET, (short) HDR_TYPE_DATA, LITTLE_ENDIAN);
buffer.putInt(SESSION_ID_FIELD_OFFSET, sessionId, LITTLE_ENDIAN);
buffer.putInt(STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
buffer.putInt(TERM_ID_FIELD_OFFSET, termId, LITTLE_ENDIAN);
buffer.putLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);
return buffer;
}
use of org.agrona.concurrent.UnsafeBuffer in project Aeron by real-logic.
the class BufferBuilderTest method shouldGrowToMultipleOfInitialCapacity.
@Test
public void shouldGrowToMultipleOfInitialCapacity() {
final int srcCapacity = INITIAL_CAPACITY * 5;
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[srcCapacity]);
bufferBuilder.append(srcBuffer, 0, srcBuffer.capacity());
assertThat(bufferBuilder.limit(), is(srcCapacity));
assertThat(bufferBuilder.capacity(), greaterThanOrEqualTo(srcCapacity));
}
Aggregations