Search in sources :

Example 91 with Selector

use of java.nio.channels.Selector 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 92 with Selector

use of java.nio.channels.Selector 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 93 with Selector

use of java.nio.channels.Selector in project aeron by real-logic.

the class ClusterNetworkTopologyTest method shouldLogReplicate.

@ParameterizedTest
@MethodSource("singleTopologyConfigurations")
@InterruptAfter(60)
void shouldLogReplicate(final List<String> hostnames, final List<String> internalHostnames, final String ingressChannel, final String logChannel) throws Exception {
    assertNotNull(hostnames);
    assertEquals(3, hostnames.size());
    setupDataCollection(3);
    final String ingressEndpoints = ingressChannel.contains("endpoint") ? null : BasicAuctionClusterClient.ingressEndpoints(hostnames);
    try (RemoteLaunchClient remote0 = RemoteLaunchClient.connect(hostnames.get(0), REMOTE_LAUNCH_PORT);
        RemoteLaunchClient remote1 = RemoteLaunchClient.connect(hostnames.get(1), REMOTE_LAUNCH_PORT)) {
        final Selector selector = Selector.open();
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote0, selector, 0);
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote1, selector, 1);
        connectAndSendMessages(ingressChannel, ingressEndpoints, selector, 10);
    }
    Thread.sleep(5_000);
    try (RemoteLaunchClient remote0 = RemoteLaunchClient.connect(hostnames.get(0), REMOTE_LAUNCH_PORT);
        RemoteLaunchClient remote2 = RemoteLaunchClient.connect(hostnames.get(2), REMOTE_LAUNCH_PORT)) {
        final Selector selector = Selector.open();
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote0, selector, 0);
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote2, selector, 2);
        connectAndSendMessages(ingressChannel, ingressEndpoints, selector, 10);
    }
}
Also used : RemoteLaunchClient(io.aeron.test.launcher.RemoteLaunchClient) Selector(java.nio.channels.Selector) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 94 with Selector

use of java.nio.channels.Selector in project aeron by real-logic.

the class ClusterNetworkTopologyTest method shouldGetEchoFromCluster.

@ParameterizedTest
@MethodSource("provideTopologyConfigurations")
@InterruptAfter(60)
void shouldGetEchoFromCluster(final List<String> hostnames, final List<String> internalHostnames, final String ingressChannel, final String logChannel) throws Exception {
    assertNotNull(hostnames);
    assertEquals(3, hostnames.size());
    setupDataCollection(3);
    final String ingressEndpoints = ingressChannel.contains("endpoint") ? null : BasicAuctionClusterClient.ingressEndpoints(hostnames);
    try (RemoteLaunchClient remote0 = RemoteLaunchClient.connect(hostnames.get(0), REMOTE_LAUNCH_PORT);
        RemoteLaunchClient remote1 = RemoteLaunchClient.connect(hostnames.get(1), REMOTE_LAUNCH_PORT);
        RemoteLaunchClient remote2 = RemoteLaunchClient.connect(hostnames.get(2), REMOTE_LAUNCH_PORT)) {
        final Selector selector = Selector.open();
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote0, selector, 0);
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote1, selector, 1);
        launchNode(hostnames, internalHostnames, ingressChannel, logChannel, remote2, selector, 2);
        connectAndSendMessages(ingressChannel, ingressEndpoints, selector, 1);
    }
}
Also used : RemoteLaunchClient(io.aeron.test.launcher.RemoteLaunchClient) Selector(java.nio.channels.Selector) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 95 with Selector

use of java.nio.channels.Selector in project smarthome by eclipse.

the class LifxLightCommunicationHandler method start.

public void start() {
    try {
        lock.lock();
        logger.debug("{} : Starting communication handler", logId);
        logger.debug("{} : Using '{}' as source identifier", logId, Long.toString(sourceId, 16));
        ScheduledFuture<?> localNetworkJob = networkJob;
        if (localNetworkJob == null || localNetworkJob.isCancelled()) {
            networkJob = scheduler.scheduleWithFixedDelay(this::receiveAndHandlePackets, 0, PACKET_INTERVAL, TimeUnit.MILLISECONDS);
        }
        currentLightState.setOffline();
        Selector localSelector = Selector.open();
        selector = localSelector;
        if (isBroadcastEnabled()) {
            broadcastKey = openBroadcastChannel(selector, logId, broadcastPort);
            selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host, macAddress, broadcastKey, unicastKey);
            broadcastPacket(new GetServiceRequest());
        } else {
            unicastKey = openUnicastChannel(selector, logId, host);
            selectorContext = new LifxSelectorContext(localSelector, sourceId, sequenceNumberSupplier, logId, host, macAddress, broadcastKey, unicastKey);
            sendPacket(new GetServiceRequest());
        }
    } catch (IOException e) {
        logger.error("{} while starting LIFX communication handler for light '{}' : {}", e.getClass().getSimpleName(), logId, e.getMessage(), e);
    } finally {
        lock.unlock();
    }
}
Also used : GetServiceRequest(org.eclipse.smarthome.binding.lifx.internal.protocol.GetServiceRequest) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Aggregations

Selector (java.nio.channels.Selector)189 SelectionKey (java.nio.channels.SelectionKey)85 IOException (java.io.IOException)71 SocketChannel (java.nio.channels.SocketChannel)52 InetSocketAddress (java.net.InetSocketAddress)38 ByteBuffer (java.nio.ByteBuffer)29 ServerSocketChannel (java.nio.channels.ServerSocketChannel)27 Test (org.junit.Test)14 DatagramChannel (java.nio.channels.DatagramChannel)13 ClosedChannelException (java.nio.channels.ClosedChannelException)12 CancelledKeyException (java.nio.channels.CancelledKeyException)11 NioEndpoint (org.apache.tomcat.util.net.NioEndpoint)10 ClosedSelectorException (java.nio.channels.ClosedSelectorException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 EOFException (java.io.EOFException)7 ServerSocket (java.net.ServerSocket)7 SelectableChannel (java.nio.channels.SelectableChannel)7 RemoteLaunchClient (io.aeron.test.launcher.RemoteLaunchClient)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 MethodSource (org.junit.jupiter.params.provider.MethodSource)6