public void run()

in core/src/main/java/org/apache/mina/transport/nio/NioSelectorLoop.java [228:289]


        public void run() {

            for (;;) {
                try {
                    if (IS_DEBUG) {
                        LOG.debug("selecting...");
                    }

                    final int readyCount = selector.select();

                    if (IS_DEBUG) {
                        LOG.debug("... done selecting : {} events", readyCount);
                    }

                    if (readyCount > 0) {
                        final Iterator<SelectionKey> it = selector.selectedKeys().iterator();

                        while (it.hasNext()) {
                            final SelectionKey key = it.next();
                            final SelectorListener listener = (SelectorListener) key.attachment();
                            int ops = key.readyOps();
                            boolean isAcceptable = (ops & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT;
                            boolean isConnectable = (ops & SelectionKey.OP_CONNECT) == SelectionKey.OP_CONNECT;
                            boolean isReadable = (ops & SelectionKey.OP_READ) == SelectionKey.OP_READ;
                            boolean isWritable = (ops & SelectionKey.OP_WRITE) == SelectionKey.OP_WRITE;
                            listener.ready(isAcceptable, isConnectable, isReadable, isReadable ? readBuffer : null,
                                    isWritable);
                            // if you don't remove the event of the set, the selector will present you this event again
                            // and again
                            if (IS_DEBUG) {
                                LOG.debug("remove");
                            }

                            it.remove();
                        }
                    }

                    // new registration
                    while (!registrationQueue.isEmpty()) {
                        final Registration reg = registrationQueue.poll();

                        try {
                            SelectionKey selectionKey = reg.channel.register(selector, reg.ops, reg.listener);

                            if (reg.getCallback() != null) {
                                reg.getCallback().done(selectionKey);
                            }
                        } catch (final ClosedChannelException ex) {
                            // dead session..
                            LOG.error("socket is already dead", ex);
                        }
                    }

                    // tasks
                    while (!runnableQueue.isEmpty()) {
                        runnableQueue.poll().run();
                    }
                } catch (final Exception e) {
                    LOG.error("Unexpected exception : ", e);
                }
            }
        }