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 {
private enum State { OPEN, BOUND, CONNECTED, CLOSED }
private static final ChannelMetadata METADATA = new ChannelMetadata(false);
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 LocalAddress localAddress;
private volatile LocalAddress remoteAddress;
@ -122,12 +125,12 @@ public class LocalChannel extends AbstractChannel {
@Override
public boolean isOpen() {
return state < 3;
return state != State.CLOSED;
}
@Override
public boolean isActive() {
return state == 2;
return state == State.CONNECTED;
}
@Override
@ -153,10 +156,10 @@ public class LocalChannel extends AbstractChannel {
@Override
protected void doRegister() throws Exception {
if (peer != null) {
state = 2;
state = State.CONNECTED;
peer.remoteAddress = parent().localAddress();
peer.state = 2;
peer.remoteAddress = parent() == null ? null : parent().localAddress();
peer.state = State.CONNECTED;
// 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
@ -178,7 +181,7 @@ public class LocalChannel extends AbstractChannel {
this.localAddress =
LocalChannelRegistry.register(this, this.localAddress,
localAddress);
state = 1;
state = State.BOUND;
}
@Override
@ -188,7 +191,7 @@ public class LocalChannel extends AbstractChannel {
@Override
protected void doClose() throws Exception {
if (state <= 2) {
if (state != State.CLOSED) {
// Update all internal state before the closeFuture is notified.
if (localAddress != null) {
if (parent() == null) {
@ -196,7 +199,7 @@ public class LocalChannel extends AbstractChannel {
}
localAddress = null;
}
state = 3;
state = State.CLOSED;
}
final LocalChannel peer = this.peer;
@ -261,10 +264,11 @@ public class LocalChannel extends AbstractChannel {
@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
if (state < 2) {
switch (state) {
case OPEN:
case BOUND:
throw new NotYetConnectedException();
}
if (state > 2) {
case CLOSED:
throw new ClosedChannelException();
}
@ -324,7 +328,7 @@ public class LocalChannel extends AbstractChannel {
return;
}
if (state == 2) {
if (state == State.CONNECTED) {
Exception cause = new AlreadyConnectedException();
safeSetFailure(promise, cause);
pipeline().fireExceptionCaught(cause);
@ -337,7 +341,7 @@ public class LocalChannel extends AbstractChannel {
connectPromise = promise;
if (state != 1) {
if (state != State.BOUND) {
// Not bound yet and no localAddress specified - get one.
if (localAddress == null) {
localAddress = new LocalAddress(LocalChannel.this);