use of io.aeron.Publication in project aeron by real-logic.
the class EgressPublisher method sendMembershipResponse.
public boolean sendMembershipResponse(final ClusterSession session, final byte[] encodedResponse) {
final Publication publication = session.responsePublication();
if (!publication.isConnected()) {
return false;
}
membershipQueryResponseEncoder.wrapAndApplyHeader(buffer, 0, messageHeaderEncoder).clusterSessionId(session.id()).correlationId(session.lastCorrelationId()).putEncodedResponse(encodedResponse, 0, encodedResponse.length);
final int length = MessageHeaderEncoder.ENCODED_LENGTH + membershipQueryResponseEncoder.encodedLength();
int attempts = SEND_ATTEMPTS;
do {
final long result = publication.offer(buffer, 0, length);
if (result > 0) {
return true;
}
} while (--attempts > 0);
return false;
}
use of io.aeron.Publication in project aeron by real-logic.
the class EmbeddedPingPong method runPing.
private static void runPing(final String embeddedDirName) throws InterruptedException {
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(EmbeddedPingPong::availablePongImageHandler).aeronDirectoryName(embeddedDirName);
System.out.println("Publishing Ping at " + PING_CHANNEL + " on stream Id " + PING_STREAM_ID);
System.out.println("Subscribing Pong at " + PONG_CHANNEL + " on stream Id " + PONG_STREAM_ID);
System.out.println("Message payload length of " + MESSAGE_LENGTH + " bytes");
final FragmentAssembler dataHandler = new FragmentAssembler(EmbeddedPingPong::pongHandler);
try (Aeron aeron = Aeron.connect(ctx);
Publication pingPublication = aeron.addPublication(PING_CHANNEL, PING_STREAM_ID);
Subscription pongSubscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID)) {
System.out.println("Waiting for new image from Pong...");
PONG_IMAGE_LATCH.await();
System.out.println("Warming up... " + WARMUP_NUMBER_OF_ITERATIONS + " iterations of " + WARMUP_NUMBER_OF_MESSAGES + " messages");
for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
roundTripMessages(dataHandler, pingPublication, pongSubscription, WARMUP_NUMBER_OF_MESSAGES);
}
Thread.sleep(100);
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
do {
HISTOGRAM.reset();
System.out.println("Pinging " + NUMBER_OF_MESSAGES + " messages");
roundTripMessages(dataHandler, pingPublication, pongSubscription, NUMBER_OF_MESSAGES);
System.out.println("Histogram of RTT latencies in microseconds.");
HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
} while (barrier.await());
}
}
use of io.aeron.Publication in project aeron by real-logic.
the class EmbeddedPingPong method startPong.
private static Thread startPong(final String embeddedDirName) {
return new Thread(() -> {
System.out.println("Subscribing Ping at " + PING_CHANNEL + " on stream Id " + PING_STREAM_ID);
System.out.println("Publishing Pong at " + PONG_CHANNEL + " on stream Id " + PONG_STREAM_ID);
final Aeron.Context ctx = new Aeron.Context().aeronDirectoryName(embeddedDirName);
try (Aeron aeron = Aeron.connect(ctx);
Publication pongPublication = aeron.addPublication(PONG_CHANNEL, PONG_STREAM_ID);
Subscription pingSubscription = aeron.addSubscription(PING_CHANNEL, PING_STREAM_ID)) {
final FragmentAssembler dataHandler = new FragmentAssembler((buffer, offset, length, header) -> pingHandler(pongPublication, buffer, offset, length));
while (RUNNING.get()) {
PING_HANDLER_IDLE_STRATEGY.idle(pingSubscription.poll(dataHandler, FRAME_COUNT_LIMIT));
}
System.out.println("Shutting down...");
}
});
}
use of io.aeron.Publication in project Aeron by real-logic.
the class DriverLoggingAgentTest method testLogMediaDriverEvents.
private void testLogMediaDriverEvents(final String channel, final String enabledEvents, final EnumSet<DriverEventCode> expectedEvents) {
before(enabledEvents, expectedEvents);
final MediaDriver.Context driverCtx = new MediaDriver.Context().errorHandler(Tests::onError).publicationLingerTimeoutNs(0).timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(1));
try (MediaDriver mediaDriver = MediaDriver.launch(driverCtx)) {
try (Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID)) {
final UnsafeBuffer offerBuffer = new UnsafeBuffer(new byte[32]);
while (publication.offer(offerBuffer) < 0) {
Tests.yield();
}
final MutableInteger counter = new MutableInteger();
final FragmentHandler handler = (buffer, offset, length, header) -> counter.value++;
while (0 == subscription.poll(handler, 1)) {
Tests.yield();
}
assertEquals(counter.get(), 1);
}
final Supplier<String> errorMessage = () -> "Pending events: " + WAIT_LIST;
while (!WAIT_LIST.isEmpty()) {
Tests.yieldingIdle(errorMessage);
}
}
}
use of io.aeron.Publication in project Aeron by real-logic.
the class AeronArchiveTest method closeOwningAeronClient.
@Test
void closeOwningAeronClient() {
final long controlSessionId = 42;
final Aeron.Context aeronContext = mock(Aeron.Context.class);
when(aeronContext.nanoClock()).thenReturn(SystemNanoClock.INSTANCE);
when(aeron.context()).thenReturn(aeronContext);
final IllegalMonitorStateException aeronException = new IllegalMonitorStateException("aeron closed");
doThrow(aeronException).when(aeron).close();
final Publication publication = mock(Publication.class);
when(publication.isConnected()).thenReturn(true);
doThrow(new IllegalStateException("publication is closed")).when(publication).close();
final Subscription subscription = mock(Subscription.class);
when(controlResponsePoller.subscription()).thenReturn(subscription);
doThrow(new IndexOutOfBoundsException("subscription")).when(subscription).close();
when(archiveProxy.publication()).thenReturn(publication);
final IndexOutOfBoundsException closeSessionException = new IndexOutOfBoundsException();
when(archiveProxy.closeSession(controlSessionId)).thenThrow(closeSessionException);
final Context context = new Context().aeron(aeron).idleStrategy(NoOpIdleStrategy.INSTANCE).messageTimeoutNs(100).lock(NoOpLock.INSTANCE).errorHandler(errorHandler).ownsAeronClient(true);
final AeronArchive aeronArchive = new AeronArchive(context, controlResponsePoller, archiveProxy, controlSessionId);
final IllegalMonitorStateException ex = assertThrows(IllegalMonitorStateException.class, aeronArchive::close);
assertSame(aeronException, ex);
final InOrder inOrder = inOrder(errorHandler);
inOrder.verify(errorHandler).onError(closeSessionException);
inOrder.verifyNoMoreInteractions();
}
Aggregations