use enum for state in LocalChannel

This commit is contained in:
Bill Gallagher 2013-10-01 15:15:48 -04:00 committed by Trustin Lee
parent a19fe06928
commit 7e5aa4a911

View File

@ -43,6 +43,8 @@ import java.util.Queue;
*/ */
public class LocalChannel extends AbstractChannel { public class LocalChannel extends AbstractChannel {
private enum State { OPEN, BOUND, CONNECTED, CLOSED }
private static final ChannelMetadata METADATA = new ChannelMetadata(false); private static final ChannelMetadata METADATA = new ChannelMetadata(false);
private static final int MAX_READER_STACK_DEPTH = 8; private static final int MAX_READER_STACK_DEPTH = 8;
@ -77,7 +79,8 @@ public class LocalChannel extends AbstractChannel {
} }
}; };
private volatile int state; // 0 - open, 1 - bound, 2 - connected, 3 - closed
private volatile State state;
private volatile LocalChannel peer; private volatile LocalChannel peer;
private volatile LocalAddress localAddress; private volatile LocalAddress localAddress;
private volatile LocalAddress remoteAddress; private volatile LocalAddress remoteAddress;
@ -122,12 +125,12 @@ public class LocalChannel extends AbstractChannel {
@Override @Override
public boolean isOpen() { public boolean isOpen() {
return state < 3; return state != State.CLOSED;
} }
@Override @Override
public boolean isActive() { public boolean isActive() {
return state == 2; return state == State.CONNECTED;
} }
@Override @Override
@ -153,10 +156,10 @@ public class LocalChannel extends AbstractChannel {
@Override @Override
protected void doRegister() throws Exception { protected void doRegister() throws Exception {
if (peer != null) { if (peer != null) {
state = 2; state = State.CONNECTED;
peer.remoteAddress = parent().localAddress(); peer.remoteAddress = parent() == null ? null : parent().localAddress();
peer.state = 2; peer.state = State.CONNECTED;
// Always call peer.eventLoop().execute() even if peer.eventLoop().inEventLoop() is true. // Always call peer.eventLoop().execute() even if peer.eventLoop().inEventLoop() is true.
// This ensures that if both channels are on the same event loop, the peer's channelActive // This ensures that if both channels are on the same event loop, the peer's channelActive
@ -178,7 +181,7 @@ public class LocalChannel extends AbstractChannel {
this.localAddress = this.localAddress =
LocalChannelRegistry.register(this, this.localAddress, LocalChannelRegistry.register(this, this.localAddress,
localAddress); localAddress);
state = 1; state = State.BOUND;
} }
@Override @Override
@ -188,7 +191,7 @@ public class LocalChannel extends AbstractChannel {
@Override @Override
protected void doClose() throws Exception { protected void doClose() throws Exception {
if (state <= 2) { if (state != State.CLOSED) {
// Update all internal state before the closeFuture is notified. // Update all internal state before the closeFuture is notified.
if (localAddress != null) { if (localAddress != null) {
if (parent() == null) { if (parent() == null) {
@ -196,7 +199,7 @@ public class LocalChannel extends AbstractChannel {
} }
localAddress = null; localAddress = null;
} }
state = 3; state = State.CLOSED;
} }
final LocalChannel peer = this.peer; final LocalChannel peer = this.peer;
@ -261,10 +264,11 @@ public class LocalChannel extends AbstractChannel {
@Override @Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception { protected void doWrite(ChannelOutboundBuffer in) throws Exception {
if (state < 2) { switch (state) {
case OPEN:
case BOUND:
throw new NotYetConnectedException(); throw new NotYetConnectedException();
} case CLOSED:
if (state > 2) {
throw new ClosedChannelException(); throw new ClosedChannelException();
} }
@ -324,7 +328,7 @@ public class LocalChannel extends AbstractChannel {
return; return;
} }
if (state == 2) { if (state == State.CONNECTED) {
Exception cause = new AlreadyConnectedException(); Exception cause = new AlreadyConnectedException();
safeSetFailure(promise, cause); safeSetFailure(promise, cause);
pipeline().fireExceptionCaught(cause); pipeline().fireExceptionCaught(cause);
@ -337,7 +341,7 @@ public class LocalChannel extends AbstractChannel {
connectPromise = promise; connectPromise = promise;
if (state != 1) { if (state != State.BOUND) {
// Not bound yet and no localAddress specified - get one. // Not bound yet and no localAddress specified - get one.
if (localAddress == null) { if (localAddress == null) {
localAddress = new LocalAddress(LocalChannel.this); localAddress = new LocalAddress(LocalChannel.this);