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