You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by mi...@apache.org on 2019/12/31 15:08:26 UTC
[httpcomponents-core] branch master updated: [HTTPCORE-620]
Refactor int constants from o.a.hc.core5.reactor.IOSession into an enum
This is an automated email from the ASF dual-hosted git repository.
michaelo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new b293e68 [HTTPCORE-620] Refactor int constants from o.a.hc.core5.reactor.IOSession into an enum
b293e68 is described below
commit b293e6878a0cb6916f2c5cb4159c0266d104ff97
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Dec 30 16:37:43 2019 -0500
[HTTPCORE-620] Refactor int constants from o.a.hc.core5.reactor.IOSession into an enum
This closes #180
---
.../apache/hc/core5/benchmark/HttpBenchmark.java | 2 +-
.../hc/core5/testing/nio/LoggingIOSession.java | 2 +-
.../org/apache/hc/core5/reactor/IOSession.java | 24 ++++++---
.../org/apache/hc/core5/reactor/IOSessionImpl.java | 26 +++-------
.../hc/core5/reactor/InternalDataChannel.java | 2 +-
.../apache/hc/core5/reactor/ssl/SSLIOSession.java | 60 +++++++++-------------
6 files changed, 53 insertions(+), 63 deletions(-)
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java
index ef61842..998aa8e 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java
@@ -245,7 +245,7 @@ public class HttpBenchmark {
}
@Override
- public int getStatus() {
+ public Status getStatus() {
return ioSession.getStatus();
}
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java
index 4ae953d..7836ac7 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingIOSession.java
@@ -162,7 +162,7 @@ public class LoggingIOSession implements IOSession {
}
@Override
- public int getStatus() {
+ public Status getStatus() {
return this.session.getStatus();
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSession.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSession.java
index 214f4a9..b8ca8c7 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSession.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSession.java
@@ -55,9 +55,19 @@ import org.apache.hc.core5.util.Timeout;
@Internal
public interface IOSession extends ByteChannel, SocketModalCloseable, Identifiable {
- int ACTIVE = 0;
- int CLOSING = 1;
- int CLOSED = Integer.MAX_VALUE;
+ public enum Status {
+
+ ACTIVE(0),
+ CLOSING(1),
+ CLOSED(Integer.MAX_VALUE);
+
+ private Status(final int rank) {
+ this.rank = rank;
+ }
+
+ public final int rank;
+
+ }
/**
* Returns event handler associated with the session.
@@ -165,15 +175,15 @@ public interface IOSession extends ByteChannel, SocketModalCloseable, Identifiab
/**
* Returns status of the session:
* <p>
- * {@link #ACTIVE}: session is active.
+ * {@link Status#ACTIVE}: session is active.
* <p>
- * {@link #CLOSING}: session is being closed.
+ * {@link Status#CLOSING}: session is being closed.
* <p>
- * {@link #CLOSED}: session has been terminated.
+ * {@link Status#CLOSED}: session has been terminated.
*
* @return session status.
*/
- int getStatus();
+ Status getStatus();
/**
* Returns value of the socket timeout in milliseconds. The value of
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
index 437aa87..3cffb58 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSessionImpl.java
@@ -36,7 +36,6 @@ import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
-import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
@@ -49,6 +48,7 @@ import org.apache.hc.core5.util.Timeout;
class IOSessionImpl implements IOSession {
+ /** Counts instances created. */
private final static AtomicLong COUNT = new AtomicLong(0);
private final SelectionKey key;
@@ -57,7 +57,7 @@ class IOSessionImpl implements IOSession {
private final Lock lock;
private final String id;
private final AtomicReference<IOEventHandler> handlerRef;
- private final AtomicInteger status;
+ private final AtomicReference<IOSession.Status> status;
private volatile Timeout socketTimeout;
private volatile long lastReadTime;
@@ -73,7 +73,7 @@ class IOSessionImpl implements IOSession {
this.socketTimeout = Timeout.DISABLED;
this.id = String.format(type + "-%08X", COUNT.getAndIncrement());
this.handlerRef = new AtomicReference<>();
- this.status = new AtomicInteger(ACTIVE);
+ this.status = new AtomicReference<>(Status.ACTIVE);
final long currentTimeMillis = System.currentTimeMillis();
this.lastReadTime = currentTimeMillis;
this.lastWriteTime = currentTimeMillis;
@@ -226,17 +226,17 @@ class IOSessionImpl implements IOSession {
}
@Override
- public int getStatus() {
+ public Status getStatus() {
return this.status.get();
}
private boolean isStatusClosed() {
- return this.status.get() == CLOSED;
+ return this.status.get() == Status.CLOSED;
}
@Override
public boolean isOpen() {
- return this.status.get() == ACTIVE && this.channel.isOpen();
+ return this.status.get() == Status.ACTIVE && this.channel.isOpen();
}
@Override
@@ -246,7 +246,7 @@ class IOSessionImpl implements IOSession {
@Override
public void close(final CloseMode closeMode) {
- if (this.status.compareAndSet(ACTIVE, CLOSED)) {
+ if (this.status.compareAndSet(Status.ACTIVE, Status.CLOSED)) {
if (closeMode == CloseMode.IMMEDIATE) {
try {
this.channel.socket().setSoLinger(true, 0);
@@ -282,17 +282,7 @@ class IOSessionImpl implements IOSession {
public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(id).append("[");
- switch (this.status.get()) {
- case ACTIVE:
- buffer.append("ACTIVE");
- break;
- case CLOSING:
- buffer.append("CLOSING");
- break;
- case CLOSED:
- buffer.append("CLOSED");
- break;
- }
+ buffer.append(this.status);
buffer.append("][");
if (this.key.isValid()) {
formatOps(buffer, this.key.interestOps());
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannel.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannel.java
index df02971..583220d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannel.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/InternalDataChannel.java
@@ -270,7 +270,7 @@ final class InternalDataChannel extends InternalChannel implements ProtocolIOSes
}
@Override
- public int getStatus() {
+ public IOSession.Status getStatus() {
return getSessionImpl().getStatus();
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
index 29f0920..f4678ab 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/ssl/SSLIOSession.java
@@ -89,7 +89,7 @@ public class SSLIOSession implements IOSession {
private int appEventMask;
private volatile boolean endOfStream;
- private volatile int status;
+ private volatile Status status = Status.ACTIVE;
private volatile boolean initialized;
private volatile Timeout socketTimeout;
private TlsDetails tlsDetails;
@@ -228,7 +228,7 @@ public class SSLIOSession implements IOSession {
this.session.getLock().lock();
try {
- if (this.status >= IOSession.CLOSING) {
+ if (this.status.rank >= Status.CLOSING.rank) {
return;
}
switch (this.sslMode) {
@@ -319,7 +319,7 @@ public class SSLIOSession implements IOSession {
// Just wrap an empty buffer because there is no data to write.
result = doWrap(EMPTY_BUFFER, outEncryptedBuf);
- if (result.getStatus() != Status.OK || result.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
+ if (result.getStatus() != SSLEngineResult.Status.OK || result.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
handshaking = false;
}
break;
@@ -352,10 +352,10 @@ public class SSLIOSession implements IOSession {
}
}
- if (this.status >= IOSession.CLOSING) {
+ if (this.status.rank >= Status.CLOSING.rank) {
this.inPlain.release();
}
- if (result.getStatus() != Status.OK) {
+ if (result.getStatus() != SSLEngineResult.Status.OK) {
handshaking = false;
}
break;
@@ -391,24 +391,24 @@ public class SSLIOSession implements IOSession {
this.session.getLock().lock();
try {
// Graceful session termination
- if (this.status == ACTIVE
+ if (this.status == Status.ACTIVE
&& (this.endOfStream || this.sslEngine.isInboundDone())) {
- this.status = CLOSING;
+ this.status = Status.CLOSING;
}
- if (this.status == CLOSING && !this.outEncrypted.hasData()) {
+ if (this.status == Status.CLOSING && !this.outEncrypted.hasData()) {
this.sslEngine.closeOutbound();
this.outboundClosedCount.incrementAndGet();
}
- if (this.status == CLOSING && this.sslEngine.isOutboundDone()
+ if (this.status == Status.CLOSING && this.sslEngine.isOutboundDone()
&& (this.endOfStream || this.sslEngine.isInboundDone())) {
- this.status = CLOSED;
+ this.status = Status.CLOSED;
}
// Abnormal session termination
- if (this.status <= CLOSING && this.endOfStream
+ if (this.status.rank <= Status.CLOSING.rank && this.endOfStream
&& this.sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP) {
- this.status = CLOSED;
+ this.status = Status.CLOSED;
}
- if (this.status == CLOSED) {
+ if (this.status == Status.CLOSED) {
this.session.close();
if (disconnectedCallback != null) {
disconnectedCallback.execute(this);
@@ -468,7 +468,7 @@ public class SSLIOSession implements IOSession {
// Clear output buffer if the session has been closed
// in case there is still `close_notify` data stuck in it
- if (this.status == CLOSED) {
+ if (this.status == Status.CLOSED) {
outEncryptedBuf.clear();
}
@@ -539,8 +539,8 @@ public class SSLIOSession implements IOSession {
inPlainBuf.clear();
}
}
- if (result.getStatus() != Status.OK) {
- if (result.getStatus() == Status.BUFFER_UNDERFLOW && endOfStream) {
+ if (result.getStatus() != SSLEngineResult.Status.OK) {
+ if (result.getStatus() == SSLEngineResult.Status.BUFFER_UNDERFLOW && endOfStream) {
throw new SSLException("Unable to decrypt incoming data due to unexpected end of stream");
}
break;
@@ -564,7 +564,7 @@ public class SSLIOSession implements IOSession {
this.session.getLock().lock();
try {
appReady = (this.appEventMask & SelectionKey.OP_WRITE) > 0
- && this.status == ACTIVE
+ && this.status == Status.ACTIVE
&& this.sslEngine.getHandshakeStatus() == HandshakeStatus.NOT_HANDSHAKING;
} finally {
this.session.getLock().unlock();
@@ -579,7 +579,7 @@ public class SSLIOSession implements IOSession {
Args.notNull(src, "Byte buffer");
this.session.getLock().lock();
try {
- if (this.status != ACTIVE) {
+ if (this.status != Status.ACTIVE) {
throw new ClosedChannelException();
}
if (!this.initialized) {
@@ -619,7 +619,7 @@ public class SSLIOSession implements IOSession {
@Override
public boolean isOpen() {
- return this.status == ACTIVE && this.session.isOpen();
+ return this.status == Status.ACTIVE && this.session.isOpen();
}
@Override
@@ -632,10 +632,10 @@ public class SSLIOSession implements IOSession {
this.session.getLock().lock();
try {
if (closeMode == CloseMode.GRACEFUL) {
- if (this.status >= CLOSING) {
+ if (this.status.rank >= Status.CLOSING.rank) {
return;
}
- this.status = CLOSING;
+ this.status = Status.CLOSING;
if (this.session.getSocketTimeout().isDisabled()) {
this.session.setSocketTimeout(Timeout.ofMilliseconds(1000));
}
@@ -651,14 +651,14 @@ public class SSLIOSession implements IOSession {
this.session.close(CloseMode.IMMEDIATE);
}
} else {
- if (this.status == CLOSED) {
+ if (this.status == Status.CLOSED) {
return;
}
this.inEncrypted.release();
this.outEncrypted.release();
this.inPlain.release();
- this.status = CLOSED;
+ this.status = Status.CLOSED;
this.session.close(closeMode);
}
} finally {
@@ -667,7 +667,7 @@ public class SSLIOSession implements IOSession {
}
@Override
- public int getStatus() {
+ public Status getStatus() {
return this.status;
}
@@ -804,17 +804,7 @@ public class SSLIOSession implements IOSession {
final StringBuilder buffer = new StringBuilder();
buffer.append(this.session);
buffer.append("[");
- switch (this.status) {
- case ACTIVE:
- buffer.append("ACTIVE");
- break;
- case CLOSING:
- buffer.append("CLOSING");
- break;
- case CLOSED:
- buffer.append("CLOSED");
- break;
- }
+ buffer.append(this.status);
buffer.append("][");
formatOps(buffer, this.appEventMask);
buffer.append("][");