/*
 * Copyright 2013 Google Inc.
 * Copyright 2014 Andreas Schildbach
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.bitcoin.net;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;

/**
 * Creates a simple connection to a server using a {@link StreamParser} to process data.
 */
public class NioClient implements MessageWriteTarget {
    private final Handler handler;
    private final NioClientManager manager = new NioClientManager();

    class Handler extends AbstractTimeoutHandler implements StreamParser {
        private final StreamParser upstreamParser;
        private MessageWriteTarget writeTarget;
        private boolean closeOnOpen = false;
        Handler(StreamParser upstreamParser, int connectTimeoutMillis) {
            this.upstreamParser = upstreamParser;
            setSocketTimeout(connectTimeoutMillis);
            setTimeoutEnabled(true);
        }

        @Override
        protected synchronized void timeoutOccurred() {
            upstreamParser.connectionClosed();
            closeOnOpen = true;
        }

        @Override
        public void connectionClosed() {
            upstreamParser.connectionClosed();
            manager.stopAsync();
        }

        @Override
        public synchronized void connectionOpened() {
            if (!closeOnOpen)
                upstreamParser.connectionOpened();
        }

        @Override
        public int receiveBytes(ByteBuffer buff) throws Exception {
            return upstreamParser.receiveBytes(buff);
        }

        @Override
        public synchronized void setWriteTarget(MessageWriteTarget writeTarget) {
            if (closeOnOpen)
                writeTarget.closeConnection();
            else {
                setTimeoutEnabled(false);
                this.writeTarget = writeTarget;
                upstreamParser.setWriteTarget(writeTarget);
            }
        }

        @Override
        public int getMaxMessageSize() {
            return upstreamParser.getMaxMessageSize();
        }
    }

    /**
     * <p>Creates a new client to the given server address using the given {@link StreamParser} to decode the data.
     * The given parser <b>MUST</b> be unique to this object. This does not block while waiting for the connection to
     * open, but will call either the {@link StreamParser#connectionOpened()} or
     * {@link StreamParser#connectionClosed()} callback on the created network event processing thread.</p>
     *
     * @param connectTimeoutMillis The connect timeout set on the connection (in milliseconds). 0 is interpreted as no
     *                             timeout.
     */
    public NioClient(final SocketAddress serverAddress, final StreamParser parser,
                     final int connectTimeoutMillis) throws IOException {
        manager.startAsync();
        manager.awaitRunning();
        handler = new Handler(parser, connectTimeoutMillis);
        manager.openConnection(serverAddress, handler);
    }

    @Override
    public void closeConnection() {
        handler.writeTarget.closeConnection();
    }

    @Override
    public synchronized void writeBytes(byte[] message) throws IOException {
        handler.writeTarget.writeBytes(message);
    }
}

@@ -1222,16 +1222,61 @@
  false;%0A
+        private boolean closeCalled = false;%0A
         
@@ -1580,73 +1580,58 @@
     
-upstreamParser.connectionClosed();%0A            closeOnOpen = true
+closeOnOpen = true;%0A            connectionClosed()
 ;%0A  
@@ -1668,24 +1668,37 @@
      public 
+synchronized 
 void connect
@@ -1707,24 +1707,129 @@
 nClosed() %7B%0A
+            manager.stopAsync();%0A            if (!closeCalled) %7B%0A                closeCalled = true;%0A    
             
@@ -1875,36 +1875,17 @@
         
-manager.stopAsync();
+%7D
 %0A       

Fix memory leak on timeout in NioClient