/* Copyright © 2015 Matthew Champion
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 * Neither the name of mattunderscore.com nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.mattunderscore.tcproxy.selector.general;

import java.io.IOException;
import java.nio.channels.ClosedChannelException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mattunderscore.tcproxy.io.IOSelectionKey;
import com.mattunderscore.tcproxy.io.IOSelector;
import com.mattunderscore.tcproxy.io.IOServerSocketChannel;
import com.mattunderscore.tcproxy.io.IOSocketChannel;
import com.mattunderscore.tcproxy.selector.SelectionRunnable;
import com.mattunderscore.tcproxy.selector.ServerSocketChannelSelector;
import com.mattunderscore.tcproxy.selector.SocketChannelSelector;
import com.mattunderscore.tcproxy.selector.threads.LifecycleState;

/**
 * A general purpose selector. {@link SelectionRunnable}s can be registered against it for both
 * {@link IOServerSocketChannel} and {@link IOSocketChannel}. These tasks can be registered from any thread. Selected
 * keys will be removed from the selected set when they are processed but will not be cancelled.
 * @author Matt Champion on 24/10/2015
 */
public final class GeneralPurposeSelector implements SocketChannelSelector, ServerSocketChannelSelector {
    private static final Logger LOG = LoggerFactory.getLogger("selector");
    private final BlockingQueue<RegistrationRequest> registrations = new ArrayBlockingQueue<>(64);
    private final LifecycleState lifecycleState = new LifecycleState();
    private final IOSelector selector;

    public GeneralPurposeSelector(IOSelector selector) {
        this.selector = selector;
    }

    @Override
    public void start() {
        lifecycleState.beginStartup();

        while (lifecycleState.isRunning()) {
            // Populate the selected set
            try {
                selector.selectNow();
            }
            catch (final IOException e) {
                LOG.debug("{} : Error selecting keys", this, e);
            }

            // Process any new registrations that have been requested
            final Collection<RegistrationRequest> newRegistrations = new HashSet<>();
            registrations.drainTo(newRegistrations);
            for (final RegistrationRequest registrationRequest : newRegistrations) {
                try {
                    registrationRequest.register(selector);
                }
                catch (ClosedChannelException e) {
                    LOG.debug("{} : Problem registering key", this, e);
                }
            }

            // Process the selector set
            final Iterator<IOSelectionKey> selectedKeys = selector.selectedKeys().iterator();
            while (selectedKeys.hasNext()) {
                final IOSelectionKey key = selectedKeys.next();
                selectedKeys.remove();
                final Registration registration = (Registration) key.attachment();
                registration.run(key);
            }
        }

        lifecycleState.endShutdown();
    }

    @Override
    public void stop() {
        lifecycleState.beginShutdown();
    }

    @Override
    public void restart() {
        stop();
        waitForStopped();
        start();
    }

    @Override
    public void waitForRunning() {
        lifecycleState.waitForRunning();
    }

    @Override
    public void waitForStopped() {
        lifecycleState.waitForStopped();
    }

    @Override
    public void register(IOSocketChannel channel, IOSelectionKey.Op op, SelectionRunnable<IOSocketChannel> runnable) {
        registrations.add(new IOSocketChannelSingleRegistrationRequest(channel, op, runnable));
    }

    @Override
    public void register(IOSocketChannel channel, Set<IOSelectionKey.Op> ops, SelectionRunnable<IOSocketChannel> runnable) {
        registrations.add(new IOSocketChannelSetRegistrationRequest(channel, ops, runnable));
    }

    @Override
    public void register(IOServerSocketChannel channel, SelectionRunnable<IOServerSocketChannel> runnable) {
        registrations.add(new IOServerSocketChannelRegistrationRequest(channel, runnable));
    }

    @Override
    public String toString() {
        return "General Purpose selector";
    }
}

@@ -2133,33 +2133,147 @@
 elector.
-SelectionRunnable
+NoBackoff;%0Aimport com.mattunderscore.tcproxy.selector.SelectionRunnable;%0Aimport com.mattunderscore.tcproxy.selector.SelectorBackoff
 ;%0Aimport
@@ -3220,16 +3220,59 @@
 elector;
+%0A    private final SelectorBackoff backoff;
 %0A%0A    pu
@@ -3318,24 +3318,154 @@
 selector) %7B%0A
+        this(selector, new NoBackoff());%0A    %7D%0A%0A    public GeneralPurposeSelector(IOSelector selector, SelectorBackoff backoff) %7B%0A
         this
@@ -3482,24 +3482,56 @@
 = selector;%0A
+        this.backoff = backoff;%0A
     %7D%0A%0A    @
@@ -4491,24 +4491,19 @@
   final 
-Iterator
+Set
 %3CIOSelec
@@ -4522,17 +4522,19 @@
 ectedKey
-s
+Set
  = selec
@@ -4551,16 +4551,150 @@
 edKeys()
+;%0A            final int selectedSize = selectedKeySet.size();%0A            final Iterator%3CIOSelectionKey%3E selectedKeys = selectedKeySet
 .iterato
@@ -4968,16 +4968,16 @@
 n(key);%0A
-
         
@@ -4973,32 +4973,76 @@
 );%0A            %7D
+%0A%0A            backoff.backoff(selectedSize);
 %0A        %7D%0A%0A    

Use a backoff in the general purpose selector.