Search in sources :

Example 96 with DatagramChannel

use of java.nio.channels.DatagramChannel in project Aeron by real-logic.

the class SendReceiveUdpPing method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws IOException if an error occurs with the channel.
 */
public static void main(final String[] args) throws IOException {
    int numChannels = 1;
    if (1 <= args.length) {
        numChannels = Integer.parseInt(args[0]);
    }
    String remoteHost = "localhost";
    if (2 <= args.length) {
        remoteHost = args[1];
    }
    System.out.printf("Number of channels: %d, Remote host: %s%n", numChannels, remoteHost);
    final Histogram histogram = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel[] receiveChannels = new DatagramChannel[numChannels];
    for (int i = 0; i < receiveChannels.length; i++) {
        receiveChannels[i] = DatagramChannel.open();
        init(receiveChannels[i]);
        receiveChannels[i].bind(new InetSocketAddress("0.0.0.0", Common.PONG_PORT + i));
    }
    final InetSocketAddress sendAddress = new InetSocketAddress(remoteHost, Common.PING_PORT);
    final DatagramChannel sendChannel = DatagramChannel.open();
    init(sendChannel);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(histogram, sendAddress, buffer, receiveChannels, sendChannel, running);
        histogram.reset();
        System.gc();
        LockSupport.parkNanos(1_000_000_000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Histogram(org.HdrHistogram.Histogram) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 97 with DatagramChannel

use of java.nio.channels.DatagramChannel in project Aeron by real-logic.

the class WriteReceiveUdpPing method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws IOException if an error occurs with the channel.
 */
public static void main(final String[] args) throws IOException {
    int numChannels = 1;
    if (1 == args.length) {
        numChannels = Integer.parseInt(args[0]);
    }
    final Histogram histogram = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(MTU_LENGTH_DEFAULT);
    final DatagramChannel[] receiveChannels = new DatagramChannel[numChannels];
    for (int i = 0; i < receiveChannels.length; i++) {
        receiveChannels[i] = DatagramChannel.open();
        init(receiveChannels[i]);
        receiveChannels[i].bind(new InetSocketAddress("localhost", PONG_PORT + i));
    }
    final InetSocketAddress writeAddress = new InetSocketAddress("localhost", PING_PORT);
    final DatagramChannel writeChannel = DatagramChannel.open();
    init(writeChannel, writeAddress);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(histogram, buffer, receiveChannels, writeChannel, running);
        histogram.reset();
        System.gc();
        LockSupport.parkNanos(1000_000_000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Histogram(org.HdrHistogram.Histogram) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 98 with DatagramChannel

use of java.nio.channels.DatagramChannel in project Aeron by real-logic.

the class SendSelectReceiveUdpPing method run.

private void run() throws IOException {
    final Histogram histogram = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    final ByteBuffer buffer = ByteBuffer.allocateDirect(Configuration.MTU_LENGTH_DEFAULT);
    final DatagramChannel 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();
    final IntSupplier handler = () -> {
        try {
            buffer.clear();
            receiveChannel.receive(buffer);
            final long receivedSequenceNumber = buffer.getLong(0);
            final long timestampNs = buffer.getLong(SIZE_OF_LONG);
            if (receivedSequenceNumber != sequenceNumber) {
                throw new IllegalStateException("data Loss:" + sequenceNumber + " to " + receivedSequenceNumber);
            }
            final long durationNs = System.nanoTime() - timestampNs;
            histogram.recordValue(durationNs);
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
        return 1;
    };
    receiveChannel.register(selector, OP_READ, handler);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (running.get()) {
        measureRoundTrip(histogram, SEND_ADDRESS, buffer, sendChannel, selector, running);
        histogram.reset();
        System.gc();
        LockSupport.parkNanos(1000 * 1000 * 1000);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Histogram(org.HdrHistogram.Histogram) IntSupplier(java.util.function.IntSupplier) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Selector(java.nio.channels.Selector)

Example 99 with DatagramChannel

use of java.nio.channels.DatagramChannel in project Aeron by real-logic.

the class ReceiveWriteUdpPong method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 * @throws IOException if an error occurs with the channel.
 */
public static void main(final String[] args) throws IOException {
    int numChannels = 1;
    if (1 == args.length) {
        numChannels = Integer.parseInt(args[0]);
    }
    final ByteBuffer buffer = ByteBuffer.allocateDirect(MTU_LENGTH_DEFAULT);
    final DatagramChannel[] receiveChannels = new DatagramChannel[numChannels];
    for (int i = 0; i < receiveChannels.length; i++) {
        receiveChannels[i] = DatagramChannel.open();
        Common.init(receiveChannels[i]);
        receiveChannels[i].bind(new InetSocketAddress("localhost", Common.PING_PORT + i));
    }
    final InetSocketAddress writeAddress = new InetSocketAddress("localhost", Common.PONG_PORT);
    final DatagramChannel writeChannel = DatagramChannel.open();
    Common.init(writeChannel, writeAddress);
    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));
    while (true) {
        buffer.clear();
        boolean available = false;
        while (!available) {
            ThreadHints.onSpinWait();
            if (!running.get()) {
                return;
            }
            for (int i = receiveChannels.length - 1; i >= 0; i--) {
                if (null != receiveChannels[i].receive(buffer)) {
                    available = true;
                    break;
                }
            }
        }
        final long receivedSequenceNumber = buffer.getLong(0);
        final long receivedTimestamp = buffer.getLong(SIZE_OF_LONG);
        buffer.clear();
        buffer.putLong(receivedSequenceNumber);
        buffer.putLong(receivedTimestamp);
        buffer.flip();
        writeChannel.write(buffer);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Example 100 with DatagramChannel

use of java.nio.channels.DatagramChannel in project jmxtrans by jmxtrans.

the class DatagramChannelAllocator method allocate.

@Override
public DatagramChannelPoolable allocate(Slot slot) throws Exception {
    DatagramChannel channel = DatagramChannel.open();
    channel.connect(new InetSocketAddress(server.getHostName(), server.getPort()));
    ChannelWriter writer = new ChannelWriter(bufferSize, charset, channel);
    return new DatagramChannelPoolable(slot, writer, channel, flushStrategy);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)211 InetSocketAddress (java.net.InetSocketAddress)91 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)57 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)21 MembershipKey (java.nio.channels.MembershipKey)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 SocketChannel (java.nio.channels.SocketChannel)17 SelectionKey (java.nio.channels.SelectionKey)16 InetAddress (java.net.InetAddress)13 Selector (java.nio.channels.Selector)13 Test (org.junit.Test)11 SocketException (java.net.SocketException)9 ClosedChannelException (java.nio.channels.ClosedChannelException)9 Histogram (org.HdrHistogram.Histogram)8 CancelledKeyException (java.nio.channels.CancelledKeyException)7 DatagramPacket (java.net.DatagramPacket)5 NetworkInterface (java.net.NetworkInterface)5 ArrayList (java.util.ArrayList)5