Search in sources :

Example 26 with DatagramChannel

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

the class LifxSelectorUtil method openBroadcastChannel.

@Nullable
public static SelectionKey openBroadcastChannel(@Nullable Selector selector, String logId, int broadcastPort) throws IOException {
    if (selector == null) {
        return null;
    }
    DatagramChannel broadcastChannel = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).setOption(StandardSocketOptions.SO_BROADCAST, true);
    broadcastChannel.configureBlocking(false);
    LOGGER.debug("{} : Binding the broadcast channel on port {}", logId, broadcastPort);
    broadcastChannel.bind(new InetSocketAddress(broadcastPort));
    return broadcastChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 27 with DatagramChannel

use of java.nio.channels.DatagramChannel in project jmeter-plugins by undera.

the class DatagramChannelWithTimeoutsTest method testOpen.

/**
 * Test of open method, of class DatagramChannelWithTimeouts.
 */
@Test
public void testOpen() throws Exception {
    System.out.println("open");
    DatagramChannel result = DatagramChannelWithTimeoutsEmul.open();
    Assert.assertNotNull(result);
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel)

Example 28 with DatagramChannel

use of java.nio.channels.DatagramChannel in project jmeter-plugins by undera.

the class DatagramChannelWithTimeoutsTest method testConnect.

/**
 * Test of connect method, of class DatagramChannelWithTimeouts.
 */
@Test
public void testConnect() throws Exception {
    System.out.println("connect");
    SocketAddress remote = new InetSocketAddress("localhost", 123);
    DatagramChannel result = instance.connect(remote);
    Assert.assertNotNull(result);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 29 with DatagramChannel

use of java.nio.channels.DatagramChannel in project fluency by komamitsu.

the class UDPHeartbeaterTest method testUDPHeartbeaterDown.

@Test
public void testUDPHeartbeaterDown() throws IOException, InterruptedException {
    final DatagramChannel datagramChannel = DatagramChannel.open();
    datagramChannel.socket().bind(null);
    int localPort = datagramChannel.socket().getLocalPort();
    datagramChannel.close();
    UDPHeartbeater.Config config = new UDPHeartbeater.Config().setPort(localPort).setIntervalMillis(500);
    UDPHeartbeater heartbeater = null;
    try {
        final AtomicInteger pongCounter = new AtomicInteger();
        heartbeater = config.createInstance();
        heartbeater.setCallback(new Heartbeater.Callback() {

            @Override
            public void onHeartbeat() {
                pongCounter.incrementAndGet();
            }

            @Override
            public void onFailure(Throwable cause) {
            }
        });
        heartbeater.start();
        TimeUnit.SECONDS.sleep(1);
        assertEquals(0, pongCounter.get());
    } finally {
        if (heartbeater != null) {
            heartbeater.close();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatagramChannel(java.nio.channels.DatagramChannel) Test(org.junit.Test)

Example 30 with DatagramChannel

use of java.nio.channels.DatagramChannel in project fluency by komamitsu.

the class UDPHeartbeater method invoke.

@Override
protected void invoke() throws IOException {
    DatagramChannel datagramChannel = null;
    try {
        datagramChannel = DatagramChannel.open();
        ByteBuffer byteBuffer = ByteBuffer.allocate(0);
        datagramChannel.send(byteBuffer, socketAddress);
        datagramChannel.receive(byteBuffer);
        pong();
    } finally {
        if (datagramChannel != null) {
            datagramChannel.close();
        }
    }
}
Also used : DatagramChannel(java.nio.channels.DatagramChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramChannel (java.nio.channels.DatagramChannel)214 InetSocketAddress (java.net.InetSocketAddress)92 ByteBuffer (java.nio.ByteBuffer)71 IOException (java.io.IOException)58 MembershipKey (java.nio.channels.MembershipKey)22 DatagramSocket (java.net.DatagramSocket)21 SocketAddress (java.net.SocketAddress)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