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);
}
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);
}
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);
}
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();
}
}
}
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();
}
}
}
Aggregations