Example usage for java.nio.channels SelectionKey interestOps

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

Introduction

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

Prototype

public abstract SelectionKey interestOps(int ops);

Source Link

Document

Sets this key's interest set to the given value.

Usage

From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkClient.java

/**
 * ??//from   ww  w  .ja  v a 2s.  co m
 * @param key
 * @throws IOException
 */
private void onConnected(SelectionKey key) throws IOException {
    if (socketChannel.isConnectionPending()) {
        socketChannel.finishConnect();
        key.interestOps(SelectionKey.OP_READ);
        // ?
        state.set(NetworkStateEnum.NETWORK_STATE_CONNECTED.value);
        logger.info(new StringBuilder("Connected to server:").append(hostName).append(":").append(port));
    }
}

From source file:edu.tsinghua.lumaqq.qq.net.TCPPort.java

public void processConnect(SelectionKey sk) throws IOException {
    //?SocketChannel
    channel.finishConnect();/*from   w  w w. j av  a  2 s .com*/
    while (!channel.isConnected()) {
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // ??
        }
        channel.finishConnect();
    }
    sk.interestOps(SelectionKey.OP_READ);
    log.debug("QQ?");
}

From source file:com.byteatebit.nbserver.simple.SelectorTask.java

@Override
public SelectionKey register(SelectableChannel channel, int ops, IOTask nioTask, IOTimeoutTask timeoutTask,
        long timeoutMs) {
    Preconditions.checkNotNull(channel, "channel cannot be null");
    Preconditions.checkNotNull(nioTask, "nioTask cannot be null");
    IOTimeoutTask ioTimeoutTask = timeoutTask == null ? defaultIoTimeoutTask : timeoutTask;
    if (LOG.isDebugEnabled())
        LOG.debug("registering channel " + channel + "for ops " + ops + " and task " + nioTask);
    SelectionKey key = null;
    NioSelectionKeyEntry selectionKeyEntry = null;
    synchronized (this) {
        try {//  w ww.j a va  2  s.c  o  m
            key = channel.keyFor(selector);
            if (key != null) {
                key.interestOps(key.interestOps() | ops);
                selectionKeyEntry = (NioSelectionKeyEntry) key.attachment();
            } else {
                selectionKeyEntry = new NioSelectionKeyEntry();
                key = channel.register(selector, ops, selectionKeyEntry);
            }
        } catch (ClosedChannelException e) {
            throw new SelectorException(e);
        }
        selectionKeyEntry.setTask(nioTask, ioTimeoutTask, ops, timeoutMs);
    }
    if (LOG.isDebugEnabled())
        LOG.debug("Total number of selection keys: " + selector.keys().size());
    return key;
}

From source file:edu.tsinghua.lumaqq.qq.net.AbstractProxy.java

public void processConnect(SelectionKey sk) throws IOException {
    if (connected)
        return;//w  w  w. j  av  a2s. co m
    //?SocketChannel
    socketChannel.finishConnect();
    while (!socketChannel.isConnected()) {
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            // ??
        }
        socketChannel.finishConnect();
    }
    sk.interestOps(SelectionKey.OP_READ);
    connected = true;
    log.debug("??");
}

From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkClient.java

/**
 * ?, ?, ?, ??//w  w w  .jav  a 2s  .  c  o  m
 * @throws IOException
 * @throws InterruptedException
 */
private void dispatch() throws IOException, InterruptedException {
    logger.debug("try dispatch");
    SelectionKey key = null;
    key = opsChangeRequest.getChannel().keyFor(selector);
    if (key != null) {
        if ((opsChangeRequest.getOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) {
            // 
            key.interestOps(SelectionKey.OP_WRITE);
            opsChangeRequest.clearOps(SelectionKey.OP_WRITE);
        } else if ((opsChangeRequest.getOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
            key.interestOps(SelectionKey.OP_READ);
            opsChangeRequest.clearOps(SelectionKey.OP_READ);
        }
    }

    // ?,  
    isWeakuped.set(false);
    if (selector.select(WaveriderConfig.WAVERIDER_DEFAULT_NETWORK_TIME_OUT) <= 0) {
        return;
    }

    // ?
    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
    while (iterator.hasNext()) {
        key = (SelectionKey) iterator.next();
        iterator.remove();
        if (!key.isValid()) {
            continue;
        } else if (key.isReadable()) {
            onRead(key);
        } else if (key.isWritable()) {
            onWrite(key);
        }
    }
}

From source file:com.openteach.diamond.network.waverider.network.DefaultNetWorkServer.java

private void dispatch() throws IOException {
    logger.debug("try dispatch");
    SelectionKey key = null;
    for (SocketChannelOPSChangeRequest request : opsChangeRequstMap.values()) {
        key = request.getChannel().keyFor(selector);
        if (key != null) {
            // /*from ww w  . ja  v a  2 s .  c  o m*/
            if ((request.getOps() & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE) {
                key.interestOps(SelectionKey.OP_WRITE);
                request.clearOps(SelectionKey.OP_WRITE);
            } else if ((request.getOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
                key.interestOps(SelectionKey.OP_READ);
                request.clearOps(SelectionKey.OP_READ);
            }
        }
    }

    isWeakuped.set(false);
    if (selector.select(WaveriderConfig.WAVERIDER_DEFAULT_NETWORK_TIME_OUT) <= 0) {
        return;
    }

    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
    while (iterator.hasNext()) {
        key = (SelectionKey) iterator.next();
        iterator.remove();
        try {
            if (!key.isValid()) {
                continue;
            } else if (key.isAcceptable()) {
                onAccept(key);
            } else if (key.isReadable()) {
                //readerExecutor.execute(new NetworkTask(key, NETWORK_OPERATION_READ));
                onRead(key);
            } else if (key.isWritable()) {
                //writerExecutor.execute(new NetworkTask(key, NETWORK_OPERATION_WRITE));
                onWrite(key);
            }
        } catch (IOException e) {
            // 
            opsChangeRequstMap.remove((SocketChannel) key.channel());
            Session session = (Session) key.attachment();
            if (session != null) {
                session.onException(e);
                // Session
                sessionManager.freeSession(session);
            }
            key.cancel();
            key.channel().close();
            e.printStackTrace();
            logger.error("OOPSException", e);
        }
    }
}

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

private void finishConnection(SelectionKey key) {
    try {//ww  w .  j  a v a  2  s .  com
        ((SocketChannel) key.channel()).finishConnect();
    } catch (IOException e) {
        @SuppressWarnings("unchecked")
        T con = (T) key.attachment();
        closeConnectionImpl(con, true);
        return;
    }

    // key might have been invalidated on finishConnect()
    if (key.isValid()) {
        key.interestOps(key.interestOps() | SelectionKey.OP_READ);
        key.interestOps(key.interestOps() & ~SelectionKey.OP_CONNECT);
    }
}

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

/**
 * Read data/*from  w  w  w  .  j  a v  a  2 s .c o m*/
 * 
 * @throws IOException
 */
private void processRead(SelectionKey sk) throws IOException {

    if (!sk.isValid())
        return;

    DaapConnectionNIO connection = (DaapConnectionNIO) sk.attachment();
    SocketChannel channel = (SocketChannel) sk.channel();

    boolean keepAlive = false;

    keepAlive = connection.read();

    if (keepAlive) {
        sk.interestOps(connection.interrestOps());
    } else {
        cancel(sk);
    }
}

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

/**
 * Notify all clients about an update of the Library
 *///from   w w w.  j av a 2 s  .c om
private void processUpdate() {

    Set keys = selector.keys();
    Iterator it = keys.iterator();
    while (it.hasNext()) {
        SelectionKey sk = (SelectionKey) it.next();
        SelectableChannel channel = (SelectableChannel) sk.channel();

        if (channel instanceof SocketChannel) {

            DaapConnection connection = (DaapConnection) sk.attachment();

            if (connection.isDaapConnection()) {
                try {
                    connection.update();
                    if (sk.isValid()) {
                        try {
                            sk.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                        } catch (CancelledKeyException err) {
                            cancel(sk);
                            LOG.error("SelectionKey.interestOps()", err);
                        }
                    }
                } catch (ClosedChannelException err) {
                    cancel(sk);
                    LOG.error("DaapConnection.update()", err);
                } catch (IOException err) {
                    cancel(sk);
                    LOG.error("DaapConnection.update()", err);
                }
            }
        }
    }
}

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

/**
 * Write data/*from   ww w . j a  v  a 2 s. co m*/
 * 
 * @throws IOException
 */
private void processWrite(SelectionKey sk) throws IOException {

    if (!sk.isValid())
        return;

    DaapConnectionNIO connection = (DaapConnectionNIO) sk.attachment();
    SocketChannel channel = (SocketChannel) sk.channel();

    boolean keepAlive = false;

    try {
        keepAlive = connection.write();
    } catch (DaapStreamException err) {

        // Broken pipe: User pressed Pause, fast-foward 
        // or whatever. Just close the connection and go 
        // ahead
        keepAlive = false;
        //LOG.error(err);
    }

    if (keepAlive) {
        sk.interestOps(connection.interrestOps());

    } else {
        cancel(sk);
    }
}