* Removed TimeBasedUuidGenerator
* Fixed a bug where DefaultChannelGroup doesn't recognize the new Integer ID
This commit is contained in:
parent
149d97d013
commit
121c88d7e2
@ -29,7 +29,6 @@ import java.util.Collection;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
@ -94,7 +93,7 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(Object o) {
|
public boolean contains(Object o) {
|
||||||
if (o instanceof UUID) {
|
if (o instanceof Integer) {
|
||||||
return nonServerChannels.containsKey(o) || serverChannels.containsKey(o);
|
return nonServerChannels.containsKey(o) || serverChannels.containsKey(o);
|
||||||
} else if (o instanceof Channel) {
|
} else if (o instanceof Channel) {
|
||||||
Channel c = (Channel) o;
|
Channel c = (Channel) o;
|
||||||
@ -123,7 +122,7 @@ public class DefaultChannelGroup extends AbstractSet<Channel> implements Channel
|
|||||||
@Override
|
@Override
|
||||||
public boolean remove(Object o) {
|
public boolean remove(Object o) {
|
||||||
Channel c = null;
|
Channel c = null;
|
||||||
if (o instanceof UUID) {
|
if (o instanceof Integer) {
|
||||||
c = nonServerChannels.remove(o);
|
c = nonServerChannels.remove(o);
|
||||||
if (c == null) {
|
if (c == null) {
|
||||||
c = serverChannels.remove(o);
|
c = serverChannels.remove(o);
|
||||||
|
@ -1,172 +0,0 @@
|
|||||||
/*
|
|
||||||
* JBoss, Home of Professional Open Source
|
|
||||||
*
|
|
||||||
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
||||||
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
||||||
* full listing of individual contributors.
|
|
||||||
*
|
|
||||||
* This is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU Lesser General Public License as
|
|
||||||
* published by the Free Software Foundation; either version 2.1 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This software is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with this software; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
||||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
||||||
*/
|
|
||||||
package org.jboss.netty.util;
|
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates a time-based {@link UUID}. Please note that this generator
|
|
||||||
* uses a pseudo HMAC which is generated based on various VM properties.
|
|
||||||
* Therefore, the pseudo HMAC can change for each VM instance even if you
|
|
||||||
* launch the VM in the same machine. Its purpose is to generate a unique
|
|
||||||
* ID for one VM at most.
|
|
||||||
*
|
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
|
||||||
*
|
|
||||||
* @version $Rev$, $Date$
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class TimeBasedUuidGenerator {
|
|
||||||
|
|
||||||
private static final long NODE;
|
|
||||||
private static final Object counterLock = new Object();
|
|
||||||
private static long timestamp = System.currentTimeMillis();
|
|
||||||
private static int clockSeq = (int) System.nanoTime();
|
|
||||||
|
|
||||||
static {
|
|
||||||
// Generate nodeKey - we can't use MAC address to support Java 5.
|
|
||||||
StringBuilder nodeKey = new StringBuilder(1024);
|
|
||||||
|
|
||||||
//// Append host / IP address information.
|
|
||||||
try {
|
|
||||||
InetAddress localhost = InetAddress.getLocalHost();
|
|
||||||
nodeKey.append(localhost.getCanonicalHostName());
|
|
||||||
nodeKey.append(':');
|
|
||||||
nodeKey.append(String.valueOf(localhost.getHostAddress()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
nodeKey.append("localhost:127.0.0.1");
|
|
||||||
}
|
|
||||||
|
|
||||||
//// Append standard system properties.
|
|
||||||
appendSystemProperty(nodeKey, "java.version");
|
|
||||||
appendSystemProperty(nodeKey, "java.home");
|
|
||||||
appendSystemProperty(nodeKey, "java.vm.version");
|
|
||||||
appendSystemProperty(nodeKey, "java.vm.vendor");
|
|
||||||
appendSystemProperty(nodeKey, "java.vm.name");
|
|
||||||
appendSystemProperty(nodeKey, "os.name");
|
|
||||||
appendSystemProperty(nodeKey, "os.arch");
|
|
||||||
appendSystemProperty(nodeKey, "os.version");
|
|
||||||
appendSystemProperty(nodeKey, "user.name");
|
|
||||||
|
|
||||||
//// Append the information from java.lang.Runtime.
|
|
||||||
nodeKey.append(':');
|
|
||||||
nodeKey.append(Runtime.getRuntime().availableProcessors());
|
|
||||||
|
|
||||||
//// Finally, append the another distinguishable string (probably PID.)
|
|
||||||
try {
|
|
||||||
Class<?> mgmtFactoryType =
|
|
||||||
Class.forName("java.lang.management.ManagementFactory");
|
|
||||||
Method getRuntimeMXBean =
|
|
||||||
mgmtFactoryType.getMethod("getRuntimeMXBean", (Class[]) null);
|
|
||||||
Object runtimeMXBean =
|
|
||||||
getRuntimeMXBean.invoke(null, (Object[]) null);
|
|
||||||
Class<?> runtimeMXBeanType =
|
|
||||||
Class.forName("java.lang.management.RuntimeMXBean");
|
|
||||||
Method getName =
|
|
||||||
runtimeMXBeanType.getMethod("getName", (Class[]) null);
|
|
||||||
String vmId = String.valueOf(
|
|
||||||
getName.invoke(runtimeMXBean, (Object[]) null));
|
|
||||||
|
|
||||||
nodeKey.append(':');
|
|
||||||
nodeKey.append(vmId);
|
|
||||||
} catch (Throwable t) {
|
|
||||||
// Perhaps running with a security manager (e.g. Applet) or on a
|
|
||||||
// platform without the java.lang.management package (e.g. Android.)
|
|
||||||
nodeKey.append(":?");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the digest of the nodeKey.
|
|
||||||
MessageDigest md;
|
|
||||||
try {
|
|
||||||
md = MessageDigest.getInstance("MD5");
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
throw new InternalError("MD5 not supported");
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] nodeKeyDigest;
|
|
||||||
try {
|
|
||||||
nodeKeyDigest = md.digest(nodeKey.toString().getBytes("UTF-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new Error("UTF-8 is not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Choose 5 bytes from the digest.
|
|
||||||
// Please note that the first byte is always 1 (multicast address.)
|
|
||||||
long node = 1;
|
|
||||||
node = node << 8 | nodeKeyDigest[1] & 0xFF;
|
|
||||||
node = node << 8 | nodeKeyDigest[4] & 0xFF;
|
|
||||||
node = node << 8 | nodeKeyDigest[7] & 0xFF;
|
|
||||||
node = node << 8 | nodeKeyDigest[10] & 0xFF;
|
|
||||||
node = node << 8 | nodeKeyDigest[13] & 0xFF;
|
|
||||||
|
|
||||||
// We're done.
|
|
||||||
NODE = node;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void appendSystemProperty(StringBuilder buf, String key) {
|
|
||||||
buf.append(':');
|
|
||||||
buf.append(SystemPropertyUtil.get(key, "null"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a new time-based {@link UUID}.
|
|
||||||
*/
|
|
||||||
public static UUID generate() {
|
|
||||||
final long ts;
|
|
||||||
final int cs;
|
|
||||||
synchronized (counterLock) {
|
|
||||||
cs = clockSeq ++ & 0x3FFF; // 0~16383
|
|
||||||
if (cs == 0) {
|
|
||||||
// Not all platform has millisecond precision for
|
|
||||||
// System.currentTimeMillis() - Just focus on generating
|
|
||||||
// unique IDs instead of using correct timestamp.
|
|
||||||
ts = ++ timestamp;
|
|
||||||
} else {
|
|
||||||
ts = timestamp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long msb = (ts & 0xFFFFFFFFL) << 32 |
|
|
||||||
(ts >>> 32 & 0xFFFF) << 16 |
|
|
||||||
ts >>> 48 & 0xFFFF;
|
|
||||||
long lsb = (long) cs << 48 | NODE;
|
|
||||||
|
|
||||||
// Set to version 1 (i.e. time-based UUID)
|
|
||||||
msb = msb & 0xFFFFFFFFFFFF0FFFL | 0x0000000000001000L;
|
|
||||||
|
|
||||||
// Set to IETF variant
|
|
||||||
lsb = lsb & 0x3FFFFFFFFFFFFFFFL | 0x8000000000000000L;
|
|
||||||
|
|
||||||
return new UUID(msb, lsb);
|
|
||||||
}
|
|
||||||
|
|
||||||
private TimeBasedUuidGenerator() {
|
|
||||||
// Unused
|
|
||||||
}
|
|
||||||
}
|
|
@ -34,7 +34,6 @@ import org.jboss.netty.bootstrap.ClientBootstrap;
|
|||||||
import org.jboss.netty.channel.ChannelFuture;
|
import org.jboss.netty.channel.ChannelFuture;
|
||||||
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
|
||||||
import org.jboss.netty.util.DummyHandler;
|
import org.jboss.netty.util.DummyHandler;
|
||||||
import org.jboss.netty.util.TimeBasedUuidGenerator;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
@ -47,12 +46,6 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class NioClientSocketShutdownTimeTest {
|
public class NioClientSocketShutdownTimeTest {
|
||||||
|
|
||||||
static {
|
|
||||||
// Initialize the MD5 algorithm before testing - it takes too long
|
|
||||||
// on some JDK.
|
|
||||||
TimeBasedUuidGenerator.generate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testShutdownTime() throws Throwable {
|
public void testShutdownTime() throws Throwable {
|
||||||
ServerSocketChannel serverSocket = ServerSocketChannel.open();
|
ServerSocketChannel serverSocket = ServerSocketChannel.open();
|
||||||
|
@ -37,7 +37,6 @@ import org.jboss.netty.channel.ChannelPipelineCoverage;
|
|||||||
import org.jboss.netty.channel.ChannelStateEvent;
|
import org.jboss.netty.channel.ChannelStateEvent;
|
||||||
import org.jboss.netty.channel.SimpleChannelHandler;
|
import org.jboss.netty.channel.SimpleChannelHandler;
|
||||||
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
|
||||||
import org.jboss.netty.util.TimeBasedUuidGenerator;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
@ -50,12 +49,6 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class NioServerSocketShutdownTimeTest {
|
public class NioServerSocketShutdownTimeTest {
|
||||||
|
|
||||||
static {
|
|
||||||
// Initialize the MD5 algorithm before testing - it takes too long
|
|
||||||
// on some JDK.
|
|
||||||
TimeBasedUuidGenerator.generate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(timeout = 10000)
|
@Test(timeout = 10000)
|
||||||
public void testSuccessfulBindAttempt() throws Exception {
|
public void testSuccessfulBindAttempt() throws Exception {
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(
|
ServerBootstrap bootstrap = new ServerBootstrap(
|
||||||
|
@ -1,67 +0,0 @@
|
|||||||
/*
|
|
||||||
* JBoss, Home of Professional Open Source
|
|
||||||
*
|
|
||||||
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
||||||
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
||||||
* full listing of individual contributors.
|
|
||||||
*
|
|
||||||
* This is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms of the GNU Lesser General Public License as
|
|
||||||
* published by the Free Software Foundation; either version 2.1 of
|
|
||||||
* the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This software is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
||||||
* Lesser General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
|
||||||
* License along with this software; if not, write to the Free
|
|
||||||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
||||||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
||||||
*/
|
|
||||||
package org.jboss.netty.util;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
||||||
* @author Trustin Lee (tlee@redhat.com)
|
|
||||||
*
|
|
||||||
* @version $Rev$, $Date$
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class TimeBasedUuidGeneratorTest {
|
|
||||||
private static final int COUNT = 16384 * 8;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldGenerateTimeBasedUuid() {
|
|
||||||
UUID uuid = TimeBasedUuidGenerator.generate();
|
|
||||||
assertEquals(1, uuid.version());
|
|
||||||
assertEquals(2, uuid.variant());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldNotDuplicate() {
|
|
||||||
List<UUID> uuids = new ArrayList<UUID>(COUNT);
|
|
||||||
for (int i = 0; i < COUNT; i ++) {
|
|
||||||
uuids.add(TimeBasedUuidGenerator.generate());
|
|
||||||
}
|
|
||||||
|
|
||||||
Collections.sort(uuids);
|
|
||||||
|
|
||||||
for (int i = 1; i < COUNT; i ++) {
|
|
||||||
UUID a = uuids.get(i - 1);
|
|
||||||
UUID b = uuids.get(i);
|
|
||||||
assertFalse("Duplicate UUID: " + a.toString(), a.equals(b));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user