* 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;
|
package org.jboss.netty.channel.local;
|
||||||
|
|
||||||
import java.net.SocketAddress;
|
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>
|
* @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> {
|
public class LocalAddress extends SocketAddress implements Comparable<LocalAddress> {
|
||||||
private static final long serialVersionUID = -3601961747680808645L;
|
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) {
|
if (id == null) {
|
||||||
throw new NullPointerException("id");
|
throw new NullPointerException("id");
|
||||||
}
|
}
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|
||||||
|
ephemeral = id.startsWith("ephemeral-");
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEphemeral() {
|
||||||
|
return ephemeral;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return id.hashCode();
|
return id.hashCode();
|
||||||
|
@ -47,7 +47,7 @@ public class LocalExample {
|
|||||||
LocalServerChannelFactory factory = LocalServerChannels.registerServerChannel("localChannel");
|
LocalServerChannelFactory factory = LocalServerChannels.registerServerChannel("localChannel");
|
||||||
ServerBootstrap bootstrap = new ServerBootstrap(factory);
|
ServerBootstrap bootstrap = new ServerBootstrap(factory);
|
||||||
EchoHandler handler = new EchoHandler();
|
EchoHandler handler = new EchoHandler();
|
||||||
LocalAddress socketAddress = new LocalAddress("1");
|
LocalAddress socketAddress = LocalAddress.getInstance("1");
|
||||||
bootstrap.getPipeline().addLast("handler", handler);
|
bootstrap.getPipeline().addLast("handler", handler);
|
||||||
bootstrap.bind(socketAddress);
|
bootstrap.bind(socketAddress);
|
||||||
|
|
||||||
|
@ -43,7 +43,7 @@ public class LocalTransportRegister {
|
|||||||
ServerBootstrap serverBootstrap = new ServerBootstrap(serverChannelFactory);
|
ServerBootstrap serverBootstrap = new ServerBootstrap(serverChannelFactory);
|
||||||
EchoHandler handler = new EchoHandler();
|
EchoHandler handler = new EchoHandler();
|
||||||
serverBootstrap.getPipeline().addLast("handler", handler);
|
serverBootstrap.getPipeline().addLast("handler", handler);
|
||||||
Channel channel = serverBootstrap.bind(new LocalAddress("localAddress"));
|
Channel channel = serverBootstrap.bind(LocalAddress.getInstance("localAddress"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
|
@ -60,7 +60,7 @@ public class NettySessionListener implements HttpSessionListener, ChannelHandler
|
|||||||
return pipeline;
|
return pipeline;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ChannelFuture future = bootstrap.connect(new LocalAddress("netty"));
|
ChannelFuture future = bootstrap.connect(LocalAddress.getInstance("netty"));
|
||||||
future.awaitUninterruptibly();
|
future.awaitUninterruptibly();
|
||||||
final Channel ch = future.getChannel();
|
final Channel ch = future.getChannel();
|
||||||
session.setAttribute(CHANNEL_PROP, ch);
|
session.setAttribute(CHANNEL_PROP, ch);
|
||||||
|
Loading…
Reference in New Issue
Block a user