Example usage for java.nio.channels SelectionKey OP_ACCEPT

List of usage examples for java.nio.channels SelectionKey OP_ACCEPT

Introduction

In this page you can find the example usage for java.nio.channels SelectionKey OP_ACCEPT.

Prototype

int OP_ACCEPT

To view the source code for java.nio.channels SelectionKey OP_ACCEPT.

Click Source Link

Document

Operation-set bit for socket-accept operations.

Usage

From source file:de.kapsi.net.daap.nio.DaapServerNIO.java

/**
 * The run loop//from w w  w.  j a v a  2s.  c o m
 */
public void run() {

    try {

        if (running) {
            LOG.error("DaapServerNIO is already running.");
            return;
        }

        selector = Selector.open();

        SelectionKey sk = ssc.register(selector, SelectionKey.OP_ACCEPT);

        process();

    } catch (IOException err) {
        LOG.error(err);
        throw new RuntimeException(err);

    } finally {
        close();
    }
}

From source file:org.openhab.binding.tcp.AbstractSocketChannelBinding.java

protected void configureListenerChannel() {

    // open the listener port
    try {//from  w  ww. j  a  va2  s  . co  m
        listenerChannel = ServerSocketChannel.open();
        listenerChannel.socket().bind(new InetSocketAddress(listenerPort));
        listenerChannel.configureBlocking(false);

        logger.info("Listening for incoming connections on {}", listenerChannel.getLocalAddress());

        synchronized (selector) {
            selector.wakeup();
            try {
                listenerKey = listenerChannel.register(selector, SelectionKey.OP_ACCEPT);
            } catch (ClosedChannelException e1) {
                logger.error("An exception occurred while registering a selector: {}", e1.getMessage());
            }
        }

    } catch (Exception e3) {
        logger.error("An exception occurred while creating the Listener Channel on port number {} ({})",
                listenerPort, e3.getMessage());
    }
}

From source file:org.quickserver.net.server.QuickServer.java

/**
 * Returns a ServerSocket object to be used for listening.
 * @since 1.4.0/*  w  w  w  .  jav a  2  s  . co  m*/
 */
protected void makeServerSocket() throws BindException, IOException {
    server = null;
    logger.log(Level.FINEST, "Binding {0} to IP: {1}", new Object[] { getName(), getBindAddr() });
    InetSocketAddress bindAddress = new InetSocketAddress(getBindAddr(), getPort());

    try {
        NetworkInterface ni = NetworkInterface.getByInetAddress(getBindAddr());
        if (ni != null) {
            logger.fine("NetworkInterface: " + ni);
        }
    } catch (Exception igrnore) {
        /*ignore*/} catch (Error igrnore) {
        /*ignore*/}

    if (getSecure().isEnable() == false) {
        logger.log(Level.FINE, "Making a normal ServerSocket for {0}", getName());
        setRunningSecure(false);

        if (getBlockingMode() == false) {
            //for non-blocking
            serverSocketChannel = ServerSocketChannel.open();
            server = serverSocketChannel.socket();
            server.bind(bindAddress, getBasicConfig().getAdvancedSettings().getBacklog());
        } else {
            //for blocking
            server = new ServerSocket(getPort(), getBasicConfig().getAdvancedSettings().getBacklog(),
                    getBindAddr());
        }
    } else {
        try {
            logger.log(Level.FINE, "Making a secure ServerSocket for {0}", getName());
            getSSLContext();
            setRunningSecure(true);

            if (getBlockingMode() == false) {

                logger.log(Level.FINE, "Making a secure ServerSocketChannel for {0}", getName());
                //for non-blocking
                serverSocketChannel = ServerSocketChannel.open();
                server = serverSocketChannel.socket();
                server.bind(bindAddress, getBasicConfig().getAdvancedSettings().getBacklog());
            } else {

                ServerSocketFactory ssf = getSSLContext().getServerSocketFactory();
                SSLServerSocket serversocket = (SSLServerSocket) ssf.createServerSocket(getPort(),
                        getBasicConfig().getAdvancedSettings().getBacklog(), getBindAddr());
                serversocket.setNeedClientAuth(secure.isClientAuthEnable());
                setRunningSecure(true);

                secureStoreManager.logSSLServerSocketInfo(serversocket);

                server = serversocket;
                serverSocketChannel = server.getChannel();

                if (serverSocketChannel == null && getBlockingMode() == false) {
                    logger.warning("Secure Server does not support Channel! So will run in blocking mode.");
                    blockingMode = false;
                }

            } //blocking
        } catch (NoSuchAlgorithmException e) {
            logger.log(Level.WARNING, "NoSuchAlgorithmException : {0}", e);
            throw new IOException("Error creating secure socket : " + e.getMessage());
        } catch (KeyManagementException e) {
            logger.log(Level.WARNING, "KeyManagementException : {0}", e);
            throw new IOException("Error creating secure socket : " + e.getMessage());
        }
    }

    server.setReuseAddress(true);

    int connectionTime = 0;
    int latency = 0;
    int bandwidth = 0;

    connectionTime = getBasicConfig().getAdvancedSettings().getPerformancePreferencesConnectionTime();
    latency = getBasicConfig().getAdvancedSettings().getPerformancePreferencesLatency();
    bandwidth = getBasicConfig().getAdvancedSettings().getPerformancePreferencesBandwidth();

    logger.log(Level.FINE, "getPerformancePreferencesConnectionTime : {0}", connectionTime);
    logger.log(Level.FINE, "getPerformancePreferencesLatency : {0}", latency);
    logger.log(Level.FINE, "getPerformancePreferencesBandwidth : {0}", bandwidth);

    server.setPerformancePreferences(connectionTime, latency, bandwidth);

    int clientSocketReceiveBufferSize = getBasicConfig().getAdvancedSettings()
            .getClientSocketReceiveBufferSize();
    if (clientSocketReceiveBufferSize > 0) {
        logger.log(Level.FINE, "clientSocketReceiveBufferSize: {0}", clientSocketReceiveBufferSize);
        server.setReceiveBufferSize(clientSocketReceiveBufferSize);
    }

    if (getBlockingMode() == false) {
        logger.log(Level.FINE, "Server Mode {0} - Non Blocking", getName());
        if (selector == null || selector.isOpen() == false) {
            logger.finest("Opening new selector");
            selector = Selector.open();
        } else {
            logger.log(Level.FINEST, "Reusing selector: {0}", selector);
        }
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        selector.wakeup();
    } else {
        logger.log(Level.FINE, "Server Mode {0} - Blocking", getName());
    }
}