Search in sources :

Example 1 with DatagramChannelWrapper

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);
    }
}
Also used : DatagramChannelWrapper(com.questdb.net.ha.config.DatagramChannelWrapper) InetSocketAddress(java.net.InetSocketAddress) JournalNetworkException(com.questdb.std.ex.JournalNetworkException) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Selector(java.nio.channels.Selector)

Example 2 with DatagramChannelWrapper

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();
    }
}
Also used : DatagramChannelWrapper(com.questdb.net.ha.config.DatagramChannelWrapper) SelectionKey(java.nio.channels.SelectionKey) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) ByteBuffer(java.nio.ByteBuffer)

Aggregations

DatagramChannelWrapper (com.questdb.net.ha.config.DatagramChannelWrapper)2 ByteBuffer (java.nio.ByteBuffer)2 DatagramChannel (java.nio.channels.DatagramChannel)2 JournalNetworkException (com.questdb.std.ex.JournalNetworkException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 SelectionKey (java.nio.channels.SelectionKey)1 Selector (java.nio.channels.Selector)1