use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class Ping method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
* @throws InterruptedException if the thread is interrupted.
*/
public static void main(final String[] args) throws InterruptedException {
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");
System.out.println("Using exclusive publications " + EXCLUSIVE_PUBLICATIONS);
try (Aeron aeron = Aeron.connect(ctx);
Subscription subscription = aeron.addSubscription(PONG_CHANNEL, PONG_STREAM_ID);
Publication publication = EXCLUSIVE_PUBLICATIONS ? aeron.addExclusivePublication(PING_CHANNEL, PING_STREAM_ID) : aeron.addPublication(PING_CHANNEL, PING_STREAM_ID)) {
System.out.println("Waiting for new image from Pong...");
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(fragmentHandler, publication, subscription, WARMUP_NUMBER_OF_MESSAGES);
Thread.yield();
}
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.close(driver);
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class FileReceiver method main.
/**
* Main method for launching the process.
*
* @param args passed to the process.
*/
public static void main(final String[] args) {
final File storageDir;
if (args.length > 1) {
storageDir = new File(args[0]);
if (!storageDir.isDirectory()) {
System.out.println(args[0] + " is not a directory");
System.exit(1);
}
} else {
storageDir = new File(SystemUtil.tmpDirName());
}
System.out.println("Files stored to " + storageDir.getAbsolutePath());
final IdleStrategy idleStrategy = new SleepingMillisIdleStrategy(1);
final AtomicBoolean running = new AtomicBoolean(true);
SigInt.register(() -> running.set(false));
try (MediaDriver mediaDriver = MediaDriver.launch();
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID)) {
System.out.println("Receiving from " + CHANNEL + " on stream id " + STREAM_ID);
final FileReceiver fileReceiver = new FileReceiver(storageDir, subscription);
while (running.get()) {
idleStrategy.idle(fileReceiver.doWork());
}
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class RegistrationAndOwnerTest method shouldHaveCorrectOwnershipOnEntities.
@ParameterizedTest
@ValueSource(strings = { "aeron:udp?endpoint=localhost:24325", "aeron:ipc" })
void shouldHaveCorrectOwnershipOnEntities(final String channel) {
final MediaDriver.Context ctx = new MediaDriver.Context().errorHandler(Tests::onError).dirDeleteOnStart(true);
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(ctx, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
Publication publication = aeron.addPublication(channel, STREAM_ID);
Counter userCounter = aeron.addCounter(1002, "Test Counter")) {
awaitConnected(subscription);
awaitConnected(publication);
final CountersReader countersReader = aeron.countersReader();
final int subscriberPositionId = subscription.imageAtIndex(0).subscriberPositionId();
assertEquals(aeron.clientId(), countersReader.getCounterOwnerId(subscriberPositionId));
assertEquals(aeron.clientId(), countersReader.getCounterOwnerId(publication.positionLimitId()));
assertEquals(aeron.clientId(), countersReader.getCounterOwnerId(userCounter.id()));
assertEquals(subscription.registrationId(), countersReader.getCounterRegistrationId(subscriberPositionId));
assertEquals(publication.registrationId(), countersReader.getCounterRegistrationId(publication.positionLimitId()));
assertEquals(userCounter.registrationId(), countersReader.getCounterRegistrationId(userCounter.id()));
} finally {
ctx.deleteDirectory();
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class SpecifiedPositionPublicationTest method shouldStartAtSpecifiedPositionForPublications.
@InterruptAfter(5)
@ParameterizedTest
@CsvSource({ CommonContext.IPC_CHANNEL + ",true", "aeron:udp?endpoint=localhost:24325,true", CommonContext.IPC_CHANNEL + ",false", "aeron:udp?endpoint=localhost:24325,false" })
void shouldStartAtSpecifiedPositionForPublications(final String initialUri, final boolean exclusive) {
final MediaDriver.Context context = new MediaDriver.Context().dirDeleteOnStart(true).ipcPublicationTermWindowLength(LogBufferDescriptor.TERM_MIN_LENGTH).threadingMode(ThreadingMode.SHARED);
final DirectBuffer msg = new UnsafeBuffer(new byte[64]);
final int termLength = 1 << 16;
final int initialTermId = randomWatcher.random().nextInt();
final int activeTermId = initialTermId + randomWatcher.random().nextInt(Integer.MAX_VALUE);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
final int termOffset = randomWatcher.random().nextInt(termLength) & -FrameDescriptor.FRAME_ALIGNMENT;
final long startPosition = LogBufferDescriptor.computePosition(activeTermId, termOffset, positionBitsToShift, initialTermId);
final PositionCalculator positionCalculator = new PositionCalculator(startPosition, termLength, termOffset);
final long nextPosition = positionCalculator.addMessage(DataHeaderFlyweight.HEADER_LENGTH + msg.capacity());
final String channel = new ChannelUriStringBuilder(initialUri).initialPosition(startPosition, initialTermId, termLength).build();
final int streamId = 1001;
final Function<Aeron, Publication> publicationSupplier = exclusive ? (a) -> a.addExclusivePublication(channel, streamId) : (a) -> a.addPublication(channel, streamId);
try (TestMediaDriver mediaDriver = TestMediaDriver.launch(context, testWatcher);
Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(mediaDriver.aeronDirectoryName()));
Subscription subscription = aeron.addSubscription(initialUri, streamId);
Publication publication = publicationSupplier.apply(aeron)) {
Tests.awaitConnected(subscription);
Tests.awaitConnected(publication);
assertEquals(startPosition, publication.position());
Tests.await(() -> publication.offer(msg) > 0);
assertEquals(nextPosition, publication.position());
final FragmentHandler fragmentHandler = (buffer, offset, length, header) -> assertEquals(nextPosition, header.position());
Tests.await(() -> subscription.poll(fragmentHandler, 1) == 1);
} finally {
context.deleteDirectory();
}
}
use of io.aeron.driver.MediaDriver in project Aeron by real-logic.
the class ClusterNetworkTopologyTest method connectAndSendMessages.
private void connectAndSendMessages(final String ingressChannel, final String ingressEndpoints, final Selector selector, final double messageCount) {
final String message = "Hello World!";
final MutableDirectBuffer messageBuffer = new UnsafeBuffer(ByteBuffer.allocate(128));
final int length = messageBuffer.putStringAscii(0, message);
final MutableReference<String> egressResponse = new MutableReference<>();
final EgressListener egressListener = (clusterSessionId, timestamp, buffer, offset, length1, header) -> {
final String stringAscii = buffer.getStringAscii(offset);
egressResponse.set(stringAscii);
};
try (MediaDriver mediaDriver = MediaDriver.launchEmbedded(new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirDeleteOnStart(true).dirDeleteOnShutdown(true));
AeronCluster.AsyncConnect asyncConnect = AeronCluster.asyncConnect(new AeronCluster.Context().messageTimeoutNs(TimeUnit.SECONDS.toNanos(CLUSTER_START_ELECTION_TIMEOUT_S * 2)).egressListener(egressListener).egressChannel("aeron:udp?endpoint=10.42.0.1:0").aeronDirectoryName(mediaDriver.aeronDirectoryName()).ingressChannel(ingressChannel).ingressEndpoints(ingressEndpoints));
AeronCluster aeronCluster = pollUntilConnected(asyncConnect, selector)) {
for (int i = 0; i < messageCount; i++) {
Tests.await(() -> {
final long position = aeronCluster.offer(messageBuffer, 0, length);
pollSelector(selector);
return 0 < position;
}, SECONDS.toNanos(5));
Tests.await(() -> {
aeronCluster.pollEgress();
pollSelector(selector);
return message.equals(egressResponse.get());
}, SECONDS.toNanos(5));
}
}
}
Aggregations