/* 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.proxy.selector;

import java.io.IOException;

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

import com.mattunderscore.tcproxy.io.socket.IOOutboundSocketChannel;
import com.mattunderscore.tcproxy.io.socket.IOSocketChannel;
import com.mattunderscore.tcproxy.proxy.AsynchronousOutboundConnectionFactory;
import com.mattunderscore.tcproxy.proxy.ConnectionImpl;
import com.mattunderscore.tcproxy.proxy.action.processor.ActionProcessorFactory;
import com.mattunderscore.tcproxy.proxy.action.processor.DefaultActionProcessorFactory;
import com.mattunderscore.tcproxy.proxy.action.queue.ActionQueue;
import com.mattunderscore.tcproxy.proxy.action.queue.ActionQueueImpl;
import com.mattunderscore.tcproxy.proxy.connection.Connection;
import com.mattunderscore.tcproxy.proxy.connection.ConnectionManager;
import com.mattunderscore.tcproxy.proxy.direction.Direction;
import com.mattunderscore.tcproxy.proxy.direction.DirectionImpl;
import com.mattunderscore.tcproxy.proxy.settings.ConnectionSettings;
import com.mattunderscore.tcproxy.selector.SocketChannelSelector;
import com.mattunderscore.tcproxy.selector.connecting.ConnectionHandler;
import com.mattunderscore.tcproxy.selector.server.Server;

/**
 * Implementation of {@link ConnectionHandler} for the proxy {@link Server}.
 * @author Matt Champion on 18/11/2015
 */
/*package*/ final class ProxyConnectionHandler implements ConnectionHandler {
    private static final Logger LOG = LoggerFactory.getLogger("acceptor");
    private final AsynchronousOutboundConnectionFactory factory;
    private final ConnectionSettings settings;
    private final ConnectionManager manager;
    private final SocketChannelSelector selector;

    public ProxyConnectionHandler(
            AsynchronousOutboundConnectionFactory factory,
            ConnectionSettings settings,
            ConnectionManager manager,
            SocketChannelSelector selector) {
        this.factory = factory;
        this.settings = settings;
        this.manager = manager;
        this.selector = selector;
    }

    @Override
    public void onConnect(final IOSocketChannel clientSide) {
        LOG.info("{} : Accepted {}", this, clientSide);
        factory.createConnection(new AsynchronousOutboundConnectionFactory.ConnectionCallback() {
            @Override
            public void onConnected(IOOutboundSocketChannel serverSide) {
                LOG.info("{} : Opened {}", this, serverSide);
                final ActionQueue actionQueue0 = new ActionQueueImpl(settings.getWriteQueueSize(), settings.getBatchSize());
                final ActionQueue actionQueue1 = new ActionQueueImpl(settings.getWriteQueueSize(), settings.getBatchSize());
                final Direction direction0 = new DirectionImpl(clientSide, serverSide, actionQueue0);
                final Direction direction1 = new DirectionImpl(serverSide, clientSide, actionQueue1);
                final Connection conn = new ConnectionImpl(manager, direction0, direction1, selector);
                final ActionProcessorFactory processorFactory = new DefaultActionProcessorFactory(conn);
                manager.register(conn);
                direction0.chainProcessor(processorFactory);
                direction1.chainProcessor(processorFactory);
            }

            @Override
            public void onException(IOException e) {
                LOG.warn("{} : There was an exception attempting to connect an outbound channel", this, e);
            }
        });
    }
}

@@ -1568,16 +1568,50 @@
 ception;
+%0Aimport java.net.ConnectException;
 %0A%0Aimport
@@ -3696,21 +3696,16 @@
 G.info(%22
-%7B%7D : 
 Accepted
@@ -3949,21 +3949,16 @@
 G.info(%22
-%7B%7D : 
 Opened %7B
@@ -4906,24 +4906,478 @@
             
+if (e instanceof ConnectException) %7B%0A                    LOG.warn(%22The target server did not accept the outbound connection%22);%0A                    try %7B%0A                        clientSide.close();%0A                    %7D%0A                    catch (IOException e1) %7B%0A                        LOG.warn(%22There was an exception attempting to close the inbound connection%22, e1);%0A                    %7D%0A                %7D%0A                else %7B%0A                    
 LOG.warn(%22%7B%7D
@@ -5378,13 +5378,8 @@
 rn(%22
-%7B%7D : 
 Ther
@@ -5445,17 +5445,29 @@
 l%22, 
-this, e);
+e);%0A                %7D
 %0A   

Handle the failure to create an outbound socket.