use of com.questdb.net.ha.config.DatagramChannelWrapper in project questdb by bluestreak01.
the class AbstractOnDemandPoller method poll.
public T poll(int retryCount, long timeout, TimeUnit timeUnit) throws JournalNetworkException {
try (DatagramChannelWrapper dcw = networkConfig.openDatagramChannel()) {
DatagramChannel dc = dcw.getChannel();
LOG.info().$("Polling on").$(dcw.getGroup()).$(" [").$(dc.getOption(StandardSocketOptions.IP_MULTICAST_IF).getName()).$(']').$();
Selector selector = Selector.open();
dc.configureBlocking(false);
dc.register(selector, SelectionKey.OP_READ);
// print out each datagram that we receive
ByteBuffer buf = ByteBuffer.allocateDirect(4096);
try {
int count = retryCount;
InetSocketAddress sa = null;
while (count > 0 && (sa = poll0(dc, dcw.getGroup(), selector, buf, timeUnit.toMillis(timeout))) == null) {
buf.clear();
count--;
}
if (count == 0) {
throw new JournalNetworkException("Cannot find QuestDB servers on network");
}
return transform(buf, sa);
} finally {
ByteBuffers.release(buf);
}
} catch (IOException e) {
throw new JournalNetworkException(e);
}
}
use of com.questdb.net.ha.config.DatagramChannelWrapper in project questdb by bluestreak01.
the class AbstractOnDemandSender method start0.
private void start0() {
try {
try (DatagramChannelWrapper dcw = serverConfig.openDatagramChannel(instance)) {
DatagramChannel dc = dcw.getChannel();
LOG.info().$("Sending to ").$(dcw.getGroup()).$(" on [").$(dc.getOption(StandardSocketOptions.IP_MULTICAST_IF).getName()).$(']').$();
selector = Selector.open();
selecting = true;
dc.configureBlocking(false);
dc.register(selector, SelectionKey.OP_READ);
ByteBuffer buf = ByteBuffer.allocateDirect(4096);
try {
while (running) {
int updated = selector.select();
if (!running) {
break;
}
if (updated > 0) {
Iterator<SelectionKey> iter = selector.selectedKeys().iterator();
while (iter.hasNext()) {
SelectionKey sk = iter.next();
iter.remove();
DatagramChannel ch = (DatagramChannel) sk.channel();
buf.clear();
SocketAddress sa = ch.receive(buf);
if (sa != null) {
buf.flip();
if (buf.remaining() >= 4 && inMessageCode == buf.getInt(0)) {
LOG.debug().$("Sending server information [").$(inMessageCode).$("] to [").$(sa).$(']').$();
buf.clear();
buf.putInt(outMessageCode);
prepareBuffer(buf);
dc.send(buf, dcw.getGroup());
}
}
}
}
}
} finally {
ByteBuffers.release(buf);
}
}
} catch (Throwable e) {
LOG.error().$("Multicast sender crashed").$(e).$();
} finally {
latch.countDown();
}
}
Aggregations