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:me.xingrz.prox.tcp.TcpProxy.java

@Override
protected ServerSocketChannel createChannel(Selector selector) throws IOException {
    ServerSocketChannel channel = ServerSocketChannel.open();
    channel.configureBlocking(false);//  w w w . j  av  a 2  s  . com
    channel.socket().bind(new InetSocketAddress(0));
    channel.register(selector, SelectionKey.OP_ACCEPT, this);
    return channel;
}

From source file:com.ok2c.lightmtp.impl.protocol.LoggingIOSession.java

private static String formatOps(final int ops) {
    final StringBuilder buffer = new StringBuilder(6);
    buffer.append('[');
    if ((ops & SelectionKey.OP_READ) > 0) {
        buffer.append('r');
    }/*from   ww w  .j  av a  2s . c om*/
    if ((ops & SelectionKey.OP_WRITE) > 0) {
        buffer.append('w');
    }
    if ((ops & SelectionKey.OP_ACCEPT) > 0) {
        buffer.append('a');
    }
    if ((ops & SelectionKey.OP_CONNECT) > 0) {
        buffer.append('c');
    }
    buffer.append(']');
    return buffer.toString();
}

From source file:com.l2jfree.network.mmocore.AcceptorThread.java

public void openServerSocket(InetAddress address, int port) throws IOException {
    ServerSocketChannel selectable = ServerSocketChannel.open();
    selectable.configureBlocking(false);

    ServerSocket ss = selectable.socket();
    ss.setReuseAddress(true);/*from www . j av  a  2 s  .c o  m*/
    ss.setReceiveBufferSize(getBufferSize());
    if (address == null) {
        ss.bind(new InetSocketAddress(port));
    } else {
        ss.bind(new InetSocketAddress(address, port));
    }
    selectable.register(getSelector(), SelectionKey.OP_ACCEPT);
}

From source file:com.github.neoio.nio.util.NIOUtils.java

public static ServerSocketChannel openServerSocket(Selector selector, SocketAddress socketAddress)
        throws NetSocketException {
    ServerSocketChannel toReturn;

    try {//  w  w w .  j  ava  2 s .c o m
        toReturn = ServerSocketChannel.open();
        toReturn.socket().bind(socketAddress);
        toReturn.configureBlocking(false);
        toReturn.register(selector, SelectionKey.OP_ACCEPT);
    } catch (IOException e) {
        logger.error("IOException occurred while opening server socket", e);
        toReturn = null;
    }

    return toReturn;
}

From source file:org.apache.synapse.transport.utils.logging.LoggingIOSession.java

private static String formatOps(int ops) {
    StringBuilder buffer = new StringBuilder(6);
    buffer.append('[');
    if ((ops & SelectionKey.OP_READ) > 0) {
        buffer.append('r');
    }/*from w  w w. j ava  2  s.c o  m*/
    if ((ops & SelectionKey.OP_WRITE) > 0) {
        buffer.append('w');
    }
    if ((ops & SelectionKey.OP_ACCEPT) > 0) {
        buffer.append('a');
    }
    if ((ops & SelectionKey.OP_CONNECT) > 0) {
        buffer.append('c');
    }
    buffer.append(']');
    return buffer.toString();
}

From source file:com.byteatebit.nbserver.simple.tcp.TcpConnectorFactory.java

@Override
public IConnector create(ISelectorRegistrarBalancer selectorRegistrarBalancer,
        IComputeTaskScheduler taskScheduler, SocketAddress listenAddress) throws IOException {
    Preconditions.checkNotNull(selectorRegistrarBalancer, "selectorRegistrarBalancer cannot be null");
    Preconditions.checkNotNull(taskScheduler, "taskScheduler cannot be null");
    Preconditions.checkNotNull(listenAddress, "listenAddress cannot be null");
    // open the server socketChannel
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(false);
    IOTimeoutTask timeoutTask = (selectionKey, ops) -> {
        LOG.error("selectionKey timeout");
        selectionKey.cancel();/*from  w ww . java  2s. com*/
        IOUtils.closeQuietly(selectionKey.channel());
    };
    for (SocketOptionValue socketOptionValue : socketOptionValues)
        serverSocketChannel.setOption(socketOptionValue.getOption(), socketOptionValue.getValue());
    SelectionKey serverSocketSelectionKey = selectorRegistrarBalancer.getSelectorRegistrar().register(
            serverSocketChannel, SelectionKey.OP_ACCEPT,
            selectionKey -> connectHandler.accept(
                    new NbContext(selectorRegistrarBalancer.getSelectorRegistrar(), taskScheduler),
                    selectionKey, serverSocketChannel),
            timeoutTask, -1);
    serverSocketChannel.socket().bind(listenAddress, maxServerSocketBacklog);
    return new TcpConnector(serverSocketSelectionKey, serverSocketChannel);
}

From source file:gridool.communication.transport.nio.GridNioServer.java

private static Selector createSelector(int port) throws IOException {
    final Selector selector = SelectorProvider.provider().openSelector();

    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.configureBlocking(false);

    ServerSocket servSocket = serverChannel.socket();
    servSocket.setReuseAddress(true);//w w  w  . j a  v  a  2  s. co m
    servSocket.bind(new InetSocketAddress(port));

    serverChannel.register(selector, SelectionKey.OP_ACCEPT);

    if (LOG.isInfoEnabled()) {
        LOG.info("GridNioServer is started at port: " + port);
    }
    return selector;
}

From source file:com.cloudbees.jenkins.plugins.sshagent.jna.AgentServer.java

public String start() throws Exception {
    authSocket = createLocalSocketAddress();
    address = new UnixSocketAddress(new File(authSocket));
    channel = UnixServerSocketChannel.open();
    channel.configureBlocking(false);/*from  w  w w  . ja  v a  2 s .  c  o  m*/
    socket = channel.socket();
    socket.bind(address);
    selector = NativeSelectorProvider.getInstance().openSelector();

    channel.register(selector, SelectionKey.OP_ACCEPT, new SshAgentServerSocketHandler());

    POSIXFactory.getPOSIX().chmod(authSocket, 0600);
    if (!new File(authSocket).exists()) {
        throw new IllegalStateException("failed to create " + authSocket + " of length " + authSocket.length()
                + " (check UNIX_PATH_MAX)");
    }

    thread = new Thread(new AgentSocketAcceptor(), "SSH Agent socket acceptor " + authSocket);
    thread.setDaemon(true);
    thread.start();
    return authSocket;
}

From source file:eu.stratosphere.nephele.taskmanager.bytebuffered.IncomingConnectionThread.java

public IncomingConnectionThread(ByteBufferedChannelManager byteBufferedChannelManager,
        boolean isListeningThread, InetSocketAddress listeningAddress) throws IOException {
    super("Incoming Connection Thread");

    this.selector = Selector.open();
    this.byteBufferedChannelManager = byteBufferedChannelManager;

    if (isListeningThread) {
        this.listeningSocket = ServerSocketChannel.open();
        this.listeningSocket.configureBlocking(false);
        listeningSocket.register(this.selector, SelectionKey.OP_ACCEPT);
        this.listeningSocket.socket().bind(listeningAddress);
        LOG.debug("Listening on " + this.listeningSocket.socket().getLocalSocketAddress());
    } else {/*w  w  w  .  j  a  va  2  s .  com*/
        this.listeningSocket = null;
    }
}

From source file:com.alibaba.napoli.gecko.core.nio.impl.SelectorManager.java

/**
 * channel/* ww w. ja  v a 2  s  . c o  m*/
 * 
 * @param channel
 * @param ops
 * @param attachment
 * @return
 */
public final Reactor registerChannel(final SelectableChannel channel, final int ops, final Object attachment) {
    this.awaitReady();
    int index = 0;
    // Accept?Reactor
    if (ops == SelectionKey.OP_ACCEPT || ops == SelectionKey.OP_CONNECT) {
        index = 0;
    } else {
        if (this.dividend > 0) {
            index = this.sets.incrementAndGet() % this.dividend + 1;
        } else {
            index = 0;
        }
    }
    final Reactor reactor = this.reactorSet[index];
    reactor.registerChannel(channel, ops, attachment);
    return reactor;

}