Add more javadocs and also two helper methods that make it easy to create new Clientbootstrap instances from an existing one
This commit is contained in:
parent
281f73fe1a
commit
d22480c0f4
@ -16,6 +16,7 @@
|
|||||||
package io.netty.bootstrap;
|
package io.netty.bootstrap;
|
||||||
|
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelPipeline;
|
import io.netty.channel.ChannelPipeline;
|
||||||
import io.netty.channel.ChannelFutureListener;
|
import io.netty.channel.ChannelFutureListener;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
@ -29,22 +30,37 @@ import java.net.SocketAddress;
|
|||||||
import java.nio.channels.ClosedChannelException;
|
import java.nio.channels.ClosedChannelException;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link Bootstrap} that makes it easy to bootstrap a {@link Channel} to use
|
||||||
|
* for clients.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class ClientBootstrap extends Bootstrap<ClientBootstrap> {
|
public class ClientBootstrap extends Bootstrap<ClientBootstrap> {
|
||||||
|
|
||||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ClientBootstrap.class);
|
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ClientBootstrap.class);
|
||||||
private SocketAddress remoteAddress;
|
private SocketAddress remoteAddress;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link SocketAddress} to connect to once the {@link #connect()} method
|
||||||
|
* is called.
|
||||||
|
*/
|
||||||
public ClientBootstrap remoteAddress(SocketAddress remoteAddress) {
|
public ClientBootstrap remoteAddress(SocketAddress remoteAddress) {
|
||||||
this.remoteAddress = remoteAddress;
|
this.remoteAddress = remoteAddress;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link #remoteAddress(SocketAddress)}
|
||||||
|
*/
|
||||||
public ClientBootstrap remoteAddress(String host, int port) {
|
public ClientBootstrap remoteAddress(String host, int port) {
|
||||||
remoteAddress = new InetSocketAddress(host, port);
|
remoteAddress = new InetSocketAddress(host, port);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link #remoteAddress(SocketAddress)}
|
||||||
|
*/
|
||||||
public ClientBootstrap remoteAddress(InetAddress host, int port) {
|
public ClientBootstrap remoteAddress(InetAddress host, int port) {
|
||||||
remoteAddress = new InetSocketAddress(host, port);
|
remoteAddress = new InetSocketAddress(host, port);
|
||||||
return this;
|
return this;
|
||||||
@ -71,12 +87,18 @@ public class ClientBootstrap extends Bootstrap<ClientBootstrap> {
|
|||||||
return future.channel().bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
return future.channel().bind(localAddress(), future).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect a {@link Channel} to the remote peer.
|
||||||
|
*/
|
||||||
public ChannelFuture connect() {
|
public ChannelFuture connect() {
|
||||||
validate();
|
validate();
|
||||||
Channel channel = factory().newChannel();
|
Channel channel = factory().newChannel();
|
||||||
return connect(channel.newFuture());
|
return connect(channel.newFuture());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link #connect()}
|
||||||
|
*/
|
||||||
public ChannelFuture connect(ChannelFuture future) {
|
public ChannelFuture connect(ChannelFuture future) {
|
||||||
validate(future);
|
validate(future);
|
||||||
if (remoteAddress == null) {
|
if (remoteAddress == null) {
|
||||||
@ -138,4 +160,23 @@ public class ClientBootstrap extends Bootstrap<ClientBootstrap> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ClientBootstrap} using this "full-setup" {@link ClientBootstrap} as template.
|
||||||
|
* Only the given parameters are replaced, the rest is configured exactly the same way as the template.
|
||||||
|
*/
|
||||||
|
public ClientBootstrap newBootstrap(SocketAddress localAddress, SocketAddress remoteAddress, ChannelHandler handler) {
|
||||||
|
validate();
|
||||||
|
ClientBootstrap cb = new ClientBootstrap().handler(handler).channelFactory(factory()).group(group()).localAddress(localAddress).remoteAddress(remoteAddress);
|
||||||
|
cb.options().putAll(options());
|
||||||
|
|
||||||
|
return cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ClientBootstrap} using this "full-setup" {@link ClientBootstrap} as template.
|
||||||
|
* Only the given parameters are replaced, the rest is configured exactly the same way as the template.
|
||||||
|
*/
|
||||||
|
public ClientBootstrap newBootstrap(SocketAddress localAddress, SocketAddress remoteAddress) {
|
||||||
|
return newBootstrap(localAddress, remoteAddress, handler());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user