/* Copyright © 2014 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.proxy;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Queue;
import java.util.Set;

/**
 * @author matt on 18/02/14.
 */
public class ReadSelector implements Runnable {
    private volatile boolean running = false;
    private final Selector selector;
    private final Queue<Connection> newConnections;
    private Queue<ConnectionWrites> newWrites;

    public ReadSelector(final Selector selector, final Queue<Connection> newConnections, final Queue<ConnectionWrites> newWrites) {
        this.selector = selector;
        this.newConnections = newConnections;
        this.newWrites = newWrites;
    }

    @Override
    public void run() {
        final ByteBuffer buffer = ByteBuffer.allocate(1024);
        running = true;
        while (running) {
            registerKeys();

            readBytes(buffer);
        }
    }

    public void stop() {
        running = false;
    }

    private void registerKeys() {
        while (!newConnections.isEmpty()) {
            try {
                final Connection connection = newConnections.poll();
                //System.out.println("Register new connection");

                final Direction cTs = connection.clientToServer();
                final SocketChannel channel0 = cTs.getFrom();
                final SelectionKey key0 = channel0.register(selector, SelectionKey.OP_READ);
                key0.attach(cTs.getWrites());

                final Direction sTc = connection.serverToClient();
                final SocketChannel channel1 = sTc.getFrom();
                final SelectionKey key1 = channel1.register(selector, SelectionKey.OP_READ);
                key1.attach(sTc.getWrites());
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void readBytes(final ByteBuffer buffer) {
        try {
            selector.select(100);
            final Set<SelectionKey> selectionKeys = selector.selectedKeys();
            for (final SelectionKey key : selectionKeys) {
                if (!key.isValid()) {
                    key.cancel();
                }
                else if (key.isReadable()) {
                    buffer.position(0);
                    final ConnectionWrites writes = (ConnectionWrites)key.attachment();
                    final SocketChannel channel = (SocketChannel)key.channel();
                    final int bytes = channel.read(buffer);
                    if (bytes > 0) {
                        buffer.flip();
                        final ByteBuffer writeBuffer = ByteBuffer.allocate(buffer.limit());
                        writeBuffer.put(buffer);
                        writeBuffer.flip();
                        if (!writes.hasData()) {
                            writes.add(writeBuffer);
                            newWrites.add(writes);
                        }
                        else {
                            writes.add(writeBuffer);
                        }
                    }
                    else if (bytes == -1) {
                        key.cancel();
                        writes.close();
                    }
                }
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -1719,21 +1719,23 @@
 va.util.
-Queue
+HashSet
 ;%0Aimport
@@ -1749,16 +1749,59 @@
 til.Set;
+%0Aimport java.util.concurrent.BlockingQueue;
 %0A%0A/**%0A *
@@ -1976,24 +1976,32 @@
 ivate final 
+Blocking
 Queue%3CConnec
@@ -2034,16 +2034,30 @@
 private 
+final Blocking
 Queue%3CCo
@@ -2131,32 +2131,40 @@
 selector, final 
+Blocking
 Queue%3CConnection
@@ -2187,16 +2187,24 @@
 , final 
+Blocking
 Queue%3CCo
@@ -2351,24 +2351,81 @@
 tes;%0A    %7D%0A%0A
+    public void stop() %7B%0A        running = false;%0A    %7D%0A%0A
     @Overrid
@@ -2577,53 +2577,124 @@
     
-registerKeys();%0A%0A            readBytes(buffer
+try %7B%0A                selector.selectNow();%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace(
 );%0A 
@@ -2692,34 +2692,32 @@
 race();%0A        
-%7D%0A
     %7D%0A%0A    publi
@@ -2715,171 +2715,229 @@
     
-public void stop() %7B%0A        running = false;%0A    %7D%0A%0A    private void registerKeys() %7B%0A        while (!newConnections.isEmpty()) %7B%0A            try %7B%0A      
+        registerKeys();%0A%0A            readBytes(buffer);%0A        %7D%0A    %7D%0A%0A    private void registerKeys() %7B%0A        final Set%3CConnection%3E connections = new HashSet%3C%3E();%0A        newConnections.drainTo(connections);%0A
         
   fi
@@ -2924,34 +2924,37 @@
 tions);%0A        
-  
+for (
 final Connection
@@ -2965,22 +2965,19 @@
 nection 
-= newC
+: c
 onnectio
@@ -2982,16 +2982,29 @@
 ions
-.poll();
+) %7B%0A            try %7B
 %0A   
@@ -3793,42 +3793,8 @@
 y %7B%0A
-            selector.select(100);%0A
     

Use BlockingQueue and drain