Search in sources :

Example 26 with SelectionKey

use of java.nio.channels.SelectionKey in project http-kit by http-kit.

the class HttpServer method accept.

void accept(SelectionKey key) {
    ServerSocketChannel ch = (ServerSocketChannel) key.channel();
    SocketChannel s;
    try {
        while ((s = ch.accept()) != null) {
            s.configureBlocking(false);
            HttpAtta atta = new HttpAtta(maxBody, maxLine, proxyProtocolOption);
            SelectionKey k = s.register(selector, OP_READ, atta);
            atta.channel = new AsyncChannel(k, this);
        }
    } catch (Exception e) {
        // eg: too many open files. do not quit
        HttpUtils.printError("accept incoming request", e);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) ServerSocketChannel(java.nio.channels.ServerSocketChannel) LineTooLargeException(org.httpkit.LineTooLargeException) ProtocolException(org.httpkit.ProtocolException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) RequestTooLargeException(org.httpkit.RequestTooLargeException)

Example 27 with SelectionKey

use of java.nio.channels.SelectionKey in project http-kit by http-kit.

the class MakeupIdelConnection method main.

public static void main(String[] args) throws IOException {
    final Selector selector = Selector.open();
    InetSocketAddress[] locals = { new InetSocketAddress("127.0.0.1", 4348) };
    // InetSocketAddress remote = new InetSocketAddress("192.168.1.114",
    // 9090);
    long start = System.currentTimeMillis();
    int connected = 0;
    int currentConnectionPerIP = 0;
    while (true) {
        if (System.currentTimeMillis() - start > 1000 * 60 * 10) {
            break;
        }
        for (int i = 0; i < connectionPerIP / STEPS && currentConnectionPerIP < connectionPerIP; ++i, ++currentConnectionPerIP) {
            for (InetSocketAddress addr : locals) {
                SocketChannel ch = SocketChannel.open();
                ch.configureBlocking(false);
                Socket s = ch.socket();
                s.setReuseAddress(true);
                // s.bind(addr);
                ch.register(selector, SelectionKey.OP_CONNECT);
                ch.connect(addr);
            }
        }
        // 10s
        int select = selector.select(1000 * 10);
        if (select > 0) {
            System.out.println("select return: " + select + " events ; current connection per ip: " + currentConnectionPerIP);
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            Iterator<SelectionKey> it = selectedKeys.iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                if (key.isConnectable()) {
                    SocketChannel ch = (SocketChannel) key.channel();
                    if (ch.finishConnect()) {
                        ++connected;
                        if (connected % (connectionPerIP * locals.length / 10) == 0) {
                            System.out.println("connected: " + connected);
                        }
                        key.interestOps(SelectionKey.OP_READ);
                    }
                }
            }
            selectedKeys.clear();
        }
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) Selector(java.nio.channels.Selector)

Example 28 with SelectionKey

use of java.nio.channels.SelectionKey in project Openfire by igniterealtime.

the class ConferenceReceiver method initLoneReceiverChannel.

private void initLoneReceiverChannel(int loneReceiverPort) {
    if (this.loneReceiverPort != loneReceiverPort && loneReceiverChannel != null) {
        close();
    }
    this.loneReceiverPort = loneReceiverPort;
    try {
        selector = Selector.open();
    } catch (IOException e) {
        Logger.println("Conference receiver failed to open selector " + e.getMessage());
        return;
    }
    if (loneReceiverPort == 0) {
        return;
    }
    Logger.println("Init lone channel using port " + loneReceiverPort);
    try {
        loneReceiverChannel = DatagramChannel.open();
        if (Logger.logLevel >= Logger.LOG_INFO) {
            Logger.println("Opened lone receiver channel " + loneReceiverChannel);
        }
    } catch (IOException e) {
        Logger.println("Conference receiver failed to open DatagramChannel " + " " + e.getMessage());
        return;
    }
    try {
        loneReceiverChannel.configureBlocking(false);
    } catch (IOException e) {
        Logger.println("Conference receiver failed to configureBlocking to false " + e.getMessage());
        return;
    }
    DatagramSocket socket = loneReceiverChannel.socket();
    try {
        socket.setReceiveBufferSize(RtpSocket.MAX_RECEIVE_BUFFER);
    } catch (SocketException e) {
        Logger.println("ConferenceReceiver failed to set receive buffer size " + e.getMessage());
        return;
    }
    try {
        socket.setSoTimeout(0);
    } catch (SocketException e) {
        Logger.println("ConferenceReceiver failed to set timeout " + e.getMessage());
        return;
    }
    InetSocketAddress bridgeAddress = Bridge.getLocalBridgeAddress();
    InetSocketAddress isa = new InetSocketAddress(bridgeAddress.getAddress(), loneReceiverPort);
    try {
        socket.bind(isa);
    } catch (IOException e) {
        Logger.println("Conference receiver unable to bind to " + loneReceiverPort + " " + e.getMessage());
        return;
    }
    try {
        SelectionKey selectionKey = loneReceiverChannel.register(selector, SelectionKey.OP_READ);
    } catch (Exception e) {
        Logger.println("Conference receiver unable to register:  " + e.getMessage());
        return;
    }
    memberCount++;
    Logger.println("Lone Channel uses port " + loneReceiverPort);
}
Also used : SocketException(java.net.SocketException) SelectionKey(java.nio.channels.SelectionKey) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) IOException(java.io.IOException) SocketException(java.net.SocketException)

Example 29 with SelectionKey

use of java.nio.channels.SelectionKey in project java-design-patterns by iluwatar.

the class NioReactor method registerChannel.

/**
   * Registers a new channel (handle) with this reactor. Reactor will start waiting for events on this channel and
   * notify of any events. While registering the channel the reactor uses {@link AbstractNioChannel#getInterestedOps()}
   * to know about the interested operation of this channel.
   * 
   * @param channel
   *          a new channel on which reactor will wait for events. The channel must be bound prior to being registered.
   * @return this
   * @throws IOException
   *           if any I/O error occurs.
   */
public NioReactor registerChannel(AbstractNioChannel channel) throws IOException {
    SelectionKey key = channel.getJavaChannel().register(selector, channel.getInterestedOps());
    key.attach(channel);
    channel.setReactor(this);
    return this;
}
Also used : SelectionKey(java.nio.channels.SelectionKey)

Example 30 with SelectionKey

use of java.nio.channels.SelectionKey in project hs4j by killme2008.

the class AbstractNioSession method flush0.

protected final void flush0() {
    SelectionKey tmpKey = null;
    Selector writeSelector = null;
    int attempts = 0;
    try {
        while (true) {
            if (writeSelector == null) {
                writeSelector = SelectorFactory.getSelector();
                if (writeSelector == null) {
                    return;
                }
                tmpKey = selectableChannel.register(writeSelector, SelectionKey.OP_WRITE);
            }
            if (writeSelector.select(1000) == 0) {
                attempts++;
                if (attempts > 2) {
                    return;
                }
            } else {
                break;
            }
        }
        onWrite(selectableChannel.keyFor(writeSelector));
    } catch (ClosedChannelException cce) {
        onException(cce);
        log.error("Flush error", cce);
        close();
    } catch (IOException ioe) {
        onException(ioe);
        log.error("Flush error", ioe);
        close();
    } finally {
        if (tmpKey != null) {
            // Cancel the key.
            tmpKey.cancel();
            tmpKey = null;
        }
        if (writeSelector != null) {
            try {
                writeSelector.selectNow();
            } catch (IOException e) {
                log.error("Temp selector selectNow error", e);
            }
            // return selector
            SelectorFactory.returnSelector(writeSelector);
        }
    }
}
Also used : SelectionKey(java.nio.channels.SelectionKey) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Selector(java.nio.channels.Selector)

Aggregations

SelectionKey (java.nio.channels.SelectionKey)190 IOException (java.io.IOException)87 SocketChannel (java.nio.channels.SocketChannel)56 Selector (java.nio.channels.Selector)42 ServerSocketChannel (java.nio.channels.ServerSocketChannel)39 ClosedChannelException (java.nio.channels.ClosedChannelException)30 InetSocketAddress (java.net.InetSocketAddress)26 CancelledKeyException (java.nio.channels.CancelledKeyException)25 ByteBuffer (java.nio.ByteBuffer)23 ClosedSelectorException (java.nio.channels.ClosedSelectorException)17 SelectableChannel (java.nio.channels.SelectableChannel)13 Test (org.junit.Test)13 Iterator (java.util.Iterator)9 Socket (java.net.Socket)7 ArrayList (java.util.ArrayList)7 SocketTimeoutException (java.net.SocketTimeoutException)6 AbstractSelectionKey (java.nio.channels.spi.AbstractSelectionKey)6 EOFException (java.io.EOFException)5 DatagramChannel (java.nio.channels.DatagramChannel)5 HashSet (java.util.HashSet)5