package org.apache.coyote.http2;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;
abstract class AbstractStream {
private static final Log log = LogFactory.getLog(AbstractStream.class);
private static final StringManager sm = StringManager.getManager(AbstractStream.class);
private final Integer identifier;
private final String idAsString;
private volatile AbstractStream parentStream = null;
private final Set<AbstractNonZeroStream> childStreams = Collections.newSetFromMap(new ConcurrentHashMap<>());
private long windowSize = ConnectionSettingsBase.DEFAULT_INITIAL_WINDOW_SIZE;
AbstractStream(Integer identifier) {
this.identifier = identifier;
this.idAsString = identifier.toString();
}
final Integer getIdentifier() {
return identifier;
}
final String getIdAsString() {
return idAsString;
}
final int getIdAsInt() {
return identifier.intValue();
}
final void detachFromParent() {
if (parentStream != null) {
parentStream.getChildStreams().remove(this);
parentStream = null;
}
}
final void addChild(AbstractNonZeroStream child) {
child.setParentStream(this);
childStreams.add(child);
}
final boolean isDescendant(AbstractStream stream) {
AbstractStream parent = stream.getParentStream();
while (parent != null && parent != this) {
parent = parent.getParentStream();
}
return parent != null;
}
final AbstractStream getParentStream() {
return parentStream;
}
final void setParentStream(AbstractStream parentStream) {
this.parentStream = parentStream;
}
final Set<AbstractNonZeroStream> getChildStreams() {
return childStreams;
}
final synchronized void setWindowSize(long windowSize) {
this.windowSize = windowSize;
}
final synchronized long getWindowSize() {
return windowSize;
}
synchronized void incrementWindowSize(int increment) throws Http2Exception {
windowSize += increment;
if (log.isDebugEnabled()) {
log.debug(sm.getString("abstractStream.windowSizeInc", getConnectionId(),
getIdAsString(), Integer.toString(increment), Long.toString(windowSize)));
}
if (windowSize > ConnectionSettingsBase.MAX_WINDOW_SIZE) {
String msg = sm.getString("abstractStream.windowSizeTooBig", getConnectionId(), identifier,
Integer.toString(increment), Long.toString(windowSize));
if (identifier.intValue() == 0) {
throw new ConnectionException(msg, Http2Error.FLOW_CONTROL_ERROR);
} else {
throw new StreamException(
msg, Http2Error.FLOW_CONTROL_ERROR, identifier.intValue());
}
}
}
final synchronized void decrementWindowSize(int decrement) {
windowSize -= decrement;
if (log.isDebugEnabled()) {
log.debug(sm.getString("abstractStream.windowSizeDec", getConnectionId(),
getIdAsString(), Integer.toString(decrement), Long.toString(windowSize)));
}
}
abstract String getConnectionId();
abstract int getWeight();
}