/**
 *     Copyright 2011 Membase, Inc.
 *
 *   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 org.couchbase.mock.memcached;

import org.couchbase.mock.memcached.protocol.BinaryResponse;
import org.couchbase.mock.memcached.protocol.BinaryCommand;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.Queue;
import org.couchbase.mock.memcached.protocol.CommandFactory;

/**
 * @author Trond Norbye
 */
public class MemcachedConnection {

    private BinaryProtocolHandler protocolHandler;
    private final byte header[];
    private BinaryCommand command;
    private final ByteBuffer input;
    private final Queue<ByteBuffer> output;
    private boolean authenticated;
    private final MemcachedServer server;

    public MemcachedConnection(MemcachedServer server) throws IOException {
        this.server = server;
        if (server.getBucket().getPassword().length() > 0) {
            authenticated = false;
        } else {
            authenticated = true;
        }
        header = new byte[24];
        input = ByteBuffer.wrap(header);
        protocolHandler = server.getProtocolHandler();
        output = new LinkedList<ByteBuffer>();
    }

    public void step() throws IOException {
        if (input.position() == header.length) {
            if (command == null) {
                command = CommandFactory.create(input);
            }

            if (command.complete()) {
                protocolHandler.execute(command, this);
                command = null;
                input.rewind();
            }
        }
    }

    public void sendResponse(BinaryResponse response) {
        output.add(response.getBuffer());
    }

    boolean hasOutput() {
        return !output.isEmpty();
    }

    public ByteBuffer getInputBuffer() {
        if (command == null) {
            return input;
        } else {
            return command.getInputBuffer();
        }

    }

    public ByteBuffer getOutputBuffer() {
        if (output.isEmpty()) {
            return null;
        } else {
            return output.remove();
        }
    }

    void shutdown() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    void setAuthenticated(boolean state) {
        authenticated = state;
    }

    public boolean isAuthenticated() {
        return authenticated;
    }
}

@@ -829,16 +829,65 @@
 Buffer;%0A
+import java.nio.channels.ClosedChannelException;%0A
 import j
@@ -907,16 +907,16 @@
 edList;%0A
-
 import j
@@ -1311,36 +1311,22 @@
 ate 
-final MemcachedServer server
+boolean closed
 ;%0A%0A 
@@ -1412,28 +1412,22 @@
     
-this.server = server
+closed = false
 ;%0A  
@@ -1798,32 +1798,112 @@
 s IOException %7B%0A
+        if (closed) %7B%0A            throw new ClosedChannelException();%0A        %7D%0A
         if (inpu
@@ -2783,70 +2783,76 @@
     
-throw new UnsupportedOperationException(%22Not yet implemented%22)
+closed = true;%0A    %7D%0A%0A    boolean isClosed() %7B%0A        return closed
 ;%0A  

Fix QUIT command