use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class Ping method main.
public static void main(final String[] args) throws Exception {
final MediaDriver driver = EMBEDDED_MEDIA_DRIVER ? MediaDriver.launchEmbedded() : null;
final Aeron.Context ctx = new Aeron.Context().availableImageHandler(Ping::availablePongImageHandler);
final FragmentHandler fragmentHandler = new FragmentAssembler(Ping::pongHandler);
if (EMBEDDED_MEDIA_DRIVER) {
ctx.aeronDirectoryName(driver.aeronDirectoryName());
}
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 length of " + MESSAGE_LENGTH + " bytes");
try (Aeron aeron = Aeron.connect(ctx)) {
System.out.println("Warming up... " + WARMUP_NUMBER_OF_ITERATIONS + " iterations of " + WARMUP_NUMBER_OF_MESSAGES + " messages");
try (Publication publication = aeron.addPublication(PING_CHANNEL, PING_STREAM_ID);
Subscription subscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID)) {
LATCH.await();
for (int i = 0; i < WARMUP_NUMBER_OF_ITERATIONS; i++) {
roundTripMessages(fragmentHandler, publication, subscription, 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(fragmentHandler, publication, subscription, NUMBER_OF_MESSAGES);
System.out.println("Histogram of RTT latencies in microseconds.");
HISTOGRAM.outputPercentileDistribution(System.out, 1000.0);
} while (barrier.await());
}
}
CloseHelper.quietClose(driver);
}
use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class SimpleSubscriber method main.
public static void main(final String[] args) throws Exception {
// Maximum number of message fragments to receive during a single 'poll' operation
final int fragmentLimitCount = 10;
// The channel (an endpoint identifier) to receive messages from
final String channel = "aeron:udp?endpoint=localhost:40123";
// A unique identifier for a stream within a channel. Stream ID 0 is reserved
// for internal use and should not be used by applications.
final int streamId = 10;
System.out.println("Subscribing to " + channel + " on stream Id " + streamId);
final AtomicBoolean running = new AtomicBoolean(true);
// Register a SIGINT handler for graceful shutdown.
SigInt.register(() -> running.set(false));
// dataHandler method is called for every new datagram received
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> {
final byte[] data = new byte[length];
buffer.getBytes(offset, data);
System.out.println(String.format("Received message (%s) to stream %d from session %x term id %x term offset %d (%d@%d)", new String(data), streamId, header.sessionId(), header.termId(), header.termOffset(), length, offset));
// Received the intended message, time to exit the program
running.set(false);
};
// Create a context, needed for client connection to media driver
// A separate media driver process need to run prior to running this application
final Aeron.Context ctx = new Aeron.Context();
// clean up resources when this try block is finished.
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(channel, streamId)) {
final IdleStrategy idleStrategy = new BackoffIdleStrategy(100, 10, TimeUnit.MICROSECONDS.toNanos(1), TimeUnit.MICROSECONDS.toNanos(100));
// Try to read the data from subscriber
while (running.get()) {
// poll delivers messages to the dataHandler as they arrive
// and returns number of fragments read, or 0
// if no data is available.
final int fragmentsRead = subscription.poll(fragmentHandler, fragmentLimitCount);
// Give the IdleStrategy a chance to spin/yield/sleep to reduce CPU
// use if no messages were received.
idleStrategy.idle(fragmentsRead);
}
System.out.println("Shutting down...");
}
}
use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class MultiSubscriberTest method shouldReceiveMessageOnSeparateSubscriptions.
@Test(timeout = 10000)
public void shouldReceiveMessageOnSeparateSubscriptions() throws Exception {
final MediaDriver.Context ctx = new MediaDriver.Context();
final FragmentHandler mockFragmentHandlerOne = mock(FragmentHandler.class);
final FragmentHandler mockFragmentHandlerTwo = mock(FragmentHandler.class);
final FragmentAssembler adapterOne = new FragmentAssembler(mockFragmentHandlerOne);
final FragmentAssembler adapterTwo = new FragmentAssembler(mockFragmentHandlerTwo);
try (MediaDriver ignore = MediaDriver.launch(ctx);
Aeron client = Aeron.connect(new Aeron.Context());
Publication publication = client.addPublication(CHANNEL_1, STREAM_ID);
Subscription subscriptionOne = client.addSubscription(CHANNEL_1, STREAM_ID);
Subscription subscriptionTwo = client.addSubscription(CHANNEL_2, STREAM_ID)) {
final byte[] expectedBytes = "Hello, World! here is a small message".getBytes();
final UnsafeBuffer srcBuffer = new UnsafeBuffer(expectedBytes);
assertThat(subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT), is(0));
assertThat(subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT), is(0));
while (publication.offer(srcBuffer) < 0L) {
Thread.yield();
}
while (subscriptionOne.poll(adapterOne, FRAGMENT_COUNT_LIMIT) == 0) {
Thread.yield();
}
while (subscriptionTwo.poll(adapterTwo, FRAGMENT_COUNT_LIMIT) == 0) {
Thread.yield();
}
verifyData(srcBuffer, mockFragmentHandlerOne);
verifyData(srcBuffer, mockFragmentHandlerTwo);
} finally {
ctx.deleteAeronDirectory();
}
}
use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class EmbeddedThroughput method main.
public static void main(final String[] args) throws Exception {
MediaDriver.loadPropertiesFiles(args);
final RateReporter reporter = new RateReporter(TimeUnit.SECONDS.toNanos(1), EmbeddedThroughput::printRate);
final FragmentHandler rateReporterHandler = rateReporterHandler(reporter);
final ExecutorService executor = Executors.newFixedThreadPool(2);
final Aeron.Context context = new Aeron.Context();
final AtomicBoolean running = new AtomicBoolean(true);
try (MediaDriver ignore = MediaDriver.launch();
Aeron aeron = Aeron.connect(context);
Publication publication = aeron.addPublication(CHANNEL, STREAM_ID);
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
executor.execute(reporter);
executor.execute(() -> SamplesUtil.subscriberLoop(rateReporterHandler, FRAGMENT_COUNT_LIMIT, running).accept(subscription));
final ContinueBarrier barrier = new ContinueBarrier("Execute again?");
do {
System.out.format("%nStreaming %,d messages of size %d bytes to %s on stream Id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID);
printingActive = true;
long backPressureCount = 0;
for (long i = 0; i < NUMBER_OF_MESSAGES; i++) {
ATOMIC_BUFFER.putLong(0, i);
OFFER_IDLE_STRATEGY.reset();
while (publication.offer(ATOMIC_BUFFER, 0, ATOMIC_BUFFER.capacity()) < 0) {
OFFER_IDLE_STRATEGY.idle();
backPressureCount++;
}
}
System.out.println("Done streaming. backPressureRatio=" + ((double) backPressureCount / NUMBER_OF_MESSAGES));
if (0 < LINGER_TIMEOUT_MS) {
System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds...");
Thread.sleep(LINGER_TIMEOUT_MS);
}
printingActive = false;
} while (barrier.await());
running.set(false);
reporter.halt();
executor.shutdown();
}
}
use of io.aeron.logbuffer.FragmentHandler in project Aeron by real-logic.
the class SubscriptionTest method shouldReadData.
@Test
public void shouldReadData() {
subscription.addImage(imageOneMock);
when(imageOneMock.poll(any(FragmentHandler.class), anyInt())).then((invocation) -> {
final FragmentHandler handler = (FragmentHandler) invocation.getArguments()[0];
handler.onFragment(atomicReadBuffer, HEADER_LENGTH, READ_BUFFER_CAPACITY - HEADER_LENGTH, header);
return 1;
});
assertThat(subscription.poll(fragmentHandler, FRAGMENT_COUNT_LIMIT), is(1));
verify(fragmentHandler).onFragment(eq(atomicReadBuffer), eq(HEADER_LENGTH), eq(READ_BUFFER_CAPACITY - HEADER_LENGTH), any(Header.class));
}
Aggregations