001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.connection;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: /**
032: *
033: *
034: * @author $author$
035: * @version $Revision: 1.19 $
036: */
037: public class ChannelDataWindow {
038: private static Log log = LogFactory.getLog(ChannelDataWindow.class);
039: long windowSpace = 0;
040:
041: /**
042: * Creates a new ChannelDataWindow object.
043: */
044: public ChannelDataWindow() {
045: }
046:
047: /**
048: *
049: *
050: * @return
051: */
052: public synchronized long getWindowSpace() {
053: return windowSpace;
054: }
055:
056: /**
057: *
058: *
059: * @param count
060: *
061: * @return
062: */
063: public synchronized long consumeWindowSpace(int count) {
064: if (windowSpace < count) {
065: waitForWindowSpace(count);
066: }
067:
068: windowSpace -= count;
069:
070: return windowSpace;
071: }
072:
073: /**
074: *
075: *
076: * @param count
077: */
078: public synchronized void increaseWindowSpace(long count) {
079: if (log.isDebugEnabled()) {
080: log.debug("Increasing window space by "
081: + String.valueOf(count));
082: }
083:
084: windowSpace += count;
085: notifyAll();
086: }
087:
088: /**
089: *
090: *
091: * @param minimum
092: */
093: public synchronized void waitForWindowSpace(int minimum) {
094: if (log.isDebugEnabled()) {
095: log.debug("Waiting for " + String.valueOf(minimum)
096: + " bytes of window space");
097: }
098:
099: while (windowSpace < minimum) {
100: try {
101: wait(50);
102: } catch (InterruptedException e) {
103: }
104: }
105: }
106: }
|