* Fixed issue: NETTY-125 (Backward incompatible) Use an integer as a Channel ID instead of UUID

* More concise Channel.toString() and ChannelEvent.toString() implementations
This commit is contained in:
Trustin Lee 2009-02-16 11:59:33 +00:00
parent 9e60db4078
commit 149d97d013
14 changed files with 90 additions and 73 deletions

View File

@ -23,9 +23,9 @@
package org.jboss.netty.channel; package org.jboss.netty.channel;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.UUID; import java.util.concurrent.ConcurrentMap;
import org.jboss.netty.util.TimeBasedUuidGenerator; import org.jboss.netty.util.ConcurrentHashMap;
/** /**
* A skeletal {@link Channel} implementation. * A skeletal {@link Channel} implementation.
@ -38,7 +38,35 @@ import org.jboss.netty.util.TimeBasedUuidGenerator;
*/ */
public abstract class AbstractChannel implements Channel { public abstract class AbstractChannel implements Channel {
private final UUID id = TimeBasedUuidGenerator.generate(); static final ConcurrentMap<Integer, Channel> allChannels = new ConcurrentHashMap<Integer, Channel>();
private static final IdDeallocator ID_DEALLOCATOR = new IdDeallocator();
private static Integer allocateId(Channel channel) {
Integer id = Integer.valueOf(System.identityHashCode(channel));
for (;;) {
// Loop until a unique ID is acquired.
// It should be found in one loop practically.
if (allChannels.putIfAbsent(id, channel) == null) {
// Successfully acquired.
return id;
} else {
// Taken by other channel at almost the same moment.
id = Integer.valueOf(id.intValue() + 1);
}
}
}
private static final class IdDeallocator implements ChannelFutureListener {
IdDeallocator() {
super();
}
public void operationComplete(ChannelFuture future) throws Exception {
allChannels.remove(future.getChannel().getId());
}
}
private final Integer id = allocateId(this);
private final Channel parent; private final Channel parent;
private final ChannelFactory factory; private final ChannelFactory factory;
private final ChannelPipeline pipeline; private final ChannelPipeline pipeline;
@ -69,10 +97,11 @@ public abstract class AbstractChannel implements Channel {
this.parent = parent; this.parent = parent;
this.factory = factory; this.factory = factory;
this.pipeline = pipeline; this.pipeline = pipeline;
closeFuture.addListener(ID_DEALLOCATOR);
pipeline.attach(this, sink); pipeline.attach(this, sink);
} }
public final UUID getId() { public final Integer getId() {
return id; return id;
} }
@ -225,9 +254,8 @@ public abstract class AbstractChannel implements Channel {
} }
StringBuilder buf = new StringBuilder(128); StringBuilder buf = new StringBuilder(128);
buf.append(getClass().getSimpleName()); buf.append("[id: 0x");
buf.append("(id: "); buf.append(Integer.toHexString(id.intValue()));
buf.append(id.toString());
if (connected) { if (connected) {
buf.append(", "); buf.append(", ");
@ -245,7 +273,7 @@ public abstract class AbstractChannel implements Channel {
buf.append(getLocalAddress()); buf.append(getLocalAddress());
} }
buf.append(')'); buf.append(']');
String strVal = buf.toString(); String strVal = buf.toString();
if (connected) { if (connected) {

View File

@ -24,7 +24,6 @@ package org.jboss.netty.channel;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.UUID;
/** /**
@ -92,9 +91,9 @@ public interface Channel extends Comparable<Channel>{
static int OP_READ_WRITE = OP_READ | OP_WRITE; static int OP_READ_WRITE = OP_READ | OP_WRITE;
/** /**
* Returns the {@link UUID} of this channel. * Returns the unique integer ID of this channel.
*/ */
UUID getId(); Integer getId();
/** /**
* Returns the {@link ChannelFactory} which created this channel. * Returns the {@link ChannelFactory} which created this channel.

View File

@ -69,11 +69,8 @@ public class DefaultChildChannelStateEvent implements ChildChannelStateEvent {
String channelString = getChannel().toString(); String channelString = getChannel().toString();
StringBuilder buf = new StringBuilder(channelString.length() + 32); StringBuilder buf = new StringBuilder(channelString.length() + 32);
buf.append(channelString); buf.append(channelString);
buf.append(" - ("); buf.append(getChildChannel().isOpen()? " CHILD_OPEN: " : " CHILD_CLOSED: ");
buf.append(getChildChannel().isOpen()? "CHILD_OPEN" : "CHILD_CLOSED");
buf.append(": ");
buf.append(getChildChannel().getId()); buf.append(getChildChannel().getId());
buf.append(')');
return buf.toString(); return buf.toString();
} }
} }

View File

@ -69,6 +69,6 @@ public class DefaultExceptionEvent implements ExceptionEvent {
@Override @Override
public String toString() { public String toString() {
return getChannel().toString() + " - (EXCEPTION: " + cause + ')'; return getChannel().toString() + " EXCEPTION: " + cause;
} }
} }

View File

@ -70,9 +70,8 @@ public class DefaultWriteCompletionEvent implements WriteCompletionEvent {
String channelString = getChannel().toString(); String channelString = getChannel().toString();
StringBuilder buf = new StringBuilder(channelString.length() + 32); StringBuilder buf = new StringBuilder(channelString.length() + 32);
buf.append(channelString); buf.append(channelString);
buf.append(" - (WRITTEN_AMOUNT: "); buf.append(" WRITTEN_AMOUNT: ");
buf.append(getWrittenAmount()); buf.append(getWrittenAmount());
buf.append(')');
return buf.toString(); return buf.toString();
} }
} }

View File

@ -81,41 +81,40 @@ public class DownstreamChannelStateEvent implements ChannelStateEvent {
String channelString = getChannel().toString(); String channelString = getChannel().toString();
StringBuilder buf = new StringBuilder(channelString.length() + 64); StringBuilder buf = new StringBuilder(channelString.length() + 64);
buf.append(channelString); buf.append(channelString);
buf.append(" - (");
switch (getState()) { switch (getState()) {
case OPEN: case OPEN:
if (Boolean.TRUE.equals(getValue())) { if (Boolean.TRUE.equals(getValue())) {
buf.append("OPEN"); buf.append(" OPEN");
} else { } else {
buf.append("CLOSE"); buf.append(" CLOSE");
} }
break; break;
case BOUND: case BOUND:
if (getValue() != null) { if (getValue() != null) {
buf.append("BIND: "); buf.append(" BIND: ");
buf.append(getValue()); buf.append(getValue());
} else { } else {
buf.append("UNBIND"); buf.append(" UNBIND");
} }
break; break;
case CONNECTED: case CONNECTED:
if (getValue() != null) { if (getValue() != null) {
buf.append("CONNECT: "); buf.append(" CONNECT: ");
buf.append(getValue()); buf.append(getValue());
} else { } else {
buf.append("DISCONNECT"); buf.append(" DISCONNECT");
} }
break; break;
case INTEREST_OPS: case INTEREST_OPS:
buf.append("CHANGE_INTEREST: "); buf.append(" CHANGE_INTEREST: ");
buf.append(getValue()); buf.append(getValue());
break; break;
default: default:
buf.append(' ');
buf.append(getState().name()); buf.append(getState().name());
buf.append(": "); buf.append(": ");
buf.append(getValue()); buf.append(getValue());
} }
buf.append(')');
return buf.toString(); return buf.toString();
} }
} }

View File

@ -83,12 +83,12 @@ public class DownstreamMessageEvent implements MessageEvent {
@Override @Override
public String toString() { public String toString() {
if (getRemoteAddress() == null) { if (getRemoteAddress() == null) {
return getChannel().toString() + " - (WRITE: " + return getChannel().toString() + " WRITE: " +
StringUtil.stripControlCharacters(getMessage()) + ')'; StringUtil.stripControlCharacters(getMessage());
} else { } else {
return getChannel().toString() + " - (WRITE: " + return getChannel().toString() + " WRITE: " +
StringUtil.stripControlCharacters(getMessage()) + ", " + StringUtil.stripControlCharacters(getMessage()) + ", " +
getRemoteAddress() + ')'; getRemoteAddress();
} }
} }
} }

View File

@ -78,40 +78,38 @@ public class UpstreamChannelStateEvent implements ChannelStateEvent {
String channelString = getChannel().toString(); String channelString = getChannel().toString();
StringBuilder buf = new StringBuilder(channelString.length() + 64); StringBuilder buf = new StringBuilder(channelString.length() + 64);
buf.append(channelString); buf.append(channelString);
buf.append(" - (");
switch (getState()) { switch (getState()) {
case OPEN: case OPEN:
if (Boolean.TRUE.equals(getValue())) { if (Boolean.TRUE.equals(getValue())) {
buf.append("OPEN"); buf.append(" OPEN");
} else { } else {
buf.append("CLOSED"); buf.append(" CLOSED");
} }
break; break;
case BOUND: case BOUND:
if (getValue() != null) { if (getValue() != null) {
buf.append("BOUND: "); buf.append(" BOUND: ");
buf.append(getValue()); buf.append(getValue());
} else { } else {
buf.append("UNBOUND"); buf.append(" UNBOUND");
} }
break; break;
case CONNECTED: case CONNECTED:
if (getValue() != null) { if (getValue() != null) {
buf.append("CONNECTED: "); buf.append(" CONNECTED: ");
buf.append(getValue()); buf.append(getValue());
} else { } else {
buf.append("DISCONNECTED"); buf.append(" DISCONNECTED");
} }
break; break;
case INTEREST_OPS: case INTEREST_OPS:
buf.append("INTEREST_CHANGED"); buf.append(" INTEREST_CHANGED");
break; break;
default: default:
buf.append(getState().name()); buf.append(getState().name());
buf.append(": "); buf.append(": ");
buf.append(getValue()); buf.append(getValue());
} }
buf.append(')');
return buf.toString(); return buf.toString();
} }
} }

View File

@ -79,12 +79,12 @@ public class UpstreamMessageEvent implements MessageEvent {
@Override @Override
public String toString() { public String toString() {
if (getRemoteAddress() == null) { if (getRemoteAddress() == null) {
return getChannel().toString() + " - (RECEIVED: " + return getChannel().toString() + " RECEIVED: " +
StringUtil.stripControlCharacters(getMessage()) + ')'; StringUtil.stripControlCharacters(getMessage());
} else { } else {
return getChannel().toString() + " - (RECEIVED: " + return getChannel().toString() + " RECEIVED: " +
StringUtil.stripControlCharacters(getMessage()) + ", " + StringUtil.stripControlCharacters(getMessage()) + ", " +
getRemoteAddress() + ')'; getRemoteAddress();
} }
} }
} }

View File

@ -24,7 +24,6 @@ package org.jboss.netty.channel.group;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
@ -38,7 +37,7 @@ import org.jboss.netty.channel.Channel;
*/ */
public interface ChannelGroup extends Set<Channel>, Comparable<ChannelGroup> { public interface ChannelGroup extends Set<Channel>, Comparable<ChannelGroup> {
String getName(); String getName();
Channel find(UUID id); Channel find(Integer id);
ChannelGroupFuture setInterestOps(int interestOps); ChannelGroupFuture setInterestOps(int interestOps);
ChannelGroupFuture setReadable(boolean readable); ChannelGroupFuture setReadable(boolean readable);
ChannelGroupFuture write(Object message); ChannelGroupFuture write(Object message);

View File

@ -22,7 +22,6 @@
*/ */
package org.jboss.netty.channel.group; package org.jboss.netty.channel.group;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
@ -39,7 +38,7 @@ public interface ChannelGroupFuture extends Iterable<ChannelFuture>{
ChannelGroup getGroup(); ChannelGroup getGroup();
ChannelFuture find(UUID channelId); ChannelFuture find(Integer channelId);
ChannelFuture find(Channel channel); ChannelFuture find(Channel channel);
/** /**

View File

@ -50,8 +50,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
private static final AtomicInteger nextId = new AtomicInteger(); private static final AtomicInteger nextId = new AtomicInteger();
private final String name; private final String name;
private final ConcurrentMap<UUID, Channel> serverChannels = new ConcurrentHashMap<UUID, Channel>(); private final ConcurrentMap<Integer, Channel> serverChannels = new ConcurrentHashMap<Integer, Channel>();
private final ConcurrentMap<UUID, Channel> nonServerChannels = new ConcurrentHashMap<UUID, Channel>(); private final ConcurrentMap<Integer, Channel> nonServerChannels = new ConcurrentHashMap<Integer, Channel>();
private final ChannelFutureListener remover = new ChannelFutureListener() { private final ChannelFutureListener remover = new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
remove(future.getChannel()); remove(future.getChannel());
@ -83,7 +83,7 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
return nonServerChannels.size() + serverChannels.size(); return nonServerChannels.size() + serverChannels.size();
} }
public Channel find(UUID id) { public Channel find(Integer id) {
Channel c = nonServerChannels.get(id); Channel c = nonServerChannels.get(id);
if (c != null) { if (c != null) {
return c; return c;
@ -110,7 +110,7 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
@Override @Override
public boolean add(Channel channel) { public boolean add(Channel channel) {
ConcurrentMap<UUID, Channel> map = ConcurrentMap<Integer, Channel> map =
channel instanceof ServerChannel? serverChannels : nonServerChannels; channel instanceof ServerChannel? serverChannels : nonServerChannels;
boolean added = map.putIfAbsent(channel.getId(), channel) == null; boolean added = map.putIfAbsent(channel.getId(), channel) == null;
@ -175,8 +175,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture close() { public ChannelGroupFuture close() {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: serverChannels.values()) { for (Channel c: serverChannels.values()) {
futures.put(c.getId(), c.close().awaitUninterruptibly()); futures.put(c.getId(), c.close().awaitUninterruptibly());
@ -189,8 +189,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture disconnect() { public ChannelGroupFuture disconnect() {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: serverChannels.values()) { for (Channel c: serverChannels.values()) {
futures.put(c.getId(), c.disconnect().awaitUninterruptibly()); futures.put(c.getId(), c.disconnect().awaitUninterruptibly());
@ -203,8 +203,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture setInterestOps(int interestOps) { public ChannelGroupFuture setInterestOps(int interestOps) {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: serverChannels.values()) { for (Channel c: serverChannels.values()) {
futures.put(c.getId(), c.setInterestOps(interestOps).awaitUninterruptibly()); futures.put(c.getId(), c.setInterestOps(interestOps).awaitUninterruptibly());
@ -217,8 +217,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture setReadable(boolean readable) { public ChannelGroupFuture setReadable(boolean readable) {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: serverChannels.values()) { for (Channel c: serverChannels.values()) {
futures.put(c.getId(), c.setReadable(readable).awaitUninterruptibly()); futures.put(c.getId(), c.setReadable(readable).awaitUninterruptibly());
@ -231,8 +231,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture unbind() { public ChannelGroupFuture unbind() {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: serverChannels.values()) { for (Channel c: serverChannels.values()) {
futures.put(c.getId(), c.unbind().awaitUninterruptibly()); futures.put(c.getId(), c.unbind().awaitUninterruptibly());
@ -245,8 +245,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture write(Object message) { public ChannelGroupFuture write(Object message) {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: this) { for (Channel c: this) {
futures.put(c.getId(), c.write(message)); futures.put(c.getId(), c.write(message));
} }
@ -254,8 +254,8 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
} }
public ChannelGroupFuture write(Object message, SocketAddress remoteAddress) { public ChannelGroupFuture write(Object message, SocketAddress remoteAddress) {
Map<UUID, ChannelFuture> futures = Map<Integer, ChannelFuture> futures =
new HashMap<UUID, ChannelFuture>(size()); new HashMap<Integer, ChannelFuture>(size());
for (Channel c: this) { for (Channel c: this) {
futures.put(c.getId(), c.write(message, remoteAddress)); futures.put(c.getId(), c.write(message, remoteAddress));
} }

View File

@ -31,7 +31,6 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.Channel;
@ -54,7 +53,7 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
InternalLoggerFactory.getInstance(DefaultChannelGroupFuture.class); InternalLoggerFactory.getInstance(DefaultChannelGroupFuture.class);
private final ChannelGroup group; private final ChannelGroup group;
final Map<UUID, ChannelFuture> futures; final Map<Integer, ChannelFuture> futures;
private volatile ChannelGroupFutureListener firstListener; private volatile ChannelGroupFutureListener firstListener;
private volatile List<ChannelGroupFutureListener> otherListeners; private volatile List<ChannelGroupFutureListener> otherListeners;
private boolean done; private boolean done;
@ -96,7 +95,7 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
this.group = group; this.group = group;
Map<UUID, ChannelFuture> futureMap = new HashMap<UUID, ChannelFuture>(); Map<Integer, ChannelFuture> futureMap = new HashMap<Integer, ChannelFuture>();
for (ChannelFuture f: futures) { for (ChannelFuture f: futures) {
futureMap.put(f.getChannel().getId(), f); futureMap.put(f.getChannel().getId(), f);
} }
@ -113,7 +112,7 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
} }
} }
DefaultChannelGroupFuture(ChannelGroup group, Map<UUID, ChannelFuture> futures) { DefaultChannelGroupFuture(ChannelGroup group, Map<Integer, ChannelFuture> futures) {
this.group = group; this.group = group;
this.futures = Collections.unmodifiableMap(futures); this.futures = Collections.unmodifiableMap(futures);
for (ChannelFuture f: this.futures.values()) { for (ChannelFuture f: this.futures.values()) {
@ -130,7 +129,7 @@ public class DefaultChannelGroupFuture implements ChannelGroupFuture {
return group; return group;
} }
public ChannelFuture find(UUID channelId) { public ChannelFuture find(Integer channelId) {
return futures.get(channelId); return futures.get(channelId);
} }

View File

@ -73,9 +73,9 @@ public class DefaultIdleStateEvent implements IdleStateEvent {
@Override @Override
public String toString() { public String toString() {
return getChannel().toString() + " - (" + getState() + " since " + return getChannel().toString() + ' ' + getState() + " since " +
DateFormat.getDateTimeInstance( DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, Locale.US).format( DateFormat.SHORT, DateFormat.SHORT, Locale.US).format(
new Date(getLastActivityTimeMillis())) + ')'; new Date(getLastActivityTimeMillis()));
} }
} }