Search in sources :

Example 1 with NioSelectedKeySet

use of org.agrona.nio.NioSelectedKeySet in project aeron by real-logic.

the class SendHackSelectReceiveUdpPing method run.

private void run() throws IOException {
    receiveChannel = DatagramChannel.open();
    Common.init(receiveChannel);
    receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_PORT));
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    final Selector selector = Selector.open();
    receiveChannel.register(selector, OP_READ, this);
    final NioSelectedKeySet keySet = Common.keySet(selector);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(HISTOGRAM, SEND_ADDRESS, buffer, sendChannel, selector, keySet, running);
        HISTOGRAM.reset();
        System.gc();
        LockSupport.parkNanos(1000 * 1000 * 1000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) Selector(java.nio.channels.Selector)

Example 2 with NioSelectedKeySet

use of org.agrona.nio.NioSelectedKeySet in project aeron by real-logic.

the class Common method keySet.

public static NioSelectedKeySet keySet(final Selector selector) {
    NioSelectedKeySet tmpSet = null;
    if (null != PUBLIC_SELECTED_KEYS_FIELD) {
        try {
            tmpSet = new NioSelectedKeySet();
            SELECTED_KEYS_FIELD.set(selector, tmpSet);
            PUBLIC_SELECTED_KEYS_FIELD.set(selector, tmpSet);
        } catch (final Exception ignore) {
            tmpSet = null;
        }
    }
    return tmpSet;
}
Also used : NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) IOException(java.io.IOException)

Example 3 with NioSelectedKeySet

use of org.agrona.nio.NioSelectedKeySet in project Aeron by real-logic.

the class Common method keySet.

static NioSelectedKeySet keySet(final Selector selector) {
    NioSelectedKeySet tmpSet = null;
    if (null != PUBLIC_SELECTED_KEYS_FIELD) {
        try {
            tmpSet = new NioSelectedKeySet();
            SELECTED_KEYS_FIELD.set(selector, tmpSet);
            PUBLIC_SELECTED_KEYS_FIELD.set(selector, tmpSet);
        } catch (final Exception ignore) {
            tmpSet = null;
        }
    }
    return tmpSet;
}
Also used : NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) IOException(java.io.IOException)

Example 4 with NioSelectedKeySet

use of org.agrona.nio.NioSelectedKeySet in project aeron by real-logic.

the class HackSelectReceiveSendUdpPong method run.

private void run() throws IOException {
    final InetSocketAddress sendAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel receiveChannel = DatagramChannel.open();
    Common.init(receiveChannel);
    receiveChannel.bind(new InetSocketAddress("localhost", Common.PING_PORT));
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    final Selector selector = Selector.open();
    final NioSelectedKeySet keySet = Common.keySet(selector);
    final ToIntFunction<SelectionKey> handler = (key) -> {
        try {
            buffer.clear();
            receiveChannel.receive(buffer);
            final long receivedSequenceNumber = buffer.getLong(0);
            final long receivedTimestamp = buffer.getLong(SIZE_OF_LONG);
            buffer.clear();
            buffer.putLong(receivedSequenceNumber);
            buffer.putLong(receivedTimestamp);
            buffer.flip();
            sendChannel.send(buffer, sendAddress);
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
        return 1;
    };
    receiveChannel.register(selector, OP_READ, null);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (true) {
        while (selector.selectNow() == 0) {
            if (!running.get()) {
                return;
            }
        }
        keySet.forEach(handler);
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) DatagramChannel(java.nio.channels.DatagramChannel) Selector(java.nio.channels.Selector) ToIntFunction(java.util.function.ToIntFunction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) SigInt(org.agrona.concurrent.SigInt) ByteBuffer(java.nio.ByteBuffer) SIZE_OF_LONG(org.agrona.BitUtil.SIZE_OF_LONG) Configuration(io.aeron.driver.Configuration) NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) OP_READ(java.nio.channels.SelectionKey.OP_READ) SelectionKey(java.nio.channels.SelectionKey) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Selector(java.nio.channels.Selector)

Example 5 with NioSelectedKeySet

use of org.agrona.nio.NioSelectedKeySet in project Aeron by real-logic.

the class SendHackSelectReceiveUdpPing method run.

private void run() throws IOException {
    receiveChannel = DatagramChannel.open();
    Common.init(receiveChannel);
    receiveChannel.bind(new InetSocketAddress("localhost", Common.PONG_PORT));
    final DatagramChannel sendChannel = DatagramChannel.open();
    Common.init(sendChannel);
    final Selector selector = Selector.open();
    receiveChannel.register(selector, OP_READ, this);
    final NioSelectedKeySet keySet = Common.keySet(selector);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(HISTOGRAM, SEND_ADDRESS, buffer, sendChannel, selector, keySet, running);
        HISTOGRAM.reset();
        System.gc();
        LockSupport.parkNanos(1000 * 1000 * 1000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NioSelectedKeySet(org.agrona.nio.NioSelectedKeySet) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) Selector(java.nio.channels.Selector)

Aggregations

NioSelectedKeySet (org.agrona.nio.NioSelectedKeySet)6 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)4 DatagramChannel (java.nio.channels.DatagramChannel)4 Selector (java.nio.channels.Selector)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Configuration (io.aeron.driver.Configuration)2 ByteBuffer (java.nio.ByteBuffer)2 SelectionKey (java.nio.channels.SelectionKey)2 OP_READ (java.nio.channels.SelectionKey.OP_READ)2 ToIntFunction (java.util.function.ToIntFunction)2 SIZE_OF_LONG (org.agrona.BitUtil.SIZE_OF_LONG)2 SigInt (org.agrona.concurrent.SigInt)2 ThreadHints (org.agrona.hints.ThreadHints)1