* Made sure there's only one LocalAddress instance with same id
* Added ephemeral port to LocalAddress
This commit is contained in:
parent
5e37b18f23
commit
632fb01bee
@ -22,6 +22,10 @@
|
||||
package org.jboss.netty.channel.local;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.jboss.netty.util.ConcurrentWeakHashMap;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:andy.taylor@jboss.org">Andy Taylor</a>
|
||||
@ -30,19 +34,51 @@ import java.net.SocketAddress;
|
||||
public class LocalAddress extends SocketAddress implements Comparable<LocalAddress> {
|
||||
private static final long serialVersionUID = -3601961747680808645L;
|
||||
|
||||
private final String id;
|
||||
private static final ConcurrentMap<String, LocalAddress> addresses =
|
||||
new ConcurrentWeakHashMap<String, LocalAddress>();
|
||||
|
||||
public LocalAddress(String id) {
|
||||
private static final AtomicLong nextEphemeralPort = new AtomicLong();
|
||||
|
||||
public static LocalAddress getInstance(String id) {
|
||||
if (id == null) {
|
||||
throw new NullPointerException("id");
|
||||
}
|
||||
LocalAddress a = addresses.get(id);
|
||||
if (a == null) {
|
||||
a = new LocalAddress(id);
|
||||
LocalAddress oldA = addresses.putIfAbsent(id, a);
|
||||
if (oldA != null) {
|
||||
a = oldA;
|
||||
}
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
public static LocalAddress newEphemeralInstance() {
|
||||
return getInstance("ephemeral-" + nextEphemeralPort.incrementAndGet());
|
||||
}
|
||||
|
||||
private final String id;
|
||||
private final boolean ephemeral;
|
||||
|
||||
private LocalAddress(String id) {
|
||||
if (id == null) {
|
||||
throw new NullPointerException("id");
|
||||
}
|
||||
this.id = id;
|
||||
|
||||
ephemeral = id.startsWith("ephemeral-");
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isEphemeral() {
|
||||
return ephemeral;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return id.hashCode();
|
||||
|
@ -47,7 +47,7 @@ public class LocalExample {
|
||||
LocalServerChannelFactory factory = LocalServerChannels.registerServerChannel("localChannel");
|
||||
ServerBootstrap bootstrap = new ServerBootstrap(factory);
|
||||
EchoHandler handler = new EchoHandler();
|
||||
LocalAddress socketAddress = new LocalAddress("1");
|
||||
LocalAddress socketAddress = LocalAddress.getInstance("1");
|
||||
bootstrap.getPipeline().addLast("handler", handler);
|
||||
bootstrap.bind(socketAddress);
|
||||
|
||||
|
@ -43,7 +43,7 @@ public class LocalTransportRegister {
|
||||
ServerBootstrap serverBootstrap = new ServerBootstrap(serverChannelFactory);
|
||||
EchoHandler handler = new EchoHandler();
|
||||
serverBootstrap.getPipeline().addLast("handler", handler);
|
||||
Channel channel = serverBootstrap.bind(new LocalAddress("localAddress"));
|
||||
Channel channel = serverBootstrap.bind(LocalAddress.getInstance("localAddress"));
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
@ -60,7 +60,7 @@ public class NettySessionListener implements HttpSessionListener, ChannelHandler
|
||||
return pipeline;
|
||||
}
|
||||
});
|
||||
ChannelFuture future = bootstrap.connect(new LocalAddress("netty"));
|
||||
ChannelFuture future = bootstrap.connect(LocalAddress.getInstance("netty"));
|
||||
future.awaitUninterruptibly();
|
||||
final Channel ch = future.getChannel();
|
||||
session.setAttribute(CHANNEL_PROP, ch);
|
||||
|
Loading…
Reference in New Issue
Block a user