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