/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.sshd.common.channel;

import java.io.IOException;

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

/**
 * A Window for a given channel.
 * Windows are used to not overflow the client or server when sending datas.
 * Both clients and servers have a local and remote window and won't send
 * anymore data until the window has been expanded.  When the local window
 * is 
 *
 * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
 */
public class Window {

    private final static Logger log = LoggerFactory.getLogger(Window.class);

    private final AbstractChannel channel;
    private final Object lock;
    private final String name;

    private int size;
    private int maxSize;
    private int packetSize;
    private boolean waiting;
    private boolean closed;

    public Window(AbstractChannel channel, Object lock, boolean client, boolean local) {
        this.channel = channel;
        this.lock = lock != null ? lock : this;
        this.name = (client ? "client" : "server") + " " + (local ? "local " : "remote") + " window";
    }

    public int getSize() {
        synchronized (lock) {
            return size;
        }
    }

    public int getMaxSize() {
        return maxSize;
    }

    public int getPacketSize() {
        return packetSize;
    }

    public void init(int size, int packetSize) {
        synchronized (lock) {
            this.size = size;
            this.maxSize = size;
            this.packetSize = packetSize;
            lock.notifyAll();
        }
    }

    public void expand(int window) {
        synchronized (lock) {
            size += window;
            if (log.isDebugEnabled()) {
                log.debug("Increase " + name + " by " + window + " up to " + size);
            }
            lock.notifyAll();
        }
    }

    public void consume(int len) {
        synchronized (lock) {
            //assert size > len;
            size -= len;
            if (log.isTraceEnabled()) {
                log.trace("Consume " + name + " by " + len + " down to " + size);
            }
        }
    }


    public void consumeAndCheck(int len) throws IOException {
        synchronized (lock) {
            //assert size > len;
            size -= len;
            if (log.isTraceEnabled()) {
                log.trace("Consume " + name + " by " + len + " down to " + size);
            }
            check(maxSize);
        }
    }

    public void check(int maxFree) throws IOException {
        synchronized (lock) {
            if (size < maxFree / 2) {
                if (log.isDebugEnabled()) {
                    log.debug("Increase " + name + " by " + (maxFree - size) + " up to " + maxFree);
                }
                channel.sendWindowAdjust(maxFree - size);
                size = maxFree;
            }
        }
    }

    public void waitAndConsume(int len) throws InterruptedException, WindowClosedException {
        synchronized (lock) {
            while (size < len && !closed) {
                log.debug("Waiting for {} bytes on {}", len, name);
                waiting = true;
                lock.wait();
            }
            if (waiting) {
                log.debug("Space available for {}", name);
                waiting = false;
            }
            if (closed) {
                throw new WindowClosedException();
            }
            size -= len;
            if (log.isTraceEnabled()) {
                log.trace("Consume " + name + " by " + len + " down to " + size);
            }
        }
    }

    public int waitForSpace() throws InterruptedException, WindowClosedException {
        synchronized (lock) {
            while (size == 0 && !closed) {
                log.debug("Waiting for some space on {}", name);
                waiting = true;
                lock.wait();
            }
            if (waiting) {
                log.debug("Space available for {}", name);
                waiting = false;
            }
            if (closed) {
                throw new WindowClosedException();
            }
            return size;
        }
    }

    public void notifyClosed() {
        synchronized (lock) {
            closed = true;
            if (waiting) {
                lock.notifyAll();
            }
        }
    }
}

@@ -3984,32 +3984,157 @@
  if (waiting) %7B%0A
+                if (closed) %7B%0A                    log.debug(%22Window %7B%7D has been closed%22, name);%0A                %7D else %7B%0A    
                 
@@ -4168,32 +4168,50 @@
 for %7B%7D%22, name);%0A
+                %7D%0A
                 
@@ -4825,32 +4825,157 @@
  if (waiting) %7B%0A
+                if (closed) %7B%0A                    log.debug(%22Window %7B%7D has been closed%22, name);%0A                %7D else %7B%0A    
                 
@@ -5009,32 +5009,50 @@
 for %7B%7D%22, name);%0A
+                %7D%0A
                 

Fix log statement when a window is unblocked